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

46 lines
2.1 KiB
TypeScript

import * as XLSX from "xlsx";
// SheetJS 0.18 types SSF, 0.20 types it `any`; this keeps the two calls checked under both.
interface SsfDates {
is_date(fmt: string): boolean;
parse_date_code(v: number): { y: number; m: number; d: number } | null;
}
const SSF: SsfDates = XLSX.SSF;
const pad = (n: number) => String(n).padStart(2, "0");
/**
* sheet_to_json rows, with Excel date-formatted cells in `dateColumn` turned into YYYY-MM-DD text. Only
* numeric cells whose number format is a date are decoded (SSF, no Date, so no time-zone shift: SheetJS
* cellDates is off by a day east of UTC). The workbook must be read with `cellNF: true` so cells keep
* their format. Any other number stays a number, for the caller's validation to reject.
*
* SheetJS 0.20 sheet_to_json turns every numeric, text or boolean cell whose `z` is a date format into a
* local-time Date (0.18 did not), which would corrupt other date-formatted columns, e.g. Batch Number. So
* sheet_to_json reads a copy without `z` on every cell and returns raw values; the date column is decoded here.
*/
export function readSheetRows(sheet: XLSX.WorkSheet, dateColumn: string): Record<string, unknown>[] {
// blankrows keeps row i at sheet row start + 1 + i, so rows line up with their cells.
const plain: XLSX.WorkSheet = {};
for (const [k, v] of Object.entries(sheet)) {
plain[k] = k.startsWith("!") ? v : { ...v, z: undefined };
}
const rows = XLSX.utils.sheet_to_json<Record<string, unknown>>(plain, { blankrows: true });
const ref = sheet["!ref"];
if (!ref) return rows;
const { s, e } = XLSX.utils.decode_range(ref);
let col = -1;
for (let c = s.c; c <= e.c; c++) {
if (String(sheet[XLSX.utils.encode_cell({ r: s.r, c })]?.v ?? "").trim() === dateColumn) col = c;
}
if (col < 0) return rows;
rows.forEach((row, i) => {
const cell = sheet[XLSX.utils.encode_cell({ r: s.r + 1 + i, c: col })];
if (cell?.t === "n" && SSF.is_date(String(cell.z ?? ""))) {
const d = SSF.parse_date_code(cell.v as number);
if (d) row[dateColumn] = `${d.y}-${pad(d.m)}-${pad(d.d)}`;
}
});
return rows;
}