@adtrackify/at-service-common 4.0.3 → 4.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/__tests__/db/products-db-service.spec.d.ts +1 -0
- package/dist/cjs/__tests__/db/products-db-service.spec.js +90 -0
- package/dist/cjs/__tests__/db/products-db-service.spec.js.map +1 -0
- package/dist/cjs/__tests__/services/cost/cost-calculation-types.spec.d.ts +1 -0
- package/dist/cjs/__tests__/services/cost/cost-calculation-types.spec.js +24 -0
- package/dist/cjs/__tests__/services/cost/cost-calculation-types.spec.js.map +1 -0
- package/dist/cjs/__tests__/services/cost/cost-calculator-service.spec.d.ts +1 -0
- package/dist/cjs/__tests__/services/cost/cost-calculator-service.spec.js +3320 -0
- package/dist/cjs/__tests__/services/cost/cost-calculator-service.spec.js.map +1 -0
- package/dist/cjs/__tests__/services/cost/cost-currency-service.spec.d.ts +1 -0
- package/dist/cjs/__tests__/services/cost/cost-currency-service.spec.js +115 -0
- package/dist/cjs/__tests__/services/cost/cost-currency-service.spec.js.map +1 -0
- package/dist/cjs/__tests__/services/cost/cost-filter-service.spec.d.ts +1 -0
- package/dist/cjs/__tests__/services/cost/cost-filter-service.spec.js +469 -0
- package/dist/cjs/__tests__/services/cost/cost-filter-service.spec.js.map +1 -0
- package/dist/cjs/__tests__/services/cost/order-cost/order-cost-resolution-service.spec.d.ts +1 -0
- package/dist/cjs/__tests__/services/cost/order-cost/order-cost-resolution-service.spec.js +207 -0
- package/dist/cjs/__tests__/services/cost/order-cost/order-cost-resolution-service.spec.js.map +1 -0
- package/dist/cjs/__tests__/services/currency-exchange-rate-lookup-service.spec.d.ts +1 -0
- package/dist/cjs/__tests__/services/currency-exchange-rate-lookup-service.spec.js +35 -0
- package/dist/cjs/__tests__/services/currency-exchange-rate-lookup-service.spec.js.map +1 -0
- package/dist/cjs/services/cost/cost-calculation-types.d.ts +69 -0
- package/dist/cjs/services/cost/cost-calculation-types.js +20 -0
- package/dist/cjs/services/cost/cost-calculation-types.js.map +1 -0
- package/dist/cjs/services/cost/cost-calculator-service.d.ts +24 -0
- package/dist/cjs/services/cost/cost-calculator-service.js +457 -0
- package/dist/cjs/services/cost/cost-calculator-service.js.map +1 -0
- package/dist/cjs/services/cost/cost-currency-service.d.ts +6 -0
- package/dist/cjs/services/cost/cost-currency-service.js +88 -0
- package/dist/cjs/services/cost/cost-currency-service.js.map +1 -0
- package/dist/cjs/services/cost/cost-filter-service.d.ts +10 -0
- package/dist/cjs/services/cost/cost-filter-service.js +122 -0
- package/dist/cjs/services/cost/cost-filter-service.js.map +1 -0
- package/dist/cjs/services/cost/index.d.ts +5 -0
- package/dist/cjs/services/cost/index.js +22 -0
- package/dist/cjs/services/cost/index.js.map +1 -0
- package/dist/cjs/services/cost/order-cost/index.d.ts +2 -0
- package/dist/cjs/services/cost/order-cost/index.js +19 -0
- package/dist/cjs/services/cost/order-cost/index.js.map +1 -0
- package/dist/cjs/services/cost/order-cost/order-cost-resolution-service.d.ts +23 -0
- package/dist/cjs/services/cost/order-cost/order-cost-resolution-service.js +362 -0
- package/dist/cjs/services/cost/order-cost/order-cost-resolution-service.js.map +1 -0
- package/dist/cjs/services/cost/order-cost/order-cost-resolution-types.d.ts +37 -0
- package/dist/cjs/services/cost/order-cost/order-cost-resolution-types.js +3 -0
- package/dist/cjs/services/cost/order-cost/order-cost-resolution-types.js.map +1 -0
- package/dist/cjs/services/currency-exchange-rate-lookup-service.d.ts +2 -1
- package/dist/cjs/services/currency-exchange-rate-lookup-service.js +28 -0
- package/dist/cjs/services/currency-exchange-rate-lookup-service.js.map +1 -1
- package/dist/cjs/services/db/identity-cache-dynamodb-service.js +1 -1
- package/dist/cjs/services/db/identity-cache-dynamodb-service.js.map +1 -1
- package/dist/cjs/services/db/index.d.ts +2 -0
- package/dist/cjs/services/db/index.js +2 -0
- package/dist/cjs/services/db/index.js.map +1 -1
- package/dist/cjs/services/db/products-db-service-types.d.ts +10 -0
- package/dist/cjs/services/db/products-db-service-types.js +3 -0
- package/dist/cjs/services/db/products-db-service-types.js.map +1 -0
- package/dist/cjs/services/db/products-db-service.d.ts +19 -0
- package/dist/cjs/services/db/products-db-service.js +282 -0
- package/dist/cjs/services/db/products-db-service.js.map +1 -0
- package/dist/cjs/services/index.d.ts +1 -0
- package/dist/cjs/services/index.js +1 -0
- package/dist/cjs/services/index.js.map +1 -1
- package/dist/esm/__tests__/db/products-db-service.spec.d.ts +1 -0
- package/dist/esm/__tests__/db/products-db-service.spec.js +88 -0
- package/dist/esm/__tests__/db/products-db-service.spec.js.map +1 -0
- package/dist/esm/__tests__/services/cost/cost-calculation-types.spec.d.ts +1 -0
- package/dist/esm/__tests__/services/cost/cost-calculation-types.spec.js +22 -0
- package/dist/esm/__tests__/services/cost/cost-calculation-types.spec.js.map +1 -0
- package/dist/esm/__tests__/services/cost/cost-calculator-service.spec.d.ts +1 -0
- package/dist/esm/__tests__/services/cost/cost-calculator-service.spec.js +3318 -0
- package/dist/esm/__tests__/services/cost/cost-calculator-service.spec.js.map +1 -0
- package/dist/esm/__tests__/services/cost/cost-currency-service.spec.d.ts +1 -0
- package/dist/esm/__tests__/services/cost/cost-currency-service.spec.js +113 -0
- package/dist/esm/__tests__/services/cost/cost-currency-service.spec.js.map +1 -0
- package/dist/esm/__tests__/services/cost/cost-filter-service.spec.d.ts +1 -0
- package/dist/esm/__tests__/services/cost/cost-filter-service.spec.js +467 -0
- package/dist/esm/__tests__/services/cost/cost-filter-service.spec.js.map +1 -0
- package/dist/esm/__tests__/services/cost/order-cost/order-cost-resolution-service.spec.d.ts +1 -0
- package/dist/esm/__tests__/services/cost/order-cost/order-cost-resolution-service.spec.js +205 -0
- package/dist/esm/__tests__/services/cost/order-cost/order-cost-resolution-service.spec.js.map +1 -0
- package/dist/esm/__tests__/services/currency-exchange-rate-lookup-service.spec.d.ts +1 -0
- package/dist/esm/__tests__/services/currency-exchange-rate-lookup-service.spec.js +33 -0
- package/dist/esm/__tests__/services/currency-exchange-rate-lookup-service.spec.js.map +1 -0
- package/dist/esm/services/cost/cost-calculation-types.d.ts +69 -0
- package/dist/esm/services/cost/cost-calculation-types.js +16 -0
- package/dist/esm/services/cost/cost-calculation-types.js.map +1 -0
- package/dist/esm/services/cost/cost-calculator-service.d.ts +24 -0
- package/dist/esm/services/cost/cost-calculator-service.js +451 -0
- package/dist/esm/services/cost/cost-calculator-service.js.map +1 -0
- package/dist/esm/services/cost/cost-currency-service.d.ts +6 -0
- package/dist/esm/services/cost/cost-currency-service.js +85 -0
- package/dist/esm/services/cost/cost-currency-service.js.map +1 -0
- package/dist/esm/services/cost/cost-filter-service.d.ts +10 -0
- package/dist/esm/services/cost/cost-filter-service.js +119 -0
- package/dist/esm/services/cost/cost-filter-service.js.map +1 -0
- package/dist/esm/services/cost/index.d.ts +5 -0
- package/dist/esm/services/cost/index.js +6 -0
- package/dist/esm/services/cost/index.js.map +1 -0
- package/dist/esm/services/cost/order-cost/index.d.ts +2 -0
- package/dist/esm/services/cost/order-cost/index.js +3 -0
- package/dist/esm/services/cost/order-cost/index.js.map +1 -0
- package/dist/esm/services/cost/order-cost/order-cost-resolution-service.d.ts +23 -0
- package/dist/esm/services/cost/order-cost/order-cost-resolution-service.js +356 -0
- package/dist/esm/services/cost/order-cost/order-cost-resolution-service.js.map +1 -0
- package/dist/esm/services/cost/order-cost/order-cost-resolution-types.d.ts +37 -0
- package/dist/esm/services/cost/order-cost/order-cost-resolution-types.js +2 -0
- package/dist/esm/services/cost/order-cost/order-cost-resolution-types.js.map +1 -0
- package/dist/esm/services/currency-exchange-rate-lookup-service.d.ts +2 -1
- package/dist/esm/services/currency-exchange-rate-lookup-service.js +28 -0
- package/dist/esm/services/currency-exchange-rate-lookup-service.js.map +1 -1
- package/dist/esm/services/db/identity-cache-dynamodb-service.js +1 -1
- package/dist/esm/services/db/identity-cache-dynamodb-service.js.map +1 -1
- package/dist/esm/services/db/index.d.ts +2 -0
- package/dist/esm/services/db/index.js +2 -0
- package/dist/esm/services/db/index.js.map +1 -1
- package/dist/esm/services/db/products-db-service-types.d.ts +10 -0
- package/dist/esm/services/db/products-db-service-types.js +2 -0
- package/dist/esm/services/db/products-db-service-types.js.map +1 -0
- package/dist/esm/services/db/products-db-service.d.ts +19 -0
- package/dist/esm/services/db/products-db-service.js +278 -0
- package/dist/esm/services/db/products-db-service.js.map +1 -0
- package/dist/esm/services/index.d.ts +1 -0
- package/dist/esm/services/index.js +1 -0
- package/dist/esm/services/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
import { DateTime } from 'luxon';
|
|
3
|
+
import { Logger } from '../../helpers/logging-helper.js';
|
|
4
|
+
import { ORDER_COST_ENTRY_TYPE, SHIPPING_METHOD, SHIPPING_RATE_TYPE, WEIGHT_UNIT, } from '@adtrackify/at-tracking-event-types';
|
|
5
|
+
import { normalizeCategory, } from './cost-calculation-types.js';
|
|
6
|
+
import { COST_CATEGORY } from '@adtrackify/at-tracking-event-types';
|
|
7
|
+
import { CostFilterService } from './cost-filter-service.js';
|
|
8
|
+
import { CostCurrencyService } from './cost-currency-service.js';
|
|
9
|
+
const WEIGHT_CONVERSION = {
|
|
10
|
+
[WEIGHT_UNIT.GRAMS]: 1,
|
|
11
|
+
[WEIGHT_UNIT.KILOGRAMS]: 1000,
|
|
12
|
+
[WEIGHT_UNIT.OUNCES]: 28.3495,
|
|
13
|
+
[WEIGHT_UNIT.POUNDS]: 453.592,
|
|
14
|
+
};
|
|
15
|
+
export const CostCalculatorService = {
|
|
16
|
+
isDateInRange: (orderDate, effectiveFrom, effectiveTo) => {
|
|
17
|
+
try {
|
|
18
|
+
const orderDt = DateTime.fromISO(orderDate);
|
|
19
|
+
if (!orderDt.isValid)
|
|
20
|
+
return true;
|
|
21
|
+
if (effectiveFrom) {
|
|
22
|
+
const fromDt = DateTime.fromISO(effectiveFrom);
|
|
23
|
+
if (fromDt.isValid && orderDt < fromDt)
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (effectiveTo) {
|
|
27
|
+
const toDt = DateTime.fromISO(effectiveTo);
|
|
28
|
+
if (toDt.isValid && orderDt > toDt)
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
Logger.error('CostCalculatorService.isDateInRange: failed', { error, orderDate, effectiveFrom, effectiveTo });
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
findMatchingWeightTierAmount: (tiers, weightGrams, weightUnit) => {
|
|
39
|
+
try {
|
|
40
|
+
if (!tiers?.length)
|
|
41
|
+
return 0;
|
|
42
|
+
const targetWeight = weightUnit ? weightGrams / (WEIGHT_CONVERSION[weightUnit] ?? 1) : weightGrams;
|
|
43
|
+
for (const tier of tiers) {
|
|
44
|
+
const isAboveMin = targetWeight >= tier.weightMin;
|
|
45
|
+
const isBelowMax = tier.weightMax === null || targetWeight <= tier.weightMax;
|
|
46
|
+
if (isAboveMin && isBelowMax) {
|
|
47
|
+
return tier.flatAmount ?? 0;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
Logger.error('CostCalculatorService.findMatchingWeightTierAmount: failed', { error, weightGrams, weightUnit, tierCount: tiers?.length });
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
findQuantityTierAmount: (tiers, quantity) => {
|
|
58
|
+
try {
|
|
59
|
+
if (!tiers?.length)
|
|
60
|
+
return 0;
|
|
61
|
+
for (const tier of tiers) {
|
|
62
|
+
const isAboveMin = quantity >= tier.quantityMin;
|
|
63
|
+
const isBelowMax = tier.quantityMax === null || quantity <= tier.quantityMax;
|
|
64
|
+
if (isAboveMin && isBelowMax) {
|
|
65
|
+
return tier.flatAmount ?? 0;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
Logger.error('CostCalculatorService.findQuantityTierAmount: failed', { error, quantity, tierCount: tiers?.length });
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
calculateHandlingCost: (configs, lineItems, lineItemCosts, orderCurrency, exchangeRatesMap) => {
|
|
76
|
+
try {
|
|
77
|
+
const fee = configs.globalProductOverride?.globalHandlingFee ?? 0;
|
|
78
|
+
if (!fee)
|
|
79
|
+
return 0;
|
|
80
|
+
const configCurrency = configs.globalProductOverride?.currency;
|
|
81
|
+
const convertedFee = configCurrency
|
|
82
|
+
? CostCurrencyService.convertAmount(fee, configCurrency, orderCurrency, exchangeRatesMap)
|
|
83
|
+
: fee;
|
|
84
|
+
let total = new Big(0);
|
|
85
|
+
for (const li of lineItems) {
|
|
86
|
+
total = total.plus(convertedFee);
|
|
87
|
+
if (lineItemCosts[li.lineItemId]) {
|
|
88
|
+
lineItemCosts[li.lineItemId].totalLineItemHandlingCost = new Big(lineItemCosts[li.lineItemId].totalLineItemHandlingCost)
|
|
89
|
+
.plus(convertedFee)
|
|
90
|
+
.toNumber();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return total.toNumber();
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
Logger.error('CostCalculatorService.calculateHandlingCost: failed', { error, configs, orderCurrency, lineItemCount: lineItems.length });
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
calculateGatewayCost: (configs, orderGateways, totalPrice, orderCreatedAt, orderCurrency, exchangeRatesMap) => {
|
|
101
|
+
try {
|
|
102
|
+
const gatewayConfig = configs.gateway;
|
|
103
|
+
if (!gatewayConfig?.entries?.length)
|
|
104
|
+
return 0;
|
|
105
|
+
const normalizedOrderGateways = orderGateways.map((g) => g.toLowerCase());
|
|
106
|
+
const matchingEntry = gatewayConfig.entries.find((entry) => {
|
|
107
|
+
const gatewayMatches = normalizedOrderGateways.includes(entry.gatewayName.toLowerCase());
|
|
108
|
+
const dateValid = CostCalculatorService.isDateInRange(orderCreatedAt, entry.effectiveFrom, entry.effectiveTo);
|
|
109
|
+
return gatewayMatches && dateValid;
|
|
110
|
+
});
|
|
111
|
+
if (!matchingEntry)
|
|
112
|
+
return 0;
|
|
113
|
+
const rawFlatAmount = matchingEntry.flatAmount ?? 0;
|
|
114
|
+
const convertedFlatAmount = CostCurrencyService.convertAmount(rawFlatAmount, matchingEntry.currency, orderCurrency, exchangeRatesMap);
|
|
115
|
+
const flatAmount = new Big(convertedFlatAmount);
|
|
116
|
+
const percentAmount = new Big(totalPrice).times(matchingEntry.percentRate ?? 0).div(100);
|
|
117
|
+
return flatAmount.plus(percentAmount).toNumber();
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
Logger.error('CostCalculatorService.calculateGatewayCost: failed', { error, configs, orderCurrency, orderGateways, totalPrice, orderCreatedAt });
|
|
121
|
+
return 0;
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
findMatchingShippingProfile: (profiles, shipCountryCode, orderCreatedAt, orderFilterContext) => {
|
|
125
|
+
try {
|
|
126
|
+
if (!profiles?.length)
|
|
127
|
+
return null;
|
|
128
|
+
let defaultProfile = null;
|
|
129
|
+
for (const profile of profiles) {
|
|
130
|
+
if (!CostCalculatorService.isDateInRange(orderCreatedAt, profile.effectiveFrom, profile.effectiveTo)) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (profile.filters && orderFilterContext && !CostFilterService.matchesFilters(profile.filters, orderFilterContext, null)) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (profile.isDefault) {
|
|
137
|
+
defaultProfile = profile;
|
|
138
|
+
}
|
|
139
|
+
if (shipCountryCode && profile.countries?.length) {
|
|
140
|
+
const normalizedCountry = shipCountryCode.toLowerCase();
|
|
141
|
+
const countryMatches = profile.countries.some((c) => c.toLowerCase() === normalizedCountry);
|
|
142
|
+
if (countryMatches) {
|
|
143
|
+
return profile;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return defaultProfile;
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
Logger.error('CostCalculatorService.findMatchingShippingProfile: failed', { error, shipCountryCode });
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
calculateShippingCost: (configs, shopifyShippingAmount, shipCountryCode, totalWeightGrams, orderCreatedAt, orderCurrency, exchangeRatesMap, orderFilterContext) => {
|
|
155
|
+
try {
|
|
156
|
+
const shippingConfig = configs.shipping;
|
|
157
|
+
if (!shippingConfig)
|
|
158
|
+
return 0;
|
|
159
|
+
switch (shippingConfig.method) {
|
|
160
|
+
case SHIPPING_METHOD.SHOPIFY_CHARGES:
|
|
161
|
+
return shopifyShippingAmount;
|
|
162
|
+
case SHIPPING_METHOD.FIXED_RATE: {
|
|
163
|
+
const fixedAmount = shippingConfig.fixedRateAmount ?? 0;
|
|
164
|
+
return CostCurrencyService.convertAmount(fixedAmount, shippingConfig.currency, orderCurrency, exchangeRatesMap);
|
|
165
|
+
}
|
|
166
|
+
case SHIPPING_METHOD.SHIPPING_PROFILES: {
|
|
167
|
+
const profile = CostCalculatorService.findMatchingShippingProfile(shippingConfig.profiles, shipCountryCode, orderCreatedAt, orderFilterContext);
|
|
168
|
+
if (!profile)
|
|
169
|
+
return 0;
|
|
170
|
+
if (profile.rateType === SHIPPING_RATE_TYPE.FLAT) {
|
|
171
|
+
const flatAmount = profile.flatAmount ?? 0;
|
|
172
|
+
return CostCurrencyService.convertAmount(flatAmount, profile.currency, orderCurrency, exchangeRatesMap);
|
|
173
|
+
}
|
|
174
|
+
if (profile.rateType === SHIPPING_RATE_TYPE.WEIGHT_TIERED) {
|
|
175
|
+
const tierAmount = CostCalculatorService.findMatchingWeightTierAmount(profile.weightTiers, totalWeightGrams, profile.weightUnit);
|
|
176
|
+
return CostCurrencyService.convertAmount(tierAmount, profile.currency, orderCurrency, exchangeRatesMap);
|
|
177
|
+
}
|
|
178
|
+
return 0;
|
|
179
|
+
}
|
|
180
|
+
default:
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
Logger.error('CostCalculatorService.calculateShippingCost: failed', { error, configs, orderCurrency, shipCountryCode, totalWeightGrams });
|
|
186
|
+
return 0;
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
calculateOtherCostEntry: (entry, context, lineItemCosts, orderCurrency, exchangeRatesMap) => {
|
|
190
|
+
const defaultResult = { orderLevel: 0, lineItemLevel: 0 };
|
|
191
|
+
try {
|
|
192
|
+
let orderLevelCost = new Big(0);
|
|
193
|
+
let lineItemLevelCost = new Big(0);
|
|
194
|
+
const { orderFilterContext, lineItems, totalWeight, totalQuantity, hasRefund, totalProductCost, totalShipping, totalPrice, grossSalePrice, netSalePrice } = context;
|
|
195
|
+
if (!CostFilterService.matchesFilters(entry.filters, orderFilterContext, null, lineItems.map(li => li.filterContext))) {
|
|
196
|
+
return defaultResult;
|
|
197
|
+
}
|
|
198
|
+
const convertFlat = (amount) => CostCurrencyService.convertAmount(amount, entry.currency, orderCurrency, exchangeRatesMap);
|
|
199
|
+
switch (entry.type) {
|
|
200
|
+
case ORDER_COST_ENTRY_TYPE.PER_ORDER:
|
|
201
|
+
orderLevelCost = new Big(convertFlat(entry.flatAmount ?? 0));
|
|
202
|
+
break;
|
|
203
|
+
case ORDER_COST_ENTRY_TYPE.PER_LINE_ITEM:
|
|
204
|
+
for (const li of lineItems) {
|
|
205
|
+
if (CostFilterService.matchesFilters(entry.filters, orderFilterContext, li.filterContext)) {
|
|
206
|
+
const amount = new Big(convertFlat(entry.flatAmount ?? 0)).times(li.quantity);
|
|
207
|
+
lineItemLevelCost = lineItemLevelCost.plus(amount);
|
|
208
|
+
if (lineItemCosts[li.lineItemId]) {
|
|
209
|
+
lineItemCosts[li.lineItemId].totalLineItemOtherCost = new Big(lineItemCosts[li.lineItemId].totalLineItemOtherCost)
|
|
210
|
+
.plus(amount)
|
|
211
|
+
.toNumber();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
break;
|
|
216
|
+
case ORDER_COST_ENTRY_TYPE.BY_ORDER_WEIGHT: {
|
|
217
|
+
if (entry.weightIntervals?.length) {
|
|
218
|
+
const tierAmount = CostCalculatorService.findMatchingWeightTierAmount(entry.weightIntervals, totalWeight, entry.weightUnit);
|
|
219
|
+
orderLevelCost = new Big(convertFlat(tierAmount));
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
const weightInUnit = entry.weightUnit
|
|
223
|
+
? totalWeight / (WEIGHT_CONVERSION[entry.weightUnit] ?? 1)
|
|
224
|
+
: totalWeight;
|
|
225
|
+
orderLevelCost = new Big(convertFlat(entry.flatAmount ?? 0)).times(weightInUnit);
|
|
226
|
+
}
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
case ORDER_COST_ENTRY_TYPE.BY_LINE_ITEM_QUANTITY:
|
|
230
|
+
for (const li of lineItems) {
|
|
231
|
+
if (CostFilterService.matchesFilters(entry.filters, orderFilterContext, li.filterContext)) {
|
|
232
|
+
let amount;
|
|
233
|
+
if (entry.quantityIntervals?.length) {
|
|
234
|
+
const tierAmount = CostCalculatorService.findQuantityTierAmount(entry.quantityIntervals, li.quantity);
|
|
235
|
+
amount = new Big(convertFlat(tierAmount));
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
amount = new Big(convertFlat(entry.flatAmount ?? 0)).times(li.quantity);
|
|
239
|
+
}
|
|
240
|
+
lineItemLevelCost = lineItemLevelCost.plus(amount);
|
|
241
|
+
if (lineItemCosts[li.lineItemId]) {
|
|
242
|
+
lineItemCosts[li.lineItemId].totalLineItemOtherCost = new Big(lineItemCosts[li.lineItemId].totalLineItemOtherCost)
|
|
243
|
+
.plus(amount)
|
|
244
|
+
.toNumber();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
break;
|
|
249
|
+
case ORDER_COST_ENTRY_TYPE.BY_ORDER_QUANTITY:
|
|
250
|
+
if (entry.quantityIntervals?.length) {
|
|
251
|
+
const tierAmount = CostCalculatorService.findQuantityTierAmount(entry.quantityIntervals, totalQuantity);
|
|
252
|
+
orderLevelCost = new Big(convertFlat(tierAmount));
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
orderLevelCost = new Big(convertFlat(entry.flatAmount ?? 0)).times(totalQuantity);
|
|
256
|
+
}
|
|
257
|
+
break;
|
|
258
|
+
case ORDER_COST_ENTRY_TYPE.PER_REFUND_ORDER:
|
|
259
|
+
if (hasRefund) {
|
|
260
|
+
orderLevelCost = new Big(convertFlat(entry.flatAmount ?? 0));
|
|
261
|
+
}
|
|
262
|
+
break;
|
|
263
|
+
case ORDER_COST_ENTRY_TYPE.PER_REFUND_LINE_ITEM:
|
|
264
|
+
for (const li of lineItems) {
|
|
265
|
+
if (li.refundedQuantity > 0 && CostFilterService.matchesFilters(entry.filters, orderFilterContext, li.filterContext)) {
|
|
266
|
+
const amount = new Big(convertFlat(entry.flatAmount ?? 0)).times(li.refundedQuantity);
|
|
267
|
+
lineItemLevelCost = lineItemLevelCost.plus(amount);
|
|
268
|
+
if (lineItemCosts[li.lineItemId]) {
|
|
269
|
+
lineItemCosts[li.lineItemId].totalLineItemOtherCost = new Big(lineItemCosts[li.lineItemId].totalLineItemOtherCost)
|
|
270
|
+
.plus(amount)
|
|
271
|
+
.toNumber();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
break;
|
|
276
|
+
case ORDER_COST_ENTRY_TYPE.PCT_COGS:
|
|
277
|
+
orderLevelCost = new Big(totalProductCost).times(entry.percentRate ?? 0).div(100);
|
|
278
|
+
break;
|
|
279
|
+
case ORDER_COST_ENTRY_TYPE.PCT_SHIPPING:
|
|
280
|
+
orderLevelCost = new Big(totalShipping).times(entry.percentRate ?? 0).div(100);
|
|
281
|
+
break;
|
|
282
|
+
case ORDER_COST_ENTRY_TYPE.PCT_TOTAL_SALES:
|
|
283
|
+
orderLevelCost = new Big(totalPrice).times(entry.percentRate ?? 0).div(100);
|
|
284
|
+
break;
|
|
285
|
+
case ORDER_COST_ENTRY_TYPE.PCT_GROSS_SALES:
|
|
286
|
+
orderLevelCost = new Big(grossSalePrice).times(entry.percentRate ?? 0).div(100);
|
|
287
|
+
break;
|
|
288
|
+
case ORDER_COST_ENTRY_TYPE.PCT_NET_SALES:
|
|
289
|
+
orderLevelCost = new Big(netSalePrice).times(entry.percentRate ?? 0).div(100);
|
|
290
|
+
break;
|
|
291
|
+
default:
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
return { orderLevel: orderLevelCost.toNumber(), lineItemLevel: lineItemLevelCost.toNumber() };
|
|
295
|
+
}
|
|
296
|
+
catch (error) {
|
|
297
|
+
Logger.error('CostCalculatorService.calculateOtherCostEntry: failed', { error, entry, orderCurrency });
|
|
298
|
+
return defaultResult;
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
calculateOtherCost: (configs, context, lineItemCosts, orderCurrency, exchangeRatesMap) => {
|
|
302
|
+
const defaultCategoryCosts = {
|
|
303
|
+
totalOtherCogs: 0,
|
|
304
|
+
otherFulfillmentCost: 0,
|
|
305
|
+
otherTransactionCost: 0,
|
|
306
|
+
marketingCost: 0,
|
|
307
|
+
agencyFeesCost: 0,
|
|
308
|
+
opexCost: 0,
|
|
309
|
+
totalOtherCost: 0,
|
|
310
|
+
};
|
|
311
|
+
const defaultResult = { orderLevel: 0, lineItemLevel: 0, total: 0, categoryCosts: defaultCategoryCosts };
|
|
312
|
+
try {
|
|
313
|
+
const orderConfig = configs.order;
|
|
314
|
+
if (!orderConfig?.entries?.length)
|
|
315
|
+
return defaultResult;
|
|
316
|
+
let orderLevelTotal = new Big(0);
|
|
317
|
+
let lineItemLevelTotal = new Big(0);
|
|
318
|
+
const categoryCosts = { ...defaultCategoryCosts };
|
|
319
|
+
for (const entry of orderConfig.entries) {
|
|
320
|
+
if (!CostCalculatorService.isDateInRange(context.orderCreatedAt, entry.effectiveFrom, entry.effectiveTo)) {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
try {
|
|
324
|
+
const entryCost = CostCalculatorService.calculateOtherCostEntry(entry, context, lineItemCosts, orderCurrency, exchangeRatesMap);
|
|
325
|
+
const entryTotal = entryCost.orderLevel + entryCost.lineItemLevel;
|
|
326
|
+
orderLevelTotal = orderLevelTotal.plus(entryCost.orderLevel);
|
|
327
|
+
lineItemLevelTotal = lineItemLevelTotal.plus(entryCost.lineItemLevel);
|
|
328
|
+
const category = normalizeCategory(entry.category);
|
|
329
|
+
switch (category) {
|
|
330
|
+
case COST_CATEGORY.COGS:
|
|
331
|
+
categoryCosts.totalOtherCogs += entryTotal;
|
|
332
|
+
break;
|
|
333
|
+
case COST_CATEGORY.FULFILLMENT:
|
|
334
|
+
categoryCosts.otherFulfillmentCost += entryTotal;
|
|
335
|
+
break;
|
|
336
|
+
case COST_CATEGORY.TRANSACTION:
|
|
337
|
+
categoryCosts.otherTransactionCost += entryTotal;
|
|
338
|
+
break;
|
|
339
|
+
case COST_CATEGORY.MARKETING:
|
|
340
|
+
categoryCosts.marketingCost += entryTotal;
|
|
341
|
+
break;
|
|
342
|
+
case COST_CATEGORY.AGENCY_FEES:
|
|
343
|
+
categoryCosts.agencyFeesCost += entryTotal;
|
|
344
|
+
break;
|
|
345
|
+
case COST_CATEGORY.OPEX:
|
|
346
|
+
categoryCosts.opexCost += entryTotal;
|
|
347
|
+
break;
|
|
348
|
+
case COST_CATEGORY.OTHER:
|
|
349
|
+
default:
|
|
350
|
+
categoryCosts.totalOtherCost += entryTotal;
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
catch (entryError) {
|
|
355
|
+
Logger.warn('CostCalculatorService.calculateOtherCost: entry failed', {
|
|
356
|
+
error: entryError,
|
|
357
|
+
entryId: entry.id,
|
|
358
|
+
category: entry.category,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return {
|
|
363
|
+
orderLevel: orderLevelTotal.toNumber(),
|
|
364
|
+
lineItemLevel: lineItemLevelTotal.toNumber(),
|
|
365
|
+
total: orderLevelTotal.plus(lineItemLevelTotal).toNumber(),
|
|
366
|
+
categoryCosts,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
catch (error) {
|
|
370
|
+
Logger.error('CostCalculatorService.calculateOtherCost: failed', { error, configs, orderCurrency });
|
|
371
|
+
return defaultResult;
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
calculateLineItemHandlingCosts: (lineItems) => {
|
|
375
|
+
try {
|
|
376
|
+
const result = {};
|
|
377
|
+
for (const li of lineItems) {
|
|
378
|
+
const handlingFee = li.variantHandlingFee ?? 0;
|
|
379
|
+
result[li.lineItemId] = new Big(handlingFee).times(li.quantity).toNumber();
|
|
380
|
+
}
|
|
381
|
+
return result;
|
|
382
|
+
}
|
|
383
|
+
catch (error) {
|
|
384
|
+
Logger.error('CostCalculatorService.calculateLineItemHandlingCosts: failed', { error, lineItemCount: lineItems.length });
|
|
385
|
+
return {};
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
calculateOrderCosts: (configs, context, orderCurrency, exchangeRatesMap) => {
|
|
389
|
+
const defaultCategoryCosts = {
|
|
390
|
+
totalOtherCogs: 0,
|
|
391
|
+
otherFulfillmentCost: 0,
|
|
392
|
+
otherTransactionCost: 0,
|
|
393
|
+
marketingCost: 0,
|
|
394
|
+
agencyFeesCost: 0,
|
|
395
|
+
opexCost: 0,
|
|
396
|
+
totalOtherCost: 0,
|
|
397
|
+
};
|
|
398
|
+
const defaultResult = {
|
|
399
|
+
orderLevelHandlingCost: 0,
|
|
400
|
+
lineItemLevelHandlingCost: 0,
|
|
401
|
+
totalHandlingCost: 0,
|
|
402
|
+
orderLevelOtherCost: 0,
|
|
403
|
+
lineItemLevelOtherCost: 0,
|
|
404
|
+
totalOtherCost: 0,
|
|
405
|
+
totalGatewayCost: 0,
|
|
406
|
+
totalShippingCost: 0,
|
|
407
|
+
lineItemCosts: {},
|
|
408
|
+
categoryCosts: defaultCategoryCosts,
|
|
409
|
+
};
|
|
410
|
+
if (!configs) {
|
|
411
|
+
return defaultResult;
|
|
412
|
+
}
|
|
413
|
+
try {
|
|
414
|
+
const lineItemCosts = {};
|
|
415
|
+
for (const li of context.lineItems) {
|
|
416
|
+
lineItemCosts[li.lineItemId] = { totalLineItemHandlingCost: 0, totalLineItemOtherCost: 0 };
|
|
417
|
+
}
|
|
418
|
+
const lineItemHandlingCosts = CostCalculatorService.calculateLineItemHandlingCosts(context.lineItems);
|
|
419
|
+
for (const [lineItemId, handlingCost] of Object.entries(lineItemHandlingCosts)) {
|
|
420
|
+
if (lineItemCosts[lineItemId]) {
|
|
421
|
+
lineItemCosts[lineItemId].totalLineItemHandlingCost = handlingCost;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
CostCalculatorService.calculateHandlingCost(configs, context.lineItems, lineItemCosts, orderCurrency, exchangeRatesMap);
|
|
425
|
+
const lineItemLevelHandlingCost = Object.values(lineItemCosts).reduce((sum, li) => new Big(sum).plus(li.totalLineItemHandlingCost).toNumber(), 0);
|
|
426
|
+
const orderLevelHandlingCost = 0;
|
|
427
|
+
const totalHandlingCost = orderLevelHandlingCost + lineItemLevelHandlingCost;
|
|
428
|
+
const totalGatewayCost = CostCalculatorService.calculateGatewayCost(configs, context.orderFilterContext.paymentGateways, context.totalPrice, context.orderCreatedAt, orderCurrency, exchangeRatesMap);
|
|
429
|
+
const shopifyShippingAmount = context.totalShipping;
|
|
430
|
+
const totalShippingCost = CostCalculatorService.calculateShippingCost(configs, shopifyShippingAmount, context.orderFilterContext.shipCountryCode, context.totalWeight, context.orderCreatedAt, orderCurrency, exchangeRatesMap, context.orderFilterContext);
|
|
431
|
+
const otherCostResult = CostCalculatorService.calculateOtherCost(configs, context, lineItemCosts, orderCurrency, exchangeRatesMap);
|
|
432
|
+
return {
|
|
433
|
+
orderLevelHandlingCost,
|
|
434
|
+
lineItemLevelHandlingCost,
|
|
435
|
+
totalHandlingCost,
|
|
436
|
+
orderLevelOtherCost: otherCostResult.orderLevel,
|
|
437
|
+
lineItemLevelOtherCost: otherCostResult.lineItemLevel,
|
|
438
|
+
totalOtherCost: otherCostResult.categoryCosts.totalOtherCost,
|
|
439
|
+
totalGatewayCost,
|
|
440
|
+
totalShippingCost,
|
|
441
|
+
lineItemCosts,
|
|
442
|
+
categoryCosts: otherCostResult.categoryCosts,
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
catch (error) {
|
|
446
|
+
Logger.error('CostCalculatorService.calculateOrderCosts: failed', { error, configs, orderCurrency });
|
|
447
|
+
return defaultResult;
|
|
448
|
+
}
|
|
449
|
+
},
|
|
450
|
+
};
|
|
451
|
+
//# sourceMappingURL=cost-calculator-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost-calculator-service.js","sourceRoot":"","sources":["../../../../src/services/cost/cost-calculator-service.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,QAAQ,CAAC;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,iCAAiC,CAAC;AACzD,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,kBAAkB,EAClB,WAAW,GAMZ,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAQL,iBAAiB,GAClB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,mBAAmB,EAAyB,MAAM,4BAA4B,CAAC;AAExF,MAAM,iBAAiB,GAAgC;IAC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AAC;YAE7E,MAAM,gBAAgB,GAAG,qBAAqB,CAAC,oBAAoB,CACjE,OAAO,EACP,OAAO,CAAC,kBAAkB,CAAC,eAAe,EAC1C,OAAO,CAAC,UAAU,EAClB,OAAO,CAAC,cAAc,EACtB,aAAa,EACb,gBAAgB,CACjB,CAAC;YAEF,MAAM,qBAAqB,GAAG,OAAO,CAAC,aAAa,CAAC;YACpD,MAAM,iBAAiB,GAAG,qBAAqB,CAAC,qBAAqB,CACnE,OAAO,EACP,qBAAqB,EACrB,OAAO,CAAC,kBAAkB,CAAC,eAAe,EAC1C,OAAO,CAAC,WAAW,EACnB,OAAO,CAAC,cAAc,EACtB,aAAa,EACb,gBAAgB,EAChB,OAAO,CAAC,kBAAkB,CAC3B,CAAC;YAEF,MAAM,eAAe,GAAG,qBAAqB,CAAC,kBAAkB,CAAC,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,gBAAgB,CAAC,CAAC;YAEnI,OAAO;gBACL,sBAAsB;gBACtB,yBAAyB;gBACzB,iBAAiB;gBACjB,mBAAmB,EAAE,eAAe,CAAC,UAAU;gBAC/C,sBAAsB,EAAE,eAAe,CAAC,aAAa;gBACrD,cAAc,EAAE,eAAe,CAAC,aAAa,CAAC,cAAc;gBAC5D,gBAAgB;gBAChB,iBAAiB;gBACjB,aAAa;gBACb,aAAa,EAAE,eAAe,CAAC,aAAa;aAC7C,CAAC;SACH;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,CAAC,KAAK,CAAC,mDAAmD,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,CAAC,CAAC;YACrG,OAAO,aAAa,CAAC;SACtB;IACH,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type CostConfigsBundle } from './cost-calculation-types.js';
|
|
2
|
+
export type ExchangeRatesMap = Record<string, Record<string, number>>;
|
|
3
|
+
export declare const CostCurrencyService: {
|
|
4
|
+
extractCurrenciesFromConfigs: (configs: CostConfigsBundle, orderCurrency: string) => string[];
|
|
5
|
+
convertAmount: (amount: number, fromCurrency: string, toCurrency: string, exchangeRatesMap: ExchangeRatesMap) => number;
|
|
6
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Logger } from '../../helpers/logging-helper.js';
|
|
2
|
+
import { convertAmountBetweenCurrencies } from '../../libs/currency.js';
|
|
3
|
+
export const CostCurrencyService = {
|
|
4
|
+
extractCurrenciesFromConfigs: (configs, orderCurrency) => {
|
|
5
|
+
try {
|
|
6
|
+
const currencies = new Set();
|
|
7
|
+
const normalizedOrderCurrency = orderCurrency.toUpperCase();
|
|
8
|
+
if (configs.globalProductOverride?.currency) {
|
|
9
|
+
const c = configs.globalProductOverride.currency.toUpperCase();
|
|
10
|
+
if (c !== normalizedOrderCurrency)
|
|
11
|
+
currencies.add(c);
|
|
12
|
+
}
|
|
13
|
+
if (configs.order?.entries) {
|
|
14
|
+
for (const entry of configs.order.entries) {
|
|
15
|
+
if (entry.currency) {
|
|
16
|
+
const c = entry.currency.toUpperCase();
|
|
17
|
+
if (c !== normalizedOrderCurrency)
|
|
18
|
+
currencies.add(c);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (configs.gateway?.entries) {
|
|
23
|
+
for (const entry of configs.gateway.entries) {
|
|
24
|
+
if (entry.currency) {
|
|
25
|
+
const c = entry.currency.toUpperCase();
|
|
26
|
+
if (c !== normalizedOrderCurrency)
|
|
27
|
+
currencies.add(c);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (configs.shipping?.currency) {
|
|
32
|
+
const c = configs.shipping.currency.toUpperCase();
|
|
33
|
+
if (c !== normalizedOrderCurrency)
|
|
34
|
+
currencies.add(c);
|
|
35
|
+
}
|
|
36
|
+
if (configs.shipping?.profiles) {
|
|
37
|
+
for (const profile of configs.shipping.profiles) {
|
|
38
|
+
if (profile.currency) {
|
|
39
|
+
const c = profile.currency.toUpperCase();
|
|
40
|
+
if (c !== normalizedOrderCurrency)
|
|
41
|
+
currencies.add(c);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return Array.from(currencies);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
Logger.error('CostCurrencyService.extractCurrenciesFromConfigs: failed', { error, orderCurrency });
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
convertAmount: (amount, fromCurrency, toCurrency, exchangeRatesMap) => {
|
|
53
|
+
try {
|
|
54
|
+
if (!amount)
|
|
55
|
+
return amount;
|
|
56
|
+
const normalizedFrom = fromCurrency.toUpperCase();
|
|
57
|
+
const normalizedTo = toCurrency.toUpperCase();
|
|
58
|
+
if (normalizedFrom === normalizedTo)
|
|
59
|
+
return amount;
|
|
60
|
+
const rates = exchangeRatesMap[normalizedFrom];
|
|
61
|
+
if (!rates) {
|
|
62
|
+
Logger.warn('CostCurrencyService.convertAmount: no rates found', {
|
|
63
|
+
fromCurrency: normalizedFrom,
|
|
64
|
+
toCurrency: normalizedTo,
|
|
65
|
+
});
|
|
66
|
+
return amount;
|
|
67
|
+
}
|
|
68
|
+
const rate = rates[normalizedTo];
|
|
69
|
+
if (!rate) {
|
|
70
|
+
Logger.warn('CostCurrencyService.convertAmount: no rate for target currency', {
|
|
71
|
+
fromCurrency: normalizedFrom,
|
|
72
|
+
toCurrency: normalizedTo,
|
|
73
|
+
});
|
|
74
|
+
return amount;
|
|
75
|
+
}
|
|
76
|
+
const converted = convertAmountBetweenCurrencies(normalizedFrom, normalizedTo, amount, rate);
|
|
77
|
+
return converted ?? amount;
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
Logger.error('CostCurrencyService.convertAmount: failed', { error, amount, fromCurrency, toCurrency });
|
|
81
|
+
return amount;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
//# sourceMappingURL=cost-currency-service.js.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,EAAE;gBACV,MAAM,CAAC,IAAI,CAAC,mDAAmD,EAAE;oBAC/D,YAAY,EAAE,cAAc;oBAC5B,UAAU,EAAE,YAAY;iBACzB,CAAC,CAAC;gBACH,OAAO,MAAM,CAAC;aACf;YAED,MAAM,IAAI,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;YACjC,IAAI,CAAC,IAAI,EAAE;gBACT,MAAM,CAAC,IAAI,CAAC,gEAAgE,EAAE;oBAC5E,YAAY,EAAE,cAAc;oBAC5B,UAAU,EAAE,YAAY;iBACzB,CAAC,CAAC;gBACH,OAAO,MAAM,CAAC;aACf;YAED,MAAM,SAAS,GAAG,8BAA8B,CAAC,cAAc,EAAE,YAAY,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;YAC7F,OAAO,SAAS,IAAI,MAAM,CAAC;SAC5B;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,CAAC,KAAK,CAAC,2CAA2C,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,CAAC,CAAC;YACvG,OAAO,MAAM,CAAC;SACf;IACH,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type CostFilter, type CostFilters } from '@adtrackify/at-tracking-event-types';
|
|
2
|
+
import { type OrderFilterContext, type LineItemFilterContext } from './cost-calculation-types.js';
|
|
3
|
+
export declare const CostFilterService: {
|
|
4
|
+
matchSingleFilter: (filter: CostFilter, value: string | undefined) => boolean;
|
|
5
|
+
matchArrayFilter: (filter: CostFilter, values: string[] | undefined) => boolean;
|
|
6
|
+
matchProductFilter: (filter: CostFilter, productId: string, variantId: string) => boolean;
|
|
7
|
+
hasProductFilters: (filters?: CostFilters) => boolean;
|
|
8
|
+
matchLineItemFilters: (filters: CostFilters, lineItem: LineItemFilterContext) => boolean;
|
|
9
|
+
matchesFilters: (filters: CostFilters | undefined, order: OrderFilterContext, lineItem: LineItemFilterContext | null, lineItems?: LineItemFilterContext[]) => boolean;
|
|
10
|
+
};
|