UNPKG

@eagleeye-solutions/integration-events-common

Version:
307 lines 14.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.getPosConnectWalletFulfilEventData = getPosConnectWalletFulfilEventData; const atomic_operations_1 = __importStar(require("./atomic-operations")); const wallet_account_transaction_entity_1 = require("./atomic-operations/wallet-account-transaction-entity"); function isInitialPosConnectWalletFulfil(event) { if (event.headers.eventName === 'POSCONNECT.WALLET.FULFIL') { if (event.request.body) { let reqBodyObj; if (typeof event.request.body === 'string') { reqBodyObj = JSON.parse(event.request.body); } else { reqBodyObj = event.request.body; } if (!reqBodyObj.basket) { return true; } } } return false; } function isFinalPosConnectWalletFulfil(event) { if (event.headers.eventName === 'POSCONNECT.WALLET.FULFIL') { if (event.request.body) { let reqBodyObj; if (typeof event.request.body === 'string') { reqBodyObj = JSON.parse(event.request.body); } else { reqBodyObj = event.request.body; } if (reqBodyObj.finalise) { return true; } } } return false; } function mergeTransactionAttributes(to, from) { if (from.storeLocation !== undefined) { to.storeLocation = from.storeLocation; } if (from.products !== undefined) { to.products = from.products; } if (from.totalBasketValueAfterDiscount !== undefined) { to.totalBasketValueAfterDiscount = from.totalBasketValueAfterDiscount; } if (from.discountsReceived !== undefined) { to.discountsReceived = from.discountsReceived; } if (from.totalPointsEarned !== undefined) { to.totalPointsEarned = from.totalPointsEarned; } if (from.transactionReference !== undefined) { to.transactionReference = from.transactionReference; } if (from.originalTransactionReference !== undefined) { to.originalTransactionReference = from.originalTransactionReference; } if (from.originalTransactionDate !== undefined) { to.originalTransactionDate = from.originalTransactionDate; } if (from.transactionDate !== undefined) { to.transactionDate = from.transactionDate; } } function getPosConnectWalletFulfilInitialEventData(event, opts) { let transactionAttributes = null; let pointsAttributes = null; let tierAttributes = null; const redeemedCoupons = []; for (const op of event.atomicOperations) { if ((0, atomic_operations_1.isWalletAccountTransactionEntityUpdatePoints)(op)) { try { pointsAttributes = (0, wallet_account_transaction_entity_1.getPointsAttributesFromWalletAccountTransactionEntity)(op); } catch { // Ignore if balance not available on this op. // By taking the latest POINTS balance observed in the operations list, // we ensure that we always have the most up-to-date balance regardless // of the its type. } } else if ((0, atomic_operations_1.isTierMembershipEntity)(op)) { tierAttributes = atomic_operations_1.default.TierMembershipEntity.getTierAttributes(op); } else if ((0, atomic_operations_1.isWalletAccountTransactionEntityUpdateRedeemEcoupon)(op)) { redeemedCoupons.push(atomic_operations_1.default.WalletAccountTransactionEntity.UpdateRedeemEcoupon.getCouponAttributes(op)); } else if ((0, atomic_operations_1.isWalletTransactionEntityUpdateSettleFulfilling)(op)) { transactionAttributes = atomic_operations_1.default.WalletTransactionEntity.UpdateSettleFulfilling.getTransactionAttributes(op); } } if (transactionAttributes === null) { throw new RangeError('WalletTransactionEntity/Update/Settle/Settled not found in POSCONNECT.WALLET.SETTLE'); } const posConnectWalletSettleEventData = { ...(pointsAttributes ? { points: pointsAttributes } : {}), ...(tierAttributes ? { tier: tierAttributes } : {}), ...(transactionAttributes ? { transaction: transactionAttributes } : {}), redeemedCoupons, currencyCode: opts.connectorConfig.configuration.currency, initial: true, final: false, }; return posConnectWalletSettleEventData; } function getPosConnectWalletFulfilMiddleEventData(event, opts) { const transactionAttributes = { products: [], }; let pointsAttributes = null; let tierAttributes = null; const redeemedCoupons = []; for (const op of event.atomicOperations) { if ((0, atomic_operations_1.isWalletAccountTransactionEntityUpdatePoints)(op)) { try { pointsAttributes = (0, wallet_account_transaction_entity_1.getPointsAttributesFromWalletAccountTransactionEntity)(op); } catch { // Ignore if balance not available on this op. // By taking the latest POINTS balance observed in the operations list, // we ensure that we always have the most up-to-date balance regardless // of the its type. } } else if ((0, atomic_operations_1.isTierMembershipEntity)(op)) { tierAttributes = atomic_operations_1.default.TierMembershipEntity.getTierAttributes(op); } else if ((0, atomic_operations_1.isWalletAccountTransactionEntityUpdateRedeemEcoupon)(op)) { redeemedCoupons.push(atomic_operations_1.default.WalletAccountTransactionEntity.UpdateRedeemEcoupon.getCouponAttributes(op)); } else if ((0, atomic_operations_1.isWalletTransactionEntityCreateFulfilFulfilled)(op)) { const fulfilledTransactionAttributes = atomic_operations_1.default.WalletTransactionEntity.CreateFulfilFulfilled.getTransactionAttributes(op); mergeTransactionAttributes(transactionAttributes, { storeLocation: fulfilledTransactionAttributes.storeLocation, products: fulfilledTransactionAttributes.products, totalBasketValueAfterDiscount: fulfilledTransactionAttributes.totalBasketValueAfterDiscount, transactionReference: fulfilledTransactionAttributes.transactionReference, transactionDate: fulfilledTransactionAttributes.transactionDate, }); } else if ((0, atomic_operations_1.isWalletTransactionEntityUpdateSettleFulfilling)(op)) { const completeTransactionAttributes = atomic_operations_1.default.WalletTransactionEntity.UpdateSettleFulfilling.getTransactionAttributes(op); mergeTransactionAttributes(transactionAttributes, { discountsReceived: completeTransactionAttributes.discountsReceived, totalPointsEarned: completeTransactionAttributes.totalPointsEarned, originalTransactionReference: completeTransactionAttributes.transactionReference, originalTransactionDate: completeTransactionAttributes.transactionDate, }); } else { opts.logger.warn(`unknown atomic operation: ${JSON.stringify({ op })}`); } } if (transactionAttributes === null) { throw new RangeError('WalletTransactionEntity/Update/Settle/Settled not found in POSCONNECT.WALLET.SETTLE'); } const posConnectWalletSettleEventData = { ...(pointsAttributes ? { points: pointsAttributes } : {}), ...(tierAttributes ? { tier: tierAttributes } : {}), ...(transactionAttributes ? { transaction: transactionAttributes } : {}), redeemedCoupons, currencyCode: opts.connectorConfig.configuration.currency, initial: false, final: false, }; return posConnectWalletSettleEventData; } function getPosConnectWalletFulfilFinalEventData(event, opts) { const transactionAttributes = { products: [], }; let pointsAttributes = null; let tierAttributes = null; const redeemedCoupons = []; for (const op of event.atomicOperations) { if ((0, atomic_operations_1.isWalletAccountTransactionEntityUpdatePoints)(op)) { try { pointsAttributes = (0, wallet_account_transaction_entity_1.getPointsAttributesFromWalletAccountTransactionEntity)(op); } catch { // Ignore if balance not available on this op. // By taking the latest POINTS balance observed in the operations list, // we ensure that we always have the most up-to-date balance regardless // of the its type. } } else if ((0, atomic_operations_1.isTierMembershipEntity)(op)) { tierAttributes = atomic_operations_1.default.TierMembershipEntity.getTierAttributes(op); } else if ((0, atomic_operations_1.isWalletAccountTransactionEntityUpdateRedeemEcoupon)(op)) { redeemedCoupons.push(atomic_operations_1.default.WalletAccountTransactionEntity.UpdateRedeemEcoupon.getCouponAttributes(op)); } else if ((0, atomic_operations_1.isWalletTransactionEntityUpdateSettleSettled)(op)) { // For the final FULFIL, we want to include the transaction reference // and timestamp from the atomic operation that updates the wallet transaction when // it is SETTLED, as this matches the reference used in initial / middle // FULFILs. const { transactionReference, transactionDate } = atomic_operations_1.default.WalletTransactionEntity.UpdateSettleSettled.getTransactionAttributes(op); mergeTransactionAttributes(transactionAttributes, { originalTransactionReference: transactionReference, originalTransactionDate: transactionDate, }); } else if ((0, atomic_operations_1.isWalletTransactionEntityCreateFulfilFulfilled)(op)) { const fulfilledTransactionAttributes = atomic_operations_1.default.WalletTransactionEntity.CreateFulfilFulfilled.getTransactionAttributes(op); mergeTransactionAttributes(transactionAttributes, { storeLocation: fulfilledTransactionAttributes.storeLocation, products: fulfilledTransactionAttributes.products, totalBasketValueAfterDiscount: fulfilledTransactionAttributes.totalBasketValueAfterDiscount, discountsReceived: fulfilledTransactionAttributes.discountsReceived, totalPointsEarned: fulfilledTransactionAttributes.totalPointsEarned, transactionReference: fulfilledTransactionAttributes.transactionReference, transactionDate: fulfilledTransactionAttributes.transactionDate, }); } } if (transactionAttributes === null) { throw new RangeError('WalletTransactionEntity/Create/Fulfil/Fulfilled not found in POSCONNECT.WALLET.SETTLE'); } const posConnectWalletSettleEventData = { ...(pointsAttributes ? { points: pointsAttributes } : {}), ...(tierAttributes ? { tier: tierAttributes } : {}), ...(transactionAttributes ? { transaction: transactionAttributes } : {}), redeemedCoupons, currencyCode: opts.connectorConfig.configuration.currency, initial: false, final: true, }; return posConnectWalletSettleEventData; } /** * Returns an array of events derived from a POSCONNECT.WALLET.FULFIL event. * * In some cases, multiple similar atomic operations may be included per event. * particularly when considering POSCONNECT.WALLET.FULFIL events that differ * depending on whether they are the first, intermediate or final FULFIL in * the sequence. * * The three functions that handle either first, intermediate or final FULFIL * events are configured to pick the relevant data (discount amounts, products, * etc) from the appropriate WalletTransactionEntity atomic operations. * * In general, a single WalletTransactionEntity is used to derive the content * of the TransactionAttributes that are returned in the output of this function, * however for intermediate FULFIL events the TransactionAttributes are derived * from multiple atomic operations and merged. * * @param event * @param configuration * @returns */ function getPosConnectWalletFulfilEventData(event, opts) { if (isInitialPosConnectWalletFulfil(event)) { return getPosConnectWalletFulfilInitialEventData(event, opts); } else if (isFinalPosConnectWalletFulfil(event)) { return getPosConnectWalletFulfilFinalEventData(event, opts); } else { return getPosConnectWalletFulfilMiddleEventData(event, opts); } } //# sourceMappingURL=posconnect-wallet-fulfil.js.map