2026-11-17 06:45:33 +00:00
|
|
|
//! Payments. Until Stripe is connected, `MOCK_PAYMENTS=true` turns on a
|
2026-11-17 11:03:55 +00:00
|
|
|
//! stand-in: "paying" grants what was bought right away (an analysis unit or
|
|
|
|
|
//! a person package), so the paid flow can be used end to end.
|
2026-11-17 06:45:33 +00:00
|
|
|
|
|
|
|
|
use axum::Json;
|
|
|
|
|
use axum::extract::State;
|
|
|
|
|
use axum_extra::extract::WithRejection;
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
use serde_json::{Value, json};
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use super::{Body, St};
|
|
|
|
|
use crate::auth::AuthUser;
|
|
|
|
|
use crate::credits::{self, Unit};
|
|
|
|
|
use crate::error::{ApiError, ApiResult};
|
2026-11-17 11:03:55 +00:00
|
|
|
use crate::persons;
|
2026-11-17 06:45:33 +00:00
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
|
pub struct MockPayment {
|
2026-11-17 11:03:55 +00:00
|
|
|
/// One analysis unit.
|
|
|
|
|
unit: Option<Unit>,
|
|
|
|
|
package: Option<Package>,
|
2026-11-17 06:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-17 11:03:55 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
|
pub enum Package {
|
|
|
|
|
/// `PERSON_PACK_SIZE` more persons.
|
|
|
|
|
Persons,
|
|
|
|
|
/// Any number of persons.
|
|
|
|
|
PersonsUnlimited,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Grants an analysis unit or a person package as a (pretend) purchase.
|
2026-11-17 06:45:33 +00:00
|
|
|
pub async fn mock(
|
|
|
|
|
State(state): St,
|
|
|
|
|
auth: AuthUser,
|
|
|
|
|
WithRejection(Json(req), _): Body<MockPayment>,
|
|
|
|
|
) -> ApiResult<Json<Value>> {
|
|
|
|
|
if !state.config.mock_payments {
|
|
|
|
|
return Err(ApiError::NotFound("payment method"));
|
|
|
|
|
}
|
2026-11-17 11:03:55 +00:00
|
|
|
let (unit, delta) = match (req.unit, req.package) {
|
|
|
|
|
(Some(unit), None) => (Some(unit), 1),
|
|
|
|
|
(None, Some(Package::Persons)) => (Some(Unit::Person), state.config.person_pack_size),
|
|
|
|
|
(None, Some(Package::PersonsUnlimited)) => (None, 0),
|
|
|
|
|
_ => return Err(ApiError::BadRequest("give either unit or package".into())),
|
|
|
|
|
};
|
2026-11-17 06:45:33 +00:00
|
|
|
let mut tx = state.db.begin().await?;
|
2026-11-17 11:03:55 +00:00
|
|
|
match unit {
|
|
|
|
|
Some(unit) => {
|
|
|
|
|
sqlx::query(
|
|
|
|
|
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, note) \
|
|
|
|
|
VALUES ($1, $2, $3, $4, 'purchase', 'mock payment')",
|
|
|
|
|
)
|
|
|
|
|
.bind(Uuid::now_v7())
|
|
|
|
|
.bind(auth.id())
|
|
|
|
|
.bind(unit.as_str())
|
|
|
|
|
.bind(delta as i32)
|
|
|
|
|
.execute(&mut *tx)
|
|
|
|
|
.await?;
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
sqlx::query(
|
|
|
|
|
"INSERT INTO entitlements (user_id, entitlement, note) VALUES ($1, 'unlimited_persons', 'mock payment') \
|
|
|
|
|
ON CONFLICT DO NOTHING",
|
|
|
|
|
)
|
|
|
|
|
.bind(auth.id())
|
|
|
|
|
.execute(&mut *tx)
|
|
|
|
|
.await?;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-17 06:45:33 +00:00
|
|
|
let balance = credits::balance(&mut tx, auth.id()).await?;
|
2026-11-17 11:03:55 +00:00
|
|
|
let persons = persons::slots(&mut tx, &auth, state.config.free_person_slots).await?;
|
2026-11-17 06:45:33 +00:00
|
|
|
tx.commit().await?;
|
2026-11-17 11:03:55 +00:00
|
|
|
Ok(Json(json!({ "analyses_left": balance.view(), "persons": persons.view() })))
|
2026-11-17 06:45:33 +00:00
|
|
|
}
|