washday-sdk 0.0.125 → 0.0.127

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@ export var DiscountCodeTypes;
4
4
  DiscountCodeTypes["NUMBER"] = "number";
5
5
  DiscountCodeTypes["FREE_DELIVERY"] = "freeDelivery";
6
6
  DiscountCodeTypes["BUY_X_GET_Y"] = "buyXGetY";
7
+ DiscountCodeTypes["FREE_ITEM"] = "freeItem";
7
8
  })(DiscountCodeTypes || (DiscountCodeTypes = {}));
8
9
  export var BuyAndGetConditionsTypes;
9
10
  (function (BuyAndGetConditionsTypes) {
@@ -1,4 +1,4 @@
1
- import { DiscountCodeTypes } from "../../enum";
1
+ import { BuyAndGetConditionsTypes, DiscountCodeTypes } from "../../enum";
2
2
  export const getProductTaxesPercentage = (productObj, store) => {
3
3
  const getTaxValue = (tax, isTaxExempt) => {
4
4
  if (!tax) {
@@ -55,3 +55,117 @@ export const getCreditApplied = (selectedCustomer, orderTotal) => {
55
55
  }
56
56
  return Math.min(customerCredit, orderTotal);
57
57
  };
58
+ export const applyDiscountToProducts = (discountCode, productsArr, isExpress, storeDiscounts = [], selectedCustomer = { discount: 0 }) => {
59
+ var _a, _b;
60
+ if (!discountCode) {
61
+ ///WE NEED TO APPLY DISCOUNT IN CASE WE HAVE AUTOMATIC DISCOUNT APPLIED
62
+ return {
63
+ newOrderProds: productsArr.map((prod) => {
64
+ const discountsToApply = storeDiscounts.filter((discount) => discount.products.includes(prod._id) && discount.isActive);
65
+ const customerDiscount = (selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.discount) / 100 || 0;
66
+ const productPercentageDiscount = discountsToApply.reduce((prev, next) => {
67
+ if (next.type === 'percentage') {
68
+ // for now we just sum percetange type discounts
69
+ return prev + next.value / 100;
70
+ }
71
+ return prev;
72
+ }, 0);
73
+ const prodPrice = isExpress ? prod.expressPrice : prod.price;
74
+ const extraAmountPerUnit = prod.extraAmount / prod.qty || 0;
75
+ const discountAmount = +((productPercentageDiscount + customerDiscount) *
76
+ (prodPrice + extraAmountPerUnit)).toFixed(2);
77
+ return Object.assign(Object.assign({}, prod), { discountAmount: discountAmount });
78
+ }),
79
+ buyAndGetProds: []
80
+ };
81
+ }
82
+ //RESTART PRODUCTS ARR DISCOUNTS
83
+ let newOrderProds = productsArr.map((prod) => {
84
+ return Object.assign(Object.assign({}, prod), { discountAmount: 0 });
85
+ });
86
+ //THIS ARRAY ALSO INCLUDES PRODUCTS OF FREE_ITEM discount code type
87
+ const buyAndGetProds = [];
88
+ if (discountCode && discountCode.type === DiscountCodeTypes.PERCENTAGE) {
89
+ const percentageDiscount = discountCode.value / 100;
90
+ if (discountCode.applyToAllProducts) {
91
+ newOrderProds = newOrderProds.map((prod) => {
92
+ const prodPrice = isExpress ? prod.expressPrice : prod.price;
93
+ const extraAmountPerUnit = prod.extraAmount / prod.qty || 0;
94
+ const discountAmount = +(percentageDiscount * (prodPrice + extraAmountPerUnit)).toFixed(2);
95
+ return Object.assign(Object.assign({}, prod), { discountAmount: discountAmount });
96
+ });
97
+ }
98
+ else {
99
+ for (let discountProduct of discountCode.products) {
100
+ const prodIdx = newOrderProds.findIndex((prod) => prod._id === discountProduct);
101
+ if (prodIdx !== -1) {
102
+ let prodObj = newOrderProds[prodIdx];
103
+ const prodPrice = isExpress ? prodObj.expressPrice : prodObj.price;
104
+ const extraAmountPerUnit = prodObj.extraAmount / prodObj.qty || 0;
105
+ const discountAmount = +(percentageDiscount * (prodPrice + extraAmountPerUnit)).toFixed(2);
106
+ prodObj.discountAmount = discountAmount;
107
+ newOrderProds[prodIdx] = prodObj;
108
+ }
109
+ }
110
+ }
111
+ }
112
+ if (discountCode && discountCode.type === DiscountCodeTypes.NUMBER) {
113
+ const discountAmount = discountCode.value;
114
+ if (!discountCode.applyOnceOnOrder) {
115
+ if (discountCode.applyToAllProducts) {
116
+ newOrderProds = newOrderProds.map((prod) => {
117
+ return Object.assign(Object.assign({}, prod), { discountAmount: discountAmount });
118
+ });
119
+ }
120
+ else {
121
+ for (let discountProduct of discountCode.products) {
122
+ const prodIdx = newOrderProds.findIndex((prod) => prod._id === discountProduct);
123
+ if (prodIdx !== -1) {
124
+ let prodObj = newOrderProds[prodIdx];
125
+ prodObj.discountAmount = discountAmount;
126
+ newOrderProds[prodIdx] = prodObj;
127
+ }
128
+ }
129
+ }
130
+ }
131
+ }
132
+ if (discountCode.buyAndGetConditions && discountCode.type === DiscountCodeTypes.BUY_X_GET_Y) {
133
+ const buyConditions = discountCode.buyAndGetConditions[0].buyConditions;
134
+ const getConditions = discountCode.buyAndGetConditions[0].getConditions;
135
+ const discountType = discountCode.buyAndGetConditions[0].getDiscountType;
136
+ const discountValue = discountCode.buyAndGetConditions[0].discountValue;
137
+ let buyConditionFulfilledCounter = 0;
138
+ for (let prodCondition of buyConditions) {
139
+ const orderProd = productsArr.find((ordProd) => ordProd._id === prodCondition.buyProduct._id);
140
+ //TODO : IN CASE WE HAVE MORE PRODUCT CONDITIONS, THIS WON'T WORK SINCE THIS LINE JUST CHECKS 1 PRODUCT CONDITION
141
+ if (orderProd) {
142
+ let qty = orderProd.qty || orderProd.quantity;
143
+ buyConditionFulfilledCounter = Math.floor(qty / prodCondition.qty);
144
+ }
145
+ }
146
+ if (buyConditionFulfilledCounter) {
147
+ for (let prodCondition of getConditions) {
148
+ //HERE WE SHOULD GET PROD PRICE MAYBE VERIFY IF IS PERCENTAGE AND TAXES ARE INCLUDED IN PRICE, WE SHOULD GET PRODUCT WITHOUT TAXES;
149
+ const prodPrice = isExpress
150
+ ? prodCondition.getProduct.expressPrice
151
+ : prodCondition.getProduct.price;
152
+ const extraAmountPerUnit = ((_a = prodCondition.getProduct) === null || _a === void 0 ? void 0 : _a.extraAmount) / ((_b = prodCondition.getProduct) === null || _b === void 0 ? void 0 : _b.qty) || 0 || 0;
153
+ const percentageDiscount = discountValue / 100;
154
+ const discountAmount = discountType === BuyAndGetConditionsTypes.FREE
155
+ ? prodPrice + extraAmountPerUnit
156
+ : +(percentageDiscount * (prodPrice + extraAmountPerUnit)).toFixed(2);
157
+ buyAndGetProds.push(Object.assign(Object.assign({}, prodCondition.getProduct), { qty: buyConditionFulfilledCounter * prodCondition.qty, quantity: buyConditionFulfilledCounter * prodCondition.qty, discountAmount: discountAmount, isBuyAndGetProduct: true }));
158
+ }
159
+ }
160
+ }
161
+ else if (discountCode.type === DiscountCodeTypes.FREE_ITEM) {
162
+ const freeProductsSettinggs = discountCode.freeProductSetting[0];
163
+ const freeProduct = freeProductsSettinggs.product;
164
+ const prodPrice = isExpress ? freeProduct.expressPrice : freeProduct.price;
165
+ const extraAmountPerUnit = freeProduct.extraAmount / freeProduct.qty || 0 || 0;
166
+ const discountAmount = prodPrice + extraAmountPerUnit;
167
+ buyAndGetProds.push(Object.assign(Object.assign({}, freeProduct), { qty: freeProductsSettinggs.qty, quantity: freeProductsSettinggs.qty, discountAmount: discountAmount, isBuyAndGetProduct: true, isFreeItem: true //we dont' really use this attribute at the moment
168
+ }));
169
+ }
170
+ return { newOrderProds, buyAndGetProds };
171
+ };
@@ -1,2 +1,3 @@
1
1
  import { calculateOrderTotal } from './calculateOrderTotal';
2
- export { calculateOrderTotal };
2
+ import { applyDiscountToProducts } from './helpers';
3
+ export { calculateOrderTotal, applyDiscountToProducts };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "washday-sdk",
3
- "version": "0.0.125",
3
+ "version": "0.0.127",
4
4
  "description": "Washday utilities functions and API",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
package/src/enum/index.ts CHANGED
@@ -2,7 +2,8 @@ export enum DiscountCodeTypes {
2
2
  PERCENTAGE = 'percentage',
3
3
  NUMBER = 'number',
4
4
  FREE_DELIVERY = 'freeDelivery',
5
- BUY_X_GET_Y = 'buyXGetY'
5
+ BUY_X_GET_Y = 'buyXGetY',
6
+ FREE_ITEM = 'freeItem'
6
7
  }
7
8
 
8
9
  export enum BuyAndGetConditionsTypes {
@@ -1,4 +1,4 @@
1
- import { DiscountCodeTypes } from "../../enum";
1
+ import { BuyAndGetConditionsTypes, DiscountCodeTypes } from "../../enum";
2
2
  import { ICustomer } from "../../interfaces/Customer";
3
3
  import { IOrderProduct, ProductLineTotals } from "../../interfaces/Product";
4
4
  import { IStore, ITaxConfig } from "../../interfaces/Store";
@@ -70,4 +70,157 @@ export const getCreditApplied = (selectedCustomer: { customer: ICustomer } | und
70
70
  return Math.min(customerCredit, orderTotal);
71
71
  };
72
72
 
73
+ export const applyDiscountToProducts = (
74
+ discountCode: {
75
+ freeProductSetting: any;
76
+ products: any;
77
+ value: number;
78
+ type: string;
79
+ applyToAllProducts: boolean;
80
+ applyOnceOnOrder: boolean;
81
+ buyAndGetConditions: any,
82
+ },
83
+ productsArr: any,
84
+ isExpress: boolean,
85
+ storeDiscounts: any = [],
86
+ selectedCustomer = { discount: 0 }
87
+ ) => {
88
+ if (!discountCode) {
89
+ ///WE NEED TO APPLY DISCOUNT IN CASE WE HAVE AUTOMATIC DISCOUNT APPLIED
90
+ return {
91
+ newOrderProds: productsArr.map((prod: any) => {
92
+ const discountsToApply = storeDiscounts.filter(
93
+ (discount: any) => discount.products.includes(prod._id) && discount.isActive
94
+ );
95
+ const customerDiscount = selectedCustomer?.discount / 100 || 0;
96
+ const productPercentageDiscount = discountsToApply.reduce((prev: number, next: any) => {
97
+ if (next.type === 'percentage') {
98
+ // for now we just sum percetange type discounts
99
+ return prev + next.value / 100;
100
+ }
101
+ return prev;
102
+ }, 0);
103
+ const prodPrice = isExpress ? prod.expressPrice : prod.price;
104
+ const extraAmountPerUnit = prod.extraAmount / prod.qty || 0;
105
+ const discountAmount = +(
106
+ (productPercentageDiscount + customerDiscount) *
107
+ (prodPrice + extraAmountPerUnit)
108
+ ).toFixed(2);
109
+ return { ...prod, discountAmount: discountAmount };
110
+ }),
111
+ buyAndGetProds: []
112
+ };
113
+ }
114
+
115
+ //RESTART PRODUCTS ARR DISCOUNTS
116
+ let newOrderProds = productsArr.map((prod: any) => {
117
+ return { ...prod, discountAmount: 0 };
118
+ });
73
119
 
120
+ //THIS ARRAY ALSO INCLUDES PRODUCTS OF FREE_ITEM discount code type
121
+ const buyAndGetProds = [];
122
+ if (discountCode && discountCode.type === DiscountCodeTypes.PERCENTAGE) {
123
+ const percentageDiscount = discountCode.value / 100;
124
+ if (discountCode.applyToAllProducts) {
125
+ newOrderProds = newOrderProds.map((prod: any) => {
126
+ const prodPrice = isExpress ? prod.expressPrice : prod.price;
127
+ const extraAmountPerUnit = prod.extraAmount / prod.qty || 0;
128
+ const discountAmount = +(percentageDiscount * (prodPrice + extraAmountPerUnit)).toFixed(2);
129
+ return {
130
+ ...prod,
131
+ discountAmount: discountAmount
132
+ };
133
+ });
134
+ } else {
135
+ for (let discountProduct of discountCode.products) {
136
+ const prodIdx = newOrderProds.findIndex((prod: any) => prod._id === discountProduct);
137
+ if (prodIdx !== -1) {
138
+ let prodObj = newOrderProds[prodIdx];
139
+ const prodPrice = isExpress ? prodObj.expressPrice : prodObj.price;
140
+ const extraAmountPerUnit = prodObj.extraAmount / prodObj.qty || 0;
141
+ const discountAmount = +(percentageDiscount * (prodPrice + extraAmountPerUnit)).toFixed(
142
+ 2
143
+ );
144
+ prodObj.discountAmount = discountAmount;
145
+ newOrderProds[prodIdx] = prodObj;
146
+ }
147
+ }
148
+ }
149
+ }
150
+ if (discountCode && discountCode.type === DiscountCodeTypes.NUMBER) {
151
+ const discountAmount = discountCode.value;
152
+ if (!discountCode.applyOnceOnOrder) {
153
+ if (discountCode.applyToAllProducts) {
154
+ newOrderProds = newOrderProds.map((prod: any) => {
155
+ return {
156
+ ...prod,
157
+ discountAmount: discountAmount
158
+ };
159
+ });
160
+ } else {
161
+ for (let discountProduct of discountCode.products) {
162
+ const prodIdx = newOrderProds.findIndex((prod: any) => prod._id === discountProduct);
163
+ if (prodIdx !== -1) {
164
+ let prodObj = newOrderProds[prodIdx];
165
+ prodObj.discountAmount = discountAmount;
166
+ newOrderProds[prodIdx] = prodObj;
167
+ }
168
+ }
169
+ }
170
+ }
171
+ }
172
+ if (discountCode.buyAndGetConditions && discountCode.type === DiscountCodeTypes.BUY_X_GET_Y) {
173
+ const buyConditions = discountCode.buyAndGetConditions[0].buyConditions;
174
+ const getConditions = discountCode.buyAndGetConditions[0].getConditions;
175
+ const discountType = discountCode.buyAndGetConditions[0].getDiscountType;
176
+ const discountValue = discountCode.buyAndGetConditions[0].discountValue;
177
+ let buyConditionFulfilledCounter = 0;
178
+
179
+ for (let prodCondition of buyConditions) {
180
+ const orderProd = productsArr.find((ordProd: any) => ordProd._id === prodCondition.buyProduct._id);
181
+ //TODO : IN CASE WE HAVE MORE PRODUCT CONDITIONS, THIS WON'T WORK SINCE THIS LINE JUST CHECKS 1 PRODUCT CONDITION
182
+ if (orderProd) {
183
+ let qty = orderProd.qty || orderProd.quantity;
184
+ buyConditionFulfilledCounter = Math.floor(qty / prodCondition.qty);
185
+ }
186
+ }
187
+
188
+ if (buyConditionFulfilledCounter) {
189
+ for (let prodCondition of getConditions) {
190
+ //HERE WE SHOULD GET PROD PRICE MAYBE VERIFY IF IS PERCENTAGE AND TAXES ARE INCLUDED IN PRICE, WE SHOULD GET PRODUCT WITHOUT TAXES;
191
+ const prodPrice = isExpress
192
+ ? prodCondition.getProduct.expressPrice
193
+ : prodCondition.getProduct.price;
194
+ const extraAmountPerUnit =
195
+ prodCondition.getProduct?.extraAmount / prodCondition.getProduct?.qty || 0 || 0;
196
+ const percentageDiscount = discountValue / 100;
197
+ const discountAmount =
198
+ discountType === BuyAndGetConditionsTypes.FREE
199
+ ? prodPrice + extraAmountPerUnit
200
+ : +(percentageDiscount * (prodPrice + extraAmountPerUnit)).toFixed(2);
201
+ buyAndGetProds.push({
202
+ ...prodCondition.getProduct,
203
+ qty: buyConditionFulfilledCounter * prodCondition.qty,
204
+ quantity: buyConditionFulfilledCounter * prodCondition.qty,
205
+ discountAmount: discountAmount,
206
+ isBuyAndGetProduct: true
207
+ });
208
+ }
209
+ }
210
+ } else if (discountCode.type === DiscountCodeTypes.FREE_ITEM) {
211
+ const freeProductsSettinggs = discountCode.freeProductSetting[0];
212
+ const freeProduct = freeProductsSettinggs.product;
213
+ const prodPrice = isExpress ? freeProduct.expressPrice : freeProduct.price;
214
+ const extraAmountPerUnit = freeProduct.extraAmount / freeProduct.qty || 0 || 0;
215
+ const discountAmount = prodPrice + extraAmountPerUnit;
216
+ buyAndGetProds.push({
217
+ ...freeProduct,
218
+ qty: freeProductsSettinggs.qty,
219
+ quantity: freeProductsSettinggs.qty,
220
+ discountAmount: discountAmount,
221
+ isBuyAndGetProduct: true, //we might use it in this kind of discounts?
222
+ isFreeItem: true //we dont' really use this attribute at the moment
223
+ });
224
+ }
225
+ return { newOrderProds, buyAndGetProds };
226
+ };
@@ -1,5 +1,7 @@
1
1
  import { calculateOrderTotal } from './calculateOrderTotal';
2
+ import { applyDiscountToProducts } from './helpers';
2
3
 
3
4
  export {
4
- calculateOrderTotal
5
+ calculateOrderTotal,
6
+ applyDiscountToProducts
5
7
  }