Skip to content

Commit 8ceb2ed

Browse files
committed
exclucivity promo bug fixes
1 parent 7ae364d commit 8ceb2ed

1 file changed

Lines changed: 121 additions & 122 deletions

File tree

cartridges/int_zinrelo_sfra/cartridge/scripts/helpers/zinreloHelpers.js

Lines changed: 121 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -99,128 +99,6 @@ function isAlreadyRedeemed(couponCode) {
9999
return (couponRedemption.length > MAX_REDEMPTIONS_PER_COUPON);
100100
}
101101

102-
/**
103-
* Gets zinrelo rewards from provided or current basket
104-
* @param {dw.order.Basket} basket basket object
105-
* @returns {Array} list of zinrelo rewards from basket
106-
*/
107-
function getRewardsFromBasket(basket) {
108-
var rewardsInBasket = [];
109-
var currentBasket = basket || BasketMgr.getCurrentBasket();
110-
111-
if (!currentBasket) {
112-
return rewardsInBasket;
113-
}
114-
115-
collections.forEach(currentBasket.couponLineItems, function (couponLineItem) {
116-
if (couponLineItem.custom.isZinreloCoupon && couponLineItem.custom.zinreloRewardID && couponLineItem.applied) {
117-
rewardsInBasket.push(couponLineItem.custom.zinreloRewardID);
118-
}
119-
});
120-
121-
return rewardsInBasket;
122-
}
123-
124-
/**
125-
* sync the couponLineItems with zinrelo rewards
126-
*/
127-
function rewardSync() {
128-
var customer = request.session.customer;
129-
var profileReward = customer && customer.profile && customer.profile.getCustom().rewardInfo;
130-
131-
if (profileReward) {
132-
var profileRewardList = JSON.parse(profileReward);
133-
var rewardsInBasket = getRewardsFromBasket();
134-
135-
profileRewardList.forEach(function (reward) {
136-
if (rewardsInBasket.indexOf(reward.reward_id) < 0) {
137-
var rewardRedeemOptions = {
138-
transactionId: reward.transactionID,
139-
customer: request.session.customer.profile
140-
};
141-
zinreloLoyaltyServiceHelpers.rejectZinreloRewardTransaction(rewardRedeemOptions);
142-
removeCouponToCart(reward);
143-
}
144-
});
145-
}
146-
}
147-
148-
/**
149-
* Generates data for in cart redemption
150-
* @param {Object} customer current customer object
151-
* @returns {Object} in cart redemption data
152-
*/
153-
function getInCartRedemptionData(customer) {
154-
var inCartRedemptionData = {};
155-
156-
// sync the couponLineItems with zinrelo rewards
157-
rewardSync();
158-
159-
const isInCartRedemptionEnabled = zinreloPreferencesHelpers.isInCartRedemptionEnabled();
160-
inCartRedemptionData.isInCartRedemptionEnabled = isInCartRedemptionEnabled;
161-
162-
if (customer && customer.raw && customer.raw.profile && isInCartRedemptionEnabled) {
163-
// Get loyalty points for current customer from zinrelo
164-
var inCartRedemptionText = zinreloContentHelpers.getInCartRedemptionText();
165-
var memberData = zinreloLoyaltyServiceHelpers.getMemberData(customer.raw.profile);
166-
var availablePoints = (memberData && memberData.availablePoints) || 0;
167-
inCartRedemptionText = inCartRedemptionText.replace(/{{AVAILABLE_POINTS}}/g, availablePoints);
168-
169-
// Get available rewards for current customer from zinrelo
170-
var zinreloRewards = zinreloLoyaltyServiceHelpers.getRewards(customer.raw.profile);
171-
inCartRedemptionData.zinreloRewards = zinreloRewards;
172-
inCartRedemptionData.inCartDropdownText = zinreloContentHelpers.getInCartDropdownText();
173-
inCartRedemptionData.inCartRedemptionText = inCartRedemptionText;
174-
175-
// Get pending transactions for current customer from zinrelo
176-
var transactionStatusList = [ZINRELO_REWARD_PENDING_STATUS];
177-
var pendingTransactions = zinreloLoyaltyServiceHelpers.getMemberTransactions(customer.raw.profile, transactionStatusList);
178-
var pendingRewards = getPendingRewards(pendingTransactions);
179-
inCartRedemptionData.pendingRewards = pendingRewards;
180-
181-
// Buttons text
182-
inCartRedemptionData.inCartRedeemButtonText = zinreloContentHelpers.getInCartRedeemButtonText();
183-
inCartRedemptionData.inCartCancelButtonText = zinreloContentHelpers.getInCartCancelButtonText();
184-
185-
inCartRedemptionData.showInCartSection = !!((zinreloRewards && zinreloRewards.length > 0) || (pendingRewards && pendingRewards.length > 0));
186-
}
187-
188-
return inCartRedemptionData;
189-
}
190-
191-
/**
192-
* Adds user in Zinrelo rewards customer group by adding reward id to session attribute
193-
* @param {string} rewardID reward id
194-
*/
195-
function addInZinreloCustomerGroup(rewardID) {
196-
// Get rewards from session
197-
var zinreloRewards = getApplicableZinreloRewards();
198-
199-
// Add the current rewards
200-
zinreloRewards.push(rewardID);
201-
202-
// Set the updated list back to session
203-
setApplicableZinreloRewards(zinreloRewards);
204-
}
205-
206-
/**
207-
* Removes user from Zinrelo rewards customer group by removing reward id to session attribute
208-
* @param {string} rewardID reward id
209-
*/
210-
function removeFromZinreloCustomerGroup(rewardID) {
211-
// Get rewards from session
212-
var zinreloRewards = getApplicableZinreloRewards();
213-
214-
// Remove the current rewards
215-
var currentRewardIndex = zinreloRewards.indexOf(rewardID);
216-
if (currentRewardIndex > -1) {
217-
zinreloRewards.splice(currentRewardIndex, 1);
218-
}
219-
220-
// Set the updated list back to session
221-
setApplicableZinreloRewards(zinreloRewards);
222-
}
223-
224102
/**
225103
* sets the reward from profile
226104
* @param {Object} rewardInfo reward details
@@ -276,6 +154,38 @@ function removeRewardsFromProfile(rewardInfo) {
276154
}
277155
}
278156

157+
/**
158+
* Adds user in Zinrelo rewards customer group by adding reward id to session attribute
159+
* @param {string} rewardID reward id
160+
*/
161+
function addInZinreloCustomerGroup(rewardID) {
162+
// Get rewards from session
163+
var zinreloRewards = getApplicableZinreloRewards();
164+
165+
// Add the current rewards
166+
zinreloRewards.push(rewardID);
167+
168+
// Set the updated list back to session
169+
setApplicableZinreloRewards(zinreloRewards);
170+
}
171+
172+
/**
173+
* Removes user from Zinrelo rewards customer group by removing reward id to session attribute
174+
* @param {string} rewardID reward id
175+
*/
176+
function removeFromZinreloCustomerGroup(rewardID) {
177+
// Get rewards from session
178+
var zinreloRewards = getApplicableZinreloRewards();
179+
180+
// Remove the current rewards
181+
var currentRewardIndex = zinreloRewards.indexOf(rewardID);
182+
if (currentRewardIndex > -1) {
183+
zinreloRewards.splice(currentRewardIndex, 1);
184+
}
185+
186+
// Set the updated list back to session
187+
setApplicableZinreloRewards(zinreloRewards);
188+
}
279189

