washday-sdk 0.0.8 → 0.0.10
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/babel.config.js +8 -0
- package/dist/api/axiosInstance.js +42 -42
- package/dist/api/customers/get.js +48 -48
- package/dist/api/customers/index.js +1 -1
- package/dist/api/index.js +12 -12
- package/dist/enum/index.js +15 -15
- package/dist/index.js +33 -33
- package/dist/interfaces/Api.js +2 -2
- package/dist/interfaces/Company.js +1 -1
- package/dist/interfaces/Customer.js +2 -2
- package/dist/interfaces/Order.js +2 -2
- package/dist/interfaces/Permission.js +2 -2
- package/dist/interfaces/Product.js +2 -2
- package/dist/interfaces/Section.js +2 -2
- package/dist/interfaces/Store.js +2 -2
- package/dist/interfaces/StoreImage.js +2 -2
- package/dist/interfaces/User.js +2 -2
- package/dist/utils/index.js +17 -17
- package/dist/utils/orders/calculateOrderTotal.js +33 -33
- package/dist/utils/orders/calculateTotalTaxesIncluded.js +65 -65
- package/dist/utils/orders/calculateTotalTaxesOverPrice.js +74 -74
- package/dist/utils/orders/helpers.js +64 -64
- package/dist/utils/orders/index.js +5 -5
- package/jest.config.js +0 -0
- package/package.json +28 -23
- package/src/api/axiosInstance.ts +45 -45
- package/src/api/customers/get.ts +32 -32
- package/src/api/index.ts +26 -21
- package/src/api/stores/get.ts +33 -0
- package/src/enum/index.ts +10 -10
- package/src/index.ts +4 -4
- package/src/interfaces/Api.ts +2 -2
- package/src/interfaces/Customer.ts +51 -51
- package/src/interfaces/Order.ts +95 -95
- package/src/interfaces/Permission.ts +24 -24
- package/src/interfaces/Product.ts +58 -58
- package/src/interfaces/Section.ts +14 -14
- package/src/interfaces/Store.ts +350 -350
- package/src/interfaces/StoreImage.ts +9 -9
- package/src/interfaces/User.ts +42 -42
- package/src/utils/index.ts +1 -1
- package/src/utils/orders/calculateOrderTotal.test.js +399 -0
- package/src/utils/orders/calculateOrderTotal.ts +60 -60
- package/src/utils/orders/calculateTotalTaxesIncluded.ts +75 -75
- package/src/utils/orders/calculateTotalTaxesOverPrice.ts +82 -82
- package/src/utils/orders/helpers.ts +73 -73
- package/src/utils/orders/index.ts +4 -4
- package/tsconfig.json +9 -9
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateTotalTaxesOverPrice = void 0;
|
|
4
|
-
const enum_1 = require("../../enum");
|
|
5
|
-
const helpers_1 = require("./helpers");
|
|
6
|
-
const calculateTotalTaxesOverPrice = (order, selectedCustomer, storeSettings, storeDiscounts, appliedOrderDiscounts, discountCodeObj) => {
|
|
7
|
-
const productTableImports = [...order.products, ...order.buyAndGetProducts].map((current) => {
|
|
8
|
-
var _a;
|
|
9
|
-
let discPercentageInteger = 0;
|
|
10
|
-
let productPercentageDiscount = 0;
|
|
11
|
-
let customerDiscount = 0;
|
|
12
|
-
let qty = current.qty;
|
|
13
|
-
// extraAmount es en general sin importar el qty, entonces aqui lo repartimos por igual
|
|
14
|
-
const extraAmountPerUnit = current.extraAmount / qty;
|
|
15
|
-
const prodPrice = (order.express ? current.expressPrice : current.price) + extraAmountPerUnit;
|
|
16
|
-
if (!order.discountCode) {
|
|
17
|
-
//IF ORDER HAS A DISCOUNT CODE WE DON'T APPLY ANY OTHER DISCOUNT (AUTOMATICALLY OR CUSTOMER DISCOUNT)
|
|
18
|
-
//THIS WILL GET THE DISCOUNTS OF THE CURRENT PRODUCT
|
|
19
|
-
// get store discounts if product applies and customer discount
|
|
20
|
-
const discountsToApply = storeDiscounts.filter((discount) => discount.products.includes(current._id) && discount.isActive);
|
|
21
|
-
customerDiscount = ((_a = selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.customer) === null || _a === void 0 ? void 0 : _a.discount) || 0;
|
|
22
|
-
productPercentageDiscount = discountsToApply.reduce((prev, next) => {
|
|
23
|
-
if (next.type === 'percentage') {
|
|
24
|
-
// for now we just sum percetange type discounts
|
|
25
|
-
return prev + next.value;
|
|
26
|
-
}
|
|
27
|
-
return prev;
|
|
28
|
-
}, 0);
|
|
29
|
-
discPercentageInteger = +((productPercentageDiscount + customerDiscount) / 100).toFixed(2);
|
|
30
|
-
discountsToApply.forEach((discount) => (appliedOrderDiscounts[discount._id] = discount)); // it will do this for every product, if two products has the same discount it will just overwrite
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
if (discountCodeObj.type === enum_1.DiscountCodeTypes.PERCENTAGE) {
|
|
34
|
-
discPercentageInteger = +(discountCodeObj.value / 100).toFixed(2);
|
|
35
|
-
if (!discountCodeObj.applyToAllProducts) {
|
|
36
|
-
discPercentageInteger = discountCodeObj.products.includes(current._id)
|
|
37
|
-
? +(discountCodeObj.value / 100).toFixed(2)
|
|
38
|
-
: 0;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (discountCodeObj.type === enum_1.DiscountCodeTypes.BUY_X_GET_Y) {
|
|
42
|
-
const discountType = discountCodeObj.buyAndGetConditions[0].getDiscountType;
|
|
43
|
-
const discountValue = discountCodeObj.buyAndGetConditions[0].discountValue;
|
|
44
|
-
if (discountType === enum_1.BuyAndGetConditionsTypes.PERCENTAGE && current.isBuyAndGetProduct) {
|
|
45
|
-
discPercentageInteger = +(discountValue / 100).toFixed(2);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
//GET DISCOUNT AMOUNT (IN CASE discPercentageInteger is 0 it could be a discount of a integer number)
|
|
50
|
-
const discountAmountPerUnit = discPercentageInteger
|
|
51
|
-
? prodPrice * discPercentageInteger
|
|
52
|
-
: (current.discountAmount || 0);
|
|
53
|
-
//GET PRODUCT PRICE WITH DISCOUNT
|
|
54
|
-
const prodPriceWithDiscountPerUnit = prodPrice - discountAmountPerUnit;
|
|
55
|
-
//GET PRODUCT TAXES APPLIED TO FINAL PRICE PER UNIT
|
|
56
|
-
const taxPercentage = (0, helpers_1.getProductTaxesPercentage)(current, storeSettings);
|
|
57
|
-
const taxPercentageInteger = taxPercentage / 100;
|
|
58
|
-
const prodPriceWithoutTaxesPerUnit = prodPriceWithDiscountPerUnit;
|
|
59
|
-
const taxesAmountPerUnit = prodPriceWithoutTaxesPerUnit * taxPercentageInteger;
|
|
60
|
-
const totalTaxesApplied = +(taxesAmountPerUnit * qty).toFixed(2);
|
|
61
|
-
let lineDiscount = qty * discountAmountPerUnit;
|
|
62
|
-
let prodLinePriceWithDiscount = +(qty * prodPriceWithDiscountPerUnit).toFixed(2);
|
|
63
|
-
return {
|
|
64
|
-
product: current,
|
|
65
|
-
qty: qty,
|
|
66
|
-
productLineImportTotal: prodPrice * qty, //this is line without discount
|
|
67
|
-
productLineTotalWithDiscount: prodLinePriceWithDiscount,
|
|
68
|
-
productLineTaxesTotal: totalTaxesApplied,
|
|
69
|
-
lineDiscountAmount: lineDiscount
|
|
70
|
-
};
|
|
71
|
-
});
|
|
72
|
-
return productTableImports;
|
|
73
|
-
};
|
|
74
|
-
exports.calculateTotalTaxesOverPrice = calculateTotalTaxesOverPrice;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateTotalTaxesOverPrice = void 0;
|
|
4
|
+
const enum_1 = require("../../enum");
|
|
5
|
+
const helpers_1 = require("./helpers");
|
|
6
|
+
const calculateTotalTaxesOverPrice = (order, selectedCustomer, storeSettings, storeDiscounts, appliedOrderDiscounts, discountCodeObj) => {
|
|
7
|
+
const productTableImports = [...order.products, ...order.buyAndGetProducts].map((current) => {
|
|
8
|
+
var _a;
|
|
9
|
+
let discPercentageInteger = 0;
|
|
10
|
+
let productPercentageDiscount = 0;
|
|
11
|
+
let customerDiscount = 0;
|
|
12
|
+
let qty = current.qty;
|
|
13
|
+
// extraAmount es en general sin importar el qty, entonces aqui lo repartimos por igual
|
|
14
|
+
const extraAmountPerUnit = current.extraAmount / qty;
|
|
15
|
+
const prodPrice = (order.express ? current.expressPrice : current.price) + extraAmountPerUnit;
|
|
16
|
+
if (!order.discountCode) {
|
|
17
|
+
//IF ORDER HAS A DISCOUNT CODE WE DON'T APPLY ANY OTHER DISCOUNT (AUTOMATICALLY OR CUSTOMER DISCOUNT)
|
|
18
|
+
//THIS WILL GET THE DISCOUNTS OF THE CURRENT PRODUCT
|
|
19
|
+
// get store discounts if product applies and customer discount
|
|
20
|
+
const discountsToApply = storeDiscounts.filter((discount) => discount.products.includes(current._id) && discount.isActive);
|
|
21
|
+
customerDiscount = ((_a = selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.customer) === null || _a === void 0 ? void 0 : _a.discount) || 0;
|
|
22
|
+
productPercentageDiscount = discountsToApply.reduce((prev, next) => {
|
|
23
|
+
if (next.type === 'percentage') {
|
|
24
|
+
// for now we just sum percetange type discounts
|
|
25
|
+
return prev + next.value;
|
|
26
|
+
}
|
|
27
|
+
return prev;
|
|
28
|
+
}, 0);
|
|
29
|
+
discPercentageInteger = +((productPercentageDiscount + customerDiscount) / 100).toFixed(2);
|
|
30
|
+
discountsToApply.forEach((discount) => (appliedOrderDiscounts[discount._id] = discount)); // it will do this for every product, if two products has the same discount it will just overwrite
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
if (discountCodeObj.type === enum_1.DiscountCodeTypes.PERCENTAGE) {
|
|
34
|
+
discPercentageInteger = +(discountCodeObj.value / 100).toFixed(2);
|
|
35
|
+
if (!discountCodeObj.applyToAllProducts) {
|
|
36
|
+
discPercentageInteger = discountCodeObj.products.includes(current._id)
|
|
37
|
+
? +(discountCodeObj.value / 100).toFixed(2)
|
|
38
|
+
: 0;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (discountCodeObj.type === enum_1.DiscountCodeTypes.BUY_X_GET_Y) {
|
|
42
|
+
const discountType = discountCodeObj.buyAndGetConditions[0].getDiscountType;
|
|
43
|
+
const discountValue = discountCodeObj.buyAndGetConditions[0].discountValue;
|
|
44
|
+
if (discountType === enum_1.BuyAndGetConditionsTypes.PERCENTAGE && current.isBuyAndGetProduct) {
|
|
45
|
+
discPercentageInteger = +(discountValue / 100).toFixed(2);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
//GET DISCOUNT AMOUNT (IN CASE discPercentageInteger is 0 it could be a discount of a integer number)
|
|
50
|
+
const discountAmountPerUnit = discPercentageInteger
|
|
51
|
+
? prodPrice * discPercentageInteger
|
|
52
|
+
: (current.discountAmount || 0);
|
|
53
|
+
//GET PRODUCT PRICE WITH DISCOUNT
|
|
54
|
+
const prodPriceWithDiscountPerUnit = prodPrice - discountAmountPerUnit;
|
|
55
|
+
//GET PRODUCT TAXES APPLIED TO FINAL PRICE PER UNIT
|
|
56
|
+
const taxPercentage = (0, helpers_1.getProductTaxesPercentage)(current, storeSettings);
|
|
57
|
+
const taxPercentageInteger = taxPercentage / 100;
|
|
58
|
+
const prodPriceWithoutTaxesPerUnit = prodPriceWithDiscountPerUnit;
|
|
59
|
+
const taxesAmountPerUnit = prodPriceWithoutTaxesPerUnit * taxPercentageInteger;
|
|
60
|
+
const totalTaxesApplied = +(taxesAmountPerUnit * qty).toFixed(2);
|
|
61
|
+
let lineDiscount = qty * discountAmountPerUnit;
|
|
62
|
+
let prodLinePriceWithDiscount = +(qty * prodPriceWithDiscountPerUnit).toFixed(2);
|
|
63
|
+
return {
|
|
64
|
+
product: current,
|
|
65
|
+
qty: qty,
|
|
66
|
+
productLineImportTotal: prodPrice * qty, //this is line without discount
|
|
67
|
+
productLineTotalWithDiscount: prodLinePriceWithDiscount,
|
|
68
|
+
productLineTaxesTotal: totalTaxesApplied,
|
|
69
|
+
lineDiscountAmount: lineDiscount
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
return productTableImports;
|
|
73
|
+
};
|
|
74
|
+
exports.calculateTotalTaxesOverPrice = calculateTotalTaxesOverPrice;
|
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getCreditApplied = exports.getShippingCost = exports.getProductLineTotals = exports.getProductTaxesPercentage = void 0;
|
|
4
|
-
const enum_1 = require("../../enum");
|
|
5
|
-
const getProductTaxesPercentage = (productObj, store) => {
|
|
6
|
-
const getTaxValue = (tax, isTaxExempt) => {
|
|
7
|
-
if (!tax) {
|
|
8
|
-
// If no store tax configured, always return 0
|
|
9
|
-
return 0;
|
|
10
|
-
}
|
|
11
|
-
if (isTaxExempt) {
|
|
12
|
-
// If store tax is configured and taxExempt is true, return 0
|
|
13
|
-
return 0;
|
|
14
|
-
}
|
|
15
|
-
// If store tax configured and taxExempt is false, return store tax
|
|
16
|
-
return tax.value;
|
|
17
|
-
};
|
|
18
|
-
const tax1 = getTaxValue(store.taxOne, !productObj.taxExemptOne);
|
|
19
|
-
const tax2 = getTaxValue(store.taxTwo, !productObj.taxExemptTwo);
|
|
20
|
-
const tax3 = getTaxValue(store.taxThree, !productObj.taxExemptThree);
|
|
21
|
-
return tax1 + tax2 + tax3;
|
|
22
|
-
};
|
|
23
|
-
exports.getProductTaxesPercentage = getProductTaxesPercentage;
|
|
24
|
-
const getProductLineTotals = (productLinesTotals) => {
|
|
25
|
-
const totalQuantity = +productLinesTotals.reduce((acum, line) => acum + line.qty, 0).toFixed(2);
|
|
26
|
-
const totalImportWithDiscount = +productLinesTotals
|
|
27
|
-
.reduce((acum, line) => acum + line.productLineTotalWithDiscount, 0)
|
|
28
|
-
.toFixed(2);
|
|
29
|
-
const totalImportWithoutDiscount = +productLinesTotals
|
|
30
|
-
.reduce((acum, line) => acum + line.productLineImportTotal, 0)
|
|
31
|
-
.toFixed(2);
|
|
32
|
-
const totalImporTaxes = +productLinesTotals
|
|
33
|
-
.reduce((acum, line) => acum + line.productLineTaxesTotal, 0)
|
|
34
|
-
.toFixed(2);
|
|
35
|
-
const totalDiscountAmount = +productLinesTotals
|
|
36
|
-
.reduce((acum, line) => acum + line.lineDiscountAmount, 0)
|
|
37
|
-
.toFixed(2);
|
|
38
|
-
return {
|
|
39
|
-
totalQuantity,
|
|
40
|
-
totalImportWithDiscount,
|
|
41
|
-
totalImportWithoutDiscount,
|
|
42
|
-
totalImporTaxes,
|
|
43
|
-
totalDiscountAmount
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
exports.getProductLineTotals = getProductLineTotals;
|
|
47
|
-
const getShippingCost = (discountObject, requireShippingService, store) => {
|
|
48
|
-
var _a, _b;
|
|
49
|
-
const shippingCost = requireShippingService ? (_b = (_a = store === null || store === void 0 ? void 0 : store.orderPageConfig) === null || _a === void 0 ? void 0 : _a.shippingServiceCost) !== null && _b !== void 0 ? _b : 0 : 0;
|
|
50
|
-
if ((discountObject === null || discountObject === void 0 ? void 0 : discountObject.type) === enum_1.DiscountCodeTypes.FREE_DELIVERY) {
|
|
51
|
-
return 0;
|
|
52
|
-
}
|
|
53
|
-
return shippingCost;
|
|
54
|
-
};
|
|
55
|
-
exports.getShippingCost = getShippingCost;
|
|
56
|
-
const getCreditApplied = (selectedCustomer, orderTotal) => {
|
|
57
|
-
var _a, _b;
|
|
58
|
-
const customerCredit = (_b = (_a = selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.customer) === null || _a === void 0 ? void 0 : _a.credit) !== null && _b !== void 0 ? _b : 0;
|
|
59
|
-
if (customerCredit === 0 || !selectedCustomer) {
|
|
60
|
-
return 0;
|
|
61
|
-
}
|
|
62
|
-
return Math.min(customerCredit, orderTotal);
|
|
63
|
-
};
|
|
64
|
-
exports.getCreditApplied = getCreditApplied;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCreditApplied = exports.getShippingCost = exports.getProductLineTotals = exports.getProductTaxesPercentage = void 0;
|
|
4
|
+
const enum_1 = require("../../enum");
|
|
5
|
+
const getProductTaxesPercentage = (productObj, store) => {
|
|
6
|
+
const getTaxValue = (tax, isTaxExempt) => {
|
|
7
|
+
if (!tax) {
|
|
8
|
+
// If no store tax configured, always return 0
|
|
9
|
+
return 0;
|
|
10
|
+
}
|
|
11
|
+
if (isTaxExempt) {
|
|
12
|
+
// If store tax is configured and taxExempt is true, return 0
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
// If store tax configured and taxExempt is false, return store tax
|
|
16
|
+
return tax.value;
|
|
17
|
+
};
|
|
18
|
+
const tax1 = getTaxValue(store.taxOne, !productObj.taxExemptOne);
|
|
19
|
+
const tax2 = getTaxValue(store.taxTwo, !productObj.taxExemptTwo);
|
|
20
|
+
const tax3 = getTaxValue(store.taxThree, !productObj.taxExemptThree);
|
|
21
|
+
return tax1 + tax2 + tax3;
|
|
22
|
+
};
|
|
23
|
+
exports.getProductTaxesPercentage = getProductTaxesPercentage;
|
|
24
|
+
const getProductLineTotals = (productLinesTotals) => {
|
|
25
|
+
const totalQuantity = +productLinesTotals.reduce((acum, line) => acum + line.qty, 0).toFixed(2);
|
|
26
|
+
const totalImportWithDiscount = +productLinesTotals
|
|
27
|
+
.reduce((acum, line) => acum + line.productLineTotalWithDiscount, 0)
|
|
28
|
+
.toFixed(2);
|
|
29
|
+
const totalImportWithoutDiscount = +productLinesTotals
|
|
30
|
+
.reduce((acum, line) => acum + line.productLineImportTotal, 0)
|
|
31
|
+
.toFixed(2);
|
|
32
|
+
const totalImporTaxes = +productLinesTotals
|
|
33
|
+
.reduce((acum, line) => acum + line.productLineTaxesTotal, 0)
|
|
34
|
+
.toFixed(2);
|
|
35
|
+
const totalDiscountAmount = +productLinesTotals
|
|
36
|
+
.reduce((acum, line) => acum + line.lineDiscountAmount, 0)
|
|
37
|
+
.toFixed(2);
|
|
38
|
+
return {
|
|
39
|
+
totalQuantity,
|
|
40
|
+
totalImportWithDiscount,
|
|
41
|
+
totalImportWithoutDiscount,
|
|
42
|
+
totalImporTaxes,
|
|
43
|
+
totalDiscountAmount
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
exports.getProductLineTotals = getProductLineTotals;
|
|
47
|
+
const getShippingCost = (discountObject, requireShippingService, store) => {
|
|
48
|
+
var _a, _b;
|
|
49
|
+
const shippingCost = requireShippingService ? (_b = (_a = store === null || store === void 0 ? void 0 : store.orderPageConfig) === null || _a === void 0 ? void 0 : _a.shippingServiceCost) !== null && _b !== void 0 ? _b : 0 : 0;
|
|
50
|
+
if ((discountObject === null || discountObject === void 0 ? void 0 : discountObject.type) === enum_1.DiscountCodeTypes.FREE_DELIVERY) {
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
return shippingCost;
|
|
54
|
+
};
|
|
55
|
+
exports.getShippingCost = getShippingCost;
|
|
56
|
+
const getCreditApplied = (selectedCustomer, orderTotal) => {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
const customerCredit = (_b = (_a = selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.customer) === null || _a === void 0 ? void 0 : _a.credit) !== null && _b !== void 0 ? _b : 0;
|
|
59
|
+
if (customerCredit === 0 || !selectedCustomer) {
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
62
|
+
return Math.min(customerCredit, orderTotal);
|
|
63
|
+
};
|
|
64
|
+
exports.getCreditApplied = getCreditApplied;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateOrderTotal = void 0;
|
|
4
|
-
const calculateOrderTotal_1 = require("./calculateOrderTotal");
|
|
5
|
-
Object.defineProperty(exports, "calculateOrderTotal", { enumerable: true, get: function () { return calculateOrderTotal_1.calculateOrderTotal; } });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateOrderTotal = void 0;
|
|
4
|
+
const calculateOrderTotal_1 = require("./calculateOrderTotal");
|
|
5
|
+
Object.defineProperty(exports, "calculateOrderTotal", { enumerable: true, get: function () { return calculateOrderTotal_1.calculateOrderTotal; } });
|
package/jest.config.js
ADDED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "washday-sdk",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Washday utilities functions and API",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "
|
|
8
|
-
"build": "tsc",
|
|
9
|
-
"publish": "npm run build && npm publish --access public"
|
|
10
|
-
},
|
|
11
|
-
"keywords": [
|
|
12
|
-
"washday"
|
|
13
|
-
],
|
|
14
|
-
"author": "Washday",
|
|
15
|
-
"license": "ISC",
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "washday-sdk",
|
|
3
|
+
"version": "0.0.10",
|
|
4
|
+
"description": "Washday utilities functions and API",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "jest",
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"publish": "npm run build && npm publish --access public"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"washday"
|
|
13
|
+
],
|
|
14
|
+
"author": "Washday",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@babel/core": "^7.24.4",
|
|
18
|
+
"@babel/preset-env": "^7.24.4",
|
|
19
|
+
"@babel/preset-typescript": "^7.24.1",
|
|
20
|
+
"babel-jest": "^29.7.0",
|
|
21
|
+
"jest": "^29.7.0",
|
|
22
|
+
"typescript": "^5.4.4"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"axios": "^1.6.8",
|
|
26
|
+
"joi": "^17.12.3"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/api/axiosInstance.ts
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
-
|
|
3
|
-
// Define the type for the Axios instance
|
|
4
|
-
let axiosInstance: AxiosInstance | null = null;
|
|
5
|
-
|
|
6
|
-
const BASE_URL: string = 'https://washday-backend.herokuapp.com/';
|
|
7
|
-
|
|
8
|
-
// Function to create or return the singleton instance
|
|
9
|
-
const getAxiosInstance = (): AxiosInstance => {
|
|
10
|
-
if (!axiosInstance) {
|
|
11
|
-
axiosInstance = axios.create({
|
|
12
|
-
baseURL: BASE_URL,
|
|
13
|
-
headers: {
|
|
14
|
-
'Content-Type': 'application/json',
|
|
15
|
-
// Add any default headers here
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// Add interceptor to set token and other options for every request
|
|
20
|
-
axiosInstance.interceptors.request.use(
|
|
21
|
-
(config) => {
|
|
22
|
-
const { token, contentType = 'application/json', responseType = 'json', contentDisposition } = config.params || {};
|
|
23
|
-
if (token) {
|
|
24
|
-
config.headers.Authorization = `Bearer ${token}`;
|
|
25
|
-
}
|
|
26
|
-
if (contentType) {
|
|
27
|
-
config.headers['Content-Type'] = contentType;
|
|
28
|
-
}
|
|
29
|
-
if (responseType) {
|
|
30
|
-
config.responseType = responseType;
|
|
31
|
-
}
|
|
32
|
-
if (contentDisposition) {
|
|
33
|
-
config.headers['Content-Disposition'] = contentDisposition;
|
|
34
|
-
}
|
|
35
|
-
return config;
|
|
36
|
-
},
|
|
37
|
-
(error) => {
|
|
38
|
-
return Promise.reject(error);
|
|
39
|
-
}
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
return axiosInstance;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export default getAxiosInstance();
|
|
1
|
+
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
|
|
3
|
+
// Define the type for the Axios instance
|
|
4
|
+
let axiosInstance: AxiosInstance | null = null;
|
|
5
|
+
|
|
6
|
+
const BASE_URL: string = 'https://washday-backend.herokuapp.com/';
|
|
7
|
+
|
|
8
|
+
// Function to create or return the singleton instance
|
|
9
|
+
const getAxiosInstance = (): AxiosInstance => {
|
|
10
|
+
if (!axiosInstance) {
|
|
11
|
+
axiosInstance = axios.create({
|
|
12
|
+
baseURL: BASE_URL,
|
|
13
|
+
headers: {
|
|
14
|
+
'Content-Type': 'application/json',
|
|
15
|
+
// Add any default headers here
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Add interceptor to set token and other options for every request
|
|
20
|
+
axiosInstance.interceptors.request.use(
|
|
21
|
+
(config) => {
|
|
22
|
+
const { token, contentType = 'application/json', responseType = 'json', contentDisposition } = config.params || {};
|
|
23
|
+
if (token) {
|
|
24
|
+
config.headers.Authorization = `Bearer ${token}`;
|
|
25
|
+
}
|
|
26
|
+
if (contentType) {
|
|
27
|
+
config.headers['Content-Type'] = contentType;
|
|
28
|
+
}
|
|
29
|
+
if (responseType) {
|
|
30
|
+
config.responseType = responseType;
|
|
31
|
+
}
|
|
32
|
+
if (contentDisposition) {
|
|
33
|
+
config.headers['Content-Disposition'] = contentDisposition;
|
|
34
|
+
}
|
|
35
|
+
return config;
|
|
36
|
+
},
|
|
37
|
+
(error) => {
|
|
38
|
+
return Promise.reject(error);
|
|
39
|
+
}
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return axiosInstance;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export default getAxiosInstance();
|
package/src/api/customers/get.ts
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import { WashdayClientInstance } from "../../interfaces/Api";
|
|
2
|
-
import { ICustomer } from "../../interfaces/Customer";
|
|
3
|
-
import { IOrderInfo } from "../../interfaces/Order";
|
|
4
|
-
import axiosInstance from "../axiosInstance";
|
|
5
|
-
|
|
6
|
-
const GET_SET_CUSTOMERS = 'api/customer';
|
|
7
|
-
const REQUEST_PARAMS = {
|
|
8
|
-
token: 'LOGGED_USER_TOKEN',
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export const getCustomerById = async function (this: WashdayClientInstance, customerId: string): Promise<{ customer: ICustomer, orderInfo: IOrderInfo }> {
|
|
13
|
-
try {
|
|
14
|
-
REQUEST_PARAMS.token = this.apiToken;
|
|
15
|
-
const response = await axiosInstance.get(`${GET_SET_CUSTOMERS}/${customerId}`, { params: { ...REQUEST_PARAMS } });
|
|
16
|
-
return response.data;
|
|
17
|
-
} catch (error) {
|
|
18
|
-
console.error('Error fetching customer:', error);
|
|
19
|
-
throw error;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export const getCustomerHighlightsById = async function (this: WashdayClientInstance, customerId: string): Promise<{ customer: ICustomer, orderInfo: IOrderInfo }> {
|
|
24
|
-
try {
|
|
25
|
-
REQUEST_PARAMS.token = this.apiToken;
|
|
26
|
-
const response = await axiosInstance.get(`${GET_SET_CUSTOMERS}/${customerId}/highlights`, { params: { ...REQUEST_PARAMS } });
|
|
27
|
-
return response.data;
|
|
28
|
-
} catch (error) {
|
|
29
|
-
console.error('Error fetching
|
|
30
|
-
throw error;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
1
|
+
import { WashdayClientInstance } from "../../interfaces/Api";
|
|
2
|
+
import { ICustomer } from "../../interfaces/Customer";
|
|
3
|
+
import { IOrderInfo } from "../../interfaces/Order";
|
|
4
|
+
import axiosInstance from "../axiosInstance";
|
|
5
|
+
|
|
6
|
+
const GET_SET_CUSTOMERS = 'api/customer';
|
|
7
|
+
const REQUEST_PARAMS = {
|
|
8
|
+
token: 'LOGGED_USER_TOKEN',
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export const getCustomerById = async function (this: WashdayClientInstance, customerId: string): Promise<{ customer: ICustomer, orderInfo: IOrderInfo }> {
|
|
13
|
+
try {
|
|
14
|
+
REQUEST_PARAMS.token = this.apiToken;
|
|
15
|
+
const response = await axiosInstance.get(`${GET_SET_CUSTOMERS}/${customerId}`, { params: { ...REQUEST_PARAMS } });
|
|
16
|
+
return response.data;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.error('Error fetching customer:', error);
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const getCustomerHighlightsById = async function (this: WashdayClientInstance, customerId: string): Promise<{ customer: ICustomer, orderInfo: IOrderInfo }> {
|
|
24
|
+
try {
|
|
25
|
+
REQUEST_PARAMS.token = this.apiToken;
|
|
26
|
+
const response = await axiosInstance.get(`${GET_SET_CUSTOMERS}/${customerId}/highlights`, { params: { ...REQUEST_PARAMS } });
|
|
27
|
+
return response.data;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.error('Error fetching getCustomerHighlightsById:', error);
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
};
|
package/src/api/index.ts
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
|
-
import { WashdayClientInstance } from "../interfaces/Api";
|
|
2
|
-
import { getCustomerById, getCustomerHighlightsById } from "./customers/get";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
import { WashdayClientInstance } from "../interfaces/Api";
|
|
2
|
+
import { getCustomerById, getCustomerHighlightsById } from "./customers/get";
|
|
3
|
+
import { getStoreById, getStores } from "./stores/get";
|
|
4
|
+
|
|
5
|
+
type WashdayClientConstructor = {
|
|
6
|
+
new(apiToken: string): {
|
|
7
|
+
apiToken: string;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const WashdayClient: WashdayClientConstructor = function WashdayClient(this: WashdayClientInstance, apiToken: string) {
|
|
12
|
+
this.apiToken = apiToken;
|
|
13
|
+
WashdayClient.prototype.customers.apiToken = apiToken;
|
|
14
|
+
} as any;
|
|
15
|
+
|
|
16
|
+
WashdayClient.prototype.customers = {
|
|
17
|
+
getCustomerById: getCustomerById,
|
|
18
|
+
getCustomerHighlightsById: getCustomerHighlightsById
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
WashdayClient.prototype.stores = {
|
|
22
|
+
getStores: getStores,
|
|
23
|
+
getStoreById: getStoreById
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export default WashdayClient;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { WashdayClientInstance } from "../../interfaces/Api";
|
|
2
|
+
import { ICustomer } from "../../interfaces/Customer";
|
|
3
|
+
import { IOrderInfo } from "../../interfaces/Order";
|
|
4
|
+
import { IStore } from "../../interfaces/Store";
|
|
5
|
+
import axiosInstance from "../axiosInstance";
|
|
6
|
+
|
|
7
|
+
const GET_SET_STORES = 'api/store';
|
|
8
|
+
const REQUEST_PARAMS = {
|
|
9
|
+
token: 'LOGGED_USER_TOKEN',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const getStores = async function (this: WashdayClientInstance): Promise<{ stores: IStore[] }> {
|
|
13
|
+
try {
|
|
14
|
+
REQUEST_PARAMS.token = this.apiToken;
|
|
15
|
+
const response = await axiosInstance.get(`${GET_SET_STORES}`, { params: { ...REQUEST_PARAMS } });
|
|
16
|
+
return response.data?.data?.stores || [];
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.error('Error fetching getStores:', error);
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export const getStoreById = async function (this: WashdayClientInstance, storeId: string): Promise<{ store: IStore }> {
|
|
25
|
+
try {
|
|
26
|
+
REQUEST_PARAMS.token = this.apiToken;
|
|
27
|
+
const response = await axiosInstance.get(`${GET_SET_STORES}/${storeId}`, { params: { ...REQUEST_PARAMS } });
|
|
28
|
+
return response.data?.data || {}
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error('Error fetching getStoreById:', error);
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
};
|
package/src/enum/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export enum DiscountCodeTypes {
|
|
2
|
-
PERCENTAGE = 'percentage',
|
|
3
|
-
NUMBER = 'number',
|
|
4
|
-
FREE_DELIVERY = 'freeDelivery',
|
|
5
|
-
BUY_X_GET_Y = 'buyXGetY'
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export enum BuyAndGetConditionsTypes {
|
|
9
|
-
PERCENTAGE = 'percentage',
|
|
10
|
-
FREE = 'free'
|
|
1
|
+
export enum DiscountCodeTypes {
|
|
2
|
+
PERCENTAGE = 'percentage',
|
|
3
|
+
NUMBER = 'number',
|
|
4
|
+
FREE_DELIVERY = 'freeDelivery',
|
|
5
|
+
BUY_X_GET_Y = 'buyXGetY'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export enum BuyAndGetConditionsTypes {
|
|
9
|
+
PERCENTAGE = 'percentage',
|
|
10
|
+
FREE = 'free'
|
|
11
11
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as utils from './utils';
|
|
2
|
-
import WashdayClient from './api';
|
|
3
|
-
|
|
4
|
-
export { WashdayClient, utils };
|
|
1
|
+
import * as utils from './utils';
|
|
2
|
+
import WashdayClient from './api';
|
|
3
|
+
|
|
4
|
+
export { WashdayClient, utils };
|
package/src/interfaces/Api.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export type WashdayClientInstance = {
|
|
2
|
-
apiToken: string;
|
|
1
|
+
export type WashdayClientInstance = {
|
|
2
|
+
apiToken: string;
|
|
3
3
|
};
|