Files
co-lite/solvway-admin-portal/app/api/promotions/paging.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

47 lines
1.8 KiB
TypeScript

// Shared paging helper (add-promotions-campaigns).
export function parsePaging(query: URLSearchParams): { page: number; size: number } {
const p = query.get("page");
const s = query.get("size");
const page = p !== null && !isNaN(parseInt(p, 10)) && parseInt(p, 10) >= 0 ? parseInt(p, 10) : 0;
const size = s !== null && !isNaN(parseInt(s, 10)) && parseInt(s, 10) > 0 ? parseInt(s, 10) : 20;
return { page, size };
}
/** `&sortBy=…&sortDir=…` suffix; the downstream service allow-lists the key. */
export function sortSuffix(query: URLSearchParams): string {
const by = query.get("sortBy");
if (!by) return "";
const dir = query.get("sortDir");
return `&sortBy=${encodeURIComponent(by)}${dir ? `&sortDir=${encodeURIComponent(dir)}` : ""}`;
}
/** `&k=v` suffix for each free-text/enum key present; the downstream service validates and escapes them. */
export function passThrough(query: URLSearchParams, keys: readonly string[]): string {
return keys.map(k => [k, query.get(k)] as const).filter(([, v]) => v).map(([k, v]) => `&${k}=${encodeURIComponent(v as string)}`).join("");
}
const DATE = /^\d{4}-\d{2}-\d{2}$/;
function validDate(v: string): boolean {
const d = new Date(v + "T00:00:00Z");
return DATE.test(v) && !isNaN(d.getTime()) && d.toISOString().slice(0, 10) === v;
}
/** Validated `&status=…&from=…&to=…` suffix for the keys in `keys`, or null when a present value is invalid. */
export function parseFilters(
query: URLSearchParams,
statuses: readonly { value: string }[],
keys: readonly ("status" | "from" | "to")[],
): string | null {
let out = "";
for (const key of keys) {
const v = query.get(key);
if (!v) continue;
const ok = key === "status" ? statuses.some(s => s.value === v) : validDate(v);
if (!ok) return null;
out += `&${key}=${encodeURIComponent(v)}`;
}
return out;
}