Merge pull request 'Header shows the user's package and what is left of it' (#9) from main into prod
Reviewed-on: #9
This commit was merged in pull request #9.
This commit is contained in:
@@ -248,6 +248,26 @@ pub async fn unlimited_until(conn: &mut PgConnection, user_id: Uuid) -> Result<O
|
|||||||
Ok(until)
|
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.
|
/// 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> {
|
pub async fn check_fair_use(conn: &mut PgConnection, user_id: Uuid, daily_limit: i64) -> Result<(), ApiError> {
|
||||||
let (count,): (i64,) = sqlx::query_as(
|
let (count,): (i64,) = sqlx::query_as(
|
||||||
|
|||||||
@@ -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 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 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 unlimited = crate::packages::unlimited_until(&mut conn, auth.id()).await?;
|
||||||
|
let package = crate::packages::current(&mut conn, auth.id(), unlimited).await?;
|
||||||
Ok(Json(json!({
|
Ok(Json(json!({
|
||||||
"user": UserView::from(&auth.user),
|
"user": UserView::from(&auth.user),
|
||||||
"providers": providers.into_iter().map(|(p,)| p).collect::<Vec<_>>(),
|
"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,
|
"birth_change": birth_change,
|
||||||
"persons": persons.view(),
|
"persons": persons.view(),
|
||||||
"unlimited_until": rfc3339_opt(unlimited),
|
"unlimited_until": rfc3339_opt(unlimited),
|
||||||
|
"package": package.map(|(p, since)| json!({ "id": p.id, "name": p.name, "since": rfc3339(since) })),
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1000,6 +1000,7 @@ async fn packages_grant_units_and_unlimited(pool: PgPool) {
|
|||||||
let admin = app.register("admin@example.com", "Admin").await;
|
let admin = app.register("admin@example.com", "Admin").await;
|
||||||
let anna = app.register("anna@example.com", "Anna").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();
|
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
|
// the catalog is public
|
||||||
let r = app.get("/v1/packages", None).await;
|
let r = app.get("/v1/packages", None).await;
|
||||||
@@ -1035,6 +1036,9 @@ async fn packages_grant_units_and_unlimited(pool: PgPool) {
|
|||||||
let (price,): (i32,) =
|
let (price,): (i32,) =
|
||||||
sqlx::query_as("SELECT price_huf FROM package_orders WHERE package = 'couple'").fetch_one(&pool).await.unwrap();
|
sqlx::query_as("SELECT price_huf FROM package_orders WHERE package = 'couple'").fetch_one(&pool).await.unwrap();
|
||||||
assert_eq!(price, 6990);
|
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
|
// unlimited: a year, and a second one extends it
|
||||||
let bela = app.register("bela@example.com", "Béla").await;
|
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();
|
let days = (until - time::OffsetDateTime::now_utc()).whole_days();
|
||||||
assert!((725..=733).contains(&days), "{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["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)
|
// 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() {
|
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;
|
let r = app.call("POST", &uri, Some(&bela), None).await;
|
||||||
assert_eq!(r.status, StatusCode::PAYMENT_REQUIRED);
|
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["unlimited_until"], Value::Null);
|
||||||
|
assert_eq!(app.get("/v1/me", Some(&bela)).await.body["package"], Value::Null); // ended
|
||||||
}
|
}
|
||||||
|
|
||||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||||
|
|||||||
@@ -57,6 +57,14 @@ p { margin: 0; }
|
|||||||
.nav a { color: var(--ink-2); text-decoration: none; padding: 10px 12px; border-radius: 8px; min-height: 44px; display: inline-flex; align-items: center; }
|
.nav a { color: var(--ink-2); text-decoration: none; padding: 10px 12px; border-radius: 8px; min-height: 44px; display: inline-flex; align-items: center; }
|
||||||
.nav a:hover, .nav a[aria-current="page"] { color: var(--gold); background: rgba(217, 180, 106, 0.08); }
|
.nav a:hover, .nav a[aria-current="page"] { color: var(--gold); background: rgba(217, 180, 106, 0.08); }
|
||||||
.who { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 14px; }
|
.who { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 14px; }
|
||||||
|
.pkg-pill {
|
||||||
|
display: inline-flex; align-items: center; gap: 6px; min-height: 32px; padding: 0 12px; border-radius: 999px;
|
||||||
|
border: 1px solid rgba(217, 180, 106, 0.5); color: var(--gold); text-decoration: none; font-size: 13px; white-space: nowrap;
|
||||||
|
}
|
||||||
|
.pkg-pill:hover { background: rgba(217, 180, 106, 0.08); color: var(--gold); }
|
||||||
|
.pkg-pill-detail { color: var(--muted); }
|
||||||
|
.pkg-pill-detail::before { content: "·"; margin-right: 6px; }
|
||||||
|
.pkg-pill-empty { border-color: var(--line-2); color: var(--ink-2); }
|
||||||
|
|
||||||
/* Layout */
|
/* Layout */
|
||||||
.page { max-width: 960px; margin: 0 auto; padding: 32px 16px 64px; display: flex; flex-direction: column; gap: 24px; }
|
.page { max-width: 960px; margin: 0 auto; padding: 32px 16px 64px; display: flex; flex-direction: column; gap: 24px; }
|
||||||
@@ -307,6 +315,7 @@ td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
|
|||||||
h1 { font-size: 32px; }
|
h1 { font-size: 32px; }
|
||||||
.site-header { padding: 10px 16px; gap: 12px; }
|
.site-header { padding: 10px 16px; gap: 12px; }
|
||||||
.nav { order: 3; flex-basis: 100%; }
|
.nav { order: 3; flex-basis: 100%; }
|
||||||
|
.who-name { display: none; }
|
||||||
.card { padding: 18px; }
|
.card { padding: 18px; }
|
||||||
.auth-aside { padding: 28px 20px; min-height: 0; }
|
.auth-aside { padding: 28px 20px; min-height: 0; }
|
||||||
.wheel { width: 60%; }
|
.wheel { width: 60%; }
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
|
|
||||||
|
import { formatDate } from "@/lib/astro";
|
||||||
import type { Me } from "@/lib/types";
|
import type { Me } from "@/lib/types";
|
||||||
|
|
||||||
import { LogoutButton } from "./LogoutButton";
|
import { LogoutButton } from "./LogoutButton";
|
||||||
@@ -13,6 +14,41 @@ export function Logo() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The user's package and what is left of it; leads to the packages page. */
|
||||||
|
function PackageBadge({ me }: { me: Me }) {
|
||||||
|
const { natal, synastry } = me.analyses_left;
|
||||||
|
const left = natal + synastry;
|
||||||
|
let name: string;
|
||||||
|
let detail: string | null;
|
||||||
|
if (me.unlimited_until) {
|
||||||
|
name = "Korlátlan";
|
||||||
|
detail = `${formatDate(me.unlimited_until).replace(/\.$/, "")}-ig`;
|
||||||
|
} else if (me.package) {
|
||||||
|
name = me.package.name;
|
||||||
|
detail = left > 0 ? `${left} elemzés` : "elfogyott";
|
||||||
|
} else if (left > 0) {
|
||||||
|
name = `${left} elemzés`;
|
||||||
|
detail = null;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const units = me.unlimited_until
|
||||||
|
? "Korlátlan elemzés és összevetés"
|
||||||
|
: `Képletelemzés: ${natal}, kapcsolati elemzés: ${synastry}`;
|
||||||
|
const empty = !me.unlimited_until && left === 0;
|
||||||
|
return (
|
||||||
|
<Link
|
||||||
|
href="/csomagok"
|
||||||
|
className={empty ? "pkg-pill pkg-pill-empty" : "pkg-pill"}
|
||||||
|
title={units}
|
||||||
|
aria-label={`Csomagod: ${name}${detail ? `, ${detail}` : ""}. ${units}.`}
|
||||||
|
>
|
||||||
|
<span className="pkg-pill-name">{name}</span>
|
||||||
|
{detail && <span className="pkg-pill-detail">{detail}</span>}
|
||||||
|
</Link>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function SiteHeader({ me }: { me: Me }) {
|
export function SiteHeader({ me }: { me: Me }) {
|
||||||
const links = [
|
const links = [
|
||||||
{ href: "/keplet", label: "Képletem" },
|
{ href: "/keplet", label: "Képletem" },
|
||||||
@@ -27,7 +63,8 @@ export function SiteHeader({ me }: { me: Me }) {
|
|||||||
<Logo />
|
<Logo />
|
||||||
<NavLinks links={links} />
|
<NavLinks links={links} />
|
||||||
<div className="who">
|
<div className="who">
|
||||||
<span>{me.user.display_name || me.user.email}</span>
|
<PackageBadge me={me} />
|
||||||
|
<span className="who-name">{me.user.display_name || me.user.email}</span>
|
||||||
<LogoutButton />
|
<LogoutButton />
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ export interface Me {
|
|||||||
persons: PersonSlots;
|
persons: PersonSlots;
|
||||||
/** The unlimited package runs until then (null: none). */
|
/** The unlimited package runs until then (null: none). */
|
||||||
unlimited_until: string | null;
|
unlimited_until: string | null;
|
||||||
|
/** The running unlimited package, or the latest main package bought or given (null: none). */
|
||||||
|
package: { id: string; name: string; since: string } | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A package from the catalog (`GET /v1/packages`). */
|
/** A package from the catalog (`GET /v1/packages`). */
|
||||||
|
|||||||
Reference in New Issue
Block a user