washday-sdk 0.0.124 → 0.0.126
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.
- package/dist/api/discounts/get.js +16 -1
- package/dist/api/index.js +2 -1
- package/dist/enum/index.js +1 -0
- package/dist/utils/orders/helpers.js +115 -1
- package/dist/utils/orders/index.js +2 -1
- package/package.json +1 -1
- package/src/api/discounts/get.ts +14 -1
- package/src/api/index.ts +2 -1
- package/src/enum/index.ts +2 -1
- package/src/interfaces/Api.ts +2 -1
- package/src/utils/orders/helpers.ts +154 -1
- package/src/utils/orders/index.ts +3 -1
|
@@ -11,6 +11,7 @@ import axiosInstance from "../axiosInstance";
|
|
|
11
11
|
const GET_SET_DISCOUNT_CODES = 'api/v2/discountCodes';
|
|
12
12
|
const GET_SET_AUTOMATIC_DISCOUNTS = 'api/discounts';
|
|
13
13
|
const GET_AVAILABLE_AUTOMATIC_DISCOUNTS = 'api/availableDiscounts';
|
|
14
|
+
const GET_DISCOUNT_CODES_CUSTOMER_APP = 'api/v2/washdayapp/discountCodes';
|
|
14
15
|
export const getDiscountCodes = function (storeId, params) {
|
|
15
16
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
17
|
try {
|
|
@@ -84,7 +85,21 @@ export const verifyDiscountCode = function (storeId, code) {
|
|
|
84
85
|
return yield axiosInstance.get(`${GET_SET_DISCOUNT_CODES}/${storeId}/${code}/verify`, config);
|
|
85
86
|
}
|
|
86
87
|
catch (error) {
|
|
87
|
-
console.error('Error fetching
|
|
88
|
+
console.error('Error fetching verifyDiscountCode:', error);
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
export const verifyDiscountCodeCustomersApp = function (storeId, code) {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
try {
|
|
96
|
+
const config = {
|
|
97
|
+
headers: { Authorization: `Bearer ${this.apiToken}` }
|
|
98
|
+
};
|
|
99
|
+
return yield axiosInstance.get(`${GET_DISCOUNT_CODES_CUSTOMER_APP}/${storeId}/${code}/verify`, config);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
console.error('Error fetching verifyDiscountCodeCustomersApp:', error);
|
|
88
103
|
throw error;
|
|
89
104
|
}
|
|
90
105
|
});
|
package/dist/api/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import { getCompanyById } from "./companies/get";
|
|
|
6
6
|
import { updateCFDIOrgLogo, updateCompanyTaxInfoCertificates } from "./companies/post";
|
|
7
7
|
import { updateCompanyById, updateCompanyLogoById, updateCompanyTaxInfoById } from "./companies/put";
|
|
8
8
|
import { getCountries } from "./countries/get";
|
|
9
|
-
import { getAutomaticDiscountById, getAutomaticDiscounts, getAvailableAutomaticDiscounts, getDiscountCodeById, getDiscountCodes, verifyDiscountCode } from './discounts/get';
|
|
9
|
+
import { getAutomaticDiscountById, getAutomaticDiscounts, getAvailableAutomaticDiscounts, getDiscountCodeById, getDiscountCodes, verifyDiscountCode, verifyDiscountCodeCustomersApp } from './discounts/get';
|
|
10
10
|
import { createAutomaticDiscount, createDiscountCode } from "./discounts/post";
|
|
11
11
|
import { deleteAutomaticDiscountById, deleteDiscountCodeById } from "./discounts/put";
|
|
12
12
|
import { deleteStoreStaffById } from "./staff/delete";
|
|
@@ -200,6 +200,7 @@ const WashdayClient = function WashdayClient(apiToken) {
|
|
|
200
200
|
getDiscountCodes: getDiscountCodes,
|
|
201
201
|
getDiscountCodeById: getDiscountCodeById,
|
|
202
202
|
verifyDiscountCode: verifyDiscountCode,
|
|
203
|
+
verifyDiscountCodeCustomersApp: verifyDiscountCodeCustomersApp,
|
|
203
204
|
createDiscountCode: createDiscountCode,
|
|
204
205
|
deleteDiscountCodeById: deleteDiscountCodeById,
|
|
205
206
|
});
|
package/dist/enum/index.js
CHANGED
|
@@ -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
|
+
};
|
package/package.json
CHANGED
package/src/api/discounts/get.ts
CHANGED
|
@@ -4,6 +4,7 @@ import axiosInstance from "../axiosInstance";
|
|
|
4
4
|
const GET_SET_DISCOUNT_CODES = 'api/v2/discountCodes';
|
|
5
5
|
const GET_SET_AUTOMATIC_DISCOUNTS = 'api/discounts';
|
|
6
6
|
const GET_AVAILABLE_AUTOMATIC_DISCOUNTS = 'api/availableDiscounts';
|
|
7
|
+
const GET_DISCOUNT_CODES_CUSTOMER_APP = 'api/v2/washdayapp/discountCodes';
|
|
7
8
|
|
|
8
9
|
export const getDiscountCodes = async function (this: WashdayClientInstance, storeId: string, params?: string): Promise<any> {
|
|
9
10
|
try {
|
|
@@ -68,7 +69,19 @@ export const verifyDiscountCode = async function (this: WashdayClientInstance, s
|
|
|
68
69
|
};
|
|
69
70
|
return await axiosInstance.get(`${GET_SET_DISCOUNT_CODES}/${storeId}/${code}/verify`, config);
|
|
70
71
|
} catch (error) {
|
|
71
|
-
console.error('Error fetching
|
|
72
|
+
console.error('Error fetching verifyDiscountCode:', error);
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const verifyDiscountCodeCustomersApp = async function (this: WashdayClientInstance, storeId: string, code: string,): Promise<any> {
|
|
78
|
+
try {
|
|
79
|
+
const config = {
|
|
80
|
+
headers: { Authorization: `Bearer ${this.apiToken}` }
|
|
81
|
+
};
|
|
82
|
+
return await axiosInstance.get(`${GET_DISCOUNT_CODES_CUSTOMER_APP}/${storeId}/${code}/verify`, config);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.error('Error fetching verifyDiscountCodeCustomersApp:', error);
|
|
72
85
|
throw error;
|
|
73
86
|
}
|
|
74
87
|
};
|
package/src/api/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { getCompanyById } from "./companies/get";
|
|
|
7
7
|
import { updateCFDIOrgLogo, updateCompanyTaxInfoCertificates } from "./companies/post";
|
|
8
8
|
import { updateCompanyById, updateCompanyLogoById, updateCompanyTaxInfoById } from "./companies/put";
|
|
9
9
|
import { getCountries } from "./countries/get";
|
|
10
|
-
import { getAutomaticDiscountById, getAutomaticDiscounts, getAvailableAutomaticDiscounts, getDiscountCodeById, getDiscountCodes, verifyDiscountCode } from './discounts/get';
|
|
10
|
+
import { getAutomaticDiscountById, getAutomaticDiscounts, getAvailableAutomaticDiscounts, getDiscountCodeById, getDiscountCodes, verifyDiscountCode, verifyDiscountCodeCustomersApp } from './discounts/get';
|
|
11
11
|
import { createAutomaticDiscount, createDiscountCode } from "./discounts/post";
|
|
12
12
|
import { deleteAutomaticDiscountById, deleteDiscountCodeById } from "./discounts/put";
|
|
13
13
|
import { deleteStoreStaffById } from "./staff/delete";
|
|
@@ -206,6 +206,7 @@ const WashdayClient: WashdayClientConstructor = function WashdayClient(this: Was
|
|
|
206
206
|
getDiscountCodes: getDiscountCodes,
|
|
207
207
|
getDiscountCodeById: getDiscountCodeById,
|
|
208
208
|
verifyDiscountCode: verifyDiscountCode,
|
|
209
|
+
verifyDiscountCodeCustomersApp: verifyDiscountCodeCustomersApp,
|
|
209
210
|
createDiscountCode: createDiscountCode,
|
|
210
211
|
deleteDiscountCodeById: deleteDiscountCodeById,
|
|
211
212
|
});
|
package/src/enum/index.ts
CHANGED
package/src/interfaces/Api.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { getCompanyById } from "../api/companies/get";
|
|
|
6
6
|
import { updateCFDIOrgLogo, updateCompanyTaxInfoCertificates } from "../api/companies/post";
|
|
7
7
|
import { updateCompanyById, updateCompanyLogoById, updateCompanyTaxInfoById } from "../api/companies/put";
|
|
8
8
|
import { getCountries } from "../api/countries/get";
|
|
9
|
-
import { getAutomaticDiscountById, getAutomaticDiscounts, getAvailableAutomaticDiscounts, getDiscountCodeById, getDiscountCodes, verifyDiscountCode } from '../api/discounts/get';
|
|
9
|
+
import { getAutomaticDiscountById, getAutomaticDiscounts, getAvailableAutomaticDiscounts, getDiscountCodeById, getDiscountCodes, verifyDiscountCode, verifyDiscountCodeCustomersApp } from '../api/discounts/get';
|
|
10
10
|
import { createAutomaticDiscount, createDiscountCode } from "../api/discounts/post";
|
|
11
11
|
import { deleteAutomaticDiscountById, deleteDiscountCodeById } from "../api/discounts/put";
|
|
12
12
|
import { deleteStoreStaffById } from "../api/staff/delete";
|
|
@@ -192,6 +192,7 @@ export interface WashdayClientInstance {
|
|
|
192
192
|
getDiscountCodes: typeof getDiscountCodes;
|
|
193
193
|
getDiscountCodeById: typeof getDiscountCodeById;
|
|
194
194
|
verifyDiscountCode: typeof verifyDiscountCode;
|
|
195
|
+
verifyDiscountCodeCustomersApp: typeof verifyDiscountCodeCustomersApp;
|
|
195
196
|
createDiscountCode: typeof createDiscountCode;
|
|
196
197
|
deleteDiscountCodeById: typeof deleteDiscountCodeById;
|
|
197
198
|
};
|
|
@@ -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
|
+
};
|