280190
/**
281191
* Removes coupon code from basket
@@ -313,6 +223,95 @@ function removeCouponToCart(rewardInfo) {
313223
return result;
314224
}
315225

226+
/**
227+
* Gets zinrelo rewards from provided or current basket
228+
* @param {dw.order.Basket} basket basket object
229+
* @returns {Array} list of zinrelo rewards from basket
230+
*/
231+
function getRewardsFromBasket(basket) {
232+
var rewardsInBasket = [];
233+
var currentBasket = basket || BasketMgr.getCurrentBasket();
234+
235+
if (!currentBasket) {
236+
return rewardsInBasket;
237+
}
238+
239+
collections.forEach(currentBasket.couponLineItems, function (couponLineItem) {
240+
if (couponLineItem.custom.isZinreloCoupon && couponLineItem.custom.zinreloRewardID && couponLineItem.applied) {
241+
rewardsInBasket.push(couponLineItem.custom.zinreloRewardID);
242+
}
243+
});
244+
245+
return rewardsInBasket;
246+
}
247+
248+
/**
249+
* sync the couponLineItems with zinrelo rewards
250+
*/
251+
function rewardSync() {
252+
var customer = request.session.customer;
253+
var profileReward = customer && customer.profile && customer.profile.getCustom().rewardInfo;
254+
255+
if (profileReward) {
256+
var profileRewardList = JSON.parse(profileReward);
257+
var rewardsInBasket = getRewardsFromBasket();
258+
259+
profileRewardList.forEach(function (reward) {
260+
if (rewardsInBasket.indexOf(reward.reward_id) < 0) {
261+
var rewardRedeemOptions = {
262+
transactionId: reward.transactionID,
263+
customer: request.session.customer.profile
264+
};
265+
zinreloLoyaltyServiceHelpers.rejectZinreloRewardTransaction(rewardRedeemOptions);
266+
removeCouponToCart(reward);
267+
}
268+
});
269+
}
270+
}
271+
272+
/**
273+
* Generates data for in cart redemption
274+
* @param {Object} customer current customer object
275+
* @returns {Object} in cart redemption data
276+
*/
277+
function getInCartRedemptionData(customer) {
278+
var inCartRedemptionData = {};
279+
280+
// sync the couponLineItems with zinrelo rewards
281+
rewardSync();
282+
283+
const isInCartRedemptionEnabled = zinreloPreferencesHelpers.isInCartRedemptionEnabled();
284+
inCartRedemptionData.isInCartRedemptionEnabled = isInCartRedemptionEnabled;
285+
286+
if (customer && customer.raw && customer.raw.profile && isInCartRedemptionEnabled) {
287+
// Get loyalty points for current customer from zinrelo
288+
var inCartRedemptionText = zinreloContentHelpers.getInCartRedemptionText();
289+
var memberData = zinreloLoyaltyServiceHelpers.getMemberData(customer.raw.profile);
290+
var availablePoints = (memberData && memberData.availablePoints) || 0;
291+
inCartRedemptionText = inCartRedemptionText.replace(/{{AVAILABLE_POINTS}}/g, availablePoints);
292+
293+
// Get available rewards for current customer from zinrelo
294+
var zinreloRewards = zinreloLoyaltyServiceHelpers.getRewards(customer.raw.profile);
295+
inCartRedemptionData.zinreloRewards = zinreloRewards;
296+
inCartRedemptionData.inCartDropdownText = zinreloContentHelpers.getInCartDropdownText();
297+
inCartRedemptionData.inCartRedemptionText = inCartRedemptionText;
298+
299+
// Get pending transactions for current customer from zinrelo
300+
var transactionStatusList = [ZINRELO_REWARD_PENDING_STATUS];
301+
var pendingTransactions = zinreloLoyaltyServiceHelpers.getMemberTransactions(customer.raw.profile, transactionStatusList);
302+
var pendingRewards = getPendingRewards(pendingTransactions);
303+
inCartRedemptionData.pendingRewards = pendingRewards;
304+
305+
// Buttons text
306+
inCartRedemptionData.inCartRedeemButtonText = zinreloContentHelpers.getInCartRedeemButtonText();
307+
inCartRedemptionData.inCartCancelButtonText = zinreloContentHelpers.getInCartCancelButtonText();
308+
309+
inCartRedemptionData.showInCartSection = !!((zinreloRewards && zinreloRewards.length > 0) || (pendingRewards && pendingRewards.length > 0));
310+
}
311+
312+
return inCartRedemptionData;
313+
}
314+
316315
/**
317316
* Applies coupon code to basket
318317
* @param {Object} rewardInfo reward info object

0 commit comments

Comments
 (0)