refactor: plan timeframes use a per-day-of-week picker (like the V2 tariff)

The timeframes model was a coarse weekday/weekend split, which couldn't express
"open Saturdays" or different rules on a specific day — and it didn't match the
V2 tariff, which already has a proper per-day-of-week picker (Hën–Die).

Replace PlanTimeframes { weekday, weekend } with { days[], fromMin, toMin }: the
allowed window applies only on the selected days (0=Sun..6=Sat; empty = every
day); on unselected days the subscriber parks free. A "night plan, free
weekends" is just days [Mon..Fri] with a 20:00→08:00 window — the exact case
from before, now expressible alongside any other day combination.

outOfWindowGap reworked to the days model (per-day membership test instead of
the weekend helper); the plans editor reuses the tariff composer's Mon-first
checkbox row and the shared tariff.dow0..6 labels. No production plans carry
timeframes yet (feature shipped today), so the shape changed directly with no
migration. Unit tests updated + extended (Saturday-only, every-day, weekday
night); 81 shared tests pass. Build+lint 12/12.

Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
This commit is contained in:
2026-06-20 18:43:21 +02:00
parent 21bd0f6227
commit e0e218fa61
8 changed files with 114 additions and 98 deletions
+5 -5
View File
@@ -27,13 +27,13 @@ interface PlanBody {
timeframes?: PlanTimeframes | null;
}
/** Validate the optional timeframes blob (minutes-of-day 0–1439, sane grace). */
/** Validate the optional timeframes blob (minutes-of-day 0–1439, days 0–6, sane grace). */
function validTimeframes(tf: PlanTimeframes | null | undefined): string | null {
if (tf == null) return null;
const okMin = (v: unknown) => v == null || (Number.isInteger(v) && (v as number) >= 0 && (v as number) <= 1439);
for (const dt of [tf.weekday, tf.weekend]) {
if (!dt) continue;
if (!dt.allDay && (!okMin(dt.fromMin) || !okMin(dt.toMin))) return "window times must be minutes-of-day (0–1439)";
const okMin = (v: unknown) => Number.isInteger(v) && (v as number) >= 0 && (v as number) <= 1439;
if (!okMin(tf.fromMin) || !okMin(tf.toMin)) return "window times must be minutes-of-day (0–1439)";
if (tf.days != null && (!Array.isArray(tf.days) || tf.days.some((d) => !Number.isInteger(d) || d < 0 || d > 6))) {
return "days must be integers 0–6 (0=Sun..6=Sat)";
}
if (tf.graceMin != null && (!Number.isInteger(tf.graceMin) || tf.graceMin < 0)) return "graceMin must be ≥ 0";
return null;
+46 -32
View File
@@ -23,18 +23,23 @@ const PERIOD_KEY: Record<SubscriptionPeriod, string> = {
month: "subs.perMonth",
};
// Day-of-week picker, Monday-first (mirrors the tariff composer). Labels come from the
// shared tariff.dow0..6 i18n keys (Hën..Die / Mon..Sun).
const DOW_ORDER = [1, 2, 3, 4, 5, 6, 0];
interface PlanForm {
planId: string; // blank on a brand-new plan; set when publishing a new version
name: string;
period: SubscriptionPeriod;
priceMajor: string;
currency: string;
// Timeframes (tariff bridge). Off → 24/7. On → weekday window (enter-after / exit-before
// as HH:MM) + weekend all-day toggle + grace minutes.
// Timeframes (tariff bridge). Off → 24/7. On → an allowed window (enter-after /
// exit-before as HH:MM) on the SELECTED days (0=Sun..6=Sat); on unselected days the
// subscriber parks free. Plus grace minutes.
restrictTimes: boolean;
wdFrom: string; // weekday window opens (HH:MM) — when the subscriber may enter
wdTo: string; // weekday window closes (HH:MM) — by when they should exit
weekendAllDay: boolean;
days: number[]; // days the window applies to; empty = every day
winFrom: string; // window opens (HH:MM) — when the subscriber may enter
winTo: string; // window closes (HH:MM) — by when they should exit
graceMin: string;
}
@@ -46,9 +51,9 @@ function emptyForm(): PlanForm {
priceMajor: "",
currency: DEFAULT_CURRENCY,
restrictTimes: false,
wdFrom: "20:00",
wdTo: "08:00",
weekendAllDay: true,
days: [1, 2, 3, 4, 5], // default Mon–Fri (the common "night plan, free weekends")
winFrom: "20:00",
winTo: "08:00",
graceMin: "0",
};
}
@@ -85,17 +90,19 @@ export function SubscriptionPlansManager() {
const major = Number(form.priceMajor);
if (!form.name.trim()) return setMsg({ kind: "err", text: t("plans.needName") });
if (!Number.isFinite(major) || major <= 0) return setMsg({ kind: "err", text: t("plans.needPrice") });
// Build the timeframes blob from the form (null = 24/7). The weekday window is the
// allowed interval [wdFrom, wdTo) (wraps midnight for a night plan); weekend is all-day
// or inherits the weekday window. The server stamps the site tz.
// Build the timeframes blob from the form (null = 24/7). The window [winFrom, winTo)
// (wraps midnight for a night plan) applies on the SELECTED days; unselected days are
// unrestricted. Empty days = every day. The server stamps the site tz.
let timeframes = null as Parameters<typeof createSubscriptionPlan>[0]["timeframes"];
if (form.restrictTimes) {
const from = hhmmToMin(form.wdFrom);
const to = hhmmToMin(form.wdTo);
const from = hhmmToMin(form.winFrom);
const to = hhmmToMin(form.winTo);
if (from == null || to == null) return setMsg({ kind: "err", text: t("plans.needWindow") });
if (form.days.length === 0) return setMsg({ kind: "err", text: t("plans.needDays") });
timeframes = {
weekday: { fromMin: from, toMin: to },
weekend: form.weekendAllDay ? { allDay: true } : { fromMin: from, toMin: to },
days: [...form.days].sort((a, b) => a - b),
fromMin: from,
toMin: to,
graceMin: Math.max(0, Math.round(Number(form.graceMin) || 0)),
};
}
@@ -126,7 +133,6 @@ export function SubscriptionPlansManager() {
/** Publish a new version of an existing plan (pre-fills its identity + last values). */
function newVersionOf(p: SubscriptionPlan) {
const tf = p.timeframes ?? null;
const wd = tf?.weekday;
setForm({
planId: p.planId,
name: p.name,
@@ -134,9 +140,9 @@ export function SubscriptionPlansManager() {
priceMajor: String(p.pricePerPeriodMinor / 100),
currency: p.currency,
restrictTimes: tf != null,
wdFrom: wd?.fromMin != null ? minToHHMM(wd.fromMin) : "20:00",
wdTo: wd?.toMin != null ? minToHHMM(wd.toMin) : "08:00",
weekendAllDay: tf?.weekend?.allDay ?? true,
days: tf?.days && tf.days.length > 0 ? [...tf.days] : [1, 2, 3, 4, 5],
winFrom: tf?.fromMin != null ? minToHHMM(tf.fromMin) : "20:00",
winTo: tf?.toMin != null ? minToHHMM(tf.toMin) : "08:00",
graceMin: String(tf?.graceMin ?? 0),
});
setMsg(null);
@@ -246,23 +252,31 @@ export function SubscriptionPlansManager() {
</label>
{form.restrictTimes && (
<div className="mt-2 grid grid-cols-[max-content_1fr] items-center gap-x-4 gap-y-2">
<label className="label">{t("plans.weekdayWindow")}</label>
<label className="label">{t("plans.days")}</label>
<span className="flex flex-wrap gap-2">
{DOW_ORDER.map((d) => (
<label key={d} className="inline-flex items-center gap-1 text-[12px] text-term-text">
<input
type="checkbox"
className="accent-term-amber"
checked={form.days.includes(d)}
onChange={() =>
setForm((f) =>
f && { ...f, days: f.days.includes(d) ? f.days.filter((x) => x !== d) : [...f.days, d] },
)
}
/>
{t(`tariff.dow${d}`)}
</label>
))}
</span>
<label className="label">{t("plans.window")}</label>
<span className="flex flex-wrap items-center gap-2 text-[12px] text-term-muted">
{t("plans.enterAfter")}
<input type="time" className="input w-28" value={form.wdFrom} onChange={(e) => setForm((f) => f && { ...f, wdFrom: e.target.value })} />
<input type="time" className="input w-28" value={form.winFrom} onChange={(e) => setForm((f) => f && { ...f, winFrom: e.target.value })} />
{t("plans.exitBefore")}
<input type="time" className="input w-28" value={form.wdTo} onChange={(e) => setForm((f) => f && { ...f, wdTo: e.target.value })} />
<input type="time" className="input w-28" value={form.winTo} onChange={(e) => setForm((f) => f && { ...f, winTo: e.target.value })} />
</span>
<label className="label">{t("plans.weekend")}</label>
<label className="flex items-center gap-2 text-[12px] text-term-text">
<input
type="checkbox"
className="accent-term-amber"
checked={form.weekendAllDay}
onChange={(e) => setForm((f) => f && { ...f, weekendAllDay: e.target.checked })}
/>
{t("plans.weekendAllDay")}
</label>
<label className="label">{t("plans.grace")}</label>
<span className="flex items-center gap-2">
<input className="input w-16" value={form.graceMin} inputMode="numeric" onChange={(e) => setForm((f) => f && { ...f, graceMin: e.target.value })} />
+6 -9
View File
@@ -494,16 +494,13 @@ export interface SubscriptionCredential {
}
export type SubscriptionPeriod = "day" | "week" | "month";
/** A subscriber's allowed parking window for a day-type (minutes-from-local-midnight).
* A scan outside the window is charged the transient tariff for the gap. */
export interface DayWindow {
allDay?: boolean;
fromMin?: number;
toMin?: number;
}
/** A subscriber's allowed parking window (minutes-from-local-midnight) on selected days.
* A scan outside the window is charged the transient tariff for the gap. days: 0=Sun..6=Sat
* (empty = every day); the window [fromMin,toMin) wraps past midnight when toMin ≤ fromMin. */
export interface PlanTimeframes {
weekday?: DayWindow;
weekend?: DayWindow;
days?: number[];
fromMin: number;
toMin: number;
graceMin?: number;
tz?: string;
}
+3 -3
View File
@@ -469,12 +469,12 @@ export const en: Catalog = {
saved: "Plan saved.",
confirmRetire: "Retire the plan “{{name}}”? It will no longer be sellable (history is kept).",
needWindow: "Enter valid window times (HH:MM).",
needDays: "Select at least one day for the window.",
restrictTimes: "Restrict parking times (charge transient tariff outside the window)",
weekdayWindow: "Weekday",
days: "Days",
window: "Window",
enterAfter: "enter after",
exitBefore: "· exit before",
weekend: "Weekend",
weekendAllDay: "all day (no restriction)",
grace: "Grace",
graceHint: "minutes tolerance around the window edges",
timeframesHint: "A scan outside the allowed window is charged the normal transient tariff for the out-of-window minutes (early entry is deferred to exit; late exit is gated until paid).",
+3 -3
View File
@@ -480,12 +480,12 @@ export const sq = {
saved: "Plani u ruajt.",
confirmRetire: "Të tërhiqet plani “{{name}}”? Nuk do të jetë më i shitshëm (historiku ruhet).",
needWindow: "Shkruaj orare të vlefshme (HH:MM).",
needDays: "Zgjidh të paktën një ditë për intervalin.",
restrictTimes: "Kufizo oraret e parkimit (tarifë kalimtare jashtë intervalit)",
weekdayWindow: "Ditë pune",
days: "Ditët",
window: "Intervali",
enterAfter: "hyrje pas",
exitBefore: "· dalje para",
weekend: "Fundjavë",
weekendAllDay: "gjithë ditën (pa kufizim)",
grace: "Tolerancë",
graceHint: "minuta tolerancë rreth kufijve të intervalit",
timeframesHint: "Një skanim jashtë intervalit të lejuar tarifohet me tarifën normale kalimtare për minutat jashtë intervalit (hyrja e hershme shtyhet në dalje; dalja e vonuar bllokohet derisa paguhet).",