Skip to content

Commit

Permalink
fixes, recursion and simple configPromotion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wsderte committed Aug 10, 2023
1 parent 031eee0 commit aa7d5b2
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 274 deletions.
2 changes: 1 addition & 1 deletion adapters/promotion/AbstractPromotionAdapter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default abstract class AbstractPromotionAdapter {
static getAllConcept: (concept: string[]) => Promise<AbstractPromotionHandler[]>;
abstract getActivePromotionsIds(): string[];
static clearOfPromotion(orderId: any): Promise<void>;
static applyPromotion: (orderId: any, spendPromotion: IconfigDiscount, promotionId: any) => Promise<void>;
static applyPromotion: (orderId: any, spendPromotion: IconfigDiscount, promotionId: any) => Promise<PromotionState>;
static initialize: (initParams?: {
[key: string]: string | number | boolean;
}) => PromotionAdapter;
Expand Down
2 changes: 1 addition & 1 deletion adapters/promotion/AbstractPromotionAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AbstractPromotionAdapter {
for (const orderDish of orderDishes) {
await OrderDish.update({ id: orderDish.id }, { discountTotal: 0, discountType: "" }).fetch();
}
await Order.updateOne({ id: order.id }, { discountTotal: 0 }); // isPromoted: false
await Order.updateOne({ id: order.id }, { discountTotal: 0 }); // isPromoting: false
}
}
exports.default = AbstractPromotionAdapter;
2 changes: 1 addition & 1 deletion adapters/promotion/AbstractPromotionAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default abstract class AbstractPromotionAdapter {
await Order.updateOne({ id: order.id }, { discountTotal: 0}) // isPromoting: false
}

