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

97 lines
4.3 KiB
TypeScript

import { apiGet, apiPost } from "@/app/lib/clientApi";
import type { ProductListPage, ProductListRow, ProductOptions, QianyiSkuOption } from "@/app/lib/types";
import type { CatalogExport, CatalogImportRequest, CatalogImportResult } from "@/app/lib/catalogImport";
// Client-side wrapper for the /products management page (add-admin-product-setup, Task E). Calls
// only the portal's own /api/products/** route handlers (mirrors app/lib/services/adminCatalog.ts
// and adminBulletins.ts) — never solvway-mobile-api directly. Toggling status is a POST (mutation),
// never a GET, so a Next.js prefetch can never flip a product's status. The catalogue setup page's
// loads and saves live in adminCatalogSetup.ts.
export const PRODUCT_STATUS_FILTER_OPTIONS = [{ value: "Y", labelKey: "CatActive" }, { value: "N", labelKey: "CatInactive" }];
export interface ProductListParams {
/** 0-based, matching AdminProductController.list(page, size, sortBy, sortDir) directly — there
* is no 1-based/0-based translation layer here (unlike bulletins) since the page component
* filters on the server; see app/products/page.tsx. */
page?: number;
pageSize?: number;
sortBy?: string;
sortDir?: "asc" | "desc";
/** Server-side filters: q matches code or name; status Y|N; brand/category are catalogue codes. */
q?: string;
status?: string;
brand?: string;
category?: string;
}
export interface ProductListResult {
items: ProductListRow[];
total: number;
}
function buildListQuery(params: ProductListParams): string {
const qs = new URLSearchParams();
if (params.page != null) qs.set("page", String(params.page));
if (params.pageSize != null) qs.set("size", String(params.pageSize));
if (params.sortBy) qs.set("sortBy", params.sortBy);
if (params.sortDir) qs.set("sortDir", params.sortDir);
for (const key of ["q", "status", "brand", "category"] as const) if (params[key]) qs.set(key, params[key]);
return qs.toString();
}
export async function listProducts(params: ProductListParams = {}): Promise<ProductListResult> {
const query = buildListQuery(params);
const page = await apiGet<ProductListPage>(`/api/products${query ? `?${query}` : ""}`);
return { items: page.content ?? [], total: page.totalElements ?? 0 };
}
/** Toggle Activate/Deactivate: POST, no body — the id is in the path (app/api/products/[id]/route.ts).
* Returns the toggled Sales Item view; the list page reloads rather than reading it. */
export async function toggleProductStatus(id: string): Promise<unknown> {
return apiPost<unknown>(`/api/products/${id}`);
}
export async function getProductOptions(): Promise<ProductOptions> {
return apiGet<ProductOptions>("/api/products/options");
}
/** Every tag in use, for the Product Setup tag suggestions (add-storefront-collections). */
export async function listProductTags(): Promise<string[]> {
return apiGet<string[]>("/api/products/tags");
}
/** Pickable QianYi SKUs matching `q` (code, title or barcode), at most 20 (add-qianyi-sku-link). */
export async function searchQianyiSkus(q: string): Promise<QianyiSkuOption[]> {
return apiGet<QianyiSkuOption[]>(`/api/products/qianyi-skus?q=${encodeURIComponent(q)}`);
}
/**
* Uploads a product image (SAME-REQUEST lifecycle, mirrors uploadBulletinImage): the mobile-api
* upload is SAME-REQUEST (file_autocommit=Y), so the returned public URL can be persisted onto the
* product only by the subsequent aggregate save.
*/
export async function uploadProductImage(input: {
fileName: string;
fileType: string;
base: string;
}): Promise<{ url: string }> {
return apiPost<{ url: string }>("/api/products/image", input);
}
/** Check a catalogue import (POST /api/products/import/validate). */
export async function checkCatalogImport(request: CatalogImportRequest): Promise<CatalogImportResult> {
return apiPost<CatalogImportResult>("/api/products/import/validate", request);
}
/** Commit a catalogue import (POST /api/products/import/commit). */
export async function commitCatalogImport(request: CatalogImportRequest): Promise<CatalogImportResult> {
return apiPost<CatalogImportResult>("/api/products/import/commit", request);
}
/** Every stored product as import-file rows, plus what the file leaves out. Read-only. */
export async function exportCatalogue(country: string): Promise<CatalogExport> {
return apiGet<CatalogExport>(`/api/products/import/export?country=${encodeURIComponent(country)}`);
}