add customer record insertion on successful creation

This commit is contained in:
Joe Ardent 2024-03-09 17:54:13 -08:00
parent 5aea86d8af
commit 7d78302427
2 changed files with 17 additions and 6 deletions

View file

@ -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

View file

@ -28,7 +28,7 @@ lazy_static! {
}
/// Displays the signup form.
pub async fn get_signup(_db: State<SqlitePool>) -> 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<SqlitePool>) -> impl IntoResponse {
/// Receives the form with the user signup fields filled out.
pub async fn post_signup(
db: State<SqlitePool>,
State(db): State<SqlitePool>,
Form(form): Form<SignupForm>,
) -> Result<impl IntoResponse, CreateUserError> {
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<SqlitePool>,
receipt: Option<Path<String>>,
) -> impl IntoResponse {
pub async fn payment_success(receipt: Option<Path<String>>) -> 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;