91 lines
3.1 KiB
JavaScript
91 lines
3.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.transformWhmcsInvoice = transformWhmcsInvoice;
|
|
exports.transformWhmcsInvoices = transformWhmcsInvoices;
|
|
const schema_1 = require("../../schema");
|
|
const raw_types_1 = require("./raw.types");
|
|
const STATUS_MAP = {
|
|
draft: "Draft",
|
|
pending: "Pending",
|
|
"payment pending": "Pending",
|
|
paid: "Paid",
|
|
unpaid: "Unpaid",
|
|
cancelled: "Cancelled",
|
|
canceled: "Cancelled",
|
|
overdue: "Overdue",
|
|
refunded: "Refunded",
|
|
collections: "Collections",
|
|
};
|
|
function mapStatus(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) {
|
|
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) {
|
|
if (!input) {
|
|
return undefined;
|
|
}
|
|
const date = new Date(input);
|
|
if (Number.isNaN(date.getTime())) {
|
|
return undefined;
|
|
}
|
|
return date.toISOString();
|
|
}
|
|
function mapItems(rawItems) {
|
|
if (!rawItems)
|
|
return [];
|
|
const parsed = raw_types_1.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,
|
|
}));
|
|
}
|
|
function transformWhmcsInvoice(rawInvoice, options = {}) {
|
|
const whmcsInvoice = raw_types_1.whmcsInvoiceRawSchema.parse(rawInvoice);
|
|
const currency = whmcsInvoice.currencycode || options.defaultCurrencyCode || "JPY";
|
|
const currencySymbol = whmcsInvoice.currencyprefix ||
|
|
whmcsInvoice.currencysuffix ||
|
|
options.defaultCurrencySymbol;
|
|
const invoice = {
|
|
id: whmcsInvoice.invoiceid ?? whmcsInvoice.id ?? 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),
|
|
};
|
|
return schema_1.invoiceSchema.parse(invoice);
|
|
}
|
|
function transformWhmcsInvoices(rawInvoices, options = {}) {
|
|
return rawInvoices.map(raw => transformWhmcsInvoice(raw, options));
|
|
}
|
|
//# sourceMappingURL=mapper.js.map
|