Skip to content

Commit

Permalink
promocode percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Feb 1, 2024
1 parent 32709e2 commit a39d878
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"'${workspaceFolder}/test/{,!(fixture)/**}/*.test.ts'",
"--exit",
"--timeout=600000",
"--grep=increase"
"--grep=Percentage"
],
"sourceMaps": true,
"internalConsoleOptions": "openOnSessionStart",
Expand Down
70 changes: 38 additions & 32 deletions adapters/promotion/default/configuredPromotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,40 +84,46 @@ class ConfiguredPromotion extends AbstractPromotion_1.default {
async applyPromotion(order) {
sails.log.debug(`Configured promotion to be applied. name: [${this.name}], id: [${this.id}]`);
// order.dishes
const orderDishes = await OrderDish.find({ order: order.id }).populate("dish");
const orderDishes = await OrderDish.find({ order: order.id, addedBy: "user" }).populate("dish");
let discountCost = new decimal_js_1.default(0);
for (const orderDish of orderDishes) {
if (typeof orderDish.dish === "string") {
throw new Error("Type error dish: applyPromotion");
}
let orderDishDiscountCost = 0;
if (!orderDish.dish) {
sails.log.error("orderDish", orderDish.id, "has no such dish");
continue;
}
if ((this.concept[0] === undefined || this.concept[0] === "") ?
false : !(0, stringsInArray_1.stringsInArray)(orderDish.dish.concept, this.concept)) {
continue;
}
let checkDishes = (0, stringsInArray_1.stringsInArray)(orderDish.dish.id, this.config.dishes);
let checkGroups = (0, stringsInArray_1.stringsInArray)(orderDish.dish.parentGroup, this.config.groups);
if (!checkDishes || !checkGroups)
continue;
if (this.configDiscount.discountType === "flat") {
orderDishDiscountCost = new decimal_js_1.default(this.configDiscount.discountAmount).mul(orderDish.amount).toNumber();
discountCost = new decimal_js_1.default(orderDishDiscountCost).add(discountCost);
// discountCost += new Decimal(orderDish.dish.price * orderDish.dish.amount).sub(this.configDiscount.discountAmount * orderDish.dish.amount ).toNumber();
}
if (this.configDiscount.discountType === "percentage") {
// let discountPrice:number = new Decimal(orderDish.dish.price).mul(orderDish.amount).mul(+this.configDiscount.discountAmount / 100).toNumber();
orderDishDiscountCost = new decimal_js_1.default(orderDish.dish.price)
.mul(orderDish.amount)
.mul(+this.configDiscount.discountAmount / 100)
.toNumber();
discountCost = new decimal_js_1.default(orderDishDiscountCost).add(discountCost);
if (!this.config.dishes.length && !this.config.groups.length) {
discountCost = new decimal_js_1.default(order.basketTotal)
.mul(+this.configDiscount.discountAmount / 100);
}
else {
for (const orderDish of orderDishes) {
if (typeof orderDish.dish === "string") {
throw new Error("Type error dish: applyPromotion");
}
let orderDishDiscountCost = 0;
if (!orderDish.dish) {
sails.log.error("orderDish", orderDish.id, "has no such dish");
continue;
}
if ((this.concept[0] === undefined || this.concept[0] === "") ?
false : !(0, stringsInArray_1.stringsInArray)(orderDish.dish.concept, this.concept)) {
continue;
}
let checkDishes = (0, stringsInArray_1.stringsInArray)(orderDish.dish.id, this.config.dishes);
let checkGroups = (0, stringsInArray_1.stringsInArray)(orderDish.dish.parentGroup, this.config.groups);
if (!checkDishes || !checkGroups)
continue;
if (this.configDiscount.discountType === "flat") {
orderDishDiscountCost = new decimal_js_1.default(this.configDiscount.discountAmount).mul(orderDish.amount).toNumber();
discountCost = new decimal_js_1.default(orderDishDiscountCost).add(discountCost);
// discountCost += new Decimal(orderDish.dish.price * orderDish.dish.amount).sub(this.configDiscount.discountAmount * orderDish.dish.amount ).toNumber();
}
if (this.configDiscount.discountType === "percentage") {
// let discountPrice:number = new Decimal(orderDish.dish.price).mul(orderDish.amount).mul(+this.configDiscount.discountAmount / 100).toNumber();
orderDishDiscountCost = new decimal_js_1.default(orderDish.dish.price)
.mul(orderDish.amount)
.mul(+this.configDiscount.discountAmount / 100)
.toNumber();
discountCost = new decimal_js_1.default(orderDishDiscountCost).add(discountCost);
}
let orderDishDiscount = new decimal_js_1.default(orderDish.discountTotal).add(orderDishDiscountCost).toNumber();
await OrderDish.update({ id: orderDish.id }, { discountTotal: orderDishDiscount, discountType: this.configDiscount.discountType }).fetch();
}
let orderDishDiscount = new decimal_js_1.default(orderDish.discountTotal).add(orderDishDiscountCost).toNumber();
await OrderDish.update({ id: orderDish.id }, { discountTotal: orderDishDiscount, discountType: this.configDiscount.discountType }).fetch();
}
// Update the order with new total
let orderDiscount = new decimal_js_1.default(order.discountTotal).add(discountCost.toNumber()).toNumber();
Expand Down
87 changes: 47 additions & 40 deletions adapters/promotion/default/configuredPromotion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,50 +114,57 @@ export default class ConfiguredPromotion extends AbstractPromotionHandler {
sails.log.debug(`Configured promotion to be applied. name: [${this.name}], id: [${this.id}]`)

// order.dishes
const orderDishes = await OrderDish.find({ order: order.id }).populate("dish");
const orderDishes = await OrderDish.find({ order: order.id , addedBy: "user"}).populate("dish");
let discountCost: Decimal = new Decimal(0);

for (const orderDish of orderDishes) {

if (typeof orderDish.dish === "string") {
throw new Error("Type error dish: applyPromotion")
}

let orderDishDiscountCost: number = 0;
if (!orderDish.dish) {
sails.log.error("orderDish", orderDish.id, "has no such dish");
continue;
}

if ((this.concept[0] === undefined || this.concept[0] === "") ?
false : !stringsInArray(orderDish.dish.concept, this.concept)) {
continue;
if(!this.config.dishes.length && !this.config.groups.length){
discountCost = new Decimal(order.basketTotal)
.mul(+this.configDiscount.discountAmount / 100);
} else {
for (const orderDish of orderDishes) {

if (typeof orderDish.dish === "string") {
throw new Error("Type error dish: applyPromotion")
}

let orderDishDiscountCost: number = 0;
if (!orderDish.dish) {
sails.log.error("orderDish", orderDish.id, "has no such dish");
continue;
}

if ((this.concept[0] === undefined || this.concept[0] === "") ?
false : !stringsInArray(orderDish.dish.concept, this.concept)) {
continue;
}

let checkDishes = stringsInArray(orderDish.dish.id, this.config.dishes)
let checkGroups = stringsInArray(orderDish.dish.parentGroup, this.config.groups)

if (!checkDishes || !checkGroups) continue

if (this.configDiscount.discountType === "flat") {
orderDishDiscountCost = new Decimal(this.configDiscount.discountAmount).mul(orderDish.amount).toNumber();
discountCost = new Decimal(orderDishDiscountCost).add(discountCost);
// discountCost += new Decimal(orderDish.dish.price * orderDish.dish.amount).sub(this.configDiscount.discountAmount * orderDish.dish.amount ).toNumber();
}

if (this.configDiscount.discountType === "percentage") {
// let discountPrice:number = new Decimal(orderDish.dish.price).mul(orderDish.amount).mul(+this.configDiscount.discountAmount / 100).toNumber();
orderDishDiscountCost = new Decimal(orderDish.dish.price)
.mul(orderDish.amount)
.mul(+this.configDiscount.discountAmount / 100)
.toNumber();
discountCost = new Decimal(orderDishDiscountCost).add(discountCost);
}

let orderDishDiscount: number = new Decimal(orderDish.discountTotal).add(orderDishDiscountCost).toNumber();
await OrderDish.update({ id: orderDish.id }, { discountTotal: orderDishDiscount, discountType: this.configDiscount.discountType }).fetch();

}

let checkDishes = stringsInArray(orderDish.dish.id, this.config.dishes)
let checkGroups = stringsInArray(orderDish.dish.parentGroup, this.config.groups)

if (!checkDishes || !checkGroups) continue

if (this.configDiscount.discountType === "flat") {
orderDishDiscountCost = new Decimal(this.configDiscount.discountAmount).mul(orderDish.amount).toNumber();
discountCost = new Decimal(orderDishDiscountCost).add(discountCost);
// discountCost += new Decimal(orderDish.dish.price * orderDish.dish.amount).sub(this.configDiscount.discountAmount * orderDish.dish.amount ).toNumber();
}

if (this.configDiscount.discountType === "percentage") {
// let discountPrice:number = new Decimal(orderDish.dish.price).mul(orderDish.amount).mul(+this.configDiscount.discountAmount / 100).toNumber();
orderDishDiscountCost = new Decimal(orderDish.dish.price)
.mul(orderDish.amount)
.mul(+this.configDiscount.discountAmount / 100)
.toNumber();
discountCost = new Decimal(orderDishDiscountCost).add(discountCost);
}

let orderDishDiscount: number = new Decimal(orderDish.discountTotal).add(orderDishDiscountCost).toNumber();
await OrderDish.update({ id: orderDish.id }, { discountTotal: orderDishDiscount, discountType: this.configDiscount.discountType }).fetch();

}


// Update the order with new total
let orderDiscount: number = new Decimal(order.discountTotal).add(discountCost.toNumber()).toNumber();
await Order.updateOne({ id: order.id }, { discountTotal: orderDiscount })
Expand Down
15 changes: 3 additions & 12 deletions adapters/promotion/default/promotionAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,12 @@ class PromotionAdapter extends AbstractPromotionAdapter_1.default {
* @returns
*/
recreateConfiguredPromotionHandler(promotionToAdd) {
if (promotionToAdd.enable === false && this.promotions[promotionToAdd.id]) {
if (this.promotions[promotionToAdd.id]) {
delete this.promotions[promotionToAdd.id];
return;
}
try {
if (!this.promotions[promotionToAdd.id]) {
this.promotions[promotionToAdd.id] = new configuredPromotion_1.default(promotionToAdd, promotionToAdd.configDiscount);
return;
}
return;
if (promotionToAdd.enable !== false) {
this.promotions[promotionToAdd.id] = new configuredPromotion_1.default(promotionToAdd, promotionToAdd.configDiscount);
}
catch (e) {
sails.log.error("recreateConfiguredPromotionHandler", e);
}
return;
}
getPromotionHandlerById(id) {
// let disc: AbstractDiscountHandler = await Discount.getById(id);
Expand Down
18 changes: 4 additions & 14 deletions adapters/promotion/default/promotionAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,13 @@ export class PromotionAdapter extends AbstractPromotionAdapter {
* @returns
*/
public recreateConfiguredPromotionHandler(promotionToAdd: Promotion): void {
if (promotionToAdd.enable === false && this.promotions[promotionToAdd.id]) {
if(this.promotions[promotionToAdd.id]){
delete this.promotions[promotionToAdd.id]
return
}

try {
if (!this.promotions[promotionToAdd.id]) {
this.promotions[promotionToAdd.id] = new ConfiguredPromotion(promotionToAdd, promotionToAdd.configDiscount);
return
}
return
} catch (e) {
sails.log.error("recreateConfiguredPromotionHandler", e)


if (promotionToAdd.enable !== false) {
this.promotions[promotionToAdd.id] = new ConfiguredPromotion(promotionToAdd, promotionToAdd.configDiscount);
}

return
}

public getPromotionHandlerById(id: string): AbstractPromotionHandler | undefined {
Expand Down
5 changes: 1 addition & 4 deletions test/integration/promotionCode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ describe("Promotion code integration test", function () {
// VALID PROMOCODE
await Order.applyPromotionCode({ id: order.id }, "TEST123");
let result = await Order.findOne({ id: order.id });
(0, chai_1.expect)(result.promotionCodeString).to.equal("TEST123");
(0, chai_1.expect)(result.discountTotal).to.equal(1.45);
(0, chai_1.expect)(result.total).to.equal(109.85);
(0, chai_1.expect)(result.basketTotal).to.equal(111.3);
(0, chai_1.expect)(result.discountTotal).to.equal(11.13);
});
});
5 changes: 1 addition & 4 deletions test/integration/promotionCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ describe("Promotion code integration test", function () {
// VALID PROMOCODE
await Order.applyPromotionCode({ id: order.id }, "TEST123");
let result = await Order.findOne({id: order.id})
expect(result.promotionCodeString).to.equal("TEST123");
expect(result.discountTotal).to.equal(1.45);
expect(result.total).to.equal(109.85);
expect(result.basketTotal).to.equal(111.3);
expect(result.discountTotal).to.equal(11.13);
})
});

0 comments on commit a39d878

Please sign in to comment.