Skip to content

Commit

Permalink
user instead test for addedBy
Browse files Browse the repository at this point in the history
  • Loading branch information
Xziy committed Jan 31, 2024
1 parent a10ebb3 commit cd0be12
Show file tree
Hide file tree
Showing 19 changed files with 292 additions and 275 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=PromotionCodes"
"--grep=prepend"
],
"sourceMaps": true,
"internalConsoleOptions": "openOnSessionStart",
Expand Down
8 changes: 4 additions & 4 deletions models/Order.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -161,7 +161,7 @@ declare let Model: {
beforeCreate(orderInit: Order, cb: (err?: string) => void): void;
afterCreate(order: Order, cb: (err?: string) => void): Promise<void>;
/** Add dish into order */
addDish(criteria: CriteriaQuery<Order>, dish: Dish | string, amount: number, modifiers: OrderModifier[], comment: string, addedBy: string, replace?: boolean, orderDishId?: number): Promise<void>;
addDish(criteria: CriteriaQuery<Order>, dish: string | Dish, amount: number, modifiers: OrderModifier[], comment: string, addedBy: "user" | "promotion" | "core" | "custom", replace?: boolean, orderDishId?: number): Promise<void>;
removeDish(criteria: CriteriaQuery<Order>, dish: OrderDish, amount: number, stack?: boolean): Promise<void>;
setCount(criteria: CriteriaQuery<Order>, dish: OrderDish, amount: number): Promise<void>;
setComment(criteria: CriteriaQuery<Order>, dish: OrderDish, comment: string): Promise<void>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -266,7 +266,7 @@ declare let Model: {
*/
countCart(criteria: CriteriaQuery<Order>, isPromoting?: boolean): Promise<Order>;
doPaid(criteria: CriteriaQuery<Order>, paymentDocument: PaymentDocument): Promise<void>;
applyPromotionCode(criteria: CriteriaQuery<Order>, promotionCodeString: string | null): Promise<Order>;
applyPromotionCode(criteria: CriteriaQuery<Order>, promotionCodeString: string): Promise<Order>;
};
declare global {
const Order: typeof Model & ORMModel<Order, null> & StateFlowModel;
Expand Down
21 changes: 16 additions & 5 deletions models/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
},
Expand Down Expand Up @@ -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?!
Expand Down
22 changes: 14 additions & 8 deletions models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion models/User.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ declare let Model: {
* @param {WaterlineCriteria} criteria
* @returns String
*/
getPhoneString(phone: Phone, target?: "login" | "print" | "string"): Promise<string>;
getPhoneString(phone: Phone, target?: "string" | "login" | "print"): Promise<string>;
/**
* Update user password
*
Expand Down
12 changes: 6 additions & 6 deletions models/UserBonusProgram.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserBonusProgram>;
delete(user: User | string, adapterOrId: string): Promise<void>;
syncAll(user: User | string): Promise<void>;
registration(user: string | User, adapterOrId: string): Promise<UserBonusProgram>;
delete(user: string | User, adapterOrId: string): Promise<void>;
syncAll(user: string | User): Promise<void>;
/** Full sync all transaction with external system */
sync(user: User | string, bonusProgram: BonusProgram | string, force?: boolean): Promise<void>;
checkEnoughToSpend(user: User | string, bonusProgram: BonusProgram | string, amount: number): Promise<boolean>;
sumCurrentBalance(user: User | string, bonusProgram: BonusProgram | string): Promise<number>;
sync(user: string | User, bonusProgram: string | BonusProgram, force?: boolean): Promise<void>;
checkEnoughToSpend(user: string | User, bonusProgram: string | BonusProgram, amount: number): Promise<boolean>;
sumCurrentBalance(user: string | User, bonusProgram: string | BonusProgram): Promise<number>;
};
declare global {
const UserBonusProgram: typeof Model & ORMModel<UserBonusProgram, "user" | "bonusProgram">;
Expand Down
6 changes: 3 additions & 3 deletions test/integration/adapters/promotion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)) {
Expand All @@ -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);
Expand Down
Loading

0 comments on commit cd0be12

Please sign in to comment.