JEI_Import_Documents-App/app/lib/pb.ts
Mavis 922cd01dd4 chore: rename JEI invoice PDFs to invoice numbers + dedupe
- 22 files renamed to <invoice_no>.pdf (154923.pdf, 151778.pdf, etc.)
- 5 duplicate files moved to invoices/_non_jei/
- 7 files left as-is (multi-invoice PDFs or unscannable:
  1.Invoice_29.05.pdf, Invoice.pdf, JEI Drilling Invoices.pdf,
  JEI SA Invoice 147842.pdf, JEI SA Proforma 148931.pdf,
  newdoc10018920260413143327_001.pdf, Supplier Invoice.pdf,
  Supplier Invoices.pdf)
2026-09-18 17:55:12 +02:00

45 lines
1.6 KiB
TypeScript

import PocketBase from "pocketbase";
// Singleton PocketBase client. Created lazily so it picks up env at runtime,
// not at build time (Next.js server-side rendering).
let _pb: PocketBase | null = null;
export function getPb(): PocketBase {
if (_pb) return _pb;
const url =
process.env.NEXT_PUBLIC_PB_URL ||
process.env.PB_URL ||
"https://pb.agent.apphub.co.za";
// IMPORTANT: PocketBase JS SDK uses the global `fetch` under the hood.
// Next.js patches fetch with a cache layer that returns stale data unless
// we explicitly opt out. Use the SDK's `beforeSend` hook (v0.21+) to disable
// caching on every request — there is no `fetch` option in the Client
// constructor (its 2nd arg is the authStore).
_pb = new PocketBase(url);
_pb.beforeSend = function (_url: string, options: any) {
options.cache = "no-store";
return { url: _url, options };
};
const token = process.env.PB_TOKEN;
if (token) {
_pb.authStore.save(token);
}
return _pb;
}
// Helper: format a number as £ currency (just a prefix — values stay as numbers)
export function fmtGBP(n: number | undefined | null): string {
if (n === undefined || n === null || isNaN(n)) return "£0.00";
return "£" + n.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
export function fmtNum(n: number | undefined | null): string {
if (n === undefined || n === null || isNaN(n)) return "0";
return n.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
export function fmtDate(iso: string | undefined | null): string {
if (!iso) return "";
// PB date format is "YYYY-MM-DD HH:MM:SS.SSSZ"; show just the date part
return iso.substring(0, 10);
}