Header shows the user's package and what is left of it
- /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:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user