barsa 7cefee4c75 Update .gitignore and enhance CSRF controller and WHMCS services
- Added exceptions to .gitignore for the portal public directory to ensure proper asset management.
- Marked CSRF token retrieval and refresh endpoints as public for improved access control.
- Refactored WHMCS invoice service to enhance error logging and processing of invoice records.
- Updated WHMCS raw types to introduce a common schema for invoices, improving data consistency and validation.
- Enhanced Salesforce order item transformation to streamline billing cycle handling.
2025-10-21 16:30:52 +09:00

154 lines
4.1 KiB
TypeScript

/**
* WHMCS Billing Provider - Mapper
*
* Transforms raw WHMCS invoice data into normalized billing domain types.
*/
import type { Invoice, InvoiceItem } from "../../contract";
import { invoiceSchema } from "../../schema";
import {
type WhmcsInvoiceRaw,
whmcsInvoiceRawSchema,
type WhmcsInvoiceListItem,
whmcsInvoiceListItemSchema,
type WhmcsInvoiceItemsRaw,
whmcsInvoiceItemsRawSchema,
} from "./raw.types";
export interface TransformInvoiceOptions {
defaultCurrencyCode?: string;
defaultCurrencySymbol?: string;
}
// Status mapping from WHMCS to domain
const STATUS_MAP: Record<string, Invoice["status"]> = {
draft: "Draft",
pending: "Pending",
"payment pending": "Pending",
paid: "Paid",
unpaid: "Unpaid",
cancelled: "Cancelled",
canceled: "Cancelled",
overdue: "Overdue",
refunded: "Refunded",
collections: "Collections",
};
function mapStatus(status: string): Invoice["status"] {
const normalized = status?.trim().toLowerCase();
if (!normalized) {
throw new Error("Invoice status missing");
}
const mapped = STATUS_MAP[normalized];
if (!mapped) {
throw new Error(`Unsupported WHMCS invoice status: ${status}`);
}
return mapped;
}
function parseAmount(amount: string | number | undefined): number {
if (typeof amount === "number") {
return amount;
}
if (!amount) {
return 0;
}
const cleaned = String(amount).replace(/[^\d.-]/g, "");
const parsed = Number.parseFloat(cleaned);
return Number.isNaN(parsed) ? 0 : parsed;
}
function formatDate(input?: string): string | undefined {
if (!input) {
return undefined;
}
const date = new Date(input);
if (Number.isNaN(date.getTime())) {
return undefined;
}
return date.toISOString();
}
function mapItems(rawItems: unknown): InvoiceItem[] {
if (!rawItems) return [];
const parsed = whmcsInvoiceItemsRawSchema.parse(rawItems);
const itemArray = Array.isArray(parsed.item) ? parsed.item : [parsed.item];
return itemArray.map(item => ({
id: item.id,
description: item.description,
amount: parseAmount(item.amount),
quantity: 1,
type: item.type,
serviceId: typeof item.relid === "number" && item.relid > 0 ? item.relid : undefined,
}));
}
/**
* Transform raw WHMCS invoice data into normalized Invoice type
*/
export function transformWhmcsInvoice(
rawInvoice: unknown,
options: TransformInvoiceOptions = {}
): Invoice {
const invoicePayload =
rawInvoice && typeof (rawInvoice as { invoiceid?: unknown }).invoiceid !== "undefined"
? whmcsInvoiceRawSchema.parse(rawInvoice)
: normalizeListInvoice(rawInvoice);
const whmcsInvoice = {
...invoicePayload,
invoiceid: invoicePayload.invoiceid ?? invoicePayload.id,
};
const currency = whmcsInvoice.currencycode || options.defaultCurrencyCode || "JPY";
const currencySymbol =
whmcsInvoice.currencyprefix ||
whmcsInvoice.currencysuffix ||
options.defaultCurrencySymbol;
// Transform to domain model
const invoice: Invoice = {
id: whmcsInvoice.invoiceid ?? 0,
number: whmcsInvoice.invoicenum || `INV-${whmcsInvoice.invoiceid}`,
status: mapStatus(whmcsInvoice.status),
currency,
currencySymbol,
total: parseAmount(whmcsInvoice.total),
subtotal: parseAmount(whmcsInvoice.subtotal),
tax: parseAmount(whmcsInvoice.tax) + parseAmount(whmcsInvoice.tax2),
issuedAt: formatDate(whmcsInvoice.date || whmcsInvoice.datecreated),
dueDate: formatDate(whmcsInvoice.duedate),
paidDate: formatDate(whmcsInvoice.datepaid),
description: whmcsInvoice.notes || undefined,
items: mapItems(whmcsInvoice.items),
};
// Validate result against domain schema
return invoiceSchema.parse(invoice);
}
/**
* Transform multiple WHMCS invoices
*/
export function transformWhmcsInvoices(
rawInvoices: unknown[],
options: TransformInvoiceOptions = {}
): Invoice[] {
return rawInvoices.map(raw => transformWhmcsInvoice(raw, options));
}
function normalizeListInvoice(rawInvoice: unknown): WhmcsInvoiceRaw & { id?: number } {
const listItem: WhmcsInvoiceListItem = whmcsInvoiceListItemSchema.parse(rawInvoice);
const invoiceid = listItem.invoiceid ?? listItem.id;
return {
...listItem,
invoiceid,
};
}