@managesome/eposmatic-toolkit 0.2.330 → 0.2.331

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.
@@ -43,16 +43,21 @@ export interface ICartOfferSyncContext {
43
43
  offers: IOffer[];
44
44
  }
45
45
  export declare function calculateCartOrderInfoAndSyncData<T extends IOrder | Partial<IOrder>>(data: T, courses?: ICourse[], listnerSettings?: ICartListnerSettings, calculateItemMetadata?: boolean, syncContext?: ICartOfferSyncContext): T;
46
+ export type IRewardClaim = {
47
+ type: 'offer';
48
+ offerId: string;
49
+ } | {
50
+ type: 'gift-card';
51
+ giftCardId: string;
52
+ mysteryGiftType?: GIFT_CARD_REWARD_TYPE_ENUM | null;
53
+ };
46
54
  export interface ISetAddItemToCartActionPayload {
47
55
  name: string;
48
56
  category_id: string;
49
57
  item: IOrderItem;
50
58
  chosen_course_id: string | null;
51
59
  replace_id?: string;
52
- walletGiftCardClaim?: {
53
- giftCardId: string;
54
- mysteryGiftType?: GIFT_CARD_REWARD_TYPE_ENUM | null;
55
- };
60
+ rewardClaim?: IRewardClaim | null;
56
61
  }
57
62
  export declare const getOrderItemComaparableData: (item: IOrderItem) => Partial<IOrderItem>;
58
63
  export declare function getAddItemToCartData(data: Partial<IOrder>, payload: ISetAddItemToCartActionPayload, courses: ICourse[], listnerSettings: ICartListnerSettings): Partial<IOrder>;
@@ -1558,11 +1558,83 @@ function calculateCartOrderInfoAndSyncData(data, courses, listnerSettings, calcu
1558
1558
  }
1559
1559
  return syncedData;
1560
1560
  }
