139 lines
4.3 KiB
TypeScript
139 lines
4.3 KiB
TypeScript
/**
|
|
* Orders Domain - Salesforce Provider Mapper
|
|
*
|
|
* Transforms Salesforce Order/OrderItem records to normalized domain contracts.
|
|
*/
|
|
|
|
import type {
|
|
OrderSummary,
|
|
OrderDetails,
|
|
OrderItemSummary,
|
|
OrderItemDetails,
|
|
} from "../../contract";
|
|
import type {
|
|
SalesforceOrderRecord,
|
|
SalesforceOrderItemRecord,
|
|
SalesforceProduct2Record,
|
|
} from "./raw.types";
|
|
|
|
// ============================================================================
|
|
// Helper Functions
|
|
// ============================================================================
|
|
|
|
function coerceNumber(value: unknown): number | undefined {
|
|
if (typeof value === "number") return value;
|
|
if (typeof value === "string") {
|
|
const parsed = Number.parseFloat(value);
|
|
return Number.isFinite(parsed) ? parsed : undefined;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function getStringField(value: unknown): string | undefined {
|
|
return typeof value === "string" ? value : undefined;
|
|
}
|
|
|
|
// ============================================================================
|
|
// Order Item Mappers
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Transform Salesforce OrderItem to OrderItemSummary
|
|
*/
|
|
export function transformOrderItemToSummary(
|
|
record: SalesforceOrderItemRecord
|
|
): OrderItemSummary {
|
|
const product = record.PricebookEntry?.Product2;
|
|
|
|
return {
|
|
productName: product?.Name,
|
|
sku: product?.StockKeepingUnit,
|
|
status: undefined, // OrderItem doesn't have a status field
|
|
billingCycle: record.Billing_Cycle__c ?? product?.Billing_Cycle__c ?? undefined,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Transform Salesforce OrderItem to OrderItemDetails
|
|
*/
|
|
export function transformOrderItemToDetails(
|
|
record: SalesforceOrderItemRecord
|
|
): OrderItemDetails {
|
|
const product = record.PricebookEntry?.Product2;
|
|
|
|
return {
|
|
id: record.Id,
|
|
orderId: record.OrderId ?? "",
|
|
quantity: record.Quantity ?? 1,
|
|
unitPrice: coerceNumber(record.UnitPrice),
|
|
totalPrice: coerceNumber(record.TotalPrice),
|
|
billingCycle: record.Billing_Cycle__c ?? product?.Billing_Cycle__c ?? undefined,
|
|
product: product ? {
|
|
id: product.Id,
|
|
name: product.Name,
|
|
sku: product.StockKeepingUnit,
|
|
itemClass: product.Item_Class__c ?? undefined,
|
|
whmcsProductId: product.WH_Product_ID__c?.toString(),
|
|
internetOfferingType: product.Internet_Offering_Type__c ?? undefined,
|
|
internetPlanTier: product.Internet_Plan_Tier__c ?? undefined,
|
|
vpnRegion: product.VPN_Region__c ?? undefined,
|
|
} : undefined,
|
|
};
|
|
}
|
|
|
|
// ============================================================================
|
|
// Order Mappers
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Transform Salesforce Order to OrderSummary
|
|
*/
|
|
export function transformOrderToSummary(
|
|
record: SalesforceOrderRecord,
|
|
itemsSummary: OrderItemSummary[] = []
|
|
): OrderSummary {
|
|
return {
|
|
id: record.Id,
|
|
orderNumber: record.OrderNumber ?? record.Id,
|
|
status: record.Status ?? "Unknown",
|
|
orderType: record.Type,
|
|
effectiveDate: record.EffectiveDate ?? record.CreatedDate ?? new Date().toISOString(),
|
|
totalAmount: coerceNumber(record.TotalAmount),
|
|
createdDate: record.CreatedDate ?? new Date().toISOString(),
|
|
lastModifiedDate: record.LastModifiedDate ?? new Date().toISOString(),
|
|
whmcsOrderId: record.WHMCS_Order_ID__c ?? undefined,
|
|
itemsSummary,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Transform Salesforce Order to OrderDetails
|
|
*/
|
|
export function transformOrderToDetails(
|
|
record: SalesforceOrderRecord,
|
|
items: OrderItemDetails[] = []
|
|
): OrderDetails {
|
|
const summary = transformOrderToSummary(record, []);
|
|
|
|
return {
|
|
...summary,
|
|
accountId: record.AccountId ?? undefined,
|
|
accountName: record.Account?.Name ?? undefined,
|
|
pricebook2Id: record.Pricebook2Id ?? undefined,
|
|
activationType: record.Activation_Type__c ?? undefined,
|
|
activationStatus: record.Activation_Status__c ?? undefined,
|
|
activationScheduledAt: record.Activation_Scheduled_At__c ?? undefined,
|
|
activationErrorCode: record.Activation_Error_Code__c ?? undefined,
|
|
activationErrorMessage: record.Activation_Error_Message__c ?? undefined,
|
|
activatedDate: record.ActivatedDate ?? undefined,
|
|
items,
|
|
itemsSummary: items.map(item => ({
|
|
productName: item.product?.name,
|
|
sku: item.product?.sku,
|
|
status: undefined,
|
|
billingCycle: item.billingCycle,
|
|
})),
|
|
};
|
|
}
|
|
|