Files
co-lite/solvway-admin-portal/app/lib/services/messages.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

116 lines
4.8 KiB
TypeScript

import { apiGet, apiPost, apiPut } from "@/app/lib/clientApi";
import type { Translate } from "@/app/lib/i18n/t";
import type { Message, MessageArea, MessageImportResult } from "@/app/lib/types";
// Client-side wrapper for the /messages management page. Calls only the
// portal's own /api/messages/** route handlers (spec: "Client calls SHALL go
// through the portal's own app/api/messages/** route handlers, which forward
// the admin JWT from the session cookie") — never solvway-mobile-api directly.
export interface MessageListParams {
searchKey?: string;
area?: MessageArea;
/** Only messages lacking this locale's wording (the backend supports zh_CN only). */
missingLocale?: "zh_CN";
/** 1-based, matching this portal's other list pages (e.g. Members). */
page: number;
pageSize: number;
/** Server-side sort: attribute name + direction; sent only when present. */
sortBy?: string;
sortDir?: "asc" | "desc";
}
export interface MessageListResult {
items: Message[];
total: number;
}
/** Friendly labels for the GeneralEnum.Area codes offered here (see app/lib/types.ts); B (stockist) is retired. */
export const AREA_OPTIONS: { value: MessageArea; label: string }[] = [
{ value: "A", label: "Admin" },
{ value: "AP", label: "MsgsAreaCustomerPortal" },
];
/** The translated name of a message area; an unknown area code is shown as-is. */
export const areaLabel = (area: string, t: Translate) => {
const match = AREA_OPTIONS.find((o) => o.value === area);
return match ? t(match.label) : area;
};
/**
* Builds the query string for GET /api/messages. `page` is 1-based here (this
* portal's list-page convention) but mobile-api's `/api/admin/messages` (and
* services-system beneath it) is 0-based, so it is translated at the boundary.
*/
export function buildMessagesQuery(params: MessageListParams): string {
const qs = new URLSearchParams();
if (params.searchKey) qs.set("searchKey", params.searchKey);
if (params.area) qs.set("area", params.area);
if (params.missingLocale) qs.set("missingLocale", params.missingLocale);
qs.set("page", String(Math.max(0, params.page - 1)));
qs.set("size", String(params.pageSize));
if (params.sortBy) qs.set("sortBy", params.sortBy);
if (params.sortDir && params.sortBy) qs.set("sortDir", params.sortDir);
return qs.toString();
}
/**
* Normalizes the raw upstream page response into `{items, total}`. The exact
* wire shape of services-system's `Page<Message>` was unverified against a
* live upstream as of Task 1 (see task-1-report.md), so this accepts both
* Spring Data's default `{content, totalElements}` shape and a simpler
* `{items, total}` shape, defaulting to an empty page for anything else.
*/
export function normalizeMessagePage(raw: unknown): MessageListResult {
if (!raw || typeof raw !== "object") return { items: [], total: 0 };
const obj = raw as Record<string, unknown>;
const items: Message[] = Array.isArray(obj.content)
? (obj.content as Message[])
: Array.isArray(obj.items)
? (obj.items as Message[])
: [];
const total =
typeof obj.totalElements === "number"
? obj.totalElements
: typeof obj.total === "number"
? obj.total
: items.length;
return { items, total };
}
export async function listMessages(params: MessageListParams): Promise<MessageListResult> {
const raw = await apiGet<unknown>(`/api/messages?${buildMessagesQuery(params)}`);
return normalizeMessagePage(raw);
}
export async function getMessage(id: number): Promise<Message> {
return apiGet<Message>(`/api/messages/${id}`);
}
/** Upsert: `payload.id` present -> update, absent -> insert (see spec + Task 1). */
export async function saveMessage(payload: Message): Promise<Message> {
return apiPost<Message>("/api/messages", payload);
}
/** Create: always POSTs without an id (add-admin-i18n-currency, Task 2). */
export async function createMessage(payload: Omit<Message, "id">): Promise<Message> {
return apiPost<Message>("/api/messages", payload);
}
/** Update: PUTs to the record's own URL; the id is in the path, not the body. */
export async function updateMessage(id: number, payload: Omit<Message, "id">): Promise<Message> {
return apiPut<Message>(`/api/messages/${id}`, payload);
}
/** Raw catalog export for the requested area, or A then AP when none is given (round-trips through import). */
export async function exportMessages(area?: MessageArea): Promise<Message[]> {
if (area) return apiGet<Message[]>(`/api/messages/export?area=${area}`);
const [admin, portal] = await Promise.all([exportMessages("A"), exportMessages("AP")]);
return [...admin, ...portal];
}
/** Bulk catalog upsert; returns the per-row summary (created/updated/errors). */
export async function importMessages(rows: Message[]): Promise<MessageImportResult> {
return apiPost<MessageImportResult>("/api/messages/import", rows);
}