123 lines
3.9 KiB
JavaScript
123 lines
3.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.mapOrderItemToWhmcs = mapOrderItemToWhmcs;
|
|
exports.mapOrderToWhmcsItems = mapOrderToWhmcsItems;
|
|
exports.buildWhmcsAddOrderPayload = buildWhmcsAddOrderPayload;
|
|
exports.createOrderNotes = createOrderNotes;
|
|
function normalizeBillingCycle(cycle) {
|
|
if (!cycle)
|
|
return "monthly";
|
|
const normalized = cycle.trim().toLowerCase();
|
|
if (normalized.includes("monthly"))
|
|
return "monthly";
|
|
if (normalized.includes("one"))
|
|
return "onetime";
|
|
if (normalized.includes("annual"))
|
|
return "annually";
|
|
if (normalized.includes("quarter"))
|
|
return "quarterly";
|
|
return "monthly";
|
|
}
|
|
function mapOrderItemToWhmcs(item, index = 0) {
|
|
if (!item.product?.whmcsProductId) {
|
|
throw new Error(`Order item ${index} missing WHMCS product ID`);
|
|
}
|
|
const whmcsItem = {
|
|
productId: item.product.whmcsProductId,
|
|
billingCycle: normalizeBillingCycle(item.billingCycle),
|
|
quantity: item.quantity,
|
|
};
|
|
return whmcsItem;
|
|
}
|
|
function mapOrderToWhmcsItems(orderDetails) {
|
|
if (!orderDetails.items || orderDetails.items.length === 0) {
|
|
throw new Error("No order items provided for WHMCS mapping");
|
|
}
|
|
const whmcsItems = [];
|
|
let serviceItems = 0;
|
|
let activationItems = 0;
|
|
orderDetails.items.forEach((item, index) => {
|
|
const mapped = mapOrderItemToWhmcs(item, index);
|
|
whmcsItems.push(mapped);
|
|
if (mapped.billingCycle === "monthly") {
|
|
serviceItems++;
|
|
}
|
|
else if (mapped.billingCycle === "onetime") {
|
|
activationItems++;
|
|
}
|
|
});
|
|
return {
|
|
whmcsItems,
|
|
summary: {
|
|
totalItems: whmcsItems.length,
|
|
serviceItems,
|
|
activationItems,
|
|
},
|
|
};
|
|
}
|
|
function buildWhmcsAddOrderPayload(params) {
|
|
const pids = [];
|
|
const billingCycles = [];
|
|
const quantities = [];
|
|
const configOptions = [];
|
|
const customFields = [];
|
|
params.items.forEach(item => {
|
|
pids.push(item.productId);
|
|
billingCycles.push(item.billingCycle);
|
|
quantities.push(item.quantity);
|
|
if (item.configOptions && Object.keys(item.configOptions).length > 0) {
|
|
const serialized = serializeForWhmcs(item.configOptions);
|
|
configOptions.push(serialized);
|
|
}
|
|
else {
|
|
configOptions.push("");
|
|
}
|
|
if (item.customFields && Object.keys(item.customFields).length > 0) {
|
|
const serialized = serializeForWhmcs(item.customFields);
|
|
customFields.push(serialized);
|
|
}
|
|
else {
|
|
customFields.push("");
|
|
}
|
|
});
|
|
const payload = {
|
|
clientid: params.clientId,
|
|
paymentmethod: params.paymentMethod,
|
|
pid: pids,
|
|
billingcycle: billingCycles,
|
|
qty: quantities,
|
|
};
|
|
if (params.promoCode) {
|
|
payload.promocode = params.promoCode;
|
|
}
|
|
if (params.noinvoice !== undefined) {
|
|
payload.noinvoice = params.noinvoice;
|
|
}
|
|
if (params.noinvoiceemail !== undefined) {
|
|
payload.noinvoiceemail = params.noinvoiceemail;
|
|
}
|
|
if (params.noemail !== undefined) {
|
|
payload.noemail = params.noemail;
|
|
}
|
|
if (configOptions.some(opt => opt !== "")) {
|
|
payload.configoptions = configOptions;
|
|
}
|
|
if (customFields.some(field => field !== "")) {
|
|
payload.customfields = customFields;
|
|
}
|
|
return payload;
|
|
}
|
|
function serializeForWhmcs(data) {
|
|
const jsonStr = JSON.stringify(data);
|
|
return Buffer.from(jsonStr).toString("base64");
|
|
}
|
|
function createOrderNotes(sfOrderId, additionalNotes) {
|
|
const notes = [];
|
|
notes.push(`sfOrderId=${sfOrderId}`);
|
|
notes.push(`provisionedAt=${new Date().toISOString()}`);
|
|
if (additionalNotes) {
|
|
notes.push(additionalNotes);
|
|
}
|
|
return notes.join("; ");
|
|
}
|
|
//# sourceMappingURL=mapper.js.map
|