From 7d78302427108c149a1dc08d1f47d217a275f610 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 9 Mar 2024 17:54:13 -0800 Subject: [PATCH] add customer record insertion on successful creation --- .../20240308005811_users_invitations.up.sql | 1 + src/handlers/handlers.rs | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/migrations/20240308005811_users_invitations.up.sql b/migrations/20240308005811_users_invitations.up.sql index ef4c5ed..1cff5b9 100644 --- a/migrations/20240308005811_users_invitations.up.sql +++ b/migrations/20240308005811_users_invitations.up.sql @@ -10,6 +10,7 @@ create table if not exists customers ( ); create index if not exists customers_username_dex on customers (lower(username)); create index if not exists customers_email_dex on customers (lower(billing_email)); +create index if not exists customers_receipt_dex on customers (receipt); create index if not exists customers_invitation_dex on customers (invitation); -- does this need to be created? it's already a foreign key create trigger if not exists update_last_updated_customers diff --git a/src/handlers/handlers.rs b/src/handlers/handlers.rs index 91eab4a..3480c97 100644 --- a/src/handlers/handlers.rs +++ b/src/handlers/handlers.rs @@ -28,7 +28,7 @@ lazy_static! { } /// Displays the signup form. -pub async fn get_signup(_db: State) -> impl IntoResponse { +pub async fn get_signup() -> impl IntoResponse { SignupPage { monthly_link: Some((*MONTHLY_LINK).to_string()), ..Default::default() @@ -37,12 +37,13 @@ pub async fn get_signup(_db: State) -> impl IntoResponse { /// Receives the form with the user signup fields filled out. pub async fn post_signup( - db: State, + State(db): State, Form(form): Form, ) -> Result { let user = validate_signup(&form).await?; if create_user(&user) { log::info!("Created user {user:?}"); + insert_user(&db, &form.username, form.receipt.trim(), None).await; Ok(SignupSuccessPage(user)) } else { Err(CreateUserError(CreateUserErrorKind::UnknownEorr)) @@ -50,10 +51,7 @@ pub async fn post_signup( } /// Redirected from Stripe with the receipt of payment. -pub async fn payment_success( - db: State, - receipt: Option>, -) -> impl IntoResponse { +pub async fn payment_success(receipt: Option>) -> impl IntoResponse { let receipt = if let Some(Path(receipt)) = receipt { receipt } else { @@ -70,6 +68,18 @@ pub async fn payment_success( //-************************************************************************ // helpers //-************************************************************************ +async fn insert_user(db: &SqlitePool, username: &str, receipt: &str, invitation: Option<&str>) { + sqlx::query!( + "insert into customers (username, receipt, invitation) values (?, ?, ?)", + username, + receipt, + invitation + ) + .execute(db) + .await + .unwrap_or_default(); +} + fn create_user(user: &User) -> bool { let token = &*ADMIN_TOKEN; let url = &*FORGEJO_URL;