Skip to content

Commit

Permalink
Order
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Jan 30, 2024
1 parent ea02be1 commit bab9dd5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
5 changes: 4 additions & 1 deletion adapters/promotion/default/promotionAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ class PromotionAdapter extends AbstractPromotionAdapter_1.default {
const order = target;
if (order.promotionCode) {
const promotionCode = order.promotionCode;
if (promotionCode.promotion.length) {
if (Array.isArray(promotionCode.promotion)) {
promotionCode.promotion.forEach((p) => {
p.sortOrder = -Infinity;
filteredByCondition.push(p);
});
}
else {
sails.log.debug(`PromotionAdapter: promotionCode.promotion should be array: \n${JSON.stringify(promotionCode, null, 2)}`);
}
}
}
// return first isJoint = false
Expand Down
4 changes: 3 additions & 1 deletion adapters/promotion/default/promotionAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ export class PromotionAdapter extends AbstractPromotionAdapter {
const order = target as Order;
if (order.promotionCode) {
const promotionCode = order.promotionCode as PromotionCode
if (promotionCode.promotion.length) {
if (Array.isArray(promotionCode.promotion)) {
(promotionCode.promotion as Promotion[]).forEach((p) => {
p.sortOrder = -Infinity;
filteredByCondition.push(p)
});
} else {
sails.log.debug(`PromotionAdapter: promotionCode.promotion should be array: \n${JSON.stringify(promotionCode, null, 2)}`)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion models/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,15 +1119,20 @@ let Model = {
order.promotionCode = await PromotionCode.findOne({ id: order.promotionCode }).populate('promotion');
}
else {
sails.log.debug(`Count: promocode [${order.promotionCodeString}] expired, order [${order.id}].`);
order.promotionCode = null;
order.promotionCodeString = null;
order.promotionCodeDescription = "Promocode expired";
order.promotionCodeCheckValidTill = null;
}
}
catch (error) {
sails.log.error(`PromotionAdapter > Problem with parse Date`);
}
}
else {
order.promotionCode = null;
order.promotionCodeCheckValidTill = null;
}
let orderPopulate = { ...order };
orderPopulate.dishes = orderDishesForPopulate;
/**
Expand All @@ -1143,6 +1148,7 @@ let Model = {
orderPopulate.discountTotal = orederPROM.discountTotal;
order = orderPopulate;
let promotionOrderToSave = {
promotionCodeDescription: order.promotionCodeDescription,
promotionState: order.promotionState,
promotionUnorderable: order.promotionUnorderable,
discountTotal: order.discountTotal,
Expand Down
9 changes: 7 additions & 2 deletions models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1354,19 +1354,23 @@ let Model = {

// If promocode is valid and allowed
if (order.promotionCode !== null && order.promotionCodeCheckValidTill !== null) {
const currentDate = new Date();
const currentDate = new Date();
try {
const promotionEndDate = new Date(order.promotionCodeCheckValidTill);
if (promotionEndDate > currentDate) {
order.promotionCode = await PromotionCode.findOne({ id: order.promotionCode as string }).populate('promotion');
} else {
sails.log.debug(`Count: promocode [${order.promotionCodeString}] expired, order [${order.id}].`)
order.promotionCode = null
order.promotionCodeString = null;
order.promotionCodeDescription = "Promocode expired"
order.promotionCodeCheckValidTill = null
}
} catch (error) {
sails.log.error(`PromotionAdapter > Problem with parse Date`)
}
} else {
order.promotionCode = null
order.promotionCodeCheckValidTill = null
}


Expand All @@ -1390,6 +1394,7 @@ let Model = {


let promotionOrderToSave = {
promotionCodeDescription: order.promotionCodeDescription,
promotionState: order.promotionState,
promotionUnorderable: order.promotionUnorderable,
discountTotal: order.discountTotal,
Expand Down

0 comments on commit bab9dd5

Please sign in to comment.