washday-sdk 0.0.193 → 0.0.195

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/index.js CHANGED
@@ -124,6 +124,7 @@ const WashdayClient = function WashdayClient(apiToken) {
124
124
  createOrderUsersApp: ordersEndpoints.postModule.createOrderUsersApp,
125
125
  bulkCreatePaymentLines: ordersEndpoints.postModule.bulkCreatePaymentLines,
126
126
  createOrderEvidence: ordersEndpoints.postModule.createOrderEvidence,
127
+ getRedeemPointsPreview: ordersEndpoints.postModule.getRedeemPointsPreview,
127
128
  deletePaymentLineById: ordersEndpoints.deleteModule.deletePaymentLineById,
128
129
  getListCustomersApp: ordersEndpoints.getModule.getListCustomersApp,
129
130
  getByIdCustomersApp: ordersEndpoints.getModule.getByIdCustomersApp,
@@ -99,3 +99,17 @@ export const createOrderEvidence = function (orderId, data) {
99
99
  }
100
100
  });
101
101
  };
102
+ export const getRedeemPointsPreview = function (data) {
103
+ return __awaiter(this, void 0, void 0, function* () {
104
+ try {
105
+ const config = {
106
+ headers: { Authorization: `Bearer ${this.apiToken}` }
107
+ };
108
+ return yield axiosInstance.post(`${GET_SET_ORDER}/redeemPointsPreview`, data, config);
109
+ }
110
+ catch (error) {
111
+ console.error('Error fetching getRedeemPointsPreview:', error);
112
+ throw error;
113
+ }
114
+ });
115
+ };
@@ -2,28 +2,39 @@ import { DiscountCodeTypes } from "../../enum";
2
2
  import { calculateTotalTaxesIncluded } from "./calculateTotalTaxesIncluded";
3
3
  import { calculateTotalTaxesOverPrice } from "./calculateTotalTaxesOverPrice";
4
4
  import { getCreditApplied, getProductLineTotals, getShippingCost } from "./helpers";
