sahab-utils 1.2.5 → 1.2.7

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.
@@ -744,6 +744,7 @@ export type WeatherData = {
744
744
 
745
745
  export type PaymentConfigs = {
746
746
  isCashAllowed: boolean;
747
+ isGooglePayAllowed?: boolean;
747
748
  paymentGateways: {
748
749
  key: string;
749
750
  isActive: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sahab-utils",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "description": "This package containts types and utils fuctions",
5
5
  "main": "index.ts",
6
6
  "scripts": {
package/ts/calcs.ts CHANGED
@@ -35,14 +35,50 @@ export function getSahabDiscountsAmount(order: Order) {
35
35
  discounts += 0.05 * orderTotal;
36
36
  } else if (order?.promoCode?.amount && order?.promoCode?.amount > 0 && !order?.promoCode?.createdByProvider) {
37
37
  if (order?.promoCode?.isPercentage) {
38
- const amount = (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
38
+ let amount = (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
39
+
40
+ // Check if promo code is for specific services/addons only
41
+ if (
42
+ (order?.promoCode?.servicesIds && order?.promoCode?.servicesIds.length > 0) ||
43
+ (order?.promoCode?.addonsIds && order?.promoCode?.addonsIds.length > 0)
44
+ ) {
45
+ let applicableTotal = 0;
46
+ order?.services?.forEach((carService) => {
47
+ carService?.services?.forEach((service) => {
48
+ if (order?.promoCode?.servicesIds?.includes(service?.serviceId || '') || order?.promoCode?.addonsIds?.includes(service?.addonId || '')) {
49
+ applicableTotal += service?.price?.price || 0;
50
+ }
51
+ });
52
+ });
53
+
54
+ amount = (preventNaN(order?.promoCode?.amount) / 100) * applicableTotal;
55
+ }
56
+
39
57
  if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
40
58
  discounts += Math.min(amount, order?.promoCode?.maxAmount);
41
59
  } else {
42
60
  discounts += amount;
43
61
  }
44
62
  } else {
45
- const amount = preventNaN(order?.promoCode?.amount);
63
+ let amount = preventNaN(order?.promoCode?.amount);
64
+
65
+ // Check if promo code is for specific services/addons only
66
+ if (
67
+ (order?.promoCode?.servicesIds && order?.promoCode?.servicesIds.length > 0) ||
68
+ (order?.promoCode?.addonsIds && order?.promoCode?.addonsIds.length > 0)
69
+ ) {
70
+ let applicableTotal = 0;
71
+ order?.services?.forEach((carService) => {
72
+ carService?.services?.forEach((service) => {
73
+ if (order?.promoCode?.servicesIds?.includes(service?.serviceId || '') || order?.promoCode?.addonsIds?.includes(service?.addonId || '')) {
74
+ applicableTotal += service?.price?.price || 0;
75
+ }
76
+ });
77
+ });
78
+
79
+ amount = Math.min(amount, applicableTotal);
80
+ }
81
+
46
82
  if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
47
83
  discounts += Math.min(amount, order?.promoCode?.maxAmount);
48
84
  } else {
@@ -58,6 +94,19 @@ export function getSahabDiscountsAmount(order: Order) {
58
94
 
59
95
  if (sahabDiscounts.length) {
60
96
  sahabDiscounts.forEach((discount) => {
97
+ // Check if discount is for specific services/addons only
98
+ if (discount?.servicesIds && discount?.servicesIds.length > 0) {
99
+ if (!discount?.servicesIds?.includes(service?.serviceId || '')) {
100
+ return;
101
+ }
102
+ }
103
+
104
+ if (discount?.addonsIds && discount?.addonsIds.length > 0) {
105
+ if (!discount?.addonsIds?.includes(service?.addonId || '')) {
106
+ return;
107
+ }
108
+ }
109
+
61
110
  if (discount?.isPercentage) {
62
111
  discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
63
112
  } else {
@@ -87,14 +136,49 @@ export function getProviderDiscountsAmount(order: Order) {
87
136
  discounts += 0.05 * orderTotal;
88
137
  } else if (order?.promoCode?.amount && order?.promoCode?.amount > 0 && order?.promoCode?.createdByProvider) {
89
138
  if (order?.promoCode?.isPercentage) {
90
- const amount = (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
139
+ let amount = (preventNaN(order?.promoCode?.amount) / 100) * orderTotal;
140
+
141
+ // Check if promo code is for specific services/addons only
142
+ if (
143
+ (order?.promoCode?.servicesIds && order?.promoCode?.servicesIds.length > 0) ||
144
+ (order?.promoCode?.addonsIds && order?.promoCode?.addonsIds.length > 0)
145
+ ) {
146
+ let applicableTotal = 0;
147
+ order?.services?.forEach((carService) => {
148
+ carService?.services?.forEach((service) => {
149
+ if (order?.promoCode?.servicesIds?.includes(service?.serviceId || '') || order?.promoCode?.addonsIds?.includes(service?.addonId || '')) {
150
+ applicableTotal += service?.price?.price || 0;
151
+ }
152
+ });
153
+ });
154
+
155
+ amount = (preventNaN(order?.promoCode?.amount) / 100) * applicableTotal;
156
+ }
157
+
91
158
  if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
92
159
  discounts += Math.min(amount, order?.promoCode?.maxAmount);
93
160
  } else {
94
161
  discounts += amount;
95
162
  }
96
163
  } else {
97
- const amount = preventNaN(order?.promoCode?.amount);
164
+ let amount = preventNaN(order?.promoCode?.amount);
165
+
166
+ // Check if promo code is for specific services/addons only
167
+ if (
168
+ (order?.promoCode?.servicesIds && order?.promoCode?.servicesIds.length > 0) ||
169
+ (order?.promoCode?.addonsIds && order?.promoCode?.addonsIds.length > 0)
170
+ ) {
171
+ let applicableTotal = 0;
172
+ order?.services?.forEach((carService) => {
173
+ carService?.services?.forEach((service) => {
174
+ if (order?.promoCode?.servicesIds?.includes(service?.serviceId || '') || order?.promoCode?.addonsIds?.includes(service?.addonId || '')) {
175
+ applicableTotal += service?.price?.price || 0;
176
+ }
177
+ });
178
+ });
179
+
180
+ amount = Math.min(amount, applicableTotal);
181
+ }
98
182
 
99
183
  if (order?.promoCode?.maxAmount && order?.promoCode?.maxAmount > 0) {
100
184
  discounts += Math.min(amount, order?.promoCode?.maxAmount);
@@ -110,6 +194,19 @@ export function getProviderDiscountsAmount(order: Order) {
110
194
 
111
195
  if (sahabDiscounts.length) {
112
196
  sahabDiscounts.forEach((discount) => {
197
+ // Check if discount is for specific services/addons only
198
+ if (discount?.servicesIds && discount?.servicesIds.length > 0) {
199
+ if (!discount?.servicesIds?.includes(service?.serviceId || '')) {
200
+ return;
201
+ }
202
+ }
203
+
204
+ if (discount?.addonsIds && discount?.addonsIds.length > 0) {
205
+ if (!discount?.addonsIds?.includes(service?.addonId || '')) {
206
+ return;
207
+ }
208
+ }
209
+
113
210
  if (discount?.isPercentage) {
114
211
  discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
115
212
  } else {
@@ -231,66 +328,6 @@ export function calculatePromoCodeAmount(order: Order) {
231
328
  return preventNaN(order?.promoCode?.amount);
232
329
  }
233
330
 
234
- export function calculateDiscountAmount(order: Order) {
235
- let discounts = 0;
236
-
237
- if (!order?.promoCode?.amount && !order?.services?.some((service) => service?.services?.some((s) => s?.discount))) {
238
- return 0;
239
- }
240
-
241
- const orderTotal = getServicesTotal(order);
242
- if (order?.promoCode?.isPercentage) {
243
- discounts += (preventNaN(order?.promoCode?.amount ?? 0) / 100) * orderTotal;
244
- } else if (order?.promoCode?.amount) {
245
- discounts += preventNaN(order?.promoCode?.amount ?? 0);
246
- }
247
-
248
- if (order?.services?.some((service) => service?.services?.some((s) => s?.discount))) {
249
- (order?.services ?? [])?.forEach((carService) => {
250
- (carService?.services || []).forEach((service) => {
251
- let sahabDiscounts = [service?.discount] as Discount[];
252
-
253
- if (sahabDiscounts.length) {
254
- sahabDiscounts.forEach((discount) => {
255
- if (discount?.isPercentage) {
256
- discounts += (preventNaN(discount?.amount ?? 0) / 100) * (service?.price?.price ?? 0);
257
- } else {
258
- discounts += preventNaN(discount?.amount ?? 0);
259
- }
260
- });
261
- }
262
- });
263
- });
264
- }
265
-
266
- return discounts;
267
- }
268
-
269
- export function getServiceTotal(providedService: ProvidedService | ProvidedAddon) {
270
- let total = 0;
271
-
272
- total += providedService?.price?.price ?? 0;
273
- let sahabDiscounts = [providedService?.discount] as Discount[];
274
-
275
- if (sahabDiscounts.length) {
276
- sahabDiscounts.forEach((discount) => {
277
- if (discount?.isPercentage) {
278
- total -= (preventNaN(discount?.amount ?? 0) / 100) * (providedService?.price?.price ?? 0);
279
- } else {
280
- total -= preventNaN(discount?.amount ?? 0);
281
- }
282
- });
283
- }
284
-
285
- if (total < 0) {
286
- total = 0;
287
- }
288
-
289
- const clientPercentage = 0.03 * total;
290
-
291
- return total + clientPercentage;
292
- }
293
-
294
331
  export function calculateOrderPercentage({
295
332
  isPaid,
296
333
  paymentId,