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
225 lines
9.1 KiB
TypeScript
225 lines
9.1 KiB
TypeScript
import { apiGet, apiPost, ApiError } from "@/app/lib/clientApi";
|
|
import { formatPoints } from "@/app/lib/format";
|
|
import type { WebOrderPage, WebOrderDetail } from "@/app/lib/types";
|
|
import type { Translate } from "@/app/lib/i18n/t";
|
|
|
|
// Client-side wrappers for the Sales Listing BFF routes (enhance-web-orders-sales-listing).
|
|
// Calls only the portal's own /api/web-orders* route handlers, which forward the admin JWT to
|
|
// mobile-api's /api/admin/webOrders/* endpoints. Reads are GET (no side effect — App Router
|
|
// prefetch-safe); the invoice download is triggered by an explicit click, never a prefetch.
|
|
|
|
/** User-facing order types (the "kind" a member sees), each mapped to backend filter keys. */
|
|
export type OrderType = "WEB" | "EVENT" | "COUNTER" | "OUTLET";
|
|
|
|
export const ORDER_TYPE_OPTIONS: { value: OrderType; label: string }[] = [
|
|
{ value: "WEB", label: "WebOrdTypeWeb" },
|
|
{ value: "EVENT", label: "WebOrdTypeEvent" },
|
|
{ value: "COUNTER", label: "WebOrdTypeCounter" },
|
|
{ value: "OUTLET", label: "Outlet" },
|
|
];
|
|
|
|
/**
|
|
* Single source of truth for Order Type: two orthogonal axes — saleChannel (kind) and
|
|
* sourcePlatform (origin; omitted = any). Both the listing filter and the row classifier read this.
|
|
*/
|
|
const ORDER_TYPE_AXES: Record<OrderType, { saleChannel: string; sourcePlatform?: string }> = {
|
|
WEB: { saleChannel: "NORMAL", sourcePlatform: "CP" },
|
|
COUNTER: { saleChannel: "NORMAL", sourcePlatform: "BO" },
|
|
EVENT: { saleChannel: "EVENT" },
|
|
OUTLET: { saleChannel: "OUTLET" },
|
|
};
|
|
|
|
/** Maps a chosen Order Type to the backend property keys. */
|
|
export function orderTypeToFilter(type: OrderType): { saleChannel?: string; sourcePlatform?: string } {
|
|
return ORDER_TYPE_AXES[type];
|
|
}
|
|
|
|
// Full-name labels for every OrderStatus code (solvway-core OrderEnum.OrderStatus), so the listing
|
|
// and the order detail never show a bare code. Order matters — it drives the status filter option order.
|
|
export const ORDER_STATUS_LABEL: Record<string, string> = {
|
|
P: "CatPending",
|
|
Y: "WebOrdStatusActive",
|
|
H: "WebOrdStatusHold",
|
|
V: "WebOrdStatusVoided",
|
|
F: "CatFailed",
|
|
C: "CANCELLED",
|
|
N: "WebOrdStatusInactive",
|
|
FP: "WebOrdStatusFailedWithPayment",
|
|
U: "WebOrdStatusUnknown",
|
|
};
|
|
|
|
export const ORDER_STATUS_TONE: Record<string, "positive" | "warning" | "negative" | "neutral"> = {
|
|
P: "warning",
|
|
Y: "positive",
|
|
H: "warning",
|
|
V: "negative",
|
|
F: "negative",
|
|
C: "negative",
|
|
N: "neutral",
|
|
FP: "negative",
|
|
U: "neutral",
|
|
};
|
|
|
|
/** The order's user-facing type from its two axes (saleChannel kind, sourcePlatform origin), or null. */
|
|
export function orderTypeOf(
|
|
saleChannel: string | null | undefined,
|
|
sourcePlatform: string | null | undefined,
|
|
): OrderType | null {
|
|
const kind = (saleChannel ?? "").toUpperCase();
|
|
const origin = (sourcePlatform ?? "").toUpperCase();
|
|
const hit = (Object.keys(ORDER_TYPE_AXES) as OrderType[]).find(t => {
|
|
const axes = ORDER_TYPE_AXES[t];
|
|
return axes.saleChannel === kind && (!axes.sourcePlatform || axes.sourcePlatform === origin);
|
|
});
|
|
return hit ?? null;
|
|
}
|
|
|
|
export const ORDER_TYPE_LABEL: Record<OrderType, string> = {
|
|
WEB: "WebOrdTypeWeb",
|
|
EVENT: "WebOrdTypeEvent",
|
|
COUNTER: "WebOrdTypeCounter",
|
|
OUTLET: "Outlet",
|
|
};
|
|
|
|
export function orderTypeLabel(
|
|
saleChannel: string | null | undefined,
|
|
sourcePlatform: string | null | undefined,
|
|
t: Translate,
|
|
): string {
|
|
const type = orderTypeOf(saleChannel, sourcePlatform);
|
|
return type ? t(ORDER_TYPE_LABEL[type]) : saleChannel || "—";
|
|
}
|
|
|
|
export type CategoryTone = "contrast" | "teal" | "violet" | "neutral";
|
|
|
|
/**
|
|
* Pill tone per order type (feedback 2026-10-21: every Type pill was the same beige). A kind, not a
|
|
* state, so it never uses the status tones; the pill always carries its label too.
|
|
*/
|
|
export const ORDER_TYPE_TONE: Record<OrderType, CategoryTone> = {
|
|
WEB: "contrast",
|
|
EVENT: "violet",
|
|
OUTLET: "teal",
|
|
COUNTER: "neutral",
|
|
};
|
|
|
|
export function orderTypeTone(saleChannel: string | null | undefined, sourcePlatform: string | null | undefined): CategoryTone {
|
|
const type = orderTypeOf(saleChannel, sourcePlatform);
|
|
return type ? ORDER_TYPE_TONE[type] : "neutral";
|
|
}
|
|
|
|
/**
|
|
* Formats a backend money string for display: "{symbol} {amount}" with two decimals and grouped
|
|
* thousands (same as formatRM), so raw backend doubles ("150.0") render cleanly. Falls back to the raw value if it isn't numeric,
|
|
* and to "—" when absent.
|
|
*/
|
|
export function formatMoney(currency: string | null | undefined, amount: string | null | undefined): string {
|
|
if (amount === null || amount === undefined || amount === "") return "—";
|
|
const n = Number(amount);
|
|
const text = Number.isFinite(n) ? formatPoints(n) : amount;
|
|
return currency ? `${currency} ${text}` : text;
|
|
}
|
|
|
|
export interface WebOrderListParams {
|
|
page?: number;
|
|
size?: number;
|
|
saleChannel?: string;
|
|
sourcePlatform?: string;
|
|
status?: string;
|
|
documentNo?: string;
|
|
buyerCode?: string;
|
|
fulfilment?: string;
|
|
dateFrom?: string;
|
|
dateTo?: string;
|
|
eventId?: string;
|
|
mine?: string;
|
|
stalePending?: boolean;
|
|
sortBy?: string;
|
|
sortDir?: "asc" | "desc";
|
|
}
|
|
|
|
/** GET /api/web-orders — paged cross-channel Sales Listing with optional filters. */
|
|
export async function listWebOrders(params: WebOrderListParams = {}): Promise<WebOrderPage> {
|
|
const qs = new URLSearchParams();
|
|
if (params.page !== undefined) qs.set("page", String(params.page));
|
|
if (params.size !== undefined) qs.set("size", String(params.size));
|
|
if (params.saleChannel) qs.set("saleChannel", params.saleChannel);
|
|
if (params.sourcePlatform) qs.set("sourcePlatform", params.sourcePlatform);
|
|
if (params.status) qs.set("status", params.status);
|
|
if (params.documentNo) qs.set("documentNo", params.documentNo);
|
|
if (params.buyerCode) qs.set("buyerCode", params.buyerCode);
|
|
if (params.fulfilment) qs.set("fulfilment", params.fulfilment);
|
|
if (params.dateFrom) qs.set("dateFrom", params.dateFrom);
|
|
if (params.dateTo) qs.set("dateTo", params.dateTo);
|
|
if (params.eventId) qs.set("eventId", params.eventId);
|
|
if (params.mine) qs.set("mine", params.mine);
|
|
if (params.stalePending) qs.set("stalePending", "Y");
|
|
if (params.sortBy) qs.set("sortBy", params.sortBy);
|
|
if (params.sortBy && params.sortDir) qs.set("sortDir", params.sortDir);
|
|
const suffix = qs.toString() ? `?${qs.toString()}` : "";
|
|
return apiGet<WebOrderPage>(`/api/web-orders${suffix}`);
|
|
}
|
|
|
|
/** GET /api/web-orders/{uuid} — one order's full detail (line items + totals + fulfilment). */
|
|
export async function getWebOrder(uuid: string): Promise<WebOrderDetail> {
|
|
return apiGet<WebOrderDetail>(`/api/web-orders/${encodeURIComponent(uuid)}`);
|
|
}
|
|
|
|
/**
|
|
* POST /api/web-orders/{uuid}/void-pending — staff void of an order still pending past counter's stale age
|
|
* (reconcile-stale-pending-orders). Rejects (ApiError 409) with counter's reason when it refuses.
|
|
*/
|
|
export async function voidPendingOrder(uuid: string): Promise<{ status?: string }> {
|
|
return apiPost<{ status?: string }>(`/api/web-orders/${encodeURIComponent(uuid)}/void-pending`, {});
|
|
}
|
|
|
|
/** Reads the invoice `Content-Disposition` filename, falling back to a uuid-based name. */
|
|
function invoiceFilename(disposition: string | null, uuid: string): string {
|
|
const match = disposition?.match(/filename="?([^"]+)"?/i);
|
|
return match?.[1] ?? `invoice-${uuid}.pdf`;
|
|
}
|
|
|
|
/**
|
|
* Downloads the order's invoice PDF and triggers a browser save. A GET with no side effect
|
|
* (it only fetches the rendered PDF); authorization is the admin module grant, enforced
|
|
* server-side. Rejects (ApiError) on any non-OK response.
|
|
*/
|
|
export async function downloadInvoice(uuid: string): Promise<void> {
|
|
const res = await fetch(`/api/web-orders/${encodeURIComponent(uuid)}/invoice`, { cache: "no-store" });
|
|
if (!res.ok) {
|
|
throw new ApiError(
|
|
res.status === 404 ? "WebOrdInvoiceNotFound" : "WebOrdInvoiceDownloadRetry",
|
|
res.status,
|
|
);
|
|
}
|
|
const blob = await res.blob();
|
|
const url = URL.createObjectURL(blob);
|
|
try {
|
|
const a = document.createElement("a");
|
|
a.href = url;
|
|
a.download = invoiceFilename(res.headers.get("content-disposition"), uuid);
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
} finally {
|
|
// Defer the revoke: invalidating the blob URL synchronously right after click() can cancel the
|
|
// download in some browsers before it has started.
|
|
setTimeout(() => URL.revokeObjectURL(url), 0);
|
|
}
|
|
}
|
|
|
|
export const ORDER_STATUS_FILTER_OPTIONS = (["P", "Y", "V", "FP"] as const).map(value => ({ value, label: ORDER_STATUS_LABEL[value] }));
|
|
|
|
// Labels are message codes; pages translate them with t(). TAKE_NOW is a POS sale handed over at the till (BABYBOO-45).
|
|
export const FULFILMENT_FILTER_OPTIONS = [
|
|
{ value: "DELIVERY", label: "Delivery" },
|
|
{ value: "PICKUP", label: "Pickup" },
|
|
{ value: "TAKE_NOW", label: "TakeNow" },
|
|
] as const;
|
|
|
|
/** Message code for each fulfilment mode the backend reports. */
|
|
export const FULFILMENT_LABEL_CODE: Record<string, string> = { PICKUP: "Pickup", DELIVERY: "Delivery", TAKE_NOW: "TakeNow" };
|
|
|
|
/** Pill tone per fulfilment mode, matching the order-type tones (Delivery like Web, Pickup like Outlet). */
|
|
export const FULFILMENT_TONE: Record<string, CategoryTone> = { DELIVERY: "contrast", PICKUP: "teal", TAKE_NOW: "neutral" };
|