public static applyPromotion: (orderId: any, spendPromotion: IconfigDiscount, promotionId: any) => Promise<void>;
public static applyPromotion: (orderId: any, spendPromotion: IconfigDiscount, promotionId: any) => Promise<PromotionState>;
public static initialize:(initParams?: {
[key: string]: string | number | boolean;
}) => PromotionAdapter;
Expand Down
1 change: 1 addition & 0 deletions adapters/promotion/default/configuredPromotion.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export default class configuredPromotion extends AbstractPromotionHandler {
action(order: Order): Promise<PromotionState>;
displayGroup(group: Group, user?: string | User): Promise<Group[]>;
displayDish(dish: Dish, user?: string | User): Promise<Dish[]>;
static applyPromotion(orderId: any, spendDiscount: IconfigDiscount, promotionId: any): Promise<PromotionState>;
}
57 changes: 32 additions & 25 deletions adapters/promotion/default/configuredPromotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ class configuredPromotion extends AbstractPromotion_1.default {
this.externalId = promotion.externalId;
}
condition(arg) {
if ((0, findModelInstance_1.default)(arg) === "Order" && this.concept.includes(arg.concept)) {
if ((0, findModelInstance_1.default)(arg) === "Order" && (0, stringsInArray_1.stringsInArray)(arg.concept, this.concept)) {
// order not used for configuredPromotion
// Order.populate()
console.log("condition");
return true;
}
if ((0, findModelInstance_1.default)(arg) === "Dish" && this.concept.includes(arg.concept)) {
if ((0, findModelInstance_1.default)(arg) === "Dish" && (0, stringsInArray_1.stringsInArray)(arg.concept, this.concept)) {
if (this.config.dishes.includes(arg.id)) {
return true;
}
}
if ((0, findModelInstance_1.default)(arg) === "Group" && this.concept.includes(arg.concept)) {
if ((0, findModelInstance_1.default)(arg) === "Group" && (0, stringsInArray_1.stringsInArray)(arg.concept, this.concept)) {
if (this.config.groups.includes(arg.id)) {
return true;
}
Expand All @@ -39,9 +40,25 @@ class configuredPromotion extends AbstractPromotion_1.default {
}
async action(order) {
console.log(this.config + " action");
// return await PromotionAdapter.applyPromotion(order.id, this.config, this.id)
// return Promise.resolve()
let mass = await configuredPromotion.applyPromotion(order.id, this.config, this.id);
return mass;
}
async displayGroup(group, user) {
if (user) {
// return await Group.display(this.concept, group.id)
return await Group.display(group);
}
}
async displayDish(dish, user) {
if (user) {
// return await Dish.display(this.concept, group.id)
return await Dish.display(dish);
}
}
static async applyPromotion(orderId, spendDiscount, promotionId) {
const order = await Order.findOne({ id: orderId });
if (order.user && typeof order.user === "string") {
const promotion = await Promotion.findOne({ id: promotionId });
// order.dishes
const orderDishes = await OrderDish.find({ order: order.id }).populate("dish");
let discountCost = new decimal_js_1.default(0);
Expand All @@ -51,29 +68,31 @@ class configuredPromotion extends AbstractPromotion_1.default {
sails.log.error("orderDish", orderDish.id, "has no such dish");
continue;
}
if (!(0, stringsInArray_1.stringsInArray)(orderDish.dish.concept, this.concept)) {
if (!(0, stringsInArray_1.stringsInArray)(orderDish.dish.concept, promotion.concept)) {
continue;
}
// TODO: if concept:arrays
// ------------------------------------------ Decimal ------------------------------------------
if (this.configDiscount.discountType === "flat") {
orderDishDiscountCost = new decimal_js_1.default(this.configDiscount.discountAmount).mul(orderDish.amount).toNumber();
if (spendDiscount.discountType === "flat") {
orderDishDiscountCost = new decimal_js_1.default(spendDiscount.discountAmount).mul(orderDish.amount).toNumber();
discountCost = new decimal_js_1.default(orderDishDiscountCost).add(discountCost);
// discountCost += new Decimal(orderDish.dish.price * orderDish.dish.amount).sub(spendDiscount.discountAmount * orderDish.dish.amount ).toNumber();
}
if (this.configDiscount.discountType === "percentage") {
if (spendDiscount.discountType === "percentage") {
// let discountPrice:number = new Decimal(orderDish.dish.price).mul(orderDish.amount).mul(+spendDiscount.discountAmount / 100).toNumber();
orderDishDiscountCost = new decimal_js_1.default(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(+spendDiscount.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();
await OrderDish.update({ id: orderDish.id }, { discountTotal: orderDishDiscount, discountType: spendDiscount.discountType }).fetch();
// await OrderDish.update({ id: orderDish.id }, { discountTotal: orderDishDiscountCost, discountType: spendDiscount.discountType}).fetch();
// await OrderDish.update({ id: orderDish.id }, { amount: orderDish.dish.price, discount: discountCost}).fetch();
}
// Update the order with new total
let orderDiscount = new decimal_js_1.default(order.discountTotal).add(discountCost.toNumber()).toNumber();
await Order.updateOne({ id: order.id }, { discountTotal: orderDiscount });
await Order.updateOne({ id: orderId }, { discountTotal: orderDiscount });
// let discountCoverage: Decimal;
// await Order.updateOne({id: orderId}, {total: order.total, discountTotal: discountCoverage.toNumber()});
}
Expand All @@ -86,17 +105,5 @@ class configuredPromotion extends AbstractPromotion_1.default {
state: {}
};
}
async displayGroup(group, user) {
if (user) {
// return await Group.display(this.concept, group.id)
return await Group.display(group);
}
}
async displayDish(dish, user) {
if (user) {
// return await Dish.display(this.concept, group.id)
return await Dish.display(dish);
}
}
}
exports.default = configuredPromotion;
71 changes: 13 additions & 58 deletions adapters/promotion/default/configuredPromotion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,82 +35,32 @@ export default class configuredPromotion extends AbstractPromotionHandler {
public externalId: string;

public condition(arg: Group | Dish | Order): boolean {
if (findModelInstanceByAttributes(arg) === "Order" && this.concept.includes(arg.concept)) {
if (findModelInstanceByAttributes(arg) === "Order" && stringsInArray(arg.concept, this.concept) ) {
// order not used for configuredPromotion
// Order.populate()
console.log("condition")
return true;
}

if (findModelInstanceByAttributes(arg) === "Dish" && this.concept.includes(arg.concept)) {
if (findModelInstanceByAttributes(arg) === "Dish" && stringsInArray(arg.concept, this.concept)) {
if(this.config.dishes.includes(arg.id)){
return true;
}
}

if (findModelInstanceByAttributes(arg) === "Group" && this.concept.includes(arg.concept)) {
if (findModelInstanceByAttributes(arg) === "Group" && stringsInArray(arg.concept, this.concept)) {
if(this.config.groups.includes(arg.id)){
return true;
}
}

return false
}

public async action(order: Order): Promise<PromotionState> {
console.log(this.config + " action")
// return await PromotionAdapter.applyPromotion(order.id, this.config, this.id)
// return Promise.resolve()

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

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

if(!stringsInArray(orderDish.dish.concept,this.concept)){
continue
}

// TODO: if concept:arrays
// ------------------------------------------ Decimal ------------------------------------------
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(spendDiscount.discountAmount * orderDish.dish.amount ).toNumber();
}

if(this.configDiscount.discountType === "percentage") {
// let discountPrice:number = new Decimal(orderDish.dish.price).mul(orderDish.amount).mul(+spendDiscount.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();

// await OrderDish.update({ id: orderDish.id }, { discountTotal: orderDishDiscountCost, discountType: spendDiscount.discountType}).fetch();
// await OrderDish.update({ id: orderDish.id }, { amount: orderDish.dish.price, discount: discountCost}).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})

// let discountCoverage: Decimal;
// await Order.updateOne({id: orderId}, {total: order.total, discountTotal: discountCoverage.toNumber()});
} else {
throw `User not found in Order, applyDiscount failed`
}
return {
message: `Configured promotion`,
type: "configured-promotion",
state: {}
}
let mass:PromotionState = await configuredPromotion.applyPromotion(order.id, this.config, this.id)
return mass
}

public async displayGroup(group: Group, user?: string | User): Promise<Group[]> {
Expand All @@ -127,7 +77,7 @@ export default class configuredPromotion extends AbstractPromotionHandler {
}
}

public static async applyPromotion(orderId, spendDiscount: IconfigDiscount, promotionId): Promise<void> {
public static async applyPromotion(orderId, spendDiscount: IconfigDiscount, promotionId): Promise<PromotionState> {
const order = await Order.findOne({ id: orderId });

if (order.user && typeof order.user === "string") {
Expand Down Expand Up @@ -178,6 +128,11 @@ export default class configuredPromotion extends AbstractPromotionHandler {
} else {
throw `User not found in Order, applyDiscount failed`;
}
return {
message: `Configured promotion`,
type: "configured-promotion",
state: {}
}
}

}
6 changes: 0 additions & 6 deletions adapters/promotion/default/promotionAdapter.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Order, { PromotionState } from "../../../models/Order";
import AbstractPromotionHandler from "../AbstractPromotion";
import AbstractPromotionAdapter from "../AbstractPromotionAdapter";
import { IconfigDiscount } from "../../../interfaces/ConfigDiscount";
import Promotion from "../../../models/Promotion";
import Group from "../../../models/Group";
import Dish from "../../../models/Dish";
Expand All @@ -20,11 +19,6 @@ export declare class PromotionAdapter extends AbstractPromotionAdapter {
static getPromotionHandlerById(id: string): Promise<AbstractPromotionHandler | undefined>;
getAllConcept(concept: string[]): Promise<AbstractPromotionHandler[]>;
getActivePromotionsIds(): string[];
static clearOfPromotion(orderId: any): Promise<void>;
/**
* @deprecated //TODO: move to configured discount
*/
static applyPromotion(orderId: any, spendDiscount: IconfigDiscount, promotionId: any): Promise<void>;
static initialize(initParams?: {
[key: string]: string | number | boolean;
}): PromotionAdapter;
Expand Down
Loading

0 comments on commit aa7d5b2

Please sign in to comment.