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 { const query = buildListQuery(params); const page = await apiGet(`/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 { return apiPost(`/api/products/${id}`); } export async function getProductOptions(): Promise { return apiGet("/api/products/options"); } /** Every tag in use, for the Product Setup tag suggestions (add-storefront-collections). */ export async function listProductTags(): Promise { return apiGet("/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 { return apiGet(`/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 { return apiPost("/api/products/import/validate", request); } /** Commit a catalogue import (POST /api/products/import/commit). */ export async function commitCatalogImport(request: CatalogImportRequest): Promise { return apiPost("/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 { return apiGet(`/api/products/import/export?country=${encodeURIComponent(country)}`); }