5
- export const calculateOrderTotal = (order, selectedCustomer, storeSettings, hasShippingCost, storeDiscounts = [], discountCodeObj) => {
5
+ export const calculateOrderTotal = (order, selectedCustomer, storeSettings, hasShippingCost, storeDiscounts = [], discountCodeObj, redeemPointsDiscount = 0 // 💡 NUEVO parámetro
6
+ ) => {
6
7
  var _a;
7
8
  try {
8
9
  const appliedOrderDiscounts = {};
9
- const productTableCalculator = (storeSettings === null || storeSettings === void 0 ? void 0 : storeSettings.taxesType) === 'over_price' ? calculateTotalTaxesOverPrice : calculateTotalTaxesIncluded;
10
+ const productTableCalculator = (storeSettings === null || storeSettings === void 0 ? void 0 : storeSettings.taxesType) === 'over_price'
11
+ ? calculateTotalTaxesOverPrice
12
+ : calculateTotalTaxesIncluded;
10
13
  const productTableImports = productTableCalculator(order, selectedCustomer, storeSettings, storeDiscounts, appliedOrderDiscounts, discountCodeObj);
11
- // PRODUCT LINE TOTALS
14
+ // === PRODUCT LINE TOTALS ===
12
15
  const { totalQuantity, totalImportWithDiscount, totalImportWithoutDiscount, totalImporTaxes, totalDiscountAmount, totalSubtotalAmount } = getProductLineTotals(productTableImports);
13
- // DISCOUNT TOTAL AND SHIPPING SERVICE COST
16
+ // === DISCOUNT CODE (monetario tipo NUMBER que se aplica una sola vez) ===
14
17
  let discountCodeAmount = 0;
15
- let shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
16
- // TODO: MAYBE THIS KIND OF DISCOUNT IS WRONG BECAUSE TAXES ARE CALCULATED BEFORE THIS POINT;
17
- if (discountCodeObj && discountCodeObj.type === DiscountCodeTypes.NUMBER && discountCodeObj.applyOnceOnOrder) {
18
- const includesProducts = discountCodeObj.applyToAllProducts || order.products.some((curr) => discountCodeObj.products.includes(curr._id));
18
+ if (discountCodeObj &&
19
+ discountCodeObj.type === DiscountCodeTypes.NUMBER &&
20
+ discountCodeObj.applyOnceOnOrder) {
21
+ const includesProducts = discountCodeObj.applyToAllProducts ||
22
+ order.products.some((curr) => discountCodeObj.products.includes(curr._id));
19
23
  discountCodeAmount = includesProducts ? discountCodeObj.value : 0;
20
24
  }
21
- // ORDER TOTAL CALCULATION AND CREDIT APPLIED
22
- let orderTotalWithOutCredit = +(totalImportWithDiscount + shippingCost - discountCodeAmount).toFixed(2);
23
- let creditApplied = getCreditApplied(selectedCustomer, orderTotalWithOutCredit);
24
- let orderTotal = +(orderTotalWithOutCredit - creditApplied).toFixed(2);
25
- return Object.assign(Object.assign({}, order), { totalQuantity, customerDiscount: order.discountCode ? 0 : (_a = selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.customer) === null || _a === void 0 ? void 0 : _a.discount, productTotal: totalImportWithDiscount, taxesTotal: totalImporTaxes, total: orderTotal, // Total final (ya sea sumando o no el IVA)
26
- creditApplied, productTotalWithoutDiscount: totalImportWithoutDiscount, totalDiscountAmount: totalDiscountAmount + discountCodeAmount, shippingServiceTotal: shippingCost, appliedOrderDiscounts, subtotal: totalSubtotalAmount });
25
+ // === SHIPPING COST ===
26
+ const shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
27
+ // === TOTAL ANTES DE CRÉDITOS/PUNTOS ===
28
+ const orderTotalWithOutCredit = +(totalImportWithDiscount +
29
+ shippingCost -
30
+ discountCodeAmount).toFixed(2);
31
+ // === APLICAR CRÉDITO Y PUNTOS REDIMIDOS ===
32
+ const creditApplied = getCreditApplied(selectedCustomer, orderTotalWithOutCredit);
33
+ const orderTotal = +(orderTotalWithOutCredit -
34
+ creditApplied -
35
+ redeemPointsDiscount).toFixed(2);
36
+ // === RETURN FINAL OBJECT ===
37
+ return Object.assign(Object.assign({}, order), { totalQuantity, customerDiscount: order.discountCode ? 0 : (_a = selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.customer) === null || _a === void 0 ? void 0 : _a.discount, productTotal: totalImportWithDiscount, taxesTotal: totalImporTaxes, total: orderTotal, creditApplied, redeemPointsApplied: redeemPointsDiscount, productTotalWithoutDiscount: totalImportWithoutDiscount, totalDiscountAmount: totalDiscountAmount + discountCodeAmount, shippingServiceTotal: shippingCost, appliedOrderDiscounts, subtotal: totalSubtotalAmount });
27
38
  }
28
39
  catch (error) {
29
40
  throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "washday-sdk",
3
- "version": "0.0.193",
3
+ "version": "0.0.195",
4
4
  "description": "Washday utilities functions and API",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
package/src/api/index.ts CHANGED
@@ -131,6 +131,7 @@ const WashdayClient: WashdayClientConstructor = function WashdayClient(this: Was
131
131
  createOrderUsersApp: ordersEndpoints.postModule.createOrderUsersApp,
132
132
  bulkCreatePaymentLines: ordersEndpoints.postModule.bulkCreatePaymentLines,
133
133
  createOrderEvidence: ordersEndpoints.postModule.createOrderEvidence,
134
+ getRedeemPointsPreview: ordersEndpoints.postModule.getRedeemPointsPreview,
134
135
  deletePaymentLineById: ordersEndpoints.deleteModule.deletePaymentLineById,
135
136
  getListCustomersApp: ordersEndpoints.getModule.getListCustomersApp,
136
137
  getByIdCustomersApp: ordersEndpoints.getModule.getByIdCustomersApp,
@@ -97,4 +97,20 @@ export const createOrderEvidence = async function (this: WashdayClientInstance,
97
97
  console.error('Error fetching createOrderEvidence:', error);
98
98
  throw error;
99
99
  }
100
+ };
101
+
102
+ export const getRedeemPointsPreview = async function (this: WashdayClientInstance, data: {
103
+ customerId: string;
104
+ storeId: string;
105
+ orderTotal: number;
106
+ }): Promise<any> {
107
+ try {
108
+ const config = {
109
+ headers: { Authorization: `Bearer ${this.apiToken}` }
110
+ };
111
+ return await axiosInstance.post(`${GET_SET_ORDER}/redeemPointsPreview`, data, config);
112
+ } catch (error) {
113
+ console.error('Error fetching getRedeemPointsPreview:', error);
114
+ throw error;
115
+ }
100
116
  };
@@ -116,6 +116,7 @@ export interface WashdayClientInstance {
116
116
  createOrderUsersApp: typeof ordersEndpoints.postModule.createOrderUsersApp,
117
117
  bulkCreatePaymentLines: typeof ordersEndpoints.postModule.bulkCreatePaymentLines,
118
118
  createOrderEvidence: typeof ordersEndpoints.postModule.createOrderEvidence,
119
+ getRedeemPointsPreview: typeof ordersEndpoints.postModule.getRedeemPointsPreview,
119
120
  deletePaymentLineById: typeof ordersEndpoints.deleteModule.deletePaymentLineById;
120
121
  getListCustomersApp: typeof ordersEndpoints.getModule.getListCustomersApp;
121
122
  getByIdCustomersApp: typeof ordersEndpoints.getModule.getByIdCustomersApp;
@@ -11,14 +11,27 @@ export const calculateOrderTotal = (
11
11
  storeSettings: IStore,
12
12
  hasShippingCost: boolean,
13
13
  storeDiscounts: any[] = [],
14
- discountCodeObj: any
14
+ discountCodeObj: any,
15
+ redeemPointsDiscount: number = 0 // 💡 NUEVO parámetro
15
16
  ): any => {
16
17
  try {
17
18
  const appliedOrderDiscounts: any = {};
18
- const productTableCalculator = storeSettings?.taxesType === 'over_price' ? calculateTotalTaxesOverPrice : calculateTotalTaxesIncluded;
19
- const productTableImports = productTableCalculator(order, selectedCustomer, storeSettings, storeDiscounts, appliedOrderDiscounts, discountCodeObj);
20
19
 
21
- // PRODUCT LINE TOTALS
20
+ const productTableCalculator =
21
+ storeSettings?.taxesType === 'over_price'
22
+ ? calculateTotalTaxesOverPrice
23
+ : calculateTotalTaxesIncluded;
24
+
25
+ const productTableImports = productTableCalculator(
26
+ order,
27
+ selectedCustomer,
28
+ storeSettings,
29
+ storeDiscounts,
30
+ appliedOrderDiscounts,
31
+ discountCodeObj
32
+ );
33
+
34
+ // === PRODUCT LINE TOTALS ===
22
35
  const {
23
36
  totalQuantity,
24
37
  totalImportWithDiscount,
@@ -28,30 +41,59 @@ export const calculateOrderTotal = (
28
41
  totalSubtotalAmount
29
42
  } = getProductLineTotals(productTableImports);
30
43
 
31
- // DISCOUNT TOTAL AND SHIPPING SERVICE COST
44
+ // === DISCOUNT CODE (monetario tipo NUMBER que se aplica una sola vez) ===
32
45
  let discountCodeAmount = 0;
33
- let shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
46
+ if (
47
+ discountCodeObj &&
48
+ discountCodeObj.type === DiscountCodeTypes.NUMBER &&
49
+ discountCodeObj.applyOnceOnOrder
50
+ ) {
51
+ const includesProducts =
52
+ discountCodeObj.applyToAllProducts ||
53
+ order.products.some((curr: any) =>
54
+ discountCodeObj.products.includes(curr._id)
55
+ );
34
56
 
35
- // TODO: MAYBE THIS KIND OF DISCOUNT IS WRONG BECAUSE TAXES ARE CALCULATED BEFORE THIS POINT;
36
- if (discountCodeObj && discountCodeObj.type === DiscountCodeTypes.NUMBER && discountCodeObj.applyOnceOnOrder) {
37
- const includesProducts = discountCodeObj.applyToAllProducts || order.products.some((curr: any) => discountCodeObj.products.includes(curr._id));
38
57
  discountCodeAmount = includesProducts ? discountCodeObj.value : 0;
39
58
  }
40
59
 
41
- // ORDER TOTAL CALCULATION AND CREDIT APPLIED
42
- let orderTotalWithOutCredit = +(totalImportWithDiscount + shippingCost - discountCodeAmount).toFixed(2);
43
- let creditApplied = getCreditApplied(selectedCustomer, orderTotalWithOutCredit);
44
- let orderTotal = +(orderTotalWithOutCredit - creditApplied).toFixed(2);
60
+ // === SHIPPING COST ===
61
+ const shippingCost = getShippingCost(
62
+ discountCodeObj,
63
+ hasShippingCost,
64
+ storeSettings
65
+ );
66
+
67
+ // === TOTAL ANTES DE CRÉDITOS/PUNTOS ===
68
+ const orderTotalWithOutCredit = +(
69
+ totalImportWithDiscount +
70
+ shippingCost -
71
+ discountCodeAmount
72
+ ).toFixed(2);
73
+
74
+ // === APLICAR CRÉDITO Y PUNTOS REDIMIDOS ===
75
+ const creditApplied = getCreditApplied(
76
+ selectedCustomer,
77
+ orderTotalWithOutCredit
78
+ );
79
+
80
+ const orderTotal = +(
81
+ orderTotalWithOutCredit -
82
+ creditApplied -
83
+ redeemPointsDiscount
84
+ ).toFixed(2);
45
85
 
86
+ // === RETURN FINAL OBJECT ===
46
87
  return {
47
88
  ...order,
48
89
  totalQuantity,
49
90
  customerDiscount: order.discountCode ? 0 : selectedCustomer?.customer?.discount,
50
- productTotal: totalImportWithDiscount, // Total neto con descuento, que luego se convierte a bruto según el método
51
- taxesTotal: totalImporTaxes, // Monto de impuestos aplicado
52
- total: orderTotal, // Total final (ya sea sumando o no el IVA)
91
+ productTotal: totalImportWithDiscount,
92
+ taxesTotal: totalImporTaxes,
93
+ total: orderTotal,
53
94
  creditApplied,
54
- productTotalWithoutDiscount: totalImportWithoutDiscount, // Precio base (neto)
95
+ redeemPointsApplied: redeemPointsDiscount, // NUEVO CAMPO
96
+ productTotalWithoutDiscount: totalImportWithoutDiscount,
55
97
  totalDiscountAmount: totalDiscountAmount + discountCodeAmount,
56
98
  shippingServiceTotal: shippingCost,
57
99
  appliedOrderDiscounts,
@@ -60,4 +102,4 @@ export const calculateOrderTotal = (
60
102
  } catch (error) {
61
103
  throw error;
62
104
  }
63
- };
105
+ };