@eagleeye-solutions/integration-events-common
Version:
Eagle Eye CDP connector common functionality
122 lines • 5.78 kB
JavaScript
;
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.getPosConnectWalletRefundEventData = getPosConnectWalletRefundEventData;
const wallet_account_transaction_entity_1 = require("./atomic-operations/wallet-account-transaction-entity");
const atomic_operations_1 = __importStar(require("./atomic-operations"));
/**
* Returns an array of events derived from a POSCONNECT.WALLET.REFUND event.
*
* Note that a refund may either be a full refund (i.e. all products in the original
* order are refunded) or a partial refund, where only a subset of the original
* products are refunded.
*
* Handling of Points accounts
*
* For a full refund, we expect a walletAccountTransactionEntity/UPDATE/POINTS
* with event = REFUND_DEBIT to be the only operation on the points account, so
* this is used to derive the new points balances.
*
* For a partial refund, we expect the same walletAccountTransactionEntity/UPDATE/POINTS
* with event = REFUND_DEBIT that deducts the original total number of points
* awarded, followed by a walletAccountTransactionEntity/UPDATE/POINTS with event = EARN
* that awards points for the non-refunded items in the original order.
*
* @param event
* @param configuration
* @returns
*/
function getPosConnectWalletRefundEventData(event, opts) {
let transactionAttributes = null;
let pointsAttributes = null;
let tierAttributes = null;
const redeemedCoupons = [];
const unredeemedCoupons = [];
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.isWalletAccountTransactionEntityUpdateUnredeemEcoupon)(op)) {
unredeemedCoupons.push(atomic_operations_1.default.WalletAccountTransactionEntity.UpdateUnredeemEcoupon.getCouponAttributes(op));
}
else if ((0, atomic_operations_1.isWalletTransactionEntityCreateRefundSettled)(op)) {
transactionAttributes =
atomic_operations_1.default.WalletTransactionEntity.CreateRefundSettled.getTransactionAttributes(op);
}
}
/* An attempt to get overall discount amount not present after a refund.
* This can be discounted from the total amount to get the proper amount
* being refunded (totalUnitCostAfterDiscount does not include cart discounts).
*/
const previousDiscounts = unredeemedCoupons.reduce((acc, coupon) => {
return acc + coupon.value;
}, 0) -
redeemedCoupons.reduce((acc, coupon) => {
return acc + coupon.value;
}, 0);
if (transactionAttributes === null) {
// TODO: more useful error message / context
throw new RangeError('WalletTransactionEntity/Update/Settle/Settled not found in POSCONNECT.WALLET.SETTLE');
}
if (transactionAttributes.totalBasketValueAfterDiscount !== undefined) {
transactionAttributes.totalBasketValueAfterDiscount -= previousDiscounts;
}
const posConnectWalletRefundEventData = {
...(pointsAttributes ? { points: pointsAttributes } : {}),
...(tierAttributes ? { tier: tierAttributes } : {}),
...(transactionAttributes ? { transaction: transactionAttributes } : {}),
redeemedCoupons,
unredeemedCoupons,
currencyCode: opts.connectorConfig.configuration.currency,
};
return posConnectWalletRefundEventData;
}
//# sourceMappingURL=posconnect-wallet-refund.js.map