Packages: from one analysis to unlimited, admin can give them
CI / test (push) Successful in 47s
CI / build (push) Successful in 4m19s
CI / deploy (push) Skipped

A fixed catalog (packages.rs, reasoning in docs/csomagok.md): Egy elemzés
2 990 Ft, Páros 6 990 Ft, Család 11 990 Ft (featured), Korlátlan 17 990 Ft
for a year, and two top-ups. Every grant is a package_orders row; units are
credit_ledger rows with reason 'package' and never expire. The unlimited
package is a time-limited 'unlimited' entitlement: analyses use no unit and
persons have no limit, with a daily fair-use cap (UNLIMITED_DAILY_LIMIT, 5);
buying it again extends it by a year.

- GET /v1/packages, mock payment by package id, /v1/me unlimited_until
- POST /v1/admin/users/{id}/packages: give a package as a gift
- /csomagok page; the paid step of an analysis offers three packages with
  the bigger one featured; person packs use the catalog; admin user list
  has a package column
- PERSON_PACK_SIZE is replaced by the catalog's +5 személy top-up

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-17 16:19:20 +00:00
co-authored by Claude Opus 5.5
parent 0bd5c39080
commit e004ea696b
29 changed files with 1019 additions and 164 deletions
+35 -45
View File
@@ -1,38 +1,49 @@
//! Payments. Until Stripe is connected, `MOCK_PAYMENTS=true` turns on a
//! 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.
//! 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.
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};
use crate::packages::{self, CATALOG, Source};
use crate::persons;
use crate::views::rfc3339;
/// The package catalog (public).
pub async fn catalog() -> Json<Value> {
Json(json!({ "packages": CATALOG.iter().map(packages::Package::view).collect::<Vec<_>>() }))
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MockPayment {
/// One analysis unit.
/// A package id from the catalog.
package: Option<String>,
/// Shorthand: the cheapest package with one unit of this kind.
unit: Option<Unit>,
package: Option<Package>,
}
#[derive(Debug, Clone, Copy, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Package {
/// `PERSON_PACK_SIZE` more persons.
Persons,
/// Any number of persons.
PersonsUnlimited,
/// What the user has now: units left, person slots, the unlimited package.
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?;
let until = packages::unlimited_until(&mut conn, auth.id()).await?;
Ok(json!({
"analyses_left": balance.view(),
"persons": persons.view(),
"unlimited_until": until.map(rfc3339),
}))
}
/// Grants an analysis unit or a person package as a (pretend) purchase.
/// Grants a package as a (pretend) purchase.
pub async fn mock(
State(state): St,
auth: AuthUser,
@@ -41,38 +52,17 @@ 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 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())),
};
let package = packages::find(id).ok_or(ApiError::NotFound("package"))?;
let mut tx = state.db.begin().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?;
packages::grant(&mut tx, auth.id(), package, Source::Mock, Some(auth.id()), Some("mock payment")).await?;
tx.commit().await?;
Ok(Json(json!({ "analyses_left": balance.view(), "persons": persons.view() })))
tracing::info!(user = %auth.id(), package = package.id, "mock payment");
Ok(Json(holdings(&state, &auth).await?))
}