diff --git a/.vscode/launch.json b/.vscode/launch.json index 8d8112fb..28a008e8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,7 @@ "'${workspaceFolder}/test/{,!(fixture)/**}/*.test.ts'", "--exit", "--timeout=600000", - "--grep=PromotionCodes" + "--grep=prepend" ], "sourceMaps": true, "internalConsoleOptions": "openOnSessionStart", diff --git a/models/Order.d.ts b/models/Order.d.ts index 3ef16fff..99df3aa1 100644 --- a/models/Order.d.ts +++ b/models/Order.d.ts @@ -33,7 +33,7 @@ declare let attributes: { /** * @deprecated will be rename to `Items` in **v2** */ - dishes: OrderDish[] | number[]; + dishes: number[] | OrderDish[]; paymentMethod: any; /** */ paymentMethodTitle: string; @@ -161,7 +161,7 @@ declare let Model: { beforeCreate(orderInit: Order, cb: (err?: string) => void): void; afterCreate(order: Order, cb: (err?: string) => void): Promise; /** Add dish into order */ - addDish(criteria: CriteriaQuery, dish: Dish | string, amount: number, modifiers: OrderModifier[], comment: string, addedBy: string, replace?: boolean, orderDishId?: number): Promise; + addDish(criteria: CriteriaQuery, dish: string | Dish, amount: number, modifiers: OrderModifier[], comment: string, addedBy: "user" | "promotion" | "core" | "custom", replace?: boolean, orderDishId?: number): Promise; removeDish(criteria: CriteriaQuery, dish: OrderDish, amount: number, stack?: boolean): Promise; setCount(criteria: CriteriaQuery, dish: OrderDish, amount: number): Promise; setComment(criteria: CriteriaQuery, dish: OrderDish, comment: string): Promise; @@ -206,7 +206,7 @@ declare let Model: { state?: string; concept?: string; isMixedConcept?: boolean; - dishes?: OrderDish[] | number[]; + dishes?: number[] | OrderDish[]; paymentMethod?: any; paymentMethodTitle?: string; paid?: boolean; @@ -266,7 +266,7 @@ declare let Model: { */ countCart(criteria: CriteriaQuery, isPromoting?: boolean): Promise; doPaid(criteria: CriteriaQuery, paymentDocument: PaymentDocument): Promise; - applyPromotionCode(criteria: CriteriaQuery, promotionCodeString: string | null): Promise; + applyPromotionCode(criteria: CriteriaQuery, promotionCodeString: string): Promise; }; declare global { const Order: typeof Model & ORMModel & StateFlowModel; diff --git a/models/Order.js b/models/Order.js index d4998d4f..f171df44 100644 --- a/models/Order.js +++ b/models/Order.js @@ -257,7 +257,14 @@ let Model = { cb(); }, /** Add dish into order */ - async addDish(criteria, dish, amount, modifiers, comment, addedBy, replace, orderDishId) { + async addDish(criteria, dish, amount, modifiers, comment, + /** + * user - added manualy by human + * promotion - cleaned in each calculate promotions + * core - is reserved for + * custom - custom integration can process it + */ + addedBy, replace, orderDishId) { await emitter.emit.apply(emitter, ["core-order-before-add-dish", ...arguments]); // TODO: when user add some dish to PAYMENT || ORDER cart state, need just make new cart clone let dishObj; @@ -494,8 +501,10 @@ let Model = { const originalOrderDishes = originalOrder.dishes; // Iterate through the original order dishes and add them to the new order for (const originalOrderDish of originalOrderDishes) { + if (originalOrderDish.addedBy !== "user") + continue; // Assuming you have an addDish method that takes an order ID and a dish object as parameters - await Order.addDish({ id: newOrder.id }, originalOrderDish.dish, originalOrderDish.amount, originalOrderDish.modifiers, null, "order-clone"); + await Order.addDish({ id: newOrder.id }, originalOrderDish.dish, originalOrderDish.amount, originalOrderDish.modifiers, null, "user"); } return newOrder; }, @@ -1161,9 +1170,11 @@ let Model = { catch (error) { sails.log.error(`Core > order > promotion calculate fail: `, error); } - // finaly - order.isPromoting = false; - await Order.update({ id: order.id }, { isPromoting: false }).fetch(); + finally { + // finaly + order.isPromoting = false; + await Order.update({ id: order.id }, { isPromoting: false }).fetch(); + } emitter.emit("core-order-after-promotion", order); } // Force unpopulate promotionCode, TODO: debug it why is not unpopulated here?! diff --git a/models/Order.ts b/models/Order.ts index db9150ce..9c906ffc 100644 --- a/models/Order.ts +++ b/models/Order.ts @@ -345,7 +345,13 @@ let Model = { amount: number, modifiers: OrderModifier[], comment: string, - addedBy: string, + /** + * user - added manualy by human + * promotion - cleaned in each calculate promotions + * core - is reserved for + * custom - custom integration can process it + */ + addedBy: "user" | "promotion" | "core" | "custom", replace?: boolean, orderDishId?: number ): Promise { @@ -626,9 +632,9 @@ let Model = { // Iterate through the original order dishes and add them to the new order for (const originalOrderDish of originalOrderDishes) { - + if(originalOrderDish.addedBy !== "user") continue; // Assuming you have an addDish method that takes an order ID and a dish object as parameters - await Order.addDish({ id: newOrder.id }, originalOrderDish.dish, originalOrderDish.amount, originalOrderDish.modifiers, null, "order-clone"); + await Order.addDish({ id: newOrder.id }, originalOrderDish.dish, originalOrderDish.amount, originalOrderDish.modifiers, null, "user"); } return newOrder; @@ -1178,9 +1184,8 @@ let Model = { } order.isPromoting = isPromoting; - emitter.emit("core-order-before-count", order); - + /** * // TODO: If countCart from payment or other changes from payment it should cancel all payment request */ @@ -1407,11 +1412,12 @@ let Model = { await Order.update({ id: order.id }, promotionOrderToSave).fetch(); } catch (error) { sails.log.error(`Core > order > promotion calculate fail: `, error) + } finally { + // finaly + order.isPromoting = false; + await Order.update({ id: order.id }, { isPromoting: false }).fetch(); } - // finaly - order.isPromoting = false; - await Order.update({ id: order.id }, { isPromoting: false }).fetch(); emitter.emit("core-order-after-promotion", order); } diff --git a/models/User.d.ts b/models/User.d.ts index 804a1fc0..3103259b 100644 --- a/models/User.d.ts +++ b/models/User.d.ts @@ -88,7 +88,7 @@ declare let Model: { * @param {WaterlineCriteria} criteria * @returns String */ - getPhoneString(phone: Phone, target?: "login" | "print" | "string"): Promise; + getPhoneString(phone: Phone, target?: "string" | "login" | "print"): Promise; /** * Update user password * diff --git a/models/UserBonusProgram.d.ts b/models/UserBonusProgram.d.ts index af2d3871..08fc4ddf 100644 --- a/models/UserBonusProgram.d.ts +++ b/models/UserBonusProgram.d.ts @@ -26,13 +26,13 @@ interface UserBonusProgram extends attributes, ORM { export default UserBonusProgram; declare let Model: { beforeCreate(init: UserBonusProgram, cb: (err?: string) => void): void; - registration(user: User | string, adapterOrId: string): Promise; - delete(user: User | string, adapterOrId: string): Promise; - syncAll(user: User | string): Promise; + registration(user: string | User, adapterOrId: string): Promise; + delete(user: string | User, adapterOrId: string): Promise; + syncAll(user: string | User): Promise; /** Full sync all transaction with external system */ - sync(user: User | string, bonusProgram: BonusProgram | string, force?: boolean): Promise; - checkEnoughToSpend(user: User | string, bonusProgram: BonusProgram | string, amount: number): Promise; - sumCurrentBalance(user: User | string, bonusProgram: BonusProgram | string): Promise; + sync(user: string | User, bonusProgram: string | BonusProgram, force?: boolean): Promise; + checkEnoughToSpend(user: string | User, bonusProgram: string | BonusProgram, amount: number): Promise; + sumCurrentBalance(user: string | User, bonusProgram: string | BonusProgram): Promise; }; declare global { const UserBonusProgram: typeof Model & ORMModel; diff --git a/test/integration/adapters/promotion.test.js b/test/integration/adapters/promotion.test.js index 30ab510d..2ff3b710 100644 --- a/test/integration/adapters/promotion.test.js +++ b/test/integration/adapters/promotion.test.js @@ -302,7 +302,7 @@ describe("Promotion adapter integration test", function () { let dish1 = await Dish.createOrUpdate((0, dish_generator_1.default)({ name: "test dish", price: 10.1, concept: "recursion", parentGroup: groupsId[0] })); let dish2 = await Dish.createOrUpdate((0, dish_generator_1.default)({ name: "test fish", price: 15.2, concept: "recursion", parentGroup: groupsId[0] })); let discountEx1 = { - id: "1aw42-idaaa", + id: "discountEx1-idaaa", badge: 'test', configDiscount: { discountType: "flat", @@ -312,7 +312,7 @@ describe("Promotion adapter integration test", function () { excludeModifiers: true }, name: "1124-name", - description: "sawdad", + description: "discountEx1 flat amount: 1 for 2 dishes", concept: ["recursion"], condition: (arg) => { if ((0, findModelInstance_1.default)(arg) === "Order" && (0, stringsInArray_1.stringsInArray)(arg.concept, discountEx1.concept)) { @@ -331,7 +331,7 @@ describe("Promotion adapter integration test", function () { action: async (order) => { let dish1 = await Dish.createOrUpdate((0, dish_generator_1.default)({ name: "test fish", price: 15.2, concept: "recursion", parentGroup: groupsId[0] })); discountEx1.configDiscount.dishes.push(dish1.id); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "promotion"); let configPromotion = new configuredPromotion_1.default(discountEx1, discountEx1.configDiscount); order.promotionFlatDiscount = 2; return await configPromotion.applyPromotion(order); diff --git a/test/integration/adapters/promotion.test.ts b/test/integration/adapters/promotion.test.ts index a7706240..32a26996 100644 --- a/test/integration/adapters/promotion.test.ts +++ b/test/integration/adapters/promotion.test.ts @@ -1,7 +1,7 @@ /// import path = require("path"); import { TestRMS } from "../../mocks/adapter/RMS"; -import { Adapter } from "../../../adapters"; +import { Adapter } from "../../../adapters"; import { expect } from "chai"; import { address, customer } from "../../mocks/customer"; import AbstractPromotionAdapter from "../../../adapters/promotion/AbstractPromotionAdapter"; @@ -25,16 +25,16 @@ import { PromotionAdapter } from "../../../adapters/promotion/default/promotionA describe("Promotion adapter integration test", function () { this.timeout(60000) let promotionAdapter: PromotionAdapter; - - before(async function() { - promotionAdapter = Adapter.getPromotionAdapter() + + before(async function () { + promotionAdapter = Adapter.getPromotionAdapter() }); - after(async function() { + after(async function () { // await Promotion.destroy({}) }) - + it("Configured discount total: 10% for all group", async () => { // // If item is added, then see that it stood in line. // // Pass the test response of Messaja @@ -43,20 +43,20 @@ describe("Promotion adapter integration test", function () { // var dishes = await Dish.find({}) - - let order = await Order.create({id: "configured-promotion-integration-testa"}).fetch(); - await Order.updateOne({id: order.id}, {concept: "road",user: "user"}); + + let order = await Order.create({ id: "configured-promotion-integration-testa" }).fetch(); + await Order.updateOne({ id: order.id }, { concept: "road", user: "user" }); const groups = await Group.find({}) const groupsId = groups.map(group => group.id) - - let dish1 = await Dish.createOrUpdate(dishGenerator({name: "test dish", price: 10.1, concept: "road",parentGroup:groupsId[0]})); - let dish2 = await Dish.createOrUpdate(dishGenerator({name: "test fish", price: 15.2, concept: "road", parentGroup:groupsId[0]})); - + + let dish1 = await Dish.createOrUpdate(dishGenerator({ name: "test dish", price: 10.1, concept: "road", parentGroup: groupsId[0] })); + let dish2 = await Dish.createOrUpdate(dishGenerator({ name: "test fish", price: 15.2, concept: "road", parentGroup: groupsId[0] })); + let dishes = await Dish.find({}) const dishesId = dishes.map(dish => dish.id) - let config:IconfigDiscount = { + let config: IconfigDiscount = { discountType: "percentage", discountAmount: 10, dishes: dishesId, @@ -74,39 +74,39 @@ describe("Promotion adapter integration test", function () { configDiscount: config, description: "aaa", externalId: "externalID" - }, - config) + }, + config) await promotionAdapter.addPromotionHandler(promotion10Percent) - - await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); - + + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); + // console.log(await OrderDish.find({order: order.id})) - let result = await Order.findOne(order.id) + let result = await Order.findOne(order.id) // console.log(result, result.promotionState[0]) expect(result.discountTotal).to.equal(11.13); }); - - - it("IsJoint: false configured discount over total discount for specific dish", async ()=>{ + + + it("IsJoint: false configured discount over total discount for specific dish", async () => { // check specific group and dish for joint:false - - let order = await Order.create({id: "configured-promotion-integration-test-joint-false"}).fetch(); - await Order.updateOne({id: order.id}, {concept: "jointfalse",user: "user"}); + + let order = await Order.create({ id: "configured-promotion-integration-test-joint-false" }).fetch(); + await Order.updateOne({ id: order.id }, { concept: "jointfalse", user: "user" }); const groups = await Group.find({}) const groupsId = groups.map(group => group.id) - let dish1 = await Dish.createOrUpdate(dishGenerator({name: "test dish", price: 10.1, concept: "jointfalse",parentGroup:groupsId[0]})); - let dish2 = await Dish.createOrUpdate(dishGenerator({name: "test fish", price: 15.2, concept: "jointfalse", parentGroup:groupsId[0]})); - + let dish1 = await Dish.createOrUpdate(dishGenerator({ name: "test dish", price: 10.1, concept: "jointfalse", parentGroup: groupsId[0] })); + let dish2 = await Dish.createOrUpdate(dishGenerator({ name: "test fish", price: 15.2, concept: "jointfalse", parentGroup: groupsId[0] })); + let dishes = await Dish.find({}) const dishesId = dishes.map(dish => dish.id) - let config:IconfigDiscount = { + let config: IconfigDiscount = { discountType: "percentage", discountAmount: 10, dishes: dishesId, @@ -121,13 +121,13 @@ describe("Promotion adapter integration test", function () { name: 'awdawd', isPublic: true, badge: 'test', - configDiscount: config, + configDiscount: config, description: "aaa", externalId: "externalID2" - }, - config) + }, + config) - let promotion1flat:AbstractPromotionHandler = discountGenerator({ + let promotion1flat: AbstractPromotionHandler = discountGenerator({ concept: ["jointfalse"], id: 'aa22-id', badge: 'test', @@ -146,44 +146,44 @@ describe("Promotion adapter integration test", function () { await promotionAdapter.addPromotionHandler(promotion10) await promotionAdapter.addPromotionHandler(promotion1flat) - await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); - - let result = await Order.findOne(order.id) + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); + + let result = await Order.findOne(order.id) expect(result.discountTotal).to.equal(11.13); }) - - it("configured discount for specific dish/group, over total discount with sortOrder", async ()=>{ - + + it("configured discount for specific dish/group, over total discount with sortOrder", async () => { + const groups = await Group.find({}) const groupsId = groups.map(group => group.id) - let order = await Order.create({id: "configured-promotion-integration-test-diff"}).fetch(); - await Order.updateOne({id: order.id}, {concept: "amongus",user: "user"}); + let order = await Order.create({ id: "configured-promotion-integration-test-diff" }).fetch(); + await Order.updateOne({ id: order.id }, { concept: "amongus", user: "user" }); + + let dish1 = await Dish.createOrUpdate(dishGenerator({ name: "test dish", price: 10.1, concept: "amongus", parentGroup: groupsId[0] })); + let dish2 = await Dish.createOrUpdate(dishGenerator({ name: "test fish", price: 15.2, concept: "amongus", parentGroup: groupsId[1] })); - let dish1 = await Dish.createOrUpdate(dishGenerator({name: "test dish", price: 10.1, concept: "amongus", parentGroup:groupsId[0] })); - let dish2 = await Dish.createOrUpdate(dishGenerator({name: "test fish", price: 15.2, concept: "amongus", parentGroup:groupsId[1]})); - let dishes = await Dish.find({}) const dishesId = dishes.map(dish => dish.id) - let config:IconfigDiscount = { + let config: IconfigDiscount = { discountType: "percentage", discountAmount: 10, dishes: dishesId, groups: groupsId, excludeModifiers: true } - let config2:IconfigDiscount = { + let config2: IconfigDiscount = { discountType: "flat", discountAmount: 1, dishes: [dish1.id], groups: groupsId, excludeModifiers: true } - + let createInModelPromotion: Promotion = { id: 'config2addaw-idawdawd', isJoint: false, @@ -191,7 +191,7 @@ describe("Promotion adapter integration test", function () { badge: 'test', isPublic: true, isDeleted: false, - createdByUser: true, + createdByUser: true, description: "aaa", concept: ["amongus"], configDiscount: config2, @@ -215,15 +215,15 @@ describe("Promotion adapter integration test", function () { configDiscount: config, description: "aaa", externalId: "externalID2awdawd" - }, - config) + }, + config) await promotionAdapter.addPromotionHandler(promotion10) - await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); - - let result = await Order.findOne(order.id) + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); + + let result = await Order.findOne(order.id) // console.log(result) expect(result.discountTotal).to.equal(5); @@ -234,20 +234,20 @@ describe("Promotion adapter integration test", function () { // */ - it("Check flat and percentage discount for specific dish/group", async ()=>{ - let order = await Order.create({id: "configured-promotion-integration-specific"}).fetch(); - await Order.updateOne({id: order.id}, {concept: "specific",user: "user"}); + it("Check flat and percentage discount for specific dish/group", async () => { + let order = await Order.create({ id: "configured-promotion-integration-specific" }).fetch(); + await Order.updateOne({ id: order.id }, { concept: "specific", user: "user" }); const groups = await Group.find({}) const groupsId = groups.map(group => group.id) - let dish1 = await Dish.createOrUpdate(dishGenerator({name: "test dish", price: 10.1, concept: "specific", parentGroup:groupsId[0]})); - let dish2 = await Dish.createOrUpdate(dishGenerator({name: "test fish", price: 15.2, concept: "specific", parentGroup:groupsId[1]})); + let dish1 = await Dish.createOrUpdate(dishGenerator({ name: "test dish", price: 10.1, concept: "specific", parentGroup: groupsId[0] })); + let dish2 = await Dish.createOrUpdate(dishGenerator({ name: "test fish", price: 15.2, concept: "specific", parentGroup: groupsId[1] })); let dishes = await Dish.find({}) const dishesId = dishes.map(dish => dish.id) - - let flatDiscount:AbstractPromotionHandler = discountGenerator({ + + let flatDiscount: AbstractPromotionHandler = discountGenerator({ concept: ["specific"], badge: 'test', id: 'flat2-id', @@ -263,7 +263,7 @@ describe("Promotion adapter integration test", function () { }, }) - let percentDiscount:AbstractPromotionHandler = discountGenerator({ + let percentDiscount: AbstractPromotionHandler = discountGenerator({ badge: 'test', concept: ["specific"], id: 'percent2-id', @@ -278,8 +278,8 @@ describe("Promotion adapter integration test", function () { excludeModifiers: true }, }) - - let percentDiscount2:AbstractPromotionHandler = discountGenerator({ + + let percentDiscount2: AbstractPromotionHandler = discountGenerator({ concept: ["specific"], badge: 'test', id: 'percent2-idawdawd', @@ -295,7 +295,7 @@ describe("Promotion adapter integration test", function () { }, }) - let percentDiscount3:AbstractPromotionHandler = discountGenerator({ + let percentDiscount3: AbstractPromotionHandler = discountGenerator({ concept: ["specific"], id: 'percent2-idawdawd', isJoint: true, @@ -316,35 +316,35 @@ describe("Promotion adapter integration test", function () { await promotionAdapter.addPromotionHandler(percentDiscount2) await promotionAdapter.addPromotionHandler(percentDiscount3) - await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); order = await Order.findOne(order.id) expect(order.discountTotal).to.equal(20.13); }) - - - it("Promotion states should passed in order discount", async ()=>{ - - let order = await Order.create({id: "configured-promotion-integration-states"}).fetch(); - await Order.updateOne({id: order.id}, {concept: "PromotionStatess",user: "user"}); + + it("Promotion states should passed in order discount", async () => { + + + let order = await Order.create({ id: "configured-promotion-integration-states" }).fetch(); + await Order.updateOne({ id: order.id }, { concept: "PromotionStatess", user: "user" }); const groups = await Group.find({}) const groupsId = groups.map(group => group.id) - - let dish1 = await Dish.createOrUpdate(dishGenerator({name: "test dish", price: 10.1, concept: "PromotionStatess",parentGroup:groupsId[0]})); - let dish2 = await Dish.createOrUpdate(dishGenerator({name: "test fish", price: 15.2, concept: "PromotionStatess",parentGroup:groupsId[0]})); - - + + let dish1 = await Dish.createOrUpdate(dishGenerator({ name: "test dish", price: 10.1, concept: "PromotionStatess", parentGroup: groupsId[0] })); + let dish2 = await Dish.createOrUpdate(dishGenerator({ name: "test fish", price: 15.2, concept: "PromotionStatess", parentGroup: groupsId[0] })); + + let dishes = await Dish.find({}) const dishesId = dishes.map(dish => dish.id) - let promotion10state:AbstractPromotionHandler = discountGenerator({ + let promotion10state: AbstractPromotionHandler = discountGenerator({ concept: ["PromotionStatess"], id: 'flat2aaawa-id', isJoint: true, @@ -361,19 +361,19 @@ describe("Promotion adapter integration test", function () { }) await promotionAdapter.addPromotionHandler(promotion10state) - await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); - let res = await Order.findOne(order.id) + let res = await Order.findOne(order.id) // await promotionAdapter.processOrder(res) // res = await Order.findOne(order.id) - + let example = { message: `Discount generator description`, type: "configured-promotion", state: {} - } + } expect(res.discountTotal).to.equal(9); expect(res.promotionState[0].message).to.equal(example.message); expect(res.promotionState[0].type).to.equal(example.type); @@ -384,103 +384,103 @@ describe("Promotion adapter integration test", function () { // await Dish.display({id: "my-displayed-dish"}) // }) - - - it("Check prepend recursion discount", async ()=>{ + + + it("Check prepend recursion discount", async () => { // for call recursion we should add dish from action in promotionHandler const groups = await Group.find({}) const groupsId = groups.map(group => group.id) - - - let order = await Order.create({id: "configured-promotion-integration-recursion"}).fetch(); - await Order.updateOne({id: order.id}, {concept: "recursion",user: "user"}); - let dish1 = await Dish.createOrUpdate(dishGenerator({name: "test dish", price: 10.1, concept: "recursion",parentGroup:groupsId[0]})); - let dish2 = await Dish.createOrUpdate(dishGenerator({name: "test fish", price: 15.2, concept: "recursion",parentGroup:groupsId[0]})); - - let discountEx1:AbstractPromotionHandler = { - id: "1aw42-idaaa", + let order = await Order.create({ id: "configured-promotion-integration-recursion" }).fetch(); + await Order.updateOne({ id: order.id }, { concept: "recursion", user: "user" }); + + let dish1 = await Dish.createOrUpdate(dishGenerator({ name: "test dish", price: 10.1, concept: "recursion", parentGroup: groupsId[0] })); + let dish2 = await Dish.createOrUpdate(dishGenerator({ name: "test fish", price: 15.2, concept: "recursion", parentGroup: groupsId[0] })); + + + let discountEx1: AbstractPromotionHandler = { + id: "discountEx1-idaaa", badge: 'test', configDiscount: { - discountType: "flat", - discountAmount: 1, - dishes: [dish1.id, dish2.id], - groups: groupsId, - excludeModifiers: true - }, + discountType: "flat", + discountAmount: 1, + dishes: [dish1.id, dish2.id], + groups: groupsId, + excludeModifiers: true + }, name: "1124-name", - description: "sawdad", + description: "discountEx1 flat amount: 1 for 2 dishes", concept: ["recursion"], - condition: (arg: Group | Dish | Order): boolean =>{ - if (findModelInstanceByAttributes(arg) === "Order" && stringsInArray(arg.concept, discountEx1.concept) ) { - + condition: (arg: Group | Dish | Order): boolean => { + if (findModelInstanceByAttributes(arg) === "Order" && stringsInArray(arg.concept, discountEx1.concept)) { + return true; - } - - if (findModelInstanceByAttributes(arg) === "Dish" && stringsInArray(arg.concept, discountEx1.concept)) { + } + + if (findModelInstanceByAttributes(arg) === "Dish" && stringsInArray(arg.concept, discountEx1.concept)) { // TODO: check if includes in IconfigDish return true; - } - - if (findModelInstanceByAttributes(arg) === "Group" && stringsInArray(arg.concept, discountEx1.concept) ) { - // TODO: check if includes in IconfigG + } + + if (findModelInstanceByAttributes(arg) === "Group" && stringsInArray(arg.concept, discountEx1.concept)) { + // TODO: check if includes in IconfigG return true; - } - - return false - }, - action: async (order: Order): Promise => { - let dish1 = await Dish.createOrUpdate(dishGenerator({name: "test fish", price: 15.2, concept: "recursion",parentGroup:groupsId[0]})); - discountEx1.configDiscount.dishes.push(dish1.id) - await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); - - let configPromotion: ConfiguredPromotion = new ConfiguredPromotion(discountEx1, discountEx1.configDiscount); - order.promotionFlatDiscount = 2; - return await configPromotion.applyPromotion(order) + } + + return false + }, + action: async (order: Order): Promise => { + let dish1 = await Dish.createOrUpdate(dishGenerator({ name: "test fish", price: 15.2, concept: "recursion", parentGroup: groupsId[0] })); + discountEx1.configDiscount.dishes.push(dish1.id) + await Order.addDish({ id: order.id }, dish1, 5, [], "", "promotion"); + + let configPromotion: ConfiguredPromotion = new ConfiguredPromotion(discountEx1, discountEx1.configDiscount); + order.promotionFlatDiscount = 2; + return await configPromotion.applyPromotion(order) }, isPublic: true, isJoint: true, // sortOrder: 0, - displayGroup: function (group:Group, user?: string): Group { + displayGroup: function (group: Group, user?: string): Group { if (this.isJoint === true && this.isPublic === true) { - + group.discountAmount = Adapter.getPromotionAdapter().promotions[this.id].configDiscount.discountAmount; group.discountType = Adapter.getPromotionAdapter().promotions[this.id].configDiscount.discountType; - } - + } + return group }, - displayDish: function (dish:Dish, user?: string): Dish { + displayDish: function (dish: Dish, user?: string): Dish { if (this.isJoint === true && this.isPublic === true) { // dish.discountAmount = Adapter.getPromotionAdapter().promotions[this.id].configDiscount.discountAmount; dish.discountType = Adapter.getPromotionAdapter().promotions[this.id].configDiscount.discountType; dish.oldPrice = dish.price - dish.price = this.configDiscount.discountType === "flat" - ? new Decimal(dish.price).minus(+this.configDiscount.discountAmount).toNumber() - : new Decimal(dish.price) + dish.price = this.configDiscount.discountType === "flat" + ? new Decimal(dish.price).minus(+this.configDiscount.discountAmount).toNumber() + : new Decimal(dish.price) .mul(+this.configDiscount.discountAmount / 100) - .toNumber() + .toNumber() } return dish }, externalId: "1-externalIdaw", - } - - - + } + + + await promotionAdapter.addPromotionHandler(discountEx1) - await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); // 5 dishes + 5 from action - await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); // 4 dishes + 5 from action - - let result = await Order.findOne(order.id) - expect(result.discountTotal).to.equal(19+2 /** 2 is flat discount*/); + + let result = await Order.findOne(order.id) + expect(result.discountTotal).to.equal(19 + 2 /** 2 is flat discount*/); }) }); diff --git a/test/integration/models/cart.flow.test.js b/test/integration/models/cart.flow.test.js index 79b5d0b4..4b748e09 100644 --- a/test/integration/models/cart.flow.test.js +++ b/test/integration/models/cart.flow.test.js @@ -10,7 +10,7 @@ describe("Flows: Checkout", function () { await sleep(500); order = await Order.create({ id: "test.order.check-dishescount" }).fetch(); dishes = await Dish.find({}); - await Order.addDish({ id: order.id }, dishes[0], 1, [], "", "test"); + await Order.addDish({ id: order.id }, dishes[0], 1, [], "", "user"); order = await Order.findOne({ id: "test.order.check-dishescount" }); if (!order) throw "Order not created"; @@ -108,7 +108,7 @@ describe("Flows: Checkout", function () { }); await sleep(500); order = await Order.create({ id: "test-checkconfig-default-requireall" }).fetch(); - await Order.addDish({ id: order.id }, dishes[0], 1, [], "", "test"); + await Order.addDish({ id: order.id }, dishes[0], 1, [], "", "user"); order = await Order.findOne({ id: order.id }); await Settings.set("CHECKOUT_STRATEGY", {}); try { @@ -130,7 +130,7 @@ describe("Flows: Checkout", function () { await Settings.set("CHECKOUT_STRATEGY", { notRequired: true }); await sleep(500); order = await Order.create({ id: "test-checkconfig-notrequired" }).fetch(); - await Order.addDish({ id: order.id }, dishes[0], 1, [], "", "test"); + await Order.addDish({ id: order.id }, dishes[0], 1, [], "", "user"); order = await Order.findOne({ id: order.id }); // for selfServices try { @@ -156,7 +156,7 @@ describe("Flows: Checkout", function () { it("good customer", async function () { await sleep(500); order = await Order.create({ id: "check-customer" }).fetch(); - await Order.addDish({ id: order.id }, dishes[0], 1, [], "", "test"); + await Order.addDish({ id: order.id }, dishes[0], 1, [], "", "user"); order = await Order.findOne({ id: order.id }); try { await Order.check({ id: order.id }, customer_1.customer, true); @@ -214,7 +214,7 @@ describe("Flows: Checkout", function () { it("good address", async function () { await sleep(500); order = await Order.create({ id: "check-address" }).fetch(); - await Order.addDish({ id: order.id }, dishes[0], 1, [], "", "test"); + await Order.addDish({ id: order.id }, dishes[0], 1, [], "", "user"); order = await Order.findOne({ id: order.id }); let address = { streetId: "1234abcd", diff --git a/test/integration/models/cart.flow.test.ts b/test/integration/models/cart.flow.test.ts index f5924c9a..82f33d12 100644 --- a/test/integration/models/cart.flow.test.ts +++ b/test/integration/models/cart.flow.test.ts @@ -19,7 +19,7 @@ describe("Flows: Checkout", function () { await sleep(500) order = await Order.create({id:"test.order.check-dishescount"}).fetch(); dishes = await Dish.find({}) - await Order.addDish({id: order.id}, dishes[0], 1, [], "", "test"); + await Order.addDish({id: order.id}, dishes[0], 1, [], "", "user"); order = await Order.findOne({id:"test.order.check-dishescount"}); if (!order) throw "Order not created"; @@ -129,7 +129,7 @@ describe("Flows: Checkout", function () { await sleep(500) order = await Order.create({id: "test-checkconfig-default-requireall"}).fetch(); - await Order.addDish({id: order.id}, dishes[0], 1, [], "", "test"); + await Order.addDish({id: order.id}, dishes[0], 1, [], "", "user"); order = await Order.findOne({id: order.id}); await Settings.set("CHECKOUT_STRATEGY", {}); @@ -154,7 +154,7 @@ describe("Flows: Checkout", function () { await sleep(500) order = await Order.create({id: "test-checkconfig-notrequired"}).fetch(); - await Order.addDish({id: order.id}, dishes[0], 1, [], "", "test"); + await Order.addDish({id: order.id}, dishes[0], 1, [], "", "user"); order = await Order.findOne({id: order.id}); // for selfServices @@ -182,7 +182,7 @@ describe("Flows: Checkout", function () { it("good customer", async function () { await sleep(500) order = await Order.create({id: "check-customer"}).fetch(); - await Order.addDish({id: order.id}, dishes[0], 1, [], "", "test"); + await Order.addDish({id: order.id}, dishes[0], 1, [], "", "user"); order = await Order.findOne({id: order.id}); try { @@ -246,7 +246,7 @@ describe("Flows: Checkout", function () { await sleep(500) order = await Order.create({id:"check-address"}).fetch(); - await Order.addDish({id: order.id}, dishes[0], 1, [], "", "test"); + await Order.addDish({id: order.id}, dishes[0], 1, [], "", "user"); order = await Order.findOne({id: order.id}); let address: Address = { diff --git a/test/integration/models/maintenance.test.js b/test/integration/models/maintenance.test.js index 245158a1..f309271e 100644 --- a/test/integration/models/maintenance.test.js +++ b/test/integration/models/maintenance.test.js @@ -25,7 +25,7 @@ describe("Maintenance", function () { let error = null; try { let order = await Order.create({ id: "test--maintenece" }).fetch(); - await Order.addDish({ id: order.id }, dishes[0], 5, [], "", "test"); + await Order.addDish({ id: order.id }, dishes[0], 5, [], "", "user"); await Order.check({ id: order.id }, customer_1.customer, true, undefined, undefined); } catch (e) { diff --git a/test/integration/models/maintenance.test.ts b/test/integration/models/maintenance.test.ts index d4f0b455..dd8b3f00 100644 --- a/test/integration/models/maintenance.test.ts +++ b/test/integration/models/maintenance.test.ts @@ -28,7 +28,7 @@ describe("Maintenance", function () { let error = null; try { let order = await Order.create({id: "test--maintenece"}).fetch(); - await Order.addDish({id: order.id}, dishes[0], 5, [], "", "test"); + await Order.addDish({id: order.id}, dishes[0], 5, [], "", "user"); await Order.check({id: order.id}, customer, true, undefined, undefined); } catch (e) { error = e; diff --git a/test/integration/models/order.test.js b/test/integration/models/order.test.js index e20eaa5f..5476d3f3 100644 --- a/test/integration/models/order.test.js +++ b/test/integration/models/order.test.js @@ -71,7 +71,7 @@ describe("Order", function () { (0, chai_1.expect)(orderDishes.length).to.equals(1); (0, chai_1.expect)(orderDishes[0].amount).to.equals(6); order = await Order.create({ id: "adddish-same-dish-increase-amount-2" }).fetch(); - await Order.addDish({ id: order.id }, dishes[0], 1, [{ id: dishes[1].id, modifierId: dishes[1].id }], "", "mod"); + await Order.addDish({ id: order.id }, dishes[0], 1, [{ id: dishes[1].id, modifierId: dishes[1].id }], "", "user"); await Order.addDish({ id: order.id }, dishes[0], 1, null, "", "user"); await Order.addDish({ id: order.id }, dishes[0], 2, null, "", "user"); orderDishes = await OrderDish.find({ order: order.id, dish: dishes[0].id }); @@ -106,11 +106,11 @@ describe("Order", function () { it("addDish 20", async function () { order = await Order.create({ id: "adddish-20" }).fetch(); for (let i = 0; i < 20; i++) { - await Order.addDish({ id: order.id }, dishes[i], 3, [], "", ""); + await Order.addDish({ id: order.id }, dishes[i], 3, [], "", "user"); } }); it("addDish 21th", async function () { - await Order.addDish({ id: order.id }, dishes[21], 3, [], "", ""); + await Order.addDish({ id: order.id }, dishes[21], 3, [], "", "user"); }); it("setSelfService", async function () { let order = await Order.create({ id: "setselfservice" }).fetch(); @@ -186,9 +186,9 @@ describe("Order", function () { it("doPaid", async function () { (0, chai_1.expect)(Order.doPaid).to.not.equals(undefined); let order = await Order.create({ id: "dopaid" }).fetch(); - await Order.addDish({ id: order.id }, dishes[0], 5, [], "", ""); - await Order.addDish({ id: order.id }, dishes[1], 3, [], "", ""); - await Order.addDish({ id: order.id }, dishes[2], 8, [], "", ""); + await Order.addDish({ id: order.id }, dishes[0], 5, [], "", "user"); + await Order.addDish({ id: order.id }, dishes[1], 3, [], "", "user"); + await Order.addDish({ id: order.id }, dishes[2], 8, [], "", "user"); await Order.check({ id: order.id }, customer_1.customer, true, undefined, undefined); const paymentMethod = (await PaymentMethod.find({}))[0]; let newPaymentDocument = { diff --git a/test/integration/models/order.test.ts b/test/integration/models/order.test.ts index ea66217d..28f4adc1 100644 --- a/test/integration/models/order.test.ts +++ b/test/integration/models/order.test.ts @@ -135,7 +135,7 @@ describe("Order", function () { expect(orderDishes[0].amount).to.equals(6); order = await Order.create({id:"adddish-same-dish-increase-amount-2"}).fetch(); - await Order.addDish({id: order.id}, dishes[0], 1, [{ id: dishes[1].id, modifierId: dishes[1].id }], "", "mod"); + await Order.addDish({id: order.id}, dishes[0], 1, [{ id: dishes[1].id, modifierId: dishes[1].id }], "", "user"); await Order.addDish({id: order.id}, dishes[0], 1, null, "", "user"); await Order.addDish({id: order.id}, dishes[0], 2, null, "", "user"); orderDishes = await OrderDish.find({ order: order.id, dish: dishes[0].id }); @@ -175,12 +175,12 @@ describe("Order", function () { it("addDish 20", async function () { order = await Order.create({id: "adddish-20"}).fetch(); for (let i = 0; i < 20; i++) { - await Order.addDish({id: order.id}, dishes[i], 3, [], "", ""); + await Order.addDish({id: order.id}, dishes[i], 3, [], "", "user"); } }); it("addDish 21th", async function () { - await Order.addDish({id: order.id}, dishes[21], 3, [], "", ""); + await Order.addDish({id: order.id}, dishes[21], 3, [], "", "user"); }); it("setSelfService", async function () { @@ -283,9 +283,9 @@ describe("Order", function () { expect(Order.doPaid).to.not.equals(undefined); let order = await Order.create({id:"dopaid"}).fetch(); - await Order.addDish({id: order.id}, dishes[0], 5, [], "", ""); - await Order.addDish({id: order.id}, dishes[1], 3, [], "", ""); - await Order.addDish({id: order.id}, dishes[2], 8, [], "", ""); + await Order.addDish({id: order.id}, dishes[0], 5, [], "", "user"); + await Order.addDish({id: order.id}, dishes[1], 3, [], "", "user"); + await Order.addDish({id: order.id}, dishes[2], 8, [], "", "user"); await Order.check({id: order.id}, customer, true, undefined, undefined); diff --git a/test/unit/discount/discount.test.d.ts b/test/unit/discount/discount.test.d.ts index 3b46f5f5..abfd020e 100644 --- a/test/unit/discount/discount.test.d.ts +++ b/test/unit/discount/discount.test.d.ts @@ -9,9 +9,9 @@ export {}; it("addDish same dish increase amount", async function () { order = await Order.create({id: "adddish-same-dish-increase-amount-1"}).fetch(); - await Order.addDish({id: order.id}, dishes[0], 2, [], "", "test"); - await Order.addDish({id: order.id}, dishes[0], 3, [], "", "test"); - await Order.addDish({id: order.id}, dishes[0], 1, null, "", "test"); + await Order.addDish({id: order.id}, dishes[0], 2, [], "", "user"); + await Order.addDish({id: order.id}, dishes[0], 3, [], "", "user"); + await Order.addDish({id: order.id}, dishes[0], 1, null, "", "user"); let orderDishes = await OrderDish.find({ order: order.id, dish: dishes[0].id }); expect(orderDishes.length).to.equals(1); @@ -19,8 +19,8 @@ export {}; order = await Order.create({id:"adddish-same-dish-increase-amount-2"}).fetch(); await Order.addDish({id: order.id}, dishes[0], 1, [{ id: dishes[1].id, modifierId: dishes[1].id }], "", "mod"); - await Order.addDish({id: order.id}, dishes[0], 1, null, "", "test"); - await Order.addDish({id: order.id}, dishes[0], 2, null, "", "test"); + await Order.addDish({id: order.id}, dishes[0], 1, null, "", "user"); + await Order.addDish({id: order.id}, dishes[0], 2, null, "", "user"); orderDishes = await OrderDish.find({ order: order.id, dish: dishes[0].id }); expect(orderDishes.length).to.equals(2); for (let dish of orderDishes) { diff --git a/test/unit/discount/discount.test.js b/test/unit/discount/discount.test.js index 34be3461..57f2b812 100644 --- a/test/unit/discount/discount.test.js +++ b/test/unit/discount/discount.test.js @@ -128,7 +128,7 @@ describe('Discount', function () { // let dish1 = await Dish.createOrUpdate(dishGenerator({name: "test dish", price: 10, concept: "origin",parentGroup:groupsId[0]})); // discountEx.configDiscount.dishes.push(dish1.id) // await promotionAdapter.addPromotionHandler(discountEx) - // await Order.addDish({id: order.id}, dish1, 5, [], "", "test"); + // await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); // await Adapter.getPromotionAdapter().clearOfPromotion(order) // await configuredPromotion.applyPromotion(order.id) // let result = await Order.findOne(order.id) @@ -142,8 +142,8 @@ describe('Discount', function () { discountEx.configDiscount.dishes.push(dish1.id); discountEx.configDiscount.dishes.push(dish2.id); await promotionAdapter.addPromotionHandler(discountEx); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "test"); - await Order.addDish({ id: order.id }, dish2, 4, [], "", "test"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); // let result1 = await Dish.findOne(dish1.id) await index_1.Adapter.getPromotionAdapter().clearOfPromotion(order); await configuredPromotion.applyPromotion(order); @@ -159,8 +159,8 @@ describe('Discount', function () { discountEx.configDiscount.dishes.push(dish1.id); discountEx.configDiscount.dishes.push(dish2.id); await promotionAdapter.addPromotionHandler(discountEx); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "testa"); - await Order.addDish({ id: order.id }, dish2, 4, [], "", "testa"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); order = await Order.findOne(order.id); await promotionAdapter.processOrder(order); let result = await Order.findOne(order.id); //.populate("dishes"); @@ -169,8 +169,8 @@ describe('Discount', function () { (0, chai_1.expect)(result.discountTotal).to.equal(11.97); let dish3 = await Dish.createOrUpdate((0, dish_generator_1.default)({ name: "test disha", price: 10, concept: "a", parentGroup: groupsId[0] })); let dish4 = await Dish.createOrUpdate((0, dish_generator_1.default)({ name: "test fisha", price: 15, concept: "a", parentGroup: groupsId[0] })); - await Order.addDish({ id: order.id }, dish3, 5, [], "", "test"); - await Order.addDish({ id: order.id }, dish4, 4, [], "", "test"); + await Order.addDish({ id: order.id }, dish3, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish4, 4, [], "", "user"); order = await Order.findOne(order.id); // console.log(order) await promotionAdapter.processOrder(order); @@ -185,8 +185,8 @@ describe('Discount', function () { discInMemory.configDiscount.dishes.push(dish1.id); discInMemory.configDiscount.dishes.push(dish2.id); await promotionAdapter.addPromotionHandler(discInMemory); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "test"); - await Order.addDish({ id: order.id }, dish2, 4, [], "", "test"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); // console.log(discInMemory) // let result1 = await Dish.findOne(dish1.id) // DiscountAdapter.applyToOrder(order) @@ -210,11 +210,11 @@ describe('Discount', function () { discountEx.configDiscount.dishes = dishesId; await promotionAdapter.addPromotionHandler(discInMemory); await promotionAdapter.addPromotionHandler(discountEx); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "testa2"); - await Order.addDish({ id: order.id }, dish2, 4, [], "", "tes"); - await Order.addDish({ id: order.id }, dish3, 5, [], "", "testa"); - await Order.addDish({ id: order.id }, dish4, 4, [], "", "test"); - await Order.addDish({ id: order.id }, dish5, 5, [], "", "testa1"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); + await Order.addDish({ id: order.id }, dish3, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish4, 4, [], "", "user"); + await Order.addDish({ id: order.id }, dish5, 5, [], "", "user"); // 29.86 + 30.59 = 60.45 order = await Order.findOne(order.id); await promotionAdapter.processOrder(order); @@ -236,11 +236,11 @@ describe('Discount', function () { discountEx.configDiscount.dishes = dishesId; await promotionAdapter.addPromotionHandler(discInMemory); await promotionAdapter.addPromotionHandler(discountEx); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "testa2"); - await Order.addDish({ id: order.id }, dish2, 4, [], "", "tes"); - await Order.addDish({ id: order.id }, dish3, 5, [], "", "testa"); - await Order.addDish({ id: order.id }, dish4, 4, [], "", "test"); - await Order.addDish({ id: order.id }, dish5, 5, [], "", "testa1"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); + await Order.addDish({ id: order.id }, dish3, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish4, 4, [], "", "user"); + await Order.addDish({ id: order.id }, dish5, 5, [], "", "user"); // 17.7 + 12.16 + 19.95 = 49.81 // await promotionAdapter.addPromotionHandler(discInMemory) // await promotionAdapter.addPromotionHandler(discountEx) @@ -266,11 +266,11 @@ describe('Discount', function () { await promotionAdapter.addPromotionHandler(disc1); await promotionAdapter.addPromotionHandler(discInMemory); await promotionAdapter.addPromotionHandler(discountEx); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "testa2"); - await Order.addDish({ id: order.id }, dish2, 4, [], "", "tes"); - await Order.addDish({ id: order.id }, dish3, 5, [], "", "testa"); - await Order.addDish({ id: order.id }, dish4, 4, [], "", "test"); - await Order.addDish({ id: order.id }, dish5, 5, [], "", "testa1"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); + await Order.addDish({ id: order.id }, dish3, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish4, 4, [], "", "user"); + await Order.addDish({ id: order.id }, dish5, 5, [], "", "user"); // 18.62 + 136.8 - 10% = 18.62 + 13,68 = 32.3+ (101 +60.8) - 10% = 32.3 + 16.18 = 48.48 + 14 = 62.48 let result = await Order.findOne(order.id); //.populate("dishes"); (0, chai_1.expect)(result.discountTotal).to.equal(62.48); @@ -309,7 +309,7 @@ describe('Discount', function () { let dish1 = await Dish.createOrUpdate((0, dish_generator_1.default)({ name: "test dish", price: 10, concept: "origin", parentGroup: groupsId[0] })); discountEx.configDiscount.dishes.push(dish1.id); await promotionAdapter.addPromotionHandler(discountEx); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "test"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); // await promotionAdapter.processOrder(order) await index_1.Adapter.getPromotionAdapter().clearOfPromotion(order); let result = await Order.findOne(order.id); @@ -322,7 +322,7 @@ describe('Discount', function () { discountEx.configDiscount.dishes.push(dish1.id); await promotionAdapter.addPromotionHandler(discountEx); // console.log(promotionAdapter.promotions) - await Order.addDish({ id: order.id }, dish1, 5, [], "", "test"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); // await Adapter.getPromotionAdapter().applyPromotion(order.id, discountEx.configDiscount, discountEx.id) // before clear let orderDishes1 = await OrderDish.find({ order: order.id }).populate("dish"); @@ -342,7 +342,7 @@ describe('Discount', function () { let dish1 = await Dish.createOrUpdate((0, dish_generator_1.default)({ name: "test dish", price: 10, concept: "clear", parentGroup: groupsId[0] })); discountEx.configDiscount.dishes.push(dish1.id); await a.addPromotionHandler(discountEx); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "test"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); // await promotionAdapter.addPromotionHandler(discountEx) // await Adapter.getPromotionAdapter().applyPromotion(order.id, discountEx.configDiscount, discountEx.id) // before clear @@ -375,9 +375,9 @@ describe('Discount', function () { it("addDish same dish increase amount", async function () { order = await Order.create({id: "adddish-same-dish-increase-amount-1"}).fetch(); - await Order.addDish({id: order.id}, dishes[0], 2, [], "", "test"); - await Order.addDish({id: order.id}, dishes[0], 3, [], "", "test"); - await Order.addDish({id: order.id}, dishes[0], 1, null, "", "test"); + await Order.addDish({id: order.id}, dishes[0], 2, [], "", "user"); + await Order.addDish({id: order.id}, dishes[0], 3, [], "", "user"); + await Order.addDish({id: order.id}, dishes[0], 1, null, "", "user"); let orderDishes = await OrderDish.find({ order: order.id, dish: dishes[0].id }); expect(orderDishes.length).to.equals(1); @@ -385,8 +385,8 @@ describe('Discount', function () { order = await Order.create({id:"adddish-same-dish-increase-amount-2"}).fetch(); await Order.addDish({id: order.id}, dishes[0], 1, [{ id: dishes[1].id, modifierId: dishes[1].id }], "", "mod"); - await Order.addDish({id: order.id}, dishes[0], 1, null, "", "test"); - await Order.addDish({id: order.id}, dishes[0], 2, null, "", "test"); + await Order.addDish({id: order.id}, dishes[0], 1, null, "", "user"); + await Order.addDish({id: order.id}, dishes[0], 2, null, "", "user"); orderDishes = await OrderDish.find({ order: order.id, dish: dishes[0].id }); expect(orderDishes.length).to.equals(2); for (let dish of orderDishes) { diff --git a/test/unit/discount/discount.test.ts b/test/unit/discount/discount.test.ts index 7c0292cf..af569e70 100644 --- a/test/unit/discount/discount.test.ts +++ b/test/unit/discount/discount.test.ts @@ -151,7 +151,7 @@ describe('Discount', function () { // discountEx.configDiscount.dishes.push(dish1.id) // await promotionAdapter.addPromotionHandler(discountEx) - // await Order.addDish({id: order.id}, dish1, 5, [], "", "test"); + // await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); // await Adapter.getPromotionAdapter().clearOfPromotion(order) // await configuredPromotion.applyPromotion(order.id) @@ -173,8 +173,8 @@ describe('Discount', function () { discountEx.configDiscount.dishes.push(dish2.id) await promotionAdapter.addPromotionHandler(discountEx) - await Order.addDish({id: order.id}, dish1, 5, [], "", "test"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "test"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); // let result1 = await Dish.findOne(dish1.id) await Adapter.getPromotionAdapter().clearOfPromotion(order) @@ -197,8 +197,8 @@ describe('Discount', function () { await promotionAdapter.addPromotionHandler(discountEx) - await Order.addDish({id: order.id}, dish1, 5, [], "", "testa"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "testa"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); order = await Order.findOne(order.id) @@ -213,8 +213,8 @@ describe('Discount', function () { let dish3 = await Dish.createOrUpdate(dishGenerator({name: "test disha", price: 10, concept: "a",parentGroup:groupsId[0]})); let dish4 = await Dish.createOrUpdate(dishGenerator({name: "test fisha", price: 15, concept: "a",parentGroup:groupsId[0]})); - await Order.addDish({id: order.id}, dish3, 5, [], "", "test"); - await Order.addDish({id: order.id}, dish4, 4, [], "", "test"); + await Order.addDish({id: order.id}, dish3, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish4, 4, [], "", "user"); order = await Order.findOne(order.id) @@ -243,8 +243,8 @@ describe('Discount', function () { await promotionAdapter.addPromotionHandler(discInMemory) - await Order.addDish({id: order.id}, dish1, 5, [], "", "test"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "test"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); // console.log(discInMemory) // let result1 = await Dish.findOne(dish1.id) @@ -280,11 +280,11 @@ describe('Discount', function () { await promotionAdapter.addPromotionHandler(discInMemory) await promotionAdapter.addPromotionHandler(discountEx) - await Order.addDish({id: order.id}, dish1, 5, [], "", "testa2"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "tes"); - await Order.addDish({id: order.id}, dish3, 5, [], "", "testa"); - await Order.addDish({id: order.id}, dish4, 4, [], "", "test"); - await Order.addDish({id: order.id}, dish5, 5, [], "", "testa1"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); + await Order.addDish({id: order.id}, dish3, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish4, 4, [], "", "user"); + await Order.addDish({id: order.id}, dish5, 5, [], "", "user"); // 29.86 + 30.59 = 60.45 @@ -318,11 +318,11 @@ describe('Discount', function () { await promotionAdapter.addPromotionHandler(discountEx) - await Order.addDish({id: order.id}, dish1, 5, [], "", "testa2"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "tes"); - await Order.addDish({id: order.id}, dish3, 5, [], "", "testa"); - await Order.addDish({id: order.id}, dish4, 4, [], "", "test"); - await Order.addDish({id: order.id}, dish5, 5, [], "", "testa1"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); + await Order.addDish({id: order.id}, dish3, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish4, 4, [], "", "user"); + await Order.addDish({id: order.id}, dish5, 5, [], "", "user"); // 17.7 + 12.16 + 19.95 = 49.81 @@ -359,11 +359,11 @@ describe('Discount', function () { await promotionAdapter.addPromotionHandler(discInMemory) await promotionAdapter.addPromotionHandler(discountEx) - await Order.addDish({id: order.id}, dish1, 5, [], "", "testa2"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "tes"); - await Order.addDish({id: order.id}, dish3, 5, [], "", "testa"); - await Order.addDish({id: order.id}, dish4, 4, [], "", "test"); - await Order.addDish({id: order.id}, dish5, 5, [], "", "testa1"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); + await Order.addDish({id: order.id}, dish3, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish4, 4, [], "", "user"); + await Order.addDish({id: order.id}, dish5, 5, [], "", "user"); // 18.62 + 136.8 - 10% = 18.62 + 13,68 = 32.3+ (101 +60.8) - 10% = 32.3 + 16.18 = 48.48 + 14 = 62.48 let result = await Order.findOne(order.id) //.populate("dishes"); @@ -425,7 +425,7 @@ describe('Discount', function () { await promotionAdapter.addPromotionHandler(discountEx) - await Order.addDish({id: order.id}, dish1, 5, [], "", "test"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); // await promotionAdapter.processOrder(order) @@ -445,7 +445,7 @@ describe('Discount', function () { await promotionAdapter.addPromotionHandler(discountEx) // console.log(promotionAdapter.promotions) - await Order.addDish({id: order.id}, dish1, 5, [], "", "test"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); // await Adapter.getPromotionAdapter().applyPromotion(order.id, discountEx.configDiscount, discountEx.id) @@ -470,7 +470,7 @@ describe('Discount', function () { discountEx.configDiscount.dishes.push(dish1.id) await a.addPromotionHandler(discountEx) - await Order.addDish({id: order.id}, dish1, 5, [], "", "test"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); // await promotionAdapter.addPromotionHandler(discountEx) @@ -522,9 +522,9 @@ describe('Discount', function () { it("addDish same dish increase amount", async function () { order = await Order.create({id: "adddish-same-dish-increase-amount-1"}).fetch(); - await Order.addDish({id: order.id}, dishes[0], 2, [], "", "test"); - await Order.addDish({id: order.id}, dishes[0], 3, [], "", "test"); - await Order.addDish({id: order.id}, dishes[0], 1, null, "", "test"); + await Order.addDish({id: order.id}, dishes[0], 2, [], "", "user"); + await Order.addDish({id: order.id}, dishes[0], 3, [], "", "user"); + await Order.addDish({id: order.id}, dishes[0], 1, null, "", "user"); let orderDishes = await OrderDish.find({ order: order.id, dish: dishes[0].id }); expect(orderDishes.length).to.equals(1); @@ -532,8 +532,8 @@ describe('Discount', function () { order = await Order.create({id:"adddish-same-dish-increase-amount-2"}).fetch(); await Order.addDish({id: order.id}, dishes[0], 1, [{ id: dishes[1].id, modifierId: dishes[1].id }], "", "mod"); - await Order.addDish({id: order.id}, dishes[0], 1, null, "", "test"); - await Order.addDish({id: order.id}, dishes[0], 2, null, "", "test"); + await Order.addDish({id: order.id}, dishes[0], 1, null, "", "user"); + await Order.addDish({id: order.id}, dishes[0], 2, null, "", "user"); orderDishes = await OrderDish.find({ order: order.id, dish: dishes[0].id }); expect(orderDishes.length).to.equals(2); for (let dish of orderDishes) { diff --git a/test/unit/discount/empty_concept.test.js b/test/unit/discount/empty_concept.test.js index 5e5b7506..e2658c0f 100644 --- a/test/unit/discount/empty_concept.test.js +++ b/test/unit/discount/empty_concept.test.js @@ -83,8 +83,8 @@ describe('Discount_Empty', function () { discountEx.configDiscount.dishes.push(dish1.id); discountEx.configDiscount.dishes.push(dish2.id); await promotionAdapter.addPromotionHandler(discountEx); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "test"); - await Order.addDish({ id: order.id }, dish2, 4, [], "", "test"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); let result = await Order.findOne(order.id); (0, chai_1.expect)(result.discountTotal).to.equal(11.97); }); @@ -96,8 +96,8 @@ describe('Discount_Empty', function () { discountEx.configDiscount.dishes.push(dish1.id); discountEx.configDiscount.dishes.push(dish2.id); await promotionAdapter.addPromotionHandler(discountEx); - await Order.addDish({ id: order.id }, dish1, 5, [], "", "test"); - await Order.addDish({ id: order.id }, dish2, 4, [], "", "test"); + await Order.addDish({ id: order.id }, dish1, 5, [], "", "user"); + await Order.addDish({ id: order.id }, dish2, 4, [], "", "user"); let result = await Order.findOne(order.id); (0, chai_1.expect)(result.discountTotal).to.equal(11.97); }); diff --git a/test/unit/discount/empty_concept.test.ts b/test/unit/discount/empty_concept.test.ts index 877602fc..beb9d98a 100644 --- a/test/unit/discount/empty_concept.test.ts +++ b/test/unit/discount/empty_concept.test.ts @@ -102,8 +102,8 @@ describe('Discount_Empty', function () { discountEx.configDiscount.dishes.push(dish2.id) await promotionAdapter.addPromotionHandler(discountEx) - await Order.addDish({id: order.id}, dish1, 5, [], "", "test"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "test"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); let result = await Order.findOne(order.id) expect(result.discountTotal).to.equal(11.97); @@ -121,8 +121,8 @@ describe('Discount_Empty', function () { discountEx.configDiscount.dishes.push(dish2.id) await promotionAdapter.addPromotionHandler(discountEx) - await Order.addDish({id: order.id}, dish1, 5, [], "", "test"); - await Order.addDish({id: order.id}, dish2, 4, [], "", "test"); + await Order.addDish({id: order.id}, dish1, 5, [], "", "user"); + await Order.addDish({id: order.id}, dish2, 4, [], "", "user"); let result = await Order.findOne(order.id) expect(result.discountTotal).to.equal(11.97);