Proceed gracefully when someone signs up repeatedly

This commit is contained in:
Max Brunsfeld 2022-09-20 16:23:02 -07:00
parent 758875305b
commit 20ec933e23

View file

@ -458,21 +458,29 @@ impl Db for PostgresDb {
) -> Result<(UserId, Option<UserId>)> { ) -> Result<(UserId, Option<UserId>)> {
let mut tx = self.pool.begin().await?; let mut tx = self.pool.begin().await?;
let (signup_id, metrics_id, inviting_user_id): (i32, i32, Option<UserId>) = sqlx::query_as( let (signup_id, metrics_id, existing_user_id, inviting_user_id): (
i32,
i32,
Option<UserId>,
Option<UserId>,
) = sqlx::query_as(
" "
SELECT id, metrics_id, inviting_user_id SELECT id, metrics_id, user_id, inviting_user_id
FROM signups FROM signups
WHERE WHERE
email_address = $1 AND email_address = $1 AND
email_confirmation_code = $2 AND email_confirmation_code = $2
user_id is NULL
", ",
) )
.bind(&invite.email_address) .bind(&invite.email_address)
.bind(&invite.email_confirmation_code) .bind(&invite.email_confirmation_code)
.fetch_optional(&mut tx) .fetch_optional(&mut tx)
.await? .await?
.ok_or_else(|| anyhow!("no such invite"))?; .ok_or_else(|| Error::Http(StatusCode::NOT_FOUND, "no such invite".to_string()))?;
if existing_user_id.is_some() {
Err(Error::Http(StatusCode::UNPROCESSABLE_ENTITY, "invitation already redeemed".to_string()))?;
}
let user_id: UserId = sqlx::query_scalar( let user_id: UserId = sqlx::query_scalar(
" "