| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { API_V1, CREDENTIALS } from "../apiContract"; |
|
|
| export interface StatementRow { |
| Customer: string; |
| Email: string; |
| Tier: string; |
| Overdue: number; |
| Receivable: number; |
| [k: string]: unknown; |
| } |
|
|
| export interface StatementTemplates { |
| subject: string; |
| intro: string; |
| footer: string; |
| } |
|
|
| export interface StatementsPayload { |
| rows: StatementRow[]; |
| loadedAt: string; |
| safeMode: boolean; |
| safeRecipients: string[]; |
| sender: { name: string; email: string; replyTo: string; company: string }; |
| templates: StatementTemplates; |
| tiers: string[]; |
| } |
|
|
| export interface SendOutcome { |
| sent: Array<{ customer: string; to: string; mailId: number }>; |
| failed: Array<{ customer: string; error: string }>; |
| skipped: Array<{ customer: string; reason: string }>; |
| safeMode: boolean; |
| test: boolean; |
| } |
|
|
| type Res<T> = { ok: true; data: T } | { ok: false; message: string }; |
|
|
| async function call<T>(path: string, init?: RequestInit): Promise<Res<T>> { |
| try { |
| const r = await fetch(`${API_V1}${path}`, { credentials: CREDENTIALS, ...init }); |
| const body = await r.json().catch(() => null); |
| if (!r.ok) { |
| const msg = body?.error?.message || `request failed (${r.status})`; |
| return { ok: false, message: msg }; |
| } |
| return { ok: true, data: body as T }; |
| } catch (e) { |
| return { ok: false, message: e instanceof Error ? e.message : "network error" }; |
| } |
| } |
|
|
| export function loadStatements(refresh = false) { |
| return call<StatementsPayload>(`/admin/statements${refresh ? "?refresh=1" : ""}`); |
| } |
|
|
| export function previewStatement(customer: string, templates: StatementTemplates) { |
| return call<{ html: string; to: string; subject: string }>("/admin/statements/preview", { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ customer, templates }), |
| }); |
| } |
|
|
| |
| export function sendStatements( |
| customers: string[], |
| templates: StatementTemplates, |
| overrideTo?: string, |
| ) { |
| return call<SendOutcome>("/admin/statements/send", { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ customers, templates, overrideTo: overrideTo || undefined }), |
| }); |
| } |
|
|