@fire-spark/core 0.54.0 → 0.56.0
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/index.d.ts +72 -8
- package/dist/index.js +401 -36
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Money, PaymentAdapter, PaymentCollectAt, PaymentContext, PaymentResult, PaymentReversal, PaymentStorage, PaymentUI, RedeemLike, RedeemSource, isDeliveryFee, isRealPaymentMethod, orderRedeems } from "@fire-spark/payments";
|
|
2
|
-
import { AndConditions, ApplyFeesInput, ApplyFeesOrderScope, CheckoutMode, CheckoutResolved, CmsBlock, Condition, Customer, CustomerIdentifyInput, CustomerRegisterInput, CustomerUpdateInput, DeviceSecretStorage, DeviceSecretsConfigureOptions, DeviceSecretsConfigureOptions as DeviceSecretsConfigureOptions$1, DeviceSecretsPayload, DeviceSecretsPayload as DeviceSecretsPayload$1, Fee, Fulfillment, FulfillmentEligibilityInput, FulfillmentEligibilityInput as FulfillmentEligibilityInput$1, FulfillmentListInput, GroupDisplayType, InvoiceCountryCode, InvoiceCountryDescriptor, KioskDeviceConfig, KioskDeviceConfigInput, KioskDeviceOverrides, KioskHealthBatchInput, KioskHealthBatchResult, KioskHeartbeatInput, KioskHeartbeatResult, KioskPickupNameRules, KioskRealtimeToken, LocalServerConfig, LocalServerConfig as LocalServerConfig$1, LocalServerHealthResult, LocalServerHealthResult as LocalServerHealthResult$1, MasivoTotpCode, MasivoTotpCreateInput, MasivoTotpVerification, MasivoTotpVerifyInput, MatchResult, MatchableRule, Menu, Menu as Menu$1, MenuLookup, MenuLookup as MenuLookup$1, MerchantPayment, ModifierControlType, ModifierGroup, OrConditions, Order as Order$1, Order as Order$2, OrderAdvanceFulfillmentInput, OrderCancelInput, OrderCancelInput as OrderCancelInput$1, OrderCancelType, OrderCancelType as OrderCancelType$1, OrderDeviceListInput, OrderDeviceListResult, OrderFulfillment, OrderFulfillmentWorkflow, OrderFulfillmentWorkflowEdge, OrderFulfillmentWorkflowNode, OrderInsertInput, OrderLine, OrderListInput, OrderRateInput, OrderRating, OrderRecord, OrderRecordPaymentInput, OrderRedeem, OrderRedemptionStep, OrderReorderInput, OrderRouteKind, OrderRouteKind as OrderRouteKind$1, OrderRouteStatus, OrderRoutes, OrderRoutes as OrderRoutes$1, OrderSetCustomerInput, OrderTotals, OrderUpdateInput, OrderValidateInput, OrderValidateOutput, PaymentCollectAt as PaymentCollectAt$1, PaymentEligibilityInput, PaymentEligibilityInput as PaymentEligibilityInput$1, PaymentIntentPutInput, PaymentMethod, PaymentProvider, PaymentTender, PaymentTenderInput, PlaceOrderResult, PosCatalog, PosFloorInput, PosFloorOccupancy, PosFloorPlan, PosFloorRealtimeToken, PosHealthBatchInput, PosHealthBatchResult, PosHeartbeatInput, PosHeartbeatResult, PosKioskOrder, PosKioskSettleInput, PosKioskSettleResult, PosOrderInput, PosOrderResult, PosTableAttachOrderInput, PosTableCloseInput, PosTableJoinInput, PosTableSeatInput, PosTableSeatReservationInput, PosTableSplitInput, PricingModifier, Product, Product as Product$1, ProductRewardSavings, Recommendation, RecurringDateCronFormatter, RecurringDateCronResult, RecurringDateCronSummary, RecurringDateCronValues, RouteEligibility, RouteEligibility as RouteEligibility$1, RouteEligibilityDetail, RouteEligibilityFlags, RouteEligibilityReason, RouteFailureKind, RouteFailureKind as RouteFailureKind$1, RouteHealthEvent, RouteHealthEvent as RouteHealthEvent$1, RouteHealthListener, RouteHealthListener as RouteHealthListener$1, RulesEngineOrder, RulesEngineOrderLine, RulesEngineParams, RulesEngineRuntime, Schedule, SdkLogContext, SdkLogErrorInfo, SdkLogLevel, SdkLogOptions, Store, StoreGetInput, StoreListInput, StoreNearbyInput, StorefrontAvailability, StorefrontMenu, StorefrontMenuScope, StorefrontMenuStreamOptions, StorefrontPaymentsListInput, StorefrontPreferences, SwitchCustomerArg, SwitchCustomerArg as SwitchCustomerArg$1, SwitchCustomerIdToken, SwitchCustomerOptions, SwitchCustomerOptions as SwitchCustomerOptions$1, SwitchCustomerResult, SwitchCustomerResult as SwitchCustomerResult$1, TaxBreakdownItem, TaxCategory, TaxIdValidationResult, TenderRefundInput, TenderReplaceInput, TenderVoidInput, Variant, VariantGroup, WordFilterCheckInput, WordFilterCheckResult } from "@fire-spark/types";
|
|
2
|
+
import { AndConditions, ApplyFeesInput, ApplyFeesOrderScope, CheckoutMode, CheckoutResolved, CmsBlock, Condition, Customer, CustomerIdentifyInput, CustomerRegisterInput, CustomerUpdateInput, DeviceSecretStorage, DeviceSecretsConfigureOptions, DeviceSecretsConfigureOptions as DeviceSecretsConfigureOptions$1, DeviceSecretsPayload, DeviceSecretsPayload as DeviceSecretsPayload$1, Fee, Fulfillment, FulfillmentEligibilityInput, FulfillmentEligibilityInput as FulfillmentEligibilityInput$1, FulfillmentListInput, GroupDisplayType, InvoiceCountryCode, InvoiceCountryDescriptor, KioskDeviceConfig, KioskDeviceConfigInput, KioskDeviceOverrides, KioskHealthBatchInput, KioskHealthBatchResult, KioskHeartbeatInput, KioskHeartbeatResult, KioskPickupNameRules, KioskRealtimeToken, LocalServerConfig, LocalServerConfig as LocalServerConfig$1, LocalServerHealthResult, LocalServerHealthResult as LocalServerHealthResult$1, MasivoTotpCode, MasivoTotpCreateInput, MasivoTotpVerification, MasivoTotpVerifyInput, MatchResult, MatchableRule, Menu, Menu as Menu$1, MenuLookup, MenuLookup as MenuLookup$1, MerchantPayment, ModifierControlType, ModifierGroup, OrConditions, Order as Order$1, Order as Order$2, OrderAdvanceFulfillmentInput, OrderCancelInput, OrderCancelInput as OrderCancelInput$1, OrderCancelType, OrderCancelType as OrderCancelType$1, OrderDeviceListInput, OrderDeviceListResult, OrderFulfillment, OrderFulfillmentWorkflow, OrderFulfillmentWorkflowEdge, OrderFulfillmentWorkflowNode, OrderInsertInput, OrderLine, OrderLine as OrderLine$1, OrderListInput, OrderRateInput, OrderRating, OrderRecord, OrderRecordPaymentInput, OrderRedeem, OrderRedeemReplaced, OrderRedemptionStep, OrderReorderInput, OrderRouteKind, OrderRouteKind as OrderRouteKind$1, OrderRouteStatus, OrderRoutes, OrderRoutes as OrderRoutes$1, OrderSetCustomerInput, OrderTotals, OrderUpdateInput, OrderValidateInput, OrderValidateOutput, PaymentCollectAt as PaymentCollectAt$1, PaymentEligibilityInput, PaymentEligibilityInput as PaymentEligibilityInput$1, PaymentIntentPutInput, PaymentMethod, PaymentProvider, PaymentTender, PaymentTenderInput, PlaceOrderResult, PosCatalog, PosFloorInput, PosFloorOccupancy, PosFloorPlan, PosFloorRealtimeToken, PosHealthBatchInput, PosHealthBatchResult, PosHeartbeatInput, PosHeartbeatResult, PosKioskOrder, PosKioskSettleInput, PosKioskSettleResult, PosOrderInput, PosOrderResult, PosTableAttachOrderInput, PosTableCloseInput, PosTableJoinInput, PosTableSeatInput, PosTableSeatReservationInput, PosTableSplitInput, PricingModifier, Product, Product as Product$1, ProductRewardSavings, Recommendation, RecurringDateCronFormatter, RecurringDateCronResult, RecurringDateCronSummary, RecurringDateCronValues, RouteEligibility, RouteEligibility as RouteEligibility$1, RouteEligibilityDetail, RouteEligibilityFlags, RouteEligibilityReason, RouteFailureKind, RouteFailureKind as RouteFailureKind$1, RouteHealthEvent, RouteHealthEvent as RouteHealthEvent$1, RouteHealthListener, RouteHealthListener as RouteHealthListener$1, RulesEngineOrder, RulesEngineOrderLine, RulesEngineParams, RulesEngineRuntime, Schedule, SdkLogContext, SdkLogErrorInfo, SdkLogLevel, SdkLogOptions, Store, StoreGetInput, StoreListInput, StoreNearbyInput, StorefrontAvailability, StorefrontMenu, StorefrontMenuScope, StorefrontMenuStreamOptions, StorefrontPaymentsListInput, StorefrontPreferences, SwitchCustomerArg, SwitchCustomerArg as SwitchCustomerArg$1, SwitchCustomerIdToken, SwitchCustomerOptions, SwitchCustomerOptions as SwitchCustomerOptions$1, SwitchCustomerResult, SwitchCustomerResult as SwitchCustomerResult$1, TaxBreakdownItem, TaxCategory, TaxIdValidationResult, TenderRefundInput, TenderReplaceInput, TenderVoidInput, Variant, VariantGroup, WordFilterCheckInput, WordFilterCheckResult } from "@fire-spark/types";
|
|
3
3
|
import { Big } from "big.js";
|
|
4
4
|
//#region src/productCustomization/catalog.utils.d.ts
|
|
5
5
|
/** Image for a product inside one modifier group, else the product image. */
|
|
@@ -681,11 +681,47 @@ type ApplyRedeemInput = {
|
|
|
681
681
|
/** ISO datetime with offset; validation fails once it has passed. */
|
|
682
682
|
expires_at?: string;
|
|
683
683
|
/**
|
|
684
|
-
* Optional
|
|
685
|
-
*
|
|
686
|
-
*
|
|
684
|
+
* Optional unit full price of a Masivo `PRODUCT` reward.
|
|
685
|
+
* - For ADD (`action: "ADD"` or `amount > 0`), reprices the gifted line
|
|
686
|
+
* to `full_price` so the REDEMPTION tender covers the complete menu
|
|
687
|
+
* price (extra units are charged normally).
|
|
688
|
+
* - For REPLACE / legacy `PRODUCT` (`amount: 0`), stored in
|
|
689
|
+
* `line.metadata.reward_savings` as informational savings only.
|
|
687
690
|
*/
|
|
688
691
|
full_price?: number;
|
|
692
|
+
/**
|
|
693
|
+
* Optional action for `PRODUCT` rewards. Pass `"ADD"` for a free-product
|
|
694
|
+
* coupon covered by a REDEMPTION tender, or `"REPLACE"` for a swap coupon.
|
|
695
|
+
*/
|
|
696
|
+
action?: "ADD" | "REPLACE";
|
|
697
|
+
/**
|
|
698
|
+
* Optional tax-inclusivity hint when repricing a `$0` line with
|
|
699
|
+
* `full_price` on an ADD `PRODUCT` reward. Defaults to the line's own
|
|
700
|
+
* tax mode when `unit_subtotal > 0`, else `true`.
|
|
701
|
+
*/
|
|
702
|
+
is_tax_inclusive?: boolean;
|
|
703
|
+
/**
|
|
704
|
+
* Pre-replace product snapshot(s) for a REPLACE `PRODUCT` coupon so
|
|
705
|
+
* loyalty validation and purchase report the pre-replace cart.
|
|
706
|
+
*/
|
|
707
|
+
replaced?: OrderRedeemReplaced[];
|
|
708
|
+
};
|
|
709
|
+
type ApplyReplaceRedeemInput = {
|
|
710
|
+
reward_id: string;
|
|
711
|
+
/** Reward units to redeem (integer ≥ 1). */
|
|
712
|
+
quantity: number;
|
|
713
|
+
/** Root product line being replaced; its snapshot is captured automatically. */
|
|
714
|
+
replaced_line_uid: string;
|
|
715
|
+
/** Replacement root line (and any modifier children) to insert in its place. */
|
|
716
|
+
replacement_lines: OrderLine$1[];
|
|
717
|
+
/** Active merchant `REDEMPTION` payment ID. */
|
|
718
|
+
payment_id: string;
|
|
719
|
+
/** Provider of the REDEMPTION payment (default `firespark`). */
|
|
720
|
+
provider?: PaymentProvider;
|
|
721
|
+
/** ISO datetime with offset; validation fails once it has passed. */
|
|
722
|
+
expires_at?: string;
|
|
723
|
+
/** Optional informational full price for `line.metadata.reward_savings`. */
|
|
724
|
+
full_price?: number;
|
|
689
725
|
};
|
|
690
726
|
type RemoveRedeemInput = {
|
|
691
727
|
reward_id: string;
|
|
@@ -710,6 +746,7 @@ declare class Order {
|
|
|
710
746
|
/** Real tenders chosen with `setTenders`; REDEMPTION ones come from redeem. */
|
|
711
747
|
private realTenders;
|
|
712
748
|
private readonly redemptionProviders;
|
|
749
|
+
private readonly originalProductPricing;
|
|
713
750
|
/**
|
|
714
751
|
* Redeems as applied, before the D18 cap trims payment amounts. The cap
|
|
715
752
|
* is re-derived from these on every edit, so a redeem trimmed while the
|
|
@@ -815,6 +852,13 @@ declare class Order {
|
|
|
815
852
|
* and adds no tender money.
|
|
816
853
|
*/
|
|
817
854
|
applyRedeem(input: ApplyRedeemInput): void;
|
|
855
|
+
/**
|
|
856
|
+
* Applies a Masivo REPLACE `PRODUCT` coupon: captures a pre-replace
|
|
857
|
+
* snapshot of `replaced_line_uid`, swaps it with `replacement_lines`,
|
|
858
|
+
* and attaches the `PRODUCT` redeem (`amount: 0`, `replaced: [snapshot]`)
|
|
859
|
+
* onto the replacement root line.
|
|
860
|
+
*/
|
|
861
|
+
applyReplaceRedeem(input: ApplyReplaceRedeemInput): void;
|
|
818
862
|
/**
|
|
819
863
|
* Removes `quantity` units of `reward_id` from `line_uid` or the order,
|
|
820
864
|
* latest applied first. Removing more units than applied clears them all
|
|
@@ -1183,6 +1227,12 @@ type ValidateOrderAgainstMenuInput = {
|
|
|
1183
1227
|
timeZone?: string;
|
|
1184
1228
|
/** Instant for product schedules. Defaults to `asOf`, then now. */
|
|
1185
1229
|
scheduleAt?: Date;
|
|
1230
|
+
/**
|
|
1231
|
+
* Authoritative unit `full_price` per `reward_id` (e.g. from Masivo
|
|
1232
|
+
* preview) for ADD `PRODUCT` coupons. When set, takes precedence over
|
|
1233
|
+
* `redeem.full_price` so tampered cart prices are rejected.
|
|
1234
|
+
*/
|
|
1235
|
+
productRewardFullPrices?: Readonly<Record<string, number>>;
|
|
1186
1236
|
};
|
|
1187
1237
|
/** Payment a redeem references, as listed for the merchant. */
|
|
1188
1238
|
type RedeemPaymentRow = {
|
|
@@ -1282,6 +1332,13 @@ declare const capRedeems: <T extends CapOrder>(order: T) => T & {
|
|
|
1282
1332
|
expires_at?: string | undefined;
|
|
1283
1333
|
payment_id: string;
|
|
1284
1334
|
amount: number;
|
|
1335
|
+
full_price?: number | undefined;
|
|
1336
|
+
replaced?: {
|
|
1337
|
+
id: string;
|
|
1338
|
+
sku?: string | undefined;
|
|
1339
|
+
quantity: number;
|
|
1340
|
+
group_subtotal: number;
|
|
1341
|
+
}[] | undefined;
|
|
1285
1342
|
}[];
|
|
1286
1343
|
quantity: number;
|
|
1287
1344
|
tax_rate: number | null;
|
|
@@ -1315,6 +1372,13 @@ declare const cappedRedeems: (order: CapOrder) => {
|
|
|
1315
1372
|
expires_at?: string | undefined;
|
|
1316
1373
|
payment_id: string;
|
|
1317
1374
|
amount: number;
|
|
1375
|
+
full_price?: number | undefined;
|
|
1376
|
+
replaced?: {
|
|
1377
|
+
id: string;
|
|
1378
|
+
sku?: string | undefined;
|
|
1379
|
+
quantity: number;
|
|
1380
|
+
group_subtotal: number;
|
|
1381
|
+
}[] | undefined;
|
|
1318
1382
|
}[];
|
|
1319
1383
|
type SimpleTenderPayment = {
|
|
1320
1384
|
id?: string;
|
|
@@ -1336,10 +1400,10 @@ declare const redeemsForPayment: <T extends {
|
|
|
1336
1400
|
amount: number;
|
|
1337
1401
|
}>(items: readonly T[], paymentId: string) => T[];
|
|
1338
1402
|
/**
|
|
1339
|
-
* Money
|
|
1340
|
-
*
|
|
1341
|
-
*
|
|
1342
|
-
*
|
|
1403
|
+
* Money redeems cover (Σ `amount`), including ADD PRODUCT rewards with
|
|
1404
|
+
* `amount > 0`. REPLACE or legacy PRODUCT rewards with `amount === 0` add 0.
|
|
1405
|
+
* The sum is paid by REDEMPTION tenders, so it is never subtracted from
|
|
1406
|
+
* line or order totals.
|
|
1343
1407
|
*/
|
|
1344
1408
|
declare const redeemedTotal: (redeem?: readonly RedeemLike[] | null) => number;
|
|
1345
1409
|
/** Redeemed money of every redeem of the order (order + root lines). */
|
package/dist/index.js
CHANGED
|
@@ -5058,9 +5058,10 @@ const fitRedeems = (order) => {
|
|
|
5058
5058
|
const net = nonFixedCap(order, true);
|
|
5059
5059
|
let used = moneyZero();
|
|
5060
5060
|
const fit = (item, line, lineUsed) => {
|
|
5061
|
-
if (!hasRedeemPayment(item)
|
|
5061
|
+
if (!hasRedeemPayment(item)) return moneyZero();
|
|
5062
5062
|
if (!(item.amount > 0)) return moneyZero();
|
|
5063
|
-
const
|
|
5063
|
+
const defaultBeforeTaxes = !isProductReward(item);
|
|
5064
|
+
const beforeTaxes = item.before_taxes ?? defaultBeforeTaxes;
|
|
5064
5065
|
let room = (beforeTaxes ? net : gross).minus(used);
|
|
5065
5066
|
if (line) {
|
|
5066
5067
|
const left = coverableOf(line, beforeTaxes).minus(lineUsed);
|
|
@@ -5087,9 +5088,12 @@ const fitRedeems = (order) => {
|
|
|
5087
5088
|
};
|
|
5088
5089
|
const keepFitted = (items, fitted) => {
|
|
5089
5090
|
return items.flatMap((item, index) => {
|
|
5090
|
-
if (isProductReward(item)) return [item];
|
|
5091
|
+
if (isProductReward(item) && !(item.amount > 0)) return [item];
|
|
5091
5092
|
const fits = fitted[index] ?? moneyZero();
|
|
5092
|
-
if (fits.lte(0)) return [
|
|
5093
|
+
if (fits.lte(0)) return isProductReward(item) ? [{
|
|
5094
|
+
...item,
|
|
5095
|
+
amount: 0
|
|
5096
|
+
}] : [];
|
|
5093
5097
|
if (fits.eq(item.amount)) return [item];
|
|
5094
5098
|
return [{
|
|
5095
5099
|
...item,
|
|
@@ -9339,6 +9343,120 @@ var PaymentSession = class {
|
|
|
9339
9343
|
}
|
|
9340
9344
|
};
|
|
9341
9345
|
//#endregion
|
|
9346
|
+
//#region src/order/productCoupon.ts
|
|
9347
|
+
/** Infers tax-inclusivity from an existing line; defaults true when $0. */
|
|
9348
|
+
const inferTaxInclusive = (line) => {
|
|
9349
|
+
if (!((line.tax_rate ?? 0) > 0)) return true;
|
|
9350
|
+
if (!(line.unit_subtotal > 0)) return true;
|
|
9351
|
+
const net = new Big(line.unit_subtotal).minus(line.unit_discount);
|
|
9352
|
+
return new Big(line.unit_total).lte(net);
|
|
9353
|
+
};
|
|
9354
|
+
/**
|
|
9355
|
+
* Reprices a line's unit and line-level money fields to `unitPrice` using
|
|
9356
|
+
* exact Big.js arithmetic (same formula as `toOrderLines`). Caller should
|
|
9357
|
+
* run `rebuildRootGroups` afterward to refresh group totals.
|
|
9358
|
+
*/
|
|
9359
|
+
const repriceLineUnit = (line, unitPrice, isTaxInclusive) => {
|
|
9360
|
+
const price = new Big(unitPrice);
|
|
9361
|
+
const qty = new Big(line.quantity);
|
|
9362
|
+
const taxRate = line.tax_rate ?? 0;
|
|
9363
|
+
const discountRate = line.discount_rate ?? 0;
|
|
9364
|
+
const unitDiscount = price.times(discountRate);
|
|
9365
|
+
const net = price.minus(unitDiscount);
|
|
9366
|
+
let unitTax = net.times(taxRate);
|
|
9367
|
+
let unitTotal = net.plus(unitTax);
|
|
9368
|
+
if (isTaxInclusive) {
|
|
9369
|
+
const divisor = new Big(1).plus(taxRate);
|
|
9370
|
+
unitTax = net.times(taxRate).div(divisor);
|
|
9371
|
+
unitTotal = net;
|
|
9372
|
+
}
|
|
9373
|
+
const r1 = { unit_subtotal: price.toNumber() };
|
|
9374
|
+
const r2 = { unit_discount: unitDiscount.toNumber() };
|
|
9375
|
+
const r3 = { unit_tax_total: unitTax.toNumber() };
|
|
9376
|
+
const r4 = { unit_total: unitTotal.toNumber() };
|
|
9377
|
+
const r5 = { subtotal: price.times(qty).toNumber() };
|
|
9378
|
+
const r6 = { discount_total: unitDiscount.times(qty).toNumber() };
|
|
9379
|
+
const r7 = { tax_total: unitTax.times(qty).toNumber() };
|
|
9380
|
+
const r8 = { total: unitTotal.times(qty).toNumber() };
|
|
9381
|
+
const m1 = {
|
|
9382
|
+
...r1,
|
|
9383
|
+
...r2,
|
|
9384
|
+
...r3,
|
|
9385
|
+
...r4
|
|
9386
|
+
};
|
|
9387
|
+
const m2 = {
|
|
9388
|
+
...r5,
|
|
9389
|
+
...r6,
|
|
9390
|
+
...r7,
|
|
9391
|
+
...r8
|
|
9392
|
+
};
|
|
9393
|
+
return {
|
|
9394
|
+
...line,
|
|
9395
|
+
...m1,
|
|
9396
|
+
...m2
|
|
9397
|
+
};
|
|
9398
|
+
};
|
|
9399
|
+
const captureUnitPricing = (line) => {
|
|
9400
|
+
const b1 = {
|
|
9401
|
+
tax_rate: line.tax_rate,
|
|
9402
|
+
discount_rate: line.discount_rate
|
|
9403
|
+
};
|
|
9404
|
+
const b2 = { unit_subtotal: line.unit_subtotal };
|
|
9405
|
+
const b3 = { unit_discount: line.unit_discount };
|
|
9406
|
+
const b4 = { unit_tax_total: line.unit_tax_total };
|
|
9407
|
+
return {
|
|
9408
|
+
...b1,
|
|
9409
|
+
...b2,
|
|
9410
|
+
...b3,
|
|
9411
|
+
...b4,
|
|
9412
|
+
unit_total: line.unit_total
|
|
9413
|
+
};
|
|
9414
|
+
};
|
|
9415
|
+
/** Restores previously captured unit pricing scaled to the line's quantity. */
|
|
9416
|
+
const restoreUnitPricing = (line, saved) => {
|
|
9417
|
+
const qty = new Big(line.quantity);
|
|
9418
|
+
const m = {
|
|
9419
|
+
subtotal: new Big(saved.unit_subtotal).times(qty).toNumber(),
|
|
9420
|
+
discount_total: new Big(saved.unit_discount).times(qty).toNumber(),
|
|
9421
|
+
tax_total: new Big(saved.unit_tax_total).times(qty).toNumber(),
|
|
9422
|
+
total: new Big(saved.unit_total).times(qty).toNumber()
|
|
9423
|
+
};
|
|
9424
|
+
return {
|
|
9425
|
+
...line,
|
|
9426
|
+
...saved,
|
|
9427
|
+
...m
|
|
9428
|
+
};
|
|
9429
|
+
};
|
|
9430
|
+
/** Exact Big.js REDEMPTION amount covering up to `quantity` gifted units. */
|
|
9431
|
+
const addProductRedeemAmount = (line, quantity, beforeTaxes) => {
|
|
9432
|
+
const units = Math.min(quantity, line.quantity);
|
|
9433
|
+
return new Big(beforeTaxes ? line.unit_subtotal : line.unit_total).times(units).toNumber();
|
|
9434
|
+
};
|
|
9435
|
+
const lineSkuOf = (line) => {
|
|
9436
|
+
const meta = line.metadata;
|
|
9437
|
+
if (!meta || typeof meta !== "object" || Array.isArray(meta)) return null;
|
|
9438
|
+
const raw = meta.sku;
|
|
9439
|
+
if (typeof raw !== "string") return null;
|
|
9440
|
+
const trimmed = raw.trim();
|
|
9441
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
9442
|
+
};
|
|
9443
|
+
/** Captures a pre-replace product snapshot from a root line. */
|
|
9444
|
+
const snapshotReplacedLine = (line, quantity) => {
|
|
9445
|
+
const qty = quantity ?? line.quantity;
|
|
9446
|
+
let baseSub = line.subtotal;
|
|
9447
|
+
if (Number.isFinite(line.group_subtotal)) baseSub = line.group_subtotal;
|
|
9448
|
+
let sub = new Big(baseSub).toNumber();
|
|
9449
|
+
if (qty !== line.quantity) sub = new Big(baseSub).times(qty).div(line.quantity).toNumber();
|
|
9450
|
+
const snapshot = {
|
|
9451
|
+
id: line.id,
|
|
9452
|
+
quantity: qty,
|
|
9453
|
+
group_subtotal: sub
|
|
9454
|
+
};
|
|
9455
|
+
const sku = lineSkuOf(line);
|
|
9456
|
+
if (sku) snapshot.sku = sku;
|
|
9457
|
+
return snapshot;
|
|
9458
|
+
};
|
|
9459
|
+
//#endregion
|
|
9342
9460
|
//#region src/schedule/schedule.ts
|
|
9343
9461
|
const DAY_OF_WEEK = [
|
|
9344
9462
|
"sunday",
|
|
@@ -9665,10 +9783,30 @@ const REWARD_TYPES = /* @__PURE__ */ new Set([
|
|
|
9665
9783
|
"DISCOUNT",
|
|
9666
9784
|
"PRODUCT"
|
|
9667
9785
|
]);
|
|
9786
|
+
const validateReplaced = (replaced) => {
|
|
9787
|
+
if (!Array.isArray(replaced) || replaced.length === 0) throw new Error("Redeem replaced must be a non-empty array");
|
|
9788
|
+
return replaced.map((item) => {
|
|
9789
|
+
if (typeof item.id !== "string" || !item.id.trim()) throw new Error("Replaced product requires id");
|
|
9790
|
+
if (!Number.isInteger(item.quantity) || item.quantity < 1) throw new Error("Replaced product quantity must be an integer ≥ 1");
|
|
9791
|
+
const sub = item.group_subtotal;
|
|
9792
|
+
if (typeof sub !== "number" || !Number.isFinite(sub) || sub < 0) throw new Error("Replaced product group_subtotal must be ≥ 0");
|
|
9793
|
+
const base = {
|
|
9794
|
+
id: item.id,
|
|
9795
|
+
quantity: item.quantity,
|
|
9796
|
+
group_subtotal: sub
|
|
9797
|
+
};
|
|
9798
|
+
if (item.sku !== void 0) {
|
|
9799
|
+
const trimmed = typeof item.sku === "string" ? item.sku.trim() : "";
|
|
9800
|
+
if (!trimmed) throw new Error("Replaced product sku must be non-empty");
|
|
9801
|
+
base.sku = trimmed;
|
|
9802
|
+
}
|
|
9803
|
+
return base;
|
|
9804
|
+
});
|
|
9805
|
+
};
|
|
9668
9806
|
/** Same rules as `vOrders.redeem()`; throws on invalid input. */
|
|
9669
9807
|
const buildRedeem = (input) => {
|
|
9670
9808
|
const { reward_id, quantity, payment_id, type } = input;
|
|
9671
|
-
const { before_taxes, expires_at } = input;
|
|
9809
|
+
const { before_taxes, expires_at, full_price, replaced } = input;
|
|
9672
9810
|
if (typeof reward_id !== "string" || !reward_id.trim()) throw new Error("Redeem requires reward_id");
|
|
9673
9811
|
if (!Number.isInteger(quantity) || quantity < 1) throw new Error("Redeem quantity must be an integer ≥ 1");
|
|
9674
9812
|
if (typeof payment_id !== "string" || !payment_id.trim()) throw new Error("Redeem requires payment_id");
|
|
@@ -9678,7 +9816,15 @@ const buildRedeem = (input) => {
|
|
|
9678
9816
|
if (product) {
|
|
9679
9817
|
if (amount === void 0) amount = 0;
|
|
9680
9818
|
if (typeof amount !== "number" || !Number.isFinite(amount) || amount < 0) throw new Error("Product reward amount must be ≥ 0");
|
|
9681
|
-
|
|
9819
|
+
if (replaced !== void 0 && amount > 0) throw new Error("Replace product reward amount must be 0");
|
|
9820
|
+
if (full_price !== void 0) {
|
|
9821
|
+
if (!(typeof full_price === "number" && Number.isFinite(full_price) && full_price >= 0)) throw new Error("Product reward full_price must be ≥ 0");
|
|
9822
|
+
}
|
|
9823
|
+
} else {
|
|
9824
|
+
if (full_price !== void 0) throw new Error("full_price is only valid on PRODUCT redeems");
|
|
9825
|
+
if (replaced !== void 0) throw new Error("replaced is only valid on PRODUCT redeems");
|
|
9826
|
+
if (typeof amount !== "number" || !(amount > 0)) throw new Error("Redeem requires amount > 0");
|
|
9827
|
+
}
|
|
9682
9828
|
if (expires_at !== void 0 && Number.isNaN(Date.parse(expires_at))) throw new Error("Redeem expires_at must be an ISO datetime");
|
|
9683
9829
|
if (typeof amount !== "number") throw new Error("Redeem requires amount > 0");
|
|
9684
9830
|
const redeem = {
|
|
@@ -9690,6 +9836,8 @@ const buildRedeem = (input) => {
|
|
|
9690
9836
|
if (type !== void 0) redeem.type = type;
|
|
9691
9837
|
if (before_taxes !== void 0) redeem.before_taxes = before_taxes;
|
|
9692
9838
|
if (expires_at !== void 0) redeem.expires_at = expires_at;
|
|
9839
|
+
if (full_price !== void 0) redeem.full_price = full_price;
|
|
9840
|
+
if (replaced !== void 0) redeem.replaced = validateReplaced(replaced);
|
|
9693
9841
|
return redeem;
|
|
9694
9842
|
};
|
|
9695
9843
|
/**
|
|
@@ -9758,15 +9906,17 @@ const subtractRedeem = (items, reward_id, quantity) => {
|
|
|
9758
9906
|
return kept;
|
|
9759
9907
|
};
|
|
9760
9908
|
/**
|
|
9761
|
-
* Money
|
|
9762
|
-
*
|
|
9763
|
-
*
|
|
9764
|
-
*
|
|
9909
|
+
* Money redeems cover (Σ `amount`), including ADD PRODUCT rewards with
|
|
9910
|
+
* `amount > 0`. REPLACE or legacy PRODUCT rewards with `amount === 0` add 0.
|
|
9911
|
+
* The sum is paid by REDEMPTION tenders, so it is never subtracted from
|
|
9912
|
+
* line or order totals.
|
|
9765
9913
|
*/
|
|
9766
9914
|
const redeemedTotal = (redeem = []) => {
|
|
9767
9915
|
return (redeem ?? []).reduce((sum, item) => {
|
|
9768
|
-
if (!hasRedeemPayment(item)
|
|
9769
|
-
|
|
9916
|
+
if (!hasRedeemPayment(item)) return sum;
|
|
9917
|
+
const amount = item.amount ?? 0;
|
|
9918
|
+
if (!(amount > 0)) return sum;
|
|
9919
|
+
return sum.plus(amount);
|
|
9770
9920
|
}, new Big(0)).toNumber();
|
|
9771
9921
|
};
|
|
9772
9922
|
/** Redeemed money of every redeem of the order (order + root lines). */
|
|
@@ -9887,8 +10037,9 @@ const redemptionSteps = (order) => {
|
|
|
9887
10037
|
reward_quantity: item.quantity
|
|
9888
10038
|
};
|
|
9889
10039
|
const s2 = { payment_id: item.payment_id };
|
|
10040
|
+
const isAddProduct = item.type === "PRODUCT" && item.amount > 0;
|
|
9890
10041
|
const s3 = {
|
|
9891
|
-
before_taxes: item.before_taxes ??
|
|
10042
|
+
before_taxes: item.before_taxes ?? !isAddProduct,
|
|
9892
10043
|
action
|
|
9893
10044
|
};
|
|
9894
10045
|
const s4 = { discount_amount: moneyNumber(fits) };
|
|
@@ -10240,6 +10391,7 @@ var Order = class Order {
|
|
|
10240
10391
|
/** Real tenders chosen with `setTenders`; REDEMPTION ones come from redeem. */
|
|
10241
10392
|
realTenders = null;
|
|
10242
10393
|
redemptionProviders = /* @__PURE__ */ new Map();
|
|
10394
|
+
originalProductPricing = /* @__PURE__ */ new Map();
|
|
10243
10395
|
/**
|
|
10244
10396
|
* Redeems as applied, before the D18 cap trims payment amounts. The cap
|
|
10245
10397
|
* is re-derived from these on every edit, so a redeem trimmed while the
|
|
@@ -10656,11 +10808,16 @@ var Order = class Order {
|
|
|
10656
10808
|
*/
|
|
10657
10809
|
applyRedeem(input) {
|
|
10658
10810
|
if (this.deleted) return;
|
|
10659
|
-
const { line_uid, provider,
|
|
10660
|
-
const
|
|
10661
|
-
|
|
10662
|
-
if (
|
|
10811
|
+
const { line_uid, provider, action, is_tax_inclusive, full_price } = input;
|
|
10812
|
+
const { replaced, ...rest } = input;
|
|
10813
|
+
const isProduct = rest.type === "PRODUCT";
|
|
10814
|
+
if (isProduct && line_uid === void 0) throw new Error("Product reward requires a root product line");
|
|
10815
|
+
if (provider) this.redemptionProviders.set(rest.payment_id, provider);
|
|
10663
10816
|
if (line_uid === void 0) {
|
|
10817
|
+
const redeem = buildRedeem({
|
|
10818
|
+
...rest,
|
|
10819
|
+
replaced
|
|
10820
|
+
});
|
|
10664
10821
|
this.commitLines({ redeem: [...this.appliedRedeem(), redeem] });
|
|
10665
10822
|
return;
|
|
10666
10823
|
}
|
|
@@ -10670,8 +10827,40 @@ var Order = class Order {
|
|
|
10670
10827
|
const msg = `Redeem on a FEE line requires a shipping fee, got ${line_uid}`;
|
|
10671
10828
|
throw new Error(msg);
|
|
10672
10829
|
}
|
|
10673
|
-
if (
|
|
10674
|
-
const hasFullPrice =
|
|
10830
|
+
if (isProduct && line.type !== "PRODUCT") throw new Error("Product reward requires a root product line");
|
|
10831
|
+
const hasFullPrice = isProduct && typeof full_price === "number" && Number.isFinite(full_price) && full_price >= 0;
|
|
10832
|
+
if (isProduct && action !== "REPLACE" && !replaced && (action === "ADD" || typeof rest.amount === "number" && rest.amount > 0)) {
|
|
10833
|
+
let targetLine = line;
|
|
10834
|
+
if (hasFullPrice) {
|
|
10835
|
+
if (!this.originalProductPricing.has(line_uid)) this.originalProductPricing.set(line_uid, captureUnitPricing(line));
|
|
10836
|
+
targetLine = repriceLineUnit(line, full_price, is_tax_inclusive ?? inferTaxInclusive(line));
|
|
10837
|
+
}
|
|
10838
|
+
const beforeTaxes = rest.before_taxes ?? false;
|
|
10839
|
+
const amount = rest.amount ?? addProductRedeemAmount(targetLine, rest.quantity, beforeTaxes);
|
|
10840
|
+
const b1 = {
|
|
10841
|
+
...rest,
|
|
10842
|
+
amount,
|
|
10843
|
+
before_taxes: beforeTaxes
|
|
10844
|
+
};
|
|
10845
|
+
const b2 = hasFullPrice ? { full_price } : {};
|
|
10846
|
+
const addRedeem = buildRedeem({
|
|
10847
|
+
...b1,
|
|
10848
|
+
...b2
|
|
10849
|
+
});
|
|
10850
|
+
const lines = this.draft.lines.map((item) => {
|
|
10851
|
+
if (item.uid !== line_uid) return item;
|
|
10852
|
+
return {
|
|
10853
|
+
...targetLine,
|
|
10854
|
+
redeem: [...item.redeem ?? [], addRedeem]
|
|
10855
|
+
};
|
|
10856
|
+
});
|
|
10857
|
+
this.commitLines({ lines });
|
|
10858
|
+
return;
|
|
10859
|
+
}
|
|
10860
|
+
const redeem = buildRedeem({
|
|
10861
|
+
...rest,
|
|
10862
|
+
replaced
|
|
10863
|
+
});
|
|
10675
10864
|
const lines = this.draft.lines.map((item) => {
|
|
10676
10865
|
if (item.uid !== line_uid) return item;
|
|
10677
10866
|
const updated = {
|
|
@@ -10690,6 +10879,53 @@ var Order = class Order {
|
|
|
10690
10879
|
this.commitLines({ lines });
|
|
10691
10880
|
}
|
|
10692
10881
|
/**
|
|
10882
|
+
* Applies a Masivo REPLACE `PRODUCT` coupon: captures a pre-replace
|
|
10883
|
+
* snapshot of `replaced_line_uid`, swaps it with `replacement_lines`,
|
|
10884
|
+
* and attaches the `PRODUCT` redeem (`amount: 0`, `replaced: [snapshot]`)
|
|
10885
|
+
* onto the replacement root line.
|
|
10886
|
+
*/
|
|
10887
|
+
applyReplaceRedeem(input) {
|
|
10888
|
+
if (this.deleted) return;
|
|
10889
|
+
const { replaced_line_uid, replacement_lines } = input;
|
|
10890
|
+
const oldLine = this.draft.lines.find((item) => item.uid === replaced_line_uid);
|
|
10891
|
+
if (!oldLine || oldLine.parent_uid != null || oldLine.type !== "PRODUCT") {
|
|
10892
|
+
const msg = `Replace requires a root product line, got ${replaced_line_uid}`;
|
|
10893
|
+
throw new Error(msg);
|
|
10894
|
+
}
|
|
10895
|
+
const newRoot = replacement_lines.find((item) => item.parent_uid == null);
|
|
10896
|
+
if (!newRoot || newRoot.type !== "PRODUCT") throw new Error("Replacement requires a root product line");
|
|
10897
|
+
const snapshot = snapshotReplacedLine(oldLine, input.quantity);
|
|
10898
|
+
const nextLines = replaceLine(...[this.draft.lines, replaced_line_uid], replacement_lines);
|
|
10899
|
+
this.commitLines({ lines: nextLines });
|
|
10900
|
+
const b1 = {
|
|
10901
|
+
reward_id: input.reward_id,
|
|
10902
|
+
quantity: input.quantity
|
|
10903
|
+
};
|
|
10904
|
+
const b2 = {
|
|
10905
|
+
type: "PRODUCT",
|
|
10906
|
+
action: "REPLACE"
|
|
10907
|
+
};
|
|
10908
|
+
const b3 = {
|
|
10909
|
+
line_uid: newRoot.uid,
|
|
10910
|
+
payment_id: input.payment_id
|
|
10911
|
+
};
|
|
10912
|
+
const b4 = {
|
|
10913
|
+
provider: input.provider,
|
|
10914
|
+
expires_at: input.expires_at
|
|
10915
|
+
};
|
|
10916
|
+
const b5 = {
|
|
10917
|
+
full_price: input.full_price,
|
|
10918
|
+
replaced: [snapshot]
|
|
10919
|
+
};
|
|
10920
|
+
this.applyRedeem({
|
|
10921
|
+
...b1,
|
|
10922
|
+
...b2,
|
|
10923
|
+
...b3,
|
|
10924
|
+
...b4,
|
|
10925
|
+
...b5
|
|
10926
|
+
});
|
|
10927
|
+
}
|
|
10928
|
+
/**
|
|
10693
10929
|
* Removes `quantity` units of `reward_id` from `line_uid` or the order,
|
|
10694
10930
|
* latest applied first. Removing more units than applied clears them all
|
|
10695
10931
|
* (and their REDEMPTION tender). A redeem that keeps some units keeps
|
|
@@ -10714,10 +10950,17 @@ var Order = class Order {
|
|
|
10714
10950
|
reward_id,
|
|
10715
10951
|
quantity
|
|
10716
10952
|
]);
|
|
10717
|
-
|
|
10953
|
+
let updated = {
|
|
10718
10954
|
...item,
|
|
10719
10955
|
redeem: nextRedeem
|
|
10720
10956
|
};
|
|
10957
|
+
if (!nextRedeem.some((r) => {
|
|
10958
|
+
return r.type === "PRODUCT" && r.amount > 0 && r.full_price != null;
|
|
10959
|
+
}) && this.originalProductPricing.has(line_uid)) {
|
|
10960
|
+
const saved = this.originalProductPricing.get(line_uid);
|
|
10961
|
+
updated = restoreUnitPricing(updated, saved);
|
|
10962
|
+
this.originalProductPricing.delete(line_uid);
|
|
10963
|
+
}
|
|
10721
10964
|
if (nextRedeem.some((r) => r.type === "PRODUCT")) return updated;
|
|
10722
10965
|
return withLineRewardSavings(updated, null);
|
|
10723
10966
|
});
|
|
@@ -12720,15 +12963,92 @@ const checkRootProductQuantities = (roots, lookup, out) => {
|
|
|
12720
12963
|
}
|
|
12721
12964
|
}
|
|
12722
12965
|
};
|
|
12723
|
-
const
|
|
12966
|
+
const productSkuOf = (product) => {
|
|
12967
|
+
const meta = product.metadata;
|
|
12968
|
+
if (!meta || typeof meta !== "object" || Array.isArray(meta)) return null;
|
|
12969
|
+
const raw = meta.sku;
|
|
12970
|
+
if (typeof raw !== "string") return null;
|
|
12971
|
+
const trimmed = raw.trim();
|
|
12972
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
12973
|
+
};
|
|
12974
|
+
const resolveAddProductFullPrice = (root, fullPrices) => {
|
|
12975
|
+
for (const r of root.redeem ?? []) {
|
|
12976
|
+
if (!isProductReward(r) || !(r.amount > 0)) continue;
|
|
12977
|
+
const auth = fullPrices?.[r.reward_id];
|
|
12978
|
+
if (auth !== void 0) return auth;
|
|
12979
|
+
if (r.full_price !== void 0) return r.full_price;
|
|
12980
|
+
}
|
|
12981
|
+
};
|
|
12982
|
+
const validateReplacedSnapshots = (root, lookup, out) => {
|
|
12983
|
+
(root.redeem ?? []).forEach((r, rIndex) => {
|
|
12984
|
+
if (!isProductReward(r) || !r.replaced?.length) return;
|
|
12985
|
+
r.replaced.forEach((snap, sIndex) => {
|
|
12986
|
+
const prefix = `redeem.${rIndex}.replaced.${sIndex}`;
|
|
12987
|
+
const product = lookup.productsById[snap.id];
|
|
12988
|
+
if (!product) {
|
|
12989
|
+
const b1 = { code: "PRODUCT_NOT_FOUND" };
|
|
12990
|
+
const b2 = {
|
|
12991
|
+
field: `${prefix}.id`,
|
|
12992
|
+
actual: snap.id
|
|
12993
|
+
};
|
|
12994
|
+
pushLineIssue(out, root.uid, {
|
|
12995
|
+
...b1,
|
|
12996
|
+
...b2
|
|
12997
|
+
});
|
|
12998
|
+
return;
|
|
12999
|
+
}
|
|
13000
|
+
if (isVariantBase(product)) {
|
|
13001
|
+
const b1 = { code: "VARIANT_BASE_NOT_ORDERABLE" };
|
|
13002
|
+
const b2 = {
|
|
13003
|
+
field: `${prefix}.id`,
|
|
13004
|
+
actual: snap.id
|
|
13005
|
+
};
|
|
13006
|
+
pushLineIssue(out, root.uid, {
|
|
13007
|
+
...b1,
|
|
13008
|
+
...b2
|
|
13009
|
+
});
|
|
13010
|
+
return;
|
|
13011
|
+
}
|
|
13012
|
+
const expectedSub = new Big(product.pricing.price).times(snap.quantity).toNumber();
|
|
13013
|
+
if (!bigEq(snap.group_subtotal, expectedSub)) {
|
|
13014
|
+
const b1 = { code: "PRICE_MISMATCH" };
|
|
13015
|
+
const b2 = { field: `${prefix}.group_subtotal` };
|
|
13016
|
+
const b3 = { expected: bigStr(expectedSub) };
|
|
13017
|
+
const b4 = { actual: bigStr(snap.group_subtotal) };
|
|
13018
|
+
pushLineIssue(out, root.uid, {
|
|
13019
|
+
...b1,
|
|
13020
|
+
...b2,
|
|
13021
|
+
...b3,
|
|
13022
|
+
...b4
|
|
13023
|
+
});
|
|
13024
|
+
}
|
|
13025
|
+
if (snap.sku !== void 0) {
|
|
13026
|
+
const expectedSku = productSkuOf(product) ?? product.id;
|
|
13027
|
+
if (snap.sku !== expectedSku) {
|
|
13028
|
+
const b1 = { code: "STRUCTURE_MISMATCH" };
|
|
13029
|
+
const b2 = {
|
|
13030
|
+
field: `${prefix}.sku`,
|
|
13031
|
+
expected: expectedSku
|
|
13032
|
+
};
|
|
13033
|
+
pushLineIssue(out, root.uid, {
|
|
13034
|
+
...b1,
|
|
13035
|
+
...b2,
|
|
13036
|
+
actual: snap.sku
|
|
13037
|
+
});
|
|
13038
|
+
}
|
|
13039
|
+
}
|
|
13040
|
+
});
|
|
13041
|
+
});
|
|
13042
|
+
};
|
|
13043
|
+
const validateProductRoot = (root, lines, lookup, locale, out, at, timeZone, fullPrices) => {
|
|
12724
13044
|
const product = lookup.productsById[root.id];
|
|
12725
13045
|
if (!product) {
|
|
12726
13046
|
pushLineIssue(out, root.uid, { code: "PRODUCT_NOT_FOUND" });
|
|
12727
|
-
return;
|
|
13047
|
+
return null;
|
|
12728
13048
|
}
|
|
12729
13049
|
if (isVariantBase(product)) {
|
|
12730
13050
|
pushLineIssue(out, root.uid, { code: "VARIANT_BASE_NOT_ORDERABLE" });
|
|
12731
|
-
return;
|
|
13051
|
+
return null;
|
|
12732
13052
|
}
|
|
12733
13053
|
if ((product.pricing.variants ?? []).length > 0 && !isOfferedVariant(lookup, product)) pushLineIssue(out, root.uid, { code: "VARIANT_NOT_OFFERED" });
|
|
12734
13054
|
const status = product.availability.status;
|
|
@@ -12737,6 +13057,7 @@ const validateProductRoot = (root, lines, lookup, locale, out, at, timeZone) =>
|
|
|
12737
13057
|
const hours = product.availability.schedules;
|
|
12738
13058
|
if (!isOnSchedule(hours, at, timeZone)) pushLineIssue(out, root.uid, { code: "OUTSIDE_HOURS" });
|
|
12739
13059
|
collectUnmatchedModifiers(lines, lookup, root.id, root.uid, out);
|
|
13060
|
+
validateReplacedSnapshots(root, lookup, out);
|
|
12740
13061
|
let expected;
|
|
12741
13062
|
if (needsSlotPlacement(lines, root.uid, lookup)) {
|
|
12742
13063
|
const placed = placeCartLines(root, lines, lookup, locale);
|
|
@@ -12748,12 +13069,20 @@ const validateProductRoot = (root, lines, lookup, locale, out, at, timeZone) =>
|
|
|
12748
13069
|
if (session.validate() !== "VALID") pushLineIssue(out, root.uid, { code: "MODIFIER_RULES_INVALID" });
|
|
12749
13070
|
expected = toOrderLines(session.root, locale);
|
|
12750
13071
|
}
|
|
12751
|
-
|
|
13072
|
+
let expectedRoot = expected.find((line) => line.parent_uid == null);
|
|
12752
13073
|
if (!expectedRoot) {
|
|
12753
13074
|
pushLineIssue(out, root.uid, { code: "STRUCTURE_MISMATCH" });
|
|
12754
|
-
return;
|
|
13075
|
+
return null;
|
|
13076
|
+
}
|
|
13077
|
+
const fullPrice = resolveAddProductFullPrice(root, fullPrices);
|
|
13078
|
+
if (fullPrice !== void 0) {
|
|
13079
|
+
const inclusive = product.pricing.is_tax_inclusive;
|
|
13080
|
+
const repriced = repriceLineUnit(expectedRoot, fullPrice, inclusive);
|
|
13081
|
+
expected = rebuildRootGroups(expected.map((l) => l.uid === expectedRoot.uid ? repriced : l));
|
|
13082
|
+
expectedRoot = expected.find((line) => line.parent_uid == null);
|
|
12755
13083
|
}
|
|
12756
13084
|
compareTrees(lines, expected, root.uid, expectedRoot.uid, out);
|
|
13085
|
+
return expectedRoot;
|
|
12757
13086
|
};
|
|
12758
13087
|
const isExpired = (redeem, now) => {
|
|
12759
13088
|
if (redeem.expires_at === void 0) return false;
|
|
@@ -12787,7 +13116,7 @@ const paymentProblem = (payment_id, payments) => {
|
|
|
12787
13116
|
* money. With `payments`, the payment must exist, be active and be a
|
|
12788
13117
|
* REDEMPTION method.
|
|
12789
13118
|
*/
|
|
12790
|
-
const redeemIssues = (items, fitted, payments) => {
|
|
13119
|
+
const redeemIssues = (items, fitted, payments, expectedLine) => {
|
|
12791
13120
|
const issues = [];
|
|
12792
13121
|
items.forEach((item, index) => {
|
|
12793
13122
|
const prefix = `redeem.${index}`;
|
|
@@ -12801,15 +13130,47 @@ const redeemIssues = (items, fitted, payments) => {
|
|
|
12801
13130
|
}
|
|
12802
13131
|
if (isProductReward(item)) {
|
|
12803
13132
|
const problem = payments ? paymentProblem(payment_id, payments) : null;
|
|
12804
|
-
if (
|
|
13133
|
+
if (problem) {
|
|
13134
|
+
const b1 = {
|
|
13135
|
+
code: "REDEEM_PAYMENT_METHOD_NOT_CONFIGURED",
|
|
13136
|
+
field: `${prefix}.payment_id`
|
|
13137
|
+
};
|
|
13138
|
+
issues.push({
|
|
13139
|
+
...b1,
|
|
13140
|
+
expected: REDEMPTION_METHOD,
|
|
13141
|
+
actual: problem
|
|
13142
|
+
});
|
|
13143
|
+
return;
|
|
13144
|
+
}
|
|
13145
|
+
if (!(item.amount > 0)) return;
|
|
13146
|
+
if (item.replaced && item.replaced.length > 0) {
|
|
13147
|
+
const b1 = {
|
|
13148
|
+
code: "REDEEM_AMOUNT_MISMATCH",
|
|
13149
|
+
field: `${prefix}.amount`,
|
|
13150
|
+
expected: "0"
|
|
13151
|
+
};
|
|
13152
|
+
issues.push({
|
|
13153
|
+
...b1,
|
|
13154
|
+
actual: bigStr(item.amount)
|
|
13155
|
+
});
|
|
13156
|
+
return;
|
|
13157
|
+
}
|
|
13158
|
+
let expected = fitted[index];
|
|
13159
|
+
if (expectedLine) {
|
|
13160
|
+
const beforeTaxes = item.before_taxes ?? false;
|
|
13161
|
+
const raw = new Big(addProductRedeemAmount(expectedLine, item.quantity, beforeTaxes));
|
|
13162
|
+
expected = expected && expected.lt(raw) ? expected : raw;
|
|
13163
|
+
}
|
|
13164
|
+
if (expected && expected.eq(item.amount)) return;
|
|
12805
13165
|
const b1 = {
|
|
12806
|
-
code: "
|
|
12807
|
-
field: `${prefix}.
|
|
13166
|
+
code: "REDEEM_AMOUNT_MISMATCH",
|
|
13167
|
+
field: `${prefix}.amount`
|
|
12808
13168
|
};
|
|
13169
|
+
const b2 = { expected: bigStr(expected?.toNumber() ?? 0) };
|
|
12809
13170
|
issues.push({
|
|
12810
13171
|
...b1,
|
|
12811
|
-
|
|
12812
|
-
actual:
|
|
13172
|
+
...b2,
|
|
13173
|
+
actual: bigStr(item.amount)
|
|
12813
13174
|
});
|
|
12814
13175
|
return;
|
|
12815
13176
|
}
|
|
@@ -12925,7 +13286,7 @@ const stepIssues = (submitted, expected) => {
|
|
|
12925
13286
|
});
|
|
12926
13287
|
return issues;
|
|
12927
13288
|
};
|
|
12928
|
-
const validateRedemptions = (input, issues, lineIssues) => {
|
|
13289
|
+
const validateRedemptions = (input, issues, lineIssues, expectedRoots) => {
|
|
12929
13290
|
const redeem = input.redeem ?? [];
|
|
12930
13291
|
const now = input.now ?? input.asOf?.getTime() ?? Date.now();
|
|
12931
13292
|
const p1 = {
|
|
@@ -12961,12 +13322,13 @@ const validateRedemptions = (input, issues, lineIssues) => {
|
|
|
12961
13322
|
...b2
|
|
12962
13323
|
});
|
|
12963
13324
|
});
|
|
13325
|
+
const expRoot = expectedRoots.get(line.uid) ?? null;
|
|
12964
13326
|
const p2 = [
|
|
12965
13327
|
items,
|
|
12966
13328
|
fitted.lines[index] ?? [],
|
|
12967
13329
|
input.payments
|
|
12968
13330
|
];
|
|
12969
|
-
for (const issue of redeemIssues(...p2)) pushLineIssue(lineIssues, line.uid, issue);
|
|
13331
|
+
for (const issue of redeemIssues(...p2, expRoot)) pushLineIssue(lineIssues, line.uid, issue);
|
|
12970
13332
|
});
|
|
12971
13333
|
if (input.redemptionSteps === void 0) return;
|
|
12972
13334
|
const expected = redemptionSteps(p1);
|
|
@@ -13028,6 +13390,7 @@ const validateOrderAgainstMenu = (input) => {
|
|
|
13028
13390
|
};
|
|
13029
13391
|
}
|
|
13030
13392
|
const lookup = indexMenu(input.menu);
|
|
13393
|
+
const expectedRoots = /* @__PURE__ */ new Map();
|
|
13031
13394
|
for (const root of roots) {
|
|
13032
13395
|
if (root.type === "FEE") {
|
|
13033
13396
|
checkFeeSelfConsistency(root, lineIssues);
|
|
@@ -13042,13 +13405,15 @@ const validateOrderAgainstMenu = (input) => {
|
|
|
13042
13405
|
const p2 = [
|
|
13043
13406
|
lineIssues,
|
|
13044
13407
|
at,
|
|
13045
|
-
zone
|
|
13408
|
+
zone,
|
|
13409
|
+
input.productRewardFullPrices
|
|
13046
13410
|
];
|
|
13047
|
-
validateProductRoot(...p1, ...p2);
|
|
13411
|
+
const expRoot = validateProductRoot(...p1, ...p2);
|
|
13412
|
+
if (expRoot) expectedRoots.set(root.uid, expRoot);
|
|
13048
13413
|
}
|
|
13049
13414
|
checkRootProductQuantities(roots, lookup, lineIssues);
|
|
13050
13415
|
if (input.expectedFeeLines) compareExpectedFeeLines(input.lines, input.expectedFeeLines, lineIssues);
|
|
13051
|
-
validateRedemptions(input, issues, lineIssues);
|
|
13416
|
+
validateRedemptions(input, issues, lineIssues, expectedRoots);
|
|
13052
13417
|
compareRootGroups(input.lines, menuLines, lineIssues);
|
|
13053
13418
|
const expectedTotals = recomputeTotals(menuLines, input.totals.currency);
|
|
13054
13419
|
for (const field of TOTALS_FIELDS) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fire-spark/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.56.0",
|
|
4
4
|
"description": "Fire Spark headless SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"big.js": "6.2.1",
|
|
21
21
|
"dayjs": "1.11.9",
|
|
22
22
|
"@types/big.js": "6.2.2",
|
|
23
|
-
"@fire-spark/payments": "^0.
|
|
24
|
-
"@fire-spark/types": "^0.
|
|
23
|
+
"@fire-spark/payments": "^0.56.0",
|
|
24
|
+
"@fire-spark/types": "^0.56.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"tsdown": "^0.22.14",
|