1561
- function getOrderDataWithSyncedWalletGiftCardClaim({ data, walletGiftCardClaim, }) {
1562
- if (walletGiftCardClaim == null) {
1561
+ function getNormalizedRewardClaim(rewardClaim) {
1562
+ if (rewardClaim == null) {
1563
+ return null;
1564
+ }
1565
+ if (rewardClaim.type === 'offer') {
1566
+ const offerId = getNormalizedOfferId(rewardClaim.offerId);
1567
+ if (offerId == null) {
1568
+ return null;
1569
+ }
1570
+ return {
1571
+ type: 'offer',
1572
+ offerId,
1573
+ };
1574
+ }
1575
+ const giftCardId = getNormalizedOfferId(rewardClaim.giftCardId);
1576
+ if (giftCardId == null) {
1577
+ return null;
1578
+ }
1579
+ return {
1580
+ type: 'gift-card',
1581
+ giftCardId,
1582
+ ...(rewardClaim.mysteryGiftType != null
1583
+ ? {
1584
+ mysteryGiftType: rewardClaim.mysteryGiftType,
1585
+ }
1586
+ : {}),
1587
+ };
1588
+ }
1589
+ function getItemDiscountDetailsWithoutRewardMarkers(itemDiscountDetails) {
1590
+ if (itemDiscountDetails == null) {
1591
+ return null;
1592
+ }
1593
+ const discountDetailsWithoutLoyaltyReward = clearLoyaltyDiscountFields(itemDiscountDetails);
1594
+ return clearVoucherDiscountFields(discountDetailsWithoutLoyaltyReward);
1595
+ }
1596
+ function getValidatedRewardItemAddContext({ item, rewardClaim, }) {
1597
+ const itemDiscountDetails = item.item_discount_details;
1598
+ const loyaltyDiscountId = getNormalizedOfferId(itemDiscountDetails?.loyalty_discount_id);
1599
+ const voucherDiscountId = getNormalizedOfferId(itemDiscountDetails?.voucher_discount_id);
1600
+ const hasRewardMarkers = loyaltyDiscountId != null || voucherDiscountId != null;
1601
+ if (!hasRewardMarkers) {
1602
+ return {
1603
+ item,
1604
+ rewardClaim: null,
1605
+ };
1606
+ }
1607
+ if (rewardClaim == null) {
1608
+ return {
1609
+ item: {
1610
+ ...item,
1611
+ item_discount_details: getItemDiscountDetailsWithoutRewardMarkers(itemDiscountDetails),
1612
+ },
1613
+ rewardClaim: null,
1614
+ };
1615
+ }
1616
+ const isMatchingRewardClaim = rewardClaim.type === 'gift-card'
1617
+ ? loyaltyDiscountId != null && voucherDiscountId == null && rewardClaim.giftCardId === loyaltyDiscountId
1618
+ : rewardClaim.offerId === loyaltyDiscountId || rewardClaim.offerId === voucherDiscountId;
1619
+ if (isMatchingRewardClaim) {
1620
+ return {
1621
+ item,
1622
+ rewardClaim,
1623
+ };
1624
+ }
1625
+ return {
1626
+ item: {
1627
+ ...item,
1628
+ item_discount_details: getItemDiscountDetailsWithoutRewardMarkers(itemDiscountDetails),
1629
+ },
1630
+ rewardClaim: null,
1631
+ };
1632
+ }
1633
+ function getOrderDataWithSyncedGiftCardRewardClaim({ data, rewardClaim, }) {
1634
+ if (rewardClaim == null || rewardClaim.type !== 'gift-card') {
1563
1635
  return data;
1564
1636
  }
1565
- const claimedGiftCardId = walletGiftCardClaim.giftCardId?.toString().trim();
1637
+ const claimedGiftCardId = rewardClaim.giftCardId?.toString().trim();
1566
1638
  if (claimedGiftCardId == null || claimedGiftCardId.length === 0) {
1567
1639
  return data;
1568
1640
  }
@@ -1577,9 +1649,9 @@ function getOrderDataWithSyncedWalletGiftCardClaim({ data, walletGiftCardClaim,
1577
1649
  return {
1578
1650
  ...giftCardRedemption,
1579
1651
  redeemed_times: toPositiveInteger(giftCardRedemption.redeemed_times, 0) + 1,
1580
- ...(walletGiftCardClaim.mysteryGiftType != null
1652
+ ...(rewardClaim.mysteryGiftType != null
1581
1653
  ? {
1582
- mystery_gift_type: walletGiftCardClaim.mysteryGiftType,
1654
+ mystery_gift_type: rewardClaim.mysteryGiftType,
1583
1655
  }
1584
1656
  : {}),
1585
1657
  };
@@ -1588,9 +1660,9 @@ function getOrderDataWithSyncedWalletGiftCardClaim({ data, walletGiftCardClaim,
1588
1660
  nextGiftCardRedemptions.push({
1589
1661
  gift_card_id: claimedGiftCardId,
1590
1662
  redeemed_times: 1,
1591
- ...(walletGiftCardClaim.mysteryGiftType != null
1663
+ ...(rewardClaim.mysteryGiftType != null
1592
1664
  ? {
1593
- mystery_gift_type: walletGiftCardClaim.mysteryGiftType,
1665
+ mystery_gift_type: rewardClaim.mysteryGiftType,
1594
1666
  }
1595
1667
  : {}),
1596
1668
  });
@@ -1607,6 +1679,43 @@ function getOrderDataWithSyncedWalletGiftCardClaim({ data, walletGiftCardClaim,
1607
1679
  wallet_redemptions: nextWalletRedemptions,
1608
1680
  };
1609
1681
  }
1682
+ function getOrderDataWithSyncedItemRewardRedemptionEvidence({ data, itemDiscountDetails, rewardClaim, }) {
1683
+ if (itemDiscountDetails == null || rewardClaim == null) {
1684
+ return data;
1685
+ }
1686
+ const evidenceOfferIds = new Set();
1687
+ const loyaltyDiscountId = getNormalizedOfferId(itemDiscountDetails.loyalty_discount_id);
1688
+ const voucherDiscountId = getNormalizedOfferId(itemDiscountDetails.voucher_discount_id);
1689
+ if (rewardClaim.type === 'offer') {
1690
+ if (rewardClaim.offerId === loyaltyDiscountId && loyaltyDiscountId != null) {
1691
+ evidenceOfferIds.add(loyaltyDiscountId);
1692
+ }
1693
+ if (rewardClaim.offerId === voucherDiscountId && voucherDiscountId != null) {
1694
+ evidenceOfferIds.add(voucherDiscountId);
1695
+ }
1696
+ }
1697
+ if (evidenceOfferIds.size === 0) {
1698
+ return data;
1699
+ }
1700
+ const nextRedeemedOffers = {
1701
+ ...(getRedeemedOffersMap(data.redeemed_offers) ?? {}),
1702
+ };
1703
+ let hasChanges = false;
1704
+ evidenceOfferIds.forEach((offerId) => {
1705
+ const currentCount = toPositiveInteger(nextRedeemedOffers[offerId], 0);
1706
+ if (currentCount <= 0) {
1707
+ nextRedeemedOffers[offerId] = 1;
1708
+ hasChanges = true;
1709
+ }
1710
+ });
1711
+ if (!hasChanges) {
1712
+ return data;
1713
+ }
1714
+ return {
1715
+ ...data,
1716
+ redeemed_offers: getRedeemedOffersMap(nextRedeemedOffers),
1717
+ };
1718
+ }
1610
1719
  const getOrderItemComaparableData = (item) => ({
1611
1720
  item_id: item.item_id,
1612
1721
  menu_id: item.menu_id,
@@ -1639,9 +1748,16 @@ function getAddItemToCartData(data, payload, courses, listnerSettings) {
1639
1748
  modifiers: item.modifiers,
1640
1749
  };
1641
1750
  };
1642
- const { category_id, name, item, chosen_course_id, replace_id, walletGiftCardClaim } = payload;
1643
- const comparableItem = getComparableItem(item);
1644
- let mainItemId = item._id;
1751
+ const { category_id, name, item, chosen_course_id, replace_id, rewardClaim } = payload;
1752
+ const normalizedRewardClaim = getNormalizedRewardClaim(rewardClaim);
1753
+ const validatedRewardItemAddContext = getValidatedRewardItemAddContext({
1754
+ item,
1755
+ rewardClaim: normalizedRewardClaim,
1756
+ });
1757
+ const itemWithValidatedRewardClaim = validatedRewardItemAddContext.item;
1758
+ const validatedRewardClaim = validatedRewardItemAddContext.rewardClaim;
1759
+ const comparableItem = getComparableItem(itemWithValidatedRewardClaim);
1760
+ let mainItemId = itemWithValidatedRewardClaim._id;
1645
1761
  const orderData = { ...data };
1646
1762
  // =====================><=====================
1647
1763
  const categories = [...(orderData.categories ?? [])];
@@ -1657,34 +1773,34 @@ function getAddItemToCartData(data, payload, courses, listnerSettings) {
1657
1773
  const totalItems = allItems.length;
1658
1774
  for (let j = 0; j < totalItems; j++) {
1659
1775
  const itm = allItems[j];
1660
- if (itm._id === item._id) {
1661
- allItems[j] = item;
1776
+ if (itm._id === itemWithValidatedRewardClaim._id) {
1777
+ allItems[j] = itemWithValidatedRewardClaim;
1662
1778
  didFindItem = true;
1663
1779
  break;
1664
1780
  }
1665
- if (itm.item_id === item.item_id && !itm.is_split) {
1781
+ if (itm.item_id === itemWithValidatedRewardClaim.item_id && !itm.is_split) {
1666
1782
  itemExists = true;
1667
1783
  }
1668
1784
  }
1669
1785
  if (!didFindItem && itemExists) {
1670
1786
  for (let j = 0; j < totalItems; j++) {
1671
1787
  const itm = allItems[j];
1672
- if (itm.item_id === item.item_id &&
1788
+ if (itm.item_id === itemWithValidatedRewardClaim.item_id &&
1673
1789
  !itm.is_split &&
1674
1790
  // We make sure that the item does not have loyalty_discount_id and voucher_discount_id
1675
1791
  itm.item_discount_details?.loyalty_discount_id == null &&
1676
1792
  itm.item_discount_details?.voucher_discount_id == null &&
1677
1793
  (0, lodash_1.isEqual)(getComparableItem(itm), comparableItem)) {
1678
1794
  mainItemId = replace_id ?? itm._id;
1679
- const newQuantity = item.quantity + itm.quantity;
1795
+ const newQuantity = itemWithValidatedRewardClaim.quantity + itm.quantity;
1680
1796
  allItems[j] = {
1681
- ...item,
1797
+ ...itemWithValidatedRewardClaim,
1682
1798
  _id: mainItemId,
1683
1799
  quantity: newQuantity,
1684
1800
  item_discount_details: getItemQuantityChangeResults({
1685
- prevQuantity: item.quantity,
1801
+ prevQuantity: itemWithValidatedRewardClaim.quantity,
1686
1802
  newQuantity,
1687
- prevItemDiscountDetails: item.item_discount_details,
1803
+ prevItemDiscountDetails: itemWithValidatedRewardClaim.item_discount_details,
1688
1804
  }),
1689
1805
  };
1690
1806
  didFindItem = true;
@@ -1693,7 +1809,7 @@ function getAddItemToCartData(data, payload, courses, listnerSettings) {
1693
1809
  }
1694
1810
  }
1695
1811
  if (!didFindItem) {
1696
- allItems.push(item);
1812
+ allItems.push(itemWithValidatedRewardClaim);
1697
1813
  }
1698
1814
  categories[i] = {
1699
1815
  ...category,
@@ -1801,11 +1917,16 @@ function getAddItemToCartData(data, payload, courses, listnerSettings) {
1801
1917
  ...(iterations.length > 0 && { iterations }),
1802
1918
  categories,
1803
1919
  };
1804
- const orderWithSyncedWalletGiftCardClaim = getOrderDataWithSyncedWalletGiftCardClaim({
1920
+ const orderWithSyncedRewardClaim = getOrderDataWithSyncedGiftCardRewardClaim({
1805
1921
  data: order,
1806
- walletGiftCardClaim,
1922
+ rewardClaim: validatedRewardClaim,
1923
+ });
1924
+ const orderWithSyncedItemRewardEvidence = getOrderDataWithSyncedItemRewardRedemptionEvidence({
1925
+ data: orderWithSyncedRewardClaim,
1926
+ itemDiscountDetails: itemWithValidatedRewardClaim.item_discount_details,
1927
+ rewardClaim: validatedRewardClaim,
1807
1928
  });
1808
- const newData = calculateCartOrderInfoAndSyncData(orderWithSyncedWalletGiftCardClaim, courses, listnerSettings);
1929
+ const newData = calculateCartOrderInfoAndSyncData(orderWithSyncedItemRewardEvidence, courses, listnerSettings);
1809
1930
  return newData;
1810
1931
  }
1811
1932
  function getSplitCartItemToTwo({ data, orderItemId, courses, listnerSettings }) {
@@ -1,5 +1,6 @@
1
1
  import { ORDER_ITEM_TYPE_ENUM, PLATFORMS_ENUM } from '../../constants';
2
2
  import { IItem, IOffer, IOrderItem } from '../../schemas';
3
+ import type { IRewardClaim } from '../store';
3
4
  import { ISimplifiedCatalogItem } from '../types';
4
5
  import { IApplyLoyalty, IApplyVoucher } from './order-engine';
5
6
  interface ICalculateGrandTotal {
@@ -33,6 +34,7 @@ export interface ICatalogItemOptionsDrawerProps<I extends ISimplifiedCatalogItem
33
34
  maxQuantity?: number;
34
35
  applyLoyalty?: IApplyLoyalty;
35
36
  applyVoucher?: IApplyVoucher;
37
+ rewardClaim?: IRewardClaim;
36
38
  isRewardItem?: boolean;
37
39
  onSelect?: (item: IOrderItem) => void;
38
40
  }
@@ -1,12 +1,23 @@
1
1
  import { ORDER_ITEM_TYPE_ENUM, PLATFORMS_ENUM } from '../../constants';
2
- import { IDiscountDetails, IItem, IModifier, IOrderItem, IPrice } from '../../schemas';
2
+ import { IDiscountDetails, IGiftCard, IItem, IModifier, IOffer, IOrderItem, IPrice } from '../../schemas';
3
3
  import { ISimplifiedCatalogItem } from '../types';
4
4
  import { IOrderEngineItem, IOrderEngineModifier, IOrderEngineOption } from '../types/order-engine';
5
5
  export declare function hasMissedRequiredItemModifier(currentModifiers: IModifier[] | null, platform: PLATFORMS_ENUM | undefined): boolean;
6
6
  export declare function syncOrderEngineItem(orderEngineItem: IOrderEngineItem): IOrderEngineItem;
7
7
  export declare function checkIfModifiersExist(modifiers: IModifier[] | null, platform: PLATFORMS_ENUM | undefined): boolean;
8
8
  export declare function makeSpaceForFreeOptions(options: IOrderEngineOption[], num_free_options: number | null | undefined): IOrderEngineOption[];
9
- export declare function convertOrderEngineItemToOrderItem(orderItem: IOrderEngineItem, tenantId: string): IOrderItem;
9
+ export type IOrderEngineItemRewardClaim = {
10
+ type: 'offer';
11
+ offerId: string;
12
+ } | {
13
+ type: 'gift-card';
14
+ giftCardId: string;
15
+ mysteryGiftType?: IGiftCard['reward_type'] | null;
16
+ };
17
+ export type IConvertedOrderEngineItem = IOrderItem & {
18
+ rewardClaim?: IOrderEngineItemRewardClaim;
19
+ };
20
+ export declare function convertOrderEngineItemToOrderItem(orderItem: IOrderEngineItem, tenantId: string, offers?: IOffer[] | null, giftCards?: IGiftCard[] | null): IConvertedOrderEngineItem;
10
21
  export declare function convertItemDiscountToOrderItemDiscount(discount_amount: IPrice | null, is_special_discount_percentage: boolean, platform: PLATFORMS_ENUM | undefined, discountId: string): IDiscountDetails | null;
11
22
  type TDiscountDetailsParams = {
12
23
  discount_value: number;
@@ -437,11 +437,50 @@ function convertOrderEngineModifiersToOrderItemModifiers(modifiers, tenantId) {
437
437
  }
438
438
  return newModifiers;
439
439
  }
440
- function convertOrderEngineItemToOrderItem(orderItem, tenantId) {
440
+ function getNormalizedDiscountId(discountId) {
441
+ if (discountId == null)
442
+ return null;
443
+ const normalizedId = discountId.toString().trim();
444
+ return normalizedId.length > 0 ? normalizedId : null;
445
+ }
446
+ function getRewardClaimFromOrderItemDiscountDetails({ itemDiscountDetails, offers, giftCards, }) {
447
+ const loyaltyDiscountId = getNormalizedDiscountId(itemDiscountDetails?.loyalty_discount_id);
448
+ const voucherDiscountId = getNormalizedDiscountId(itemDiscountDetails?.voucher_discount_id);
449
+ if (loyaltyDiscountId == null && voucherDiscountId == null) {
450
+ return undefined;
451
+ }
452
+ const matchedGiftCard = loyaltyDiscountId != null
453
+ ? ((giftCards ?? []).find((giftCard) => giftCard._id === loyaltyDiscountId) ?? null)
454
+ : null;
455
+ if (matchedGiftCard != null) {
456
+ return {
457
+ type: 'gift-card',
458
+ giftCardId: matchedGiftCard._id,
459
+ ...(matchedGiftCard.reward_type != null
460
+ ? {
461
+ mysteryGiftType: matchedGiftCard.reward_type,
462
+ }
463
+ : {}),
464
+ };
465
+ }
466
+ const matchedOfferId = voucherDiscountId != null
467
+ ? ((offers ?? []).find((offer) => offer._id === voucherDiscountId)?._id ?? null)
468
+ : loyaltyDiscountId != null
469
+ ? ((offers ?? []).find((offer) => offer._id === loyaltyDiscountId)?._id ?? null)
470
+ : null;
471
+ if (matchedOfferId == null) {
472
+ return undefined;
473
+ }
474
+ return {
475
+ type: 'offer',
476
+ offerId: matchedOfferId,
477
+ };
478
+ }
479
+ function convertOrderEngineItemToOrderItem(orderItem, tenantId, offers, giftCards) {
441
480
  // When excluded_discount_refs exist, resolve percentage discounts to fixed amounts
442
481
  // so that cart-actions (which doesn't know about exclusions) recalculates correctly.
443
482
  const item_discount_details = resolveDiscountDetailsForExcludedRefs(orderItem);
444
- return {
483
+ const convertedOrderItem = {
445
484
  _id: orderItem._id,
446
485
  item_id: orderItem.item_id,
447
486
  menu_id: orderItem.menu_id,
@@ -462,6 +501,19 @@ function convertOrderEngineItemToOrderItem(orderItem, tenantId) {
462
501
  _gst_amount: 0,
463
502
  _net_amount: 0,
464
503
  };
504
+ const rewardClaim = getRewardClaimFromOrderItemDiscountDetails({
505
+ itemDiscountDetails: convertedOrderItem.item_discount_details,
506
+ offers,
507
+ giftCards,
508
+ });
509
+ return {
510
+ ...convertedOrderItem,
511
+ ...(rewardClaim != null
512
+ ? {
513
+ rewardClaim,
514
+ }
515
+ : {}),
516
+ };
465
517
  }
466
518
  // ==========================================================><====================================================
467
519
  function convertItemDiscountToOrderItemDiscount(discount_amount, is_special_discount_percentage, platform, discountId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.330",
3
+ "version": "0.2.331",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",