Header shows the user's package and what is left of it
CI / test (push) Successful in 51s
CI / build (push) Successful in 4m0s
CI / deploy (push) Skipped

- /v1/me: `package` = the running unlimited package, or the latest main
  package bought or given (top-ups do not count; an ended unlimited not)
- header badge: "Család · 11 elemzés", "Korlátlan · 2027. november 17-ig",
  "Páros · elfogyott"; links to the packages page, details in the tooltip

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 19:24:52 +00:00
co-authored by Claude Opus 5.5
parent 21640ffd9e
commit 742b88183b
6 changed files with 77 additions and 1 deletions
+20
View File
@@ -248,6 +248,26 @@ pub async fn unlimited_until(conn: &mut PgConnection, user_id: Uuid) -> Result<O
Ok(until)
}
/// The package to show for the user (header, profile): the running
/// unlimited one, or else the latest bought or given package of the main
/// ladder (top-ups do not count). `None`: no package.
pub async fn current(
conn: &mut PgConnection,
user_id: Uuid,
unlimited_until: Option<OffsetDateTime>,
) -> Result<Option<(&'static Package, OffsetDateTime)>, ApiError> {
let orders: Vec<(String, OffsetDateTime)> =
sqlx::query_as("SELECT package, created_at FROM package_orders WHERE user_id = $1 ORDER BY created_at DESC")
.bind(user_id)
.fetch_all(conn)
.await?;
Ok(orders.into_iter().find_map(|(id, at)| {
let p = find(&id).filter(|p| p.tier == Tier::Main)?;
// While an unlimited package runs, only that is shown; an ended one never.
(p.unlimited_months.is_some() == unlimited_until.is_some()).then_some((p, at))
}))
}
/// The fair-use cap of the unlimited package: new detailed analyses in 24 hours.
pub async fn check_fair_use(conn: &mut PgConnection, user_id: Uuid, daily_limit: i64) -> Result<(), ApiError> {
let (count,): (i64,) = sqlx::query_as(
+2
View File
@@ -33,6 +33,7 @@ pub async fn me(State(state): St, auth: AuthUser) -> ApiResult<Json<Value>> {
let birth_change = crate::birth_changes::view(&mut conn, &auth).await?;
let persons = crate::persons::slots(&mut conn, &auth, state.config.free_person_slots).await?;
let unlimited = crate::packages::unlimited_until(&mut conn, auth.id()).await?;
let package = crate::packages::current(&mut conn, auth.id(), unlimited).await?;
Ok(Json(json!({
"user": UserView::from(&auth.user),
"providers": providers.into_iter().map(|(p,)| p).collect::<Vec<_>>(),
@@ -41,6 +42,7 @@ pub async fn me(State(state): St, auth: AuthUser) -> ApiResult<Json<Value>> {
"birth_change": birth_change,
"persons": persons.view(),
"unlimited_until": rfc3339_opt(unlimited),
"package": package.map(|(p, since)| json!({ "id": p.id, "name": p.name, "since": rfc3339(since) })),
})))
}
+6
View File
@@ -1000,6 +1000,7 @@ async fn packages_grant_units_and_unlimited(pool: PgPool) {
let admin = app.register("admin@example.com", "Admin").await;
let anna = app.register("anna@example.com", "Anna").await;
let anna_id = app.get("/v1/me", Some(&anna)).await.body["user"]["id"].as_str().unwrap().to_string();
assert_eq!(app.get("/v1/me", Some(&anna)).await.body["package"], Value::Null);
// the catalog is public
let r = app.get("/v1/packages", None).await;
@@ -1035,6 +1036,9 @@ async fn packages_grant_units_and_unlimited(pool: PgPool) {
let (price,): (i32,) =
sqlx::query_as("SELECT price_huf FROM package_orders WHERE package = 'couple'").fetch_one(&pool).await.unwrap();
assert_eq!(price, 6990);
// the header shows the latest main package; a top-up does not change it
app.post("/v1/payments/mock", Some(&anna), json!({"package": "addon_persons"})).await;
assert_eq!(app.get("/v1/me", Some(&anna)).await.body["package"]["name"], "Páros");
// unlimited: a year, and a second one extends it
let bela = app.register("bela@example.com", "Béla").await;
@@ -1049,6 +1053,7 @@ async fn packages_grant_units_and_unlimited(pool: PgPool) {
let days = (until - time::OffsetDateTime::now_utc()).whole_days();
assert!((725..=733).contains(&days), "{days}");
assert_eq!(app.get("/v1/me", Some(&bela)).await.body["persons"]["unlimited"], true);
assert_eq!(app.get("/v1/me", Some(&bela)).await.body["package"]["id"], "unlimited");
// analyses use no unit, up to the daily cap (3 in tests)
for (i, date) in ["1980-01-01", "1981-01-01", "1982-01-01"].into_iter().enumerate() {
@@ -1068,6 +1073,7 @@ async fn packages_grant_units_and_unlimited(pool: PgPool) {
let r = app.call("POST", &uri, Some(&bela), None).await;
assert_eq!(r.status, StatusCode::PAYMENT_REQUIRED);
assert_eq!(app.get("/v1/me", Some(&bela)).await.body["unlimited_until"], Value::Null);
assert_eq!(app.get("/v1/me", Some(&bela)).await.body["package"], Value::Null); // ended
}
#[sqlx::test(migrator = "db::MIGRATOR")]