2026-11-17 16:19:20 +00:00
|
|
|
//! Packages and payments. Until Stripe is connected, `MOCK_PAYMENTS=true`
|
|
|
|
|
//! turns on a stand-in: "paying" grants the package right away, 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 super::{Body, St};
|
|
|
|
|
use crate::auth::AuthUser;
|
|
|
|
|
use crate::credits::{self, Unit};
|
|
|
|
|
use crate::error::{ApiError, ApiResult};
|
2026-11-17 16:19:20 +00:00
|
|
|
use crate::packages::{self, CATALOG, Source};
|
2026-11-17 11:03:55 +00:00
|
|
|
use crate::persons;
|
2026-11-17 16:19:20 +00:00
|
|
|
|
|
|
|
|
/// The package catalog (public).
|
|
|
|
|
pub async fn catalog() -> Json<Value> {
|
|
|
|
|
Json(json!({ "packages": CATALOG.iter().map(packages::Package::view).collect::<Vec<_>>() }))
|
|
|
|
|
}
|
2026-11-17 06:45:33 +00:00
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
|
pub struct MockPayment {
|
2026-11-17 16:19:20 +00:00
|
|
|
/// A package id from the catalog.
|
|
|
|
|
package: Option<String>,
|
|
|
|
|
/// Shorthand: the cheapest package with one unit of this kind.
|
2026-11-17 11:03:55 +00:00
|
|
|
unit: Option<Unit>,
|
2026-11-17 06:45:33 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-18 06:20:31 +00:00
|
|
|
/// What the user has now: units left and person slots.
|
2026-11-17 16:19:20 +00:00
|
|
|
pub async fn holdings(state: &crate::AppState, auth: &AuthUser) -> Result<Value, ApiError> {
|
|
|
|
|
let mut conn = state.db.acquire().await?;
|
|
|
|
|
let balance = credits::balance(&mut conn, auth.id()).await?;
|
|
|
|
|
let persons = persons::slots(&mut conn, auth, state.config.free_person_slots).await?;
|
|
|
|
|
Ok(json!({
|
|
|
|
|
"analyses_left": balance.view(),
|
|
|
|
|
"persons": persons.view(),
|
|
|
|
|
}))
|
2026-11-17 11:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-17 16:19:20 +00:00
|
|
|
/// Grants a 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 16:19:20 +00:00
|
|
|
let id = match (req.package.as_deref(), req.unit) {
|
|
|
|
|
(Some(id), None) => id,
|
|
|
|
|
(None, Some(Unit::Natal)) => "single",
|
|
|
|
|
(None, Some(Unit::Synastry)) => "addon_synastry",
|
|
|
|
|
(None, Some(Unit::Person)) => "addon_persons",
|
|
|
|
|
_ => return Err(ApiError::BadRequest("give either package or unit".into())),
|
2026-11-17 11:03:55 +00:00
|
|
|
};
|
2026-11-17 16:19:20 +00:00
|
|
|
let package = packages::find(id).ok_or(ApiError::NotFound("package"))?;
|
2026-11-17 06:45:33 +00:00
|
|
|
let mut tx = state.db.begin().await?;
|
2026-11-17 16:19:20 +00:00
|
|
|
packages::grant(&mut tx, auth.id(), package, Source::Mock, Some(auth.id()), Some("mock payment")).await?;
|
2026-11-17 06:45:33 +00:00
|
|
|
tx.commit().await?;
|
2026-11-17 16:19:20 +00:00
|
|
|
tracing::info!(user = %auth.id(), package = package.id, "mock payment");
|
|
|
|
|
Ok(Json(holdings(&state, &auth).await?))
|
2026-11-17 06:45:33 +00:00
|
|
|
}
|