Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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

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

This file was deleted.

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

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

25 changes: 20 additions & 5 deletions backend/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ impl DB {
initial_balance: Decimal,
) -> SqlxResult<ValidationResult<EnsureUserCreatedSuccess>> {
let balance = Text(initial_balance);
let mut transaction = self.pool.begin().await?;

// First try to find user by kinde_id
let existing_user = sqlx::query!(
Expand All @@ -754,17 +755,19 @@ impl DB {
"#,
kinde_id
)
.fetch_optional(&self.pool)
.fetch_optional(transaction.as_mut())
.await?;

if let Some(user) = existing_user {
transaction.commit().await?;
return Ok(Ok(EnsureUserCreatedSuccess {
id: user.id,
name: None,
}));
}

let Some(requested_name) = requested_name else {
transaction.commit().await?;
return Ok(Err(ValidationFailure::NoNameProvidedForNewUser));
};

Expand All @@ -777,7 +780,7 @@ impl DB {
requested_name,
kinde_id
)
.fetch_optional(&self.pool)
.fetch_optional(transaction.as_mut())
.await?;

let final_name = if conflicting_account.is_some() {
Expand All @@ -786,18 +789,30 @@ impl DB {
requested_name.to_string()
};

let id = sqlx::query_scalar!(
sqlx::query!(
r#"
INSERT INTO account (kinde_id, name, balance)
VALUES (?, ?, ?)
RETURNING id
"#,
kinde_id,
final_name,
balance,
)
.fetch_one(&self.pool)
.execute(transaction.as_mut())
.await?;

let id = sqlx::query_scalar!(
r#"
SELECT id AS "id!: i64"
FROM account
WHERE kinde_id = ?
"#,
kinde_id
)
.fetch_one(transaction.as_mut())
.await?;

transaction.commit().await?;
Ok(Ok(EnsureUserCreatedSuccess {
id,
name: Some(final_name),
Expand Down