diff --git a/.vscode/launch.json b/.vscode/launch.json index 0c86681e..101f118f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,7 @@ "'${workspaceFolder}/test/{,!(fixture)/**}/*.test.ts'", "--exit", "--timeout=600000", - "--grep=increase" + "--grep=Percentage" ], "sourceMaps": true, "internalConsoleOptions": "openOnSessionStart", diff --git a/adapters/promotion/default/configuredPromotion.js b/adapters/promotion/default/configuredPromotion.js index 25df541b..2c66cf30 100644 --- a/adapters/promotion/default/configuredPromotion.js +++ b/adapters/promotion/default/configuredPromotion.js @@ -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(); diff --git a/adapters/promotion/default/configuredPromotion.ts b/adapters/promotion/default/configuredPromotion.ts index 7fb3077c..e2a96135 100644 --- a/adapters/promotion/default/configuredPromotion.ts +++ b/adapters/promotion/default/configuredPromotion.ts @@ -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 }) diff --git a/adapters/promotion/default/promotionAdapter.js b/adapters/promotion/default/promotionAdapter.js index 05f4489e..2b34dfb3 100644 --- a/adapters/promotion/default/promotionAdapter.js +++ b/adapters/promotion/default/promotionAdapter.js @@ -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); diff --git a/adapters/promotion/default/promotionAdapter.ts b/adapters/promotion/default/promotionAdapter.ts index 3b67c429..e07548be 100644 --- a/adapters/promotion/default/promotionAdapter.ts +++ b/adapters/promotion/default/promotionAdapter.ts @@ -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 { diff --git a/test/integration/promotionCode.test.js b/test/integration/promotionCode.test.js index 9e7989b9..eed99a16 100644 --- a/test/integration/promotionCode.test.js +++ b/test/integration/promotionCode.test.js @@ -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); }); }); diff --git a/test/integration/promotionCode.test.ts b/test/integration/promotionCode.test.ts index 00d0c367..d36d616f 100644 --- a/test/integration/promotionCode.test.ts +++ b/test/integration/promotionCode.test.ts @@ -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); }) });