Skip to content

Commit

Permalink
Make promo codes case-insensitive (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Kozarin committed Sep 26, 2024
1 parent 8adcce6 commit 6ec6154
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions src/repo/promo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Debug;
use anyhow::anyhow;
use sqlx::Postgres;
use sqlx::{FromRow, Postgres};
use teloxide::types::UserId;
use crate::repository;

Expand Down Expand Up @@ -33,6 +33,12 @@ pub struct PromoCodeParams {
pub capacity: u32,
}

#[derive(FromRow)]
struct PromoCodeInfo {
found_code: String,
bonus_length: i32,
}

repository!(Promo,
#[cfg(test)]
pub async fn create(&self, p: PromoCodeParams) -> anyhow::Result<()> {
Expand All @@ -46,14 +52,14 @@ repository!(Promo,
pub async fn activate(&self, user_id: UserId, code: &str) -> Result<ActivationResult, ActivationError> {
let mut tx = self.pool.begin().await?;

let bonus_length = Self::find_code_length_and_decr_capacity(&mut tx, code)
let PromoCodeInfo { found_code, bonus_length } = Self::find_code_length_and_decr_capacity(&mut tx, code)
.await?
.ok_or(ActivationError::NoActivationsLeft)?;
let chats_affected = Self::grow_dick(&mut tx, user_id, bonus_length).await?;
if chats_affected < 1 {
return Err(ActivationError::NoDicks)
}
Self::add_activation(&mut tx, user_id, code, chats_affected)
Self::add_activation(&mut tx, user_id, &found_code, chats_affected)
.await
.map_err(|err| {
match err.downcast() {
Expand All @@ -72,14 +78,14 @@ repository!(Promo,
Ok(ActivationResult{ chats_affected, bonus_length })
}
,
async fn find_code_length_and_decr_capacity(tx: &mut sqlx::Transaction<'_, Postgres>, code: &str) -> anyhow::Result<Option<i32>> {
sqlx::query_scalar!(
async fn find_code_length_and_decr_capacity(tx: &mut sqlx::Transaction<'_, Postgres>, code: &str) -> anyhow::Result<Option<PromoCodeInfo>> {
sqlx::query_as!(PromoCodeInfo,
"UPDATE Promo_Codes SET capacity = (capacity - 1)
WHERE code = $1 AND capacity > 0 AND
WHERE lower(code) = lower($1) AND capacity > 0 AND
(current_date BETWEEN since AND until
OR
current_date >= since AND until IS NULL)
RETURNING bonus_length",
RETURNING bonus_length, code as found_code",
code)
.fetch_optional(&mut **tx)
.await
Expand Down
3 changes: 2 additions & 1 deletion src/repo/test/promo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::repo::test::{start_postgres, UID};
use crate::repo::test::dicks::{check_dick, create_dick, create_user};

const PROMO_CODE: &str = "test10";
const PROMO_CODE_UPPERCASE: &str = "TEST10";
const PROMO_BONUS: u32 = 10;

#[tokio::test]
Expand All @@ -22,7 +23,7 @@ async fn activate() {

create_user(&db).await;
create_dick(&db).await;
let res = promo.activate(UserId(UID as u64), PROMO_CODE)
let res = promo.activate(UserId(UID as u64), PROMO_CODE_UPPERCASE)
.await.expect("couldn't activate the promo code");
assert_eq!(res.chats_affected, 1);
assert_eq!(res.bonus_length, PROMO_BONUS as i32);
Expand Down

0 comments on commit 6ec6154

Please sign in to comment.