Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
// Report periods are calendar dates in Malaysia time (UTC+8, no DST), never the browser's or UTC's date:
|
|
// at 02:00 in Malaysia the UTC date is still yesterday, which is exactly when "today's orders" go missing.
|
|
|
|
export type PeriodPreset = "all" | "month" | "lastMonth" | "30d" | "year";
|
|
|
|
export interface DateRange {
|
|
from: string;
|
|
to: string;
|
|
}
|
|
|
|
/**
|
|
* A backend `LocalDateTime` as an instant. The API sends it without an offset and the value IS Malaysia wall
|
|
* time, so a zone-less value ("2026-10-27T00:15:00", "2026-10-27 00:15", "2026-10-27") is pinned to +08:00;
|
|
* a zoned value is read as-is. Unparseable input gives an Invalid Date, as `new Date` does.
|
|
*/
|
|
export function malaysiaInstant(value: string): Date {
|
|
const s = value.replace(" ", "T");
|
|
if (/[zZ]|[+-]\d{2}:?\d{2}$/.test(s)) return new Date(s);
|
|
return new Date(`${/^\d{4}-\d{2}-\d{2}$/.test(s) ? `${s}T00:00:00` : s}+08:00`);
|
|
}
|
|
|
|
/** Today's date in Malaysia as `yyyy-MM-dd`. */
|
|
export function malaysiaToday(now: Date = new Date()): string {
|
|
return new Intl.DateTimeFormat("en-CA", { timeZone: "Asia/Kuala_Lumpur" }).format(now);
|
|
}
|
|
|
|
const iso = (d: Date) => d.toISOString().slice(0, 10);
|
|
|
|
/** Inclusive `yyyy-MM-dd` bounds for a preset; empty strings mean unbounded. */
|
|
export function periodRange(preset: PeriodPreset, now: Date = new Date()): DateRange {
|
|
const today = malaysiaToday(now);
|
|
const [y, m] = today.split("-").map(Number);
|
|
switch (preset) {
|
|
case "month":
|
|
return { from: iso(new Date(Date.UTC(y, m - 1, 1))), to: iso(new Date(Date.UTC(y, m, 0))) };
|
|
case "lastMonth":
|
|
return { from: iso(new Date(Date.UTC(y, m - 2, 1))), to: iso(new Date(Date.UTC(y, m - 1, 0))) };
|
|
case "30d": {
|
|
const d = new Date(`${today}T00:00:00Z`);
|
|
d.setUTCDate(d.getUTCDate() - 30);
|
|
return { from: iso(d), to: today };
|
|
}
|
|
case "year":
|
|
return { from: `${y}-01-01`, to: today };
|
|
default:
|
|
return { from: "", to: "" };
|
|
}
|
|
}
|