Persons: other people's charts with package slots, list page, switcher
- charts.kind own/person; a new person uses a 'person' unit (not refunded on delete); person packages and an unlimited entitlement; admins unlimited - a person's birth data can be corrected within an hour (admins any time) - /kepletek list with tiles, /kepletek/uj, correction page, mock person packages, chart page title switches between charts keeping the tab Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
//! Payments. Until Stripe is connected, `MOCK_PAYMENTS=true` turns on a
|
||||
//! stand-in: "paying" adds one analysis unit right away, so the paid flow can
|
||||
//! be used end to end.
|
||||
//! 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.
|
||||
|
||||
use axum::Json;
|
||||
use axum::extract::State;
|
||||
@@ -13,14 +13,26 @@ use super::{Body, St};
|
||||
use crate::auth::AuthUser;
|
||||
use crate::credits::{self, Unit};
|
||||
use crate::error::{ApiError, ApiResult};
|
||||
use crate::persons;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct MockPayment {
|
||||
unit: Unit,
|
||||
/// One analysis unit.
|
||||
unit: Option<Unit>,
|
||||
package: Option<Package>,
|
||||
}
|
||||
|
||||
/// Adds one unit of the given kind as a (pretend) purchase.
|
||||
#[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.
|
||||
pub async fn mock(
|
||||
State(state): St,
|
||||
auth: AuthUser,
|
||||
@@ -29,16 +41,38 @@ pub async fn mock(
|
||||
if !state.config.mock_payments {
|
||||
return Err(ApiError::NotFound("payment method"));
|
||||
}
|
||||
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())),
|
||||
};
|
||||
let mut tx = state.db.begin().await?;
|
||||
sqlx::query(
|
||||
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason, note) VALUES ($1, $2, $3, 1, 'purchase', 'mock payment')",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(auth.id())
|
||||
.bind(req.unit.as_str())
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
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?;
|
||||
}
|
||||
}
|
||||
let balance = credits::balance(&mut tx, auth.id()).await?;
|
||||
let persons = persons::slots(&mut tx, &auth, state.config.free_person_slots).await?;
|
||||
tx.commit().await?;
|
||||
Ok(Json(json!({ "analyses_left": balance.view() })))
|
||||
Ok(Json(json!({ "analyses_left": balance.view(), "persons": persons.view() })))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user