@lyxa.ai/marketing 1.0.44 → 1.0.47

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.
Files changed (113) hide show
  1. package/dist/lib/index.d.ts +170 -28
  2. package/dist/lib/index.d.ts.map +1 -1
  3. package/dist/lib/modules/coupon/routers/coupon.router.d.ts +169 -27
  4. package/dist/lib/modules/coupon/routers/coupon.router.d.ts.map +1 -1
  5. package/dist/lib/modules/coupon/routers/coupon.router.js +13 -4
  6. package/dist/lib/modules/coupon/routers/coupon.router.js.map +1 -1
  7. package/dist/lib/modules/coupon/services/coupon.service.d.ts +4 -1
  8. package/dist/lib/modules/coupon/services/coupon.service.d.ts.map +1 -1
  9. package/dist/lib/modules/coupon/services/coupon.service.js +134 -15
  10. package/dist/lib/modules/coupon/services/coupon.service.js.map +1 -1
  11. package/dist/lib/modules/coupon/services/custom-coupon.service.d.ts +208 -3
  12. package/dist/lib/modules/coupon/services/custom-coupon.service.d.ts.map +1 -1
  13. package/dist/lib/modules/coupon/services/global-coupon.service.d.ts.map +1 -1
  14. package/dist/lib/modules/coupon/services/global-coupon.service.js +2 -0
  15. package/dist/lib/modules/coupon/services/global-coupon.service.js.map +1 -1
  16. package/dist/lib/modules/coupon/services/helper.service.d.ts +214 -4
  17. package/dist/lib/modules/coupon/services/helper.service.d.ts.map +1 -1
  18. package/dist/lib/modules/coupon/services/helper.service.js +114 -16
  19. package/dist/lib/modules/coupon/services/helper.service.js.map +1 -1
  20. package/dist/lib/modules/coupon/services/individual-store-coupon.service.d.ts +208 -3
  21. package/dist/lib/modules/coupon/services/individual-store-coupon.service.d.ts.map +1 -1
  22. package/dist/lib/modules/coupon/services/individual-user-coupon.service.d.ts +1 -1
  23. package/dist/lib/modules/coupon/services/individual-user-coupon.service.d.ts.map +1 -1
  24. package/dist/lib/modules/coupon/services/individual-user-coupon.service.js +20 -11
  25. package/dist/lib/modules/coupon/services/individual-user-coupon.service.js.map +1 -1
  26. package/dist/lib/modules/coupon/services/patterns/repository/CouponMetricsRepository.d.ts +10 -4
  27. package/dist/lib/modules/coupon/services/patterns/repository/CouponMetricsRepository.d.ts.map +1 -1
  28. package/dist/lib/modules/coupon/services/patterns/repository/CouponMetricsRepository.js +90 -14
  29. package/dist/lib/modules/coupon/services/patterns/repository/CouponMetricsRepository.js.map +1 -1
  30. package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.d.ts +2 -2
  31. package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.d.ts.map +1 -1
  32. package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.js +151 -45
  33. package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.js.map +1 -1
  34. package/dist/lib/modules/coupon/services/referral-reward-coupon.service.d.ts.map +1 -1
  35. package/dist/lib/modules/coupon/services/referral-reward-coupon.service.js +17 -4
  36. package/dist/lib/modules/coupon/services/referral-reward-coupon.service.js.map +1 -1
  37. package/dist/lib/modules/coupon/services/shops-categories-coupon.service.d.ts +208 -3
  38. package/dist/lib/modules/coupon/services/shops-categories-coupon.service.d.ts.map +1 -1
  39. package/dist/lib/modules/coupon/transformers/coupon.transformer.d.ts +5 -1
  40. package/dist/lib/modules/coupon/transformers/coupon.transformer.d.ts.map +1 -1
  41. package/dist/lib/modules/coupon/validations/all-coupons.validation.d.ts +243 -23
  42. package/dist/lib/modules/coupon/validations/all-coupons.validation.d.ts.map +1 -1
  43. package/dist/lib/modules/coupon/validations/coupon.validation.d.ts +412 -48
  44. package/dist/lib/modules/coupon/validations/coupon.validation.d.ts.map +1 -1
  45. package/dist/lib/modules/coupon/validations/coupon.validation.js +39 -6
  46. package/dist/lib/modules/coupon/validations/coupon.validation.js.map +1 -1
  47. package/dist/lib/modules/coupon/validations/create-coupon.validation.d.ts +94 -6
  48. package/dist/lib/modules/coupon/validations/create-coupon.validation.d.ts.map +1 -1
  49. package/dist/lib/modules/coupon/validations/custom-coupon.validation.d.ts +9 -0
  50. package/dist/lib/modules/coupon/validations/custom-coupon.validation.d.ts.map +1 -1
  51. package/dist/lib/modules/coupon/validations/custom-coupon.validation.js +1 -0
  52. package/dist/lib/modules/coupon/validations/custom-coupon.validation.js.map +1 -1
  53. package/dist/lib/modules/coupon/validations/individual-store-coupon.validation.d.ts +200 -122
  54. package/dist/lib/modules/coupon/validations/individual-store-coupon.validation.d.ts.map +1 -1
  55. package/dist/lib/modules/coupon/validations/individual-store-coupon.validation.js +20 -3
  56. package/dist/lib/modules/coupon/validations/individual-store-coupon.validation.js.map +1 -1
  57. package/dist/lib/modules/coupon/validations/individual-user-coupon.validation.d.ts +9 -0
  58. package/dist/lib/modules/coupon/validations/individual-user-coupon.validation.d.ts.map +1 -1
  59. package/dist/lib/modules/coupon/validations/individual-user-coupon.validation.js +1 -0
  60. package/dist/lib/modules/coupon/validations/individual-user-coupon.validation.js.map +1 -1
  61. package/dist/lib/modules/coupon/validations/shops-categories-coupon.validation.d.ts +9 -0
  62. package/dist/lib/modules/coupon/validations/shops-categories-coupon.validation.d.ts.map +1 -1
  63. package/dist/lib/modules/coupon/validations/shops-categories-coupon.validation.js +1 -0
  64. package/dist/lib/modules/coupon/validations/shops-categories-coupon.validation.js.map +1 -1
  65. package/dist/lib/modules/marketing/validations/buy1-get1-marketing.validation.d.ts +6 -6
  66. package/dist/lib/modules/marketing/validations/discount-marketing.validation.d.ts +6 -6
  67. package/dist/lib/modules/marketing/validations/product-marketing.validation.d.ts +2 -2
  68. package/dist/lib/modules/punch-marketing-history/routers/punch-marketing-history.router.d.ts +1 -1
  69. package/dist/lib/modules/punch-marketing-history/validations/punch-marketing-history.validation.d.ts +2 -2
  70. package/dist/types/index.d.ts +170 -28
  71. package/dist/types/index.d.ts.map +1 -1
  72. package/dist/types/modules/coupon/routers/coupon.router.d.ts +169 -27
  73. package/dist/types/modules/coupon/routers/coupon.router.d.ts.map +1 -1
  74. package/dist/types/modules/coupon/services/coupon.service.d.ts +4 -1
  75. package/dist/types/modules/coupon/services/coupon.service.d.ts.map +1 -1
  76. package/dist/types/modules/coupon/services/custom-coupon.service.d.ts +208 -3
  77. package/dist/types/modules/coupon/services/custom-coupon.service.d.ts.map +1 -1
  78. package/dist/types/modules/coupon/services/global-coupon.service.d.ts.map +1 -1
  79. package/dist/types/modules/coupon/services/helper.service.d.ts +214 -4
  80. package/dist/types/modules/coupon/services/helper.service.d.ts.map +1 -1
  81. package/dist/types/modules/coupon/services/individual-store-coupon.service.d.ts +208 -3
  82. package/dist/types/modules/coupon/services/individual-store-coupon.service.d.ts.map +1 -1
  83. package/dist/types/modules/coupon/services/individual-user-coupon.service.d.ts +1 -1
  84. package/dist/types/modules/coupon/services/individual-user-coupon.service.d.ts.map +1 -1
  85. package/dist/types/modules/coupon/services/patterns/repository/CouponMetricsRepository.d.ts +10 -4
  86. package/dist/types/modules/coupon/services/patterns/repository/CouponMetricsRepository.d.ts.map +1 -1
  87. package/dist/types/modules/coupon/services/patterns/repository/CouponRepository.d.ts +2 -2
  88. package/dist/types/modules/coupon/services/patterns/repository/CouponRepository.d.ts.map +1 -1
  89. package/dist/types/modules/coupon/services/referral-reward-coupon.service.d.ts.map +1 -1
  90. package/dist/types/modules/coupon/services/shops-categories-coupon.service.d.ts +208 -3
  91. package/dist/types/modules/coupon/services/shops-categories-coupon.service.d.ts.map +1 -1
  92. package/dist/types/modules/coupon/transformers/coupon.transformer.d.ts +5 -1
  93. package/dist/types/modules/coupon/transformers/coupon.transformer.d.ts.map +1 -1
  94. package/dist/types/modules/coupon/validations/all-coupons.validation.d.ts +243 -23
  95. package/dist/types/modules/coupon/validations/all-coupons.validation.d.ts.map +1 -1
  96. package/dist/types/modules/coupon/validations/coupon.validation.d.ts +412 -48
  97. package/dist/types/modules/coupon/validations/coupon.validation.d.ts.map +1 -1
  98. package/dist/types/modules/coupon/validations/create-coupon.validation.d.ts +94 -6
  99. package/dist/types/modules/coupon/validations/create-coupon.validation.d.ts.map +1 -1
  100. package/dist/types/modules/coupon/validations/custom-coupon.validation.d.ts +9 -0
  101. package/dist/types/modules/coupon/validations/custom-coupon.validation.d.ts.map +1 -1
  102. package/dist/types/modules/coupon/validations/individual-store-coupon.validation.d.ts +200 -122
  103. package/dist/types/modules/coupon/validations/individual-store-coupon.validation.d.ts.map +1 -1
  104. package/dist/types/modules/coupon/validations/individual-user-coupon.validation.d.ts +9 -0
  105. package/dist/types/modules/coupon/validations/individual-user-coupon.validation.d.ts.map +1 -1
  106. package/dist/types/modules/coupon/validations/shops-categories-coupon.validation.d.ts +9 -0
  107. package/dist/types/modules/coupon/validations/shops-categories-coupon.validation.d.ts.map +1 -1
  108. package/dist/types/modules/marketing/validations/buy1-get1-marketing.validation.d.ts +6 -6
  109. package/dist/types/modules/marketing/validations/discount-marketing.validation.d.ts +6 -6
  110. package/dist/types/modules/marketing/validations/product-marketing.validation.d.ts +2 -2
  111. package/dist/types/modules/punch-marketing-history/routers/punch-marketing-history.router.d.ts +1 -1
  112. package/dist/types/modules/punch-marketing-history/validations/punch-marketing-history.validation.d.ts +2 -2
  113. package/package.json +2 -2
@@ -23,7 +23,7 @@ class CouponRepository {
23
23
  }
24
24
  return this._coreUserModel;
25
25
  }
26
- async getUserCoupons(user, shop, itemType) {
26
+ async getUserCoupons(user, shop, vendor, itemType) {
27
27
  const [exchangeRate, referralSettings] = await Promise.all([
28
28
  (0, currency_1.getExchangeRate)(),
29
29
  this.referralSettingsModel.findOne(),
@@ -52,7 +52,7 @@ class CouponRepository {
52
52
  { $lte: [{ $ifNull: ['$duration.start', now] }, now] },
53
53
  ],
54
54
  },
55
- ...(shop
55
+ ...(shop || vendor
56
56
  ? {}
57
57
  : {
58
58
  $and: [
@@ -68,6 +68,7 @@ class CouponRepository {
68
68
  {
69
69
  $and: [
70
70
  { $or: [{ shops: { $exists: false } }, { shops: { $size: 0 } }] },
71
+ { $or: [{ vendors: { $exists: false } }, { vendors: { $size: 0 } }] },
71
72
  { $or: [{ itemTypes: { $exists: false } }, { itemTypes: { $size: 0 } }] },
72
73
  ],
73
74
  },
@@ -104,7 +105,17 @@ class CouponRepository {
104
105
  pipeline: [{ $project: { name: 1 } }],
105
106
  },
106
107
  },
108
+ {
109
+ $lookup: {
110
+ from: 'vendors',
111
+ localField: 'vendor',
112
+ foreignField: '_id',
113
+ as: 'vendor',
114
+ pipeline: [{ $project: { name: 1 } }],
115
+ },
116
+ },
107
117
  { $unwind: { path: '$shop', preserveNullAndEmptyArrays: true } },
118
+ { $unwind: { path: '$vendor', preserveNullAndEmptyArrays: true } },
108
119
  { $unwind: { path: '$referralUser', preserveNullAndEmptyArrays: true } },
109
120
  { $unwind: { path: '$influencer', preserveNullAndEmptyArrays: true } },
110
121
  {
@@ -140,7 +151,7 @@ class CouponRepository {
140
151
  couponUsage: [
141
152
  {
142
153
  $match: {
143
- 'adjustedFinance.coupon': { $exists: true },
154
+ 'adjustedFinance.coupon.coupon': { $exists: true },
144
155
  },
145
156
  },
146
157
  {
@@ -157,19 +168,66 @@ class CouponRepository {
157
168
  as: 'orderStats',
158
169
  },
159
170
  },
171
+ {
172
+ $lookup: {
173
+ from: 'serviceBookings',
174
+ let: { userId: '$_id' },
175
+ pipeline: [
176
+ {
177
+ $match: {
178
+ $expr: { $eq: ['$user', '$$userId'] },
179
+ status: { $ne: enum_1.ServiceBookingStatus.CANCELLED },
180
+ },
181
+ },
182
+ {
183
+ $facet: {
184
+ orderedVendors: [
185
+ {
186
+ $group: {
187
+ _id: null,
188
+ vendors: { $addToSet: '$vendor' },
189
+ },
190
+ },
191
+ ],
192
+ couponUsage: [
193
+ {
194
+ $match: {
195
+ 'adjustedFinance.coupon.coupon': { $exists: true },
196
+ },
197
+ },
198
+ {
199
+ $group: {
200
+ _id: '$adjustedFinance.coupon.coupon',
201
+ snap: { $first: '$adjustedFinance.coupon' },
202
+ count: { $sum: 1 },
203
+ },
204
+ },
205
+ ],
206
+ },
207
+ },
208
+ ],
209
+ as: 'serviceBookingStats',
210
+ },
211
+ },
160
212
  {
161
213
  $addFields: {
162
214
  orderedShops: {
163
215
  $ifNull: [{ $arrayElemAt: ['$orderStats.orderedShops.shops', 0] }, []],
164
216
  },
217
+ orderedVendors: {
218
+ $ifNull: [{ $arrayElemAt: ['$serviceBookingStats.orderedVendors.vendors', 0] }, []],
219
+ },
165
220
  couponUsage: {
166
- $ifNull: [{ $arrayElemAt: ['$orderStats.couponUsage', 0] }, []],
221
+ $concatArrays: [
222
+ { $ifNull: [{ $arrayElemAt: ['$orderStats.couponUsage', 0] }, []] },
223
+ { $ifNull: [{ $arrayElemAt: ['$serviceBookingStats.couponUsage', 0] }, []] },
224
+ ],
167
225
  },
168
226
  },
169
227
  },
170
228
  { $unwind: '$couponDocs' },
171
- ...(shop ? [this.filterShopCoupons(shop, itemType)] : []),
172
- ...(shop ? this.filterFirstOrderOnlyCoupons(shop) : this.filterFirstOrderOnlyCoupons()),
229
+ ...(shop || vendor ? this.filterShopVendorCoupons(shop, vendor, itemType) : []),
230
+ ...this.filterFirstOrderOnlyCoupons({ shop, vendor }),
173
231
  {
174
232
  $addFields: {
175
233
  usageData: {
@@ -188,7 +246,21 @@ class CouponRepository {
188
246
  },
189
247
  {
190
248
  $addFields: {
191
- usedCount: { $ifNull: ['$usageData.count', 0] },
249
+ usedCount: {
250
+ $sum: {
251
+ $map: {
252
+ input: {
253
+ $filter: {
254
+ input: '$couponUsage',
255
+ as: 'cu',
256
+ cond: { $eq: ['$$cu._id', '$couponDocs._id'] },
257
+ },
258
+ },
259
+ as: 'cu',
260
+ in: { $ifNull: ['$$cu.count', 0] },
261
+ },
262
+ },
263
+ },
192
264
  },
193
265
  },
194
266
  {
@@ -378,68 +450,102 @@ class CouponRepository {
378
450
  }
379
451
  return '$' + formatterUSD.format(Number(value ?? 0));
380
452
  }
381
- filterShopCoupons(shop, itemType) {
382
- return {
383
- $match: {
384
- $expr: {
453
+ filterShopVendorCoupons(shop, vendor, itemType) {
454
+ return [
455
+ {
456
+ $match: {
457
+ $expr: {
458
+ $or: [
459
+ {
460
+ $and: [
461
+ { $eq: ['$couponDocs.couponType', enum_1.CouponType.INDIVIDUAL_STORE] },
462
+ shop
463
+ ? { $eq: ['$couponDocs.shop._id', shop] }
464
+ : { $eq: ['$couponDocs.vendor._id', vendor] },
465
+ ],
466
+ },
467
+ {
468
+ $and: [
469
+ { $eq: ['$couponDocs.couponType', enum_1.CouponType.SHOPS_CATEGORIES] },
470
+ {
471
+ $or: [
472
+ { $in: [itemType, { $ifNull: ['$couponDocs.itemTypes', []] }] },
473
+ ...(shop ? [{ $in: [shop, { $ifNull: ['$couponDocs.shops', []] }] }] : []),
474
+ ...(vendor ? [{ $in: [vendor, { $ifNull: ['$couponDocs.vendors', []] }] }] : []),
475
+ ],
476
+ },
477
+ ],
478
+ },
479
+ {
480
+ $not: [
481
+ {
482
+ $in: [
483
+ '$couponDocs.couponType',
484
+ [enum_1.CouponType.INDIVIDUAL_STORE, enum_1.CouponType.SHOPS_CATEGORIES],
485
+ ],
486
+ },
487
+ ],
488
+ },
489
+ ],
490
+ },
491
+ },
492
+ },
493
+ ];
494
+ }
495
+ filterFirstOrderOnlyCoupons(data) {
496
+ const { shop, vendor } = data;
497
+ const hasOrderedFromStore = vendor
498
+ ? { $in: [vendor, '$orderedVendors'] }
499
+ : shop
500
+ ? { $in: [shop, '$orderedShops'] }
501
+ : {
385
502
  $or: [
503
+ { $in: ['$couponDocs.shop._id', '$orderedShops'] },
504
+ { $in: ['$couponDocs.vendor._id', '$orderedVendors'] },
505
+ ],
506
+ };
507
+ const hasUnusedStoreInCategory = vendor
508
+ ? { $in: [vendor, '$orderedVendors'] }
509
+ : shop
510
+ ? { $in: [shop, '$orderedShops'] }
511
+ : {
512
+ $and: [
386
513
  {
387
- $and: [
388
- { $eq: ['$couponDocs.couponType', enum_1.CouponType.INDIVIDUAL_STORE] },
389
- { $eq: ['$couponDocs.shop._id', shop] },
390
- ],
391
- },
392
- {
393
- $and: [
394
- { $eq: ['$couponDocs.couponType', enum_1.CouponType.SHOPS_CATEGORIES] },
514
+ $eq: [
395
515
  {
396
- $or: [
397
- { $eq: ['$couponDocs.itemType', itemType] },
398
- { $in: [shop, { $ifNull: ['$couponDocs.shops', []] }] },
399
- ],
516
+ $size: {
517
+ $setDifference: [{ $ifNull: ['$couponDocs.shops', []] }, '$orderedShops'],
518
+ },
400
519
  },
520
+ 0,
401
521
  ],
402
522
  },
403
523
  {
404
- $not: [
524
+ $eq: [
405
525
  {
406
- $in: ['$couponDocs.couponType', [enum_1.CouponType.INDIVIDUAL_STORE, enum_1.CouponType.SHOPS_CATEGORIES]],
526
+ $size: {
527
+ $setDifference: [{ $ifNull: ['$couponDocs.vendors', []] }, '$orderedVendors'],
528
+ },
407
529
  },
530
+ 0,
408
531
  ],
409
532
  },
410
533
  ],
411
- },
412
- },
413
- };
414
- }
415
- filterFirstOrderOnlyCoupons(shop) {
534
+ };
416
535
  return [
417
536
  {
418
537
  $addFields: {
419
538
  hasOrderedFromStore: {
420
539
  $cond: [
421
540
  { $eq: ['$couponDocs.couponType', enum_1.CouponType.INDIVIDUAL_STORE] },
422
- { $in: [shop ?? '$couponDocs.shop._id', '$orderedShops'] },
541
+ hasOrderedFromStore,
423
542
  false,
424
543
  ],
425
544
  },
426
545
  hasUnusedShopInCategory: {
427
546
  $cond: [
428
547
  { $eq: ['$couponDocs.couponType', enum_1.CouponType.SHOPS_CATEGORIES] },
429
- {
430
- $cond: [
431
- { $ifNull: [shop, false] },
432
- { $in: [shop, '$orderedShops'] },
433
- {
434
- $eq: [
435
- {
436
- $size: { $setDifference: [{ $ifNull: ['$couponDocs.shops', []] }, '$orderedShops'] },
437
- },
438
- 0,
439
- ],
440
- },
441
- ],
442
- },
548
+ hasUnusedStoreInCategory,
443
549
  false,
444
550
  ],
445
551
  },
@@ -1 +1 @@
1
- {"version":3,"file":"CouponRepository.js","sourceRoot":"/","sources":["modules/coupon/services/patterns/repository/CouponRepository.ts"],"names":[],"mappings":";;;AAAA,sEAQmD;AAGnD,gHAAkH;AAClH,4DAM2C;AAC3C,oEAAwE;AACxE,gEA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IAAI,sBAAsB,EAAE,eAAe,CAAC,EAAE;4BAC1D,KAAK;yBACL;qBACD;oBACD,uBAAuB,EAAE;wBACxB,KAAK,EAAE;4BACN,EAAE,GAAG,EAAE,CAAC,wBAAwB,EAAE,iBAAU,CAAC,gBAAgB,CAAC,EAAE;4BAChE;gCACC,KAAK,EAAE;oCACN,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE;oCAC1B,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,eAAe,CAAC,EAAE;oCAChC;wCACC,GAAG,EAAE;4CACJ;gDACC,KAAK,EAAE,EAAE,cAAc,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,mBAAmB,EAAE,EAAE,CAAC,EAAE,EAAE,eAAe,CAAC,EAAE;6CACpF;4CACD,CAAC;yCACD;qCACD;iCACD;6BACD;4BACD,KAAK;yBACL;qBACD;iBACD;aACD;YACD;gBACC,MAAM,EAAE;oBACP,KAAK,EAAE;wBACN,IAAI,EAAE;4BACL,IAAI,EAAE;gCACL;oCACC,GAAG,EAAE,CAAC,wBAAwB,EAAE,CAAC,iBAAU,CAAC,gBAAgB,EAAE,iBAAU,CAAC,gBAAgB,CAAC,CAAC;iCAC3F;gCACD,EAAE,GAAG,EAAE,CAAC,4BAA4B,EAAE,IAAI,CAAC,EAAE;gCAC7C,EAAE,GAAG,EAAE,CAAC,sBAAsB,EAAE,0BAA0B,CAAC,EAAE;6BAC7D;yBACD;qBACD;iBACD;aACD;SACD,CAAC;IACH,CAAC;CACD;AAxhBD,4CAwhBC","sourcesContent":["import {\n\tCoupon,\n\tCouponModel,\n\tgetCoreUserModel,\n\tReferralSetting,\n\tReferralSettingModel,\n\tUser,\n\tUserModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport { SoftDeleteModel } from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport { mongoose } from '@typegoose/typegoose';\nimport { getRoundSecondaryCurrencyStage } from '@lyxa.ai/core/dist/utilities/pipelines/common-pipeline-functions';\nimport {\n\tCouponType,\n\tItemType,\n\tRegularOrderStatus,\n\tStatus,\n\tValueType,\n} from '@lyxa.ai/core/dist/utilities/enum';\nimport { getExchangeRate } from '@lyxa.ai/core/dist/utilities/currency';\nimport { roundSecondaryCurrency } from '@lyxa.ai/core/dist/utilities/shared';\nimport { dayjs } from '@lyxa.ai/core/dist/utilities/dayjs';\n\nexport class CouponRepository {\n\tprivate readonly model: typeof CouponModel & SoftDeleteModel<Coupon>;\n\tprivate readonly userModel: typeof UserModel & SoftDeleteModel<User>;\n\tprivate readonly referralSettingsModel: typeof ReferralSettingModel & SoftDeleteModel<ReferralSetting>;\n\tprivate _coreUserModel: ReturnType<typeof getCoreUserModel> | null = null;\n\n\tconstructor() {\n\t\tthis.model = CouponModel as typeof CouponModel & SoftDeleteModel<Coupon>;\n\t\tthis.userModel = UserModel as typeof UserModel & SoftDeleteModel<User>;\n\t\tthis.referralSettingsModel = ReferralSettingModel as typeof ReferralSettingModel &\n\t\t\tSoftDeleteModel<ReferralSetting>;\n\t}\n\n\tprivate get coreUserModel() {\n\t\tif (!this._coreUserModel) {\n\t\t\tthis._coreUserModel = getCoreUserModel();\n\t\t}\n\t\treturn this._coreUserModel;\n\t}\n\n\tpublic async getUserCoupons(\n\t\tuser: mongoose.Types.ObjectId | undefined,\n\t\tshop: mongoose.Types.ObjectId | undefined,\n\t\titemType: ItemType | undefined\n\t): Promise<any> {\n\t\tconst [exchangeRate, referralSettings] = await Promise.all([\n\t\t\tgetExchangeRate(),\n\t\t\tthis.referralSettingsModel.findOne(),\n\t\t]);\n\n\t\tconst secondaryReferralReceiverValue =\n\t\t\treferralSettings?.receiver?.valueType == ValueType.PERCENTAGE\n\t\t\t\t? null\n\t\t\t\t: roundSecondaryCurrency((referralSettings?.receiver?.value ?? 0) * exchangeRate);\n\n\t\tconst secondaryReferralReceiverMinOrderAmount = roundSecondaryCurrency(\n\t\t\t(referralSettings?.receiver?.minimumOrderValue ?? 0) * exchangeRate\n\t\t);\n\n\t\tconst now = dayjs().toDate();\n\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\t// Match the user\n\t\t\t{\n\t\t\t\t$match: { _id: user },\n\t\t\t},\n\n\t\t\t// Lookup coupons with nested referral/influencer/shop\n\t\t\t{\n\t\t\t\t$lookup: {\n\t\t\t\t\tfrom: 'coupons',\n\t\t\t\t\tlocalField: 'coupons',\n\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\tas: 'couponDocs',\n\t\t\t\t\tpipeline: [\n\t\t\t\t\t\t// Active and started\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t$expr: {\n\t\t\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t\t\t{ $eq: [{ $ifNull: ['$status', Status.ACTIVE] }, Status.ACTIVE] },\n\t\t\t\t\t\t\t\t\t\t{ $lte: [{ $ifNull: ['$duration.start', now] }, now] },\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t...(shop\n\t\t\t\t\t\t\t\t\t? {}\n\t\t\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t\t$nor: [\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ couponType: CouponType.INDIVIDUAL_STORE },\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ couponType: CouponType.SHOPS_CATEGORIES },\n\t\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ couponType: { $ne: CouponType.CUSTOM_COUPON } },\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ $or: [{ shops: { $exists: false } }, { shops: { $size: 0 } }] },\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ $or: [{ itemTypes: { $exists: false } }, { itemTypes: { $size: 0 } }] },\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t// Lookup referralUser and influencer\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\tfrom: 'users',\n\t\t\t\t\t\t\t\tlocalField: 'referralUser',\n\t\t\t\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\t\t\t\tas: 'referralUser',\n\t\t\t\t\t\t\t\tpipeline: [{ $project: { coreUser: 1 } }],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\tfrom: 'users',\n\t\t\t\t\t\t\t\tlocalField: 'influencer',\n\t\t\t\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\t\t\t\tas: 'influencer',\n\t\t\t\t\t\t\t\tpipeline: [{ $project: { coreUser: 1 } }],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t// Lookup shop\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\tfrom: 'shops',\n\t\t\t\t\t\t\t\tlocalField: 'shop',\n\t\t\t\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\t\t\t\tas: 'shop',\n\t\t\t\t\t\t\t\tpipeline: [{ $project: { name: 1 } }],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ $unwind: { path: '$shop', preserveNullAndEmptyArrays: true } },\n\t\t\t\t\t\t{ $unwind: { path: '$referralUser', preserveNullAndEmptyArrays: true } },\n\t\t\t\t\t\t{ $unwind: { path: '$influencer', preserveNullAndEmptyArrays: true } },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$addFields: {\n\t\t\t\t\t\t\t\treferringUser: { $ifNull: ['$referralUser.coreUser', '$influencer.coreUser'] },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ $project: { referralUser: 0, influencer: 0 } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Lookup orders once and compute:\n\t\t\t// - total usage per coupon\n\t\t\t// - all shops user ordered from\n\t\t\t{\n\t\t\t\t$lookup: {\n\t\t\t\t\tfrom: 'orders',\n\t\t\t\t\tlet: { userId: '$_id' },\n\t\t\t\t\tpipeline: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t$expr: { $eq: ['$user', '$$userId'] },\n\t\t\t\t\t\t\t\tstatus: { $ne: RegularOrderStatus.CANCELLED },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$facet: {\n\t\t\t\t\t\t\t\t// ordered shops for first-order logic\n\t\t\t\t\t\t\t\torderedShops: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t\t\t\t\t_id: null,\n\t\t\t\t\t\t\t\t\t\t\tshops: { $addToSet: '$shop' },\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\n\t\t\t\t\t\t\t\t// coupon usage WITH SNAPSHOT\n\t\t\t\t\t\t\t\tcouponUsage: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t\t\t\t'adjustedFinance.coupon': { $exists: true },\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t\t\t\t\t_id: '$adjustedFinance.coupon.coupon',\n\t\t\t\t\t\t\t\t\t\t\tsnap: { $first: '$adjustedFinance.coupon' },\n\t\t\t\t\t\t\t\t\t\t\tcount: { $sum: 1 },\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tas: 'orderStats',\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\torderedShops: {\n\t\t\t\t\t\t$ifNull: [{ $arrayElemAt: ['$orderStats.orderedShops.shops', 0] }, []],\n\t\t\t\t\t},\n\t\t\t\t\tcouponUsage: {\n\t\t\t\t\t\t$ifNull: [{ $arrayElemAt: ['$orderStats.couponUsage', 0] }, []],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Unwind coupons to classify individually\n\t\t\t{ $unwind: '$couponDocs' },\n\n\t\t\t// Filter coupons by shop/itemType\n\t\t\t...(shop ? [this.filterShopCoupons(shop, itemType)] : []),\n\n\t\t\t// First-order-only coupon filter\n\t\t\t...(shop ? this.filterFirstOrderOnlyCoupons(shop) : this.filterFirstOrderOnlyCoupons()),\n\n\t\t\t// Compute usage count per coupon\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\tusageData: {\n\t\t\t\t\t\t$arrayElemAt: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$filter: {\n\t\t\t\t\t\t\t\t\tinput: '$couponUsage',\n\t\t\t\t\t\t\t\t\tas: 'cu',\n\t\t\t\t\t\t\t\t\tcond: { $eq: ['$$cu._id', '$couponDocs._id'] },\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\tusedCount: { $ifNull: ['$usageData.count', 0] },\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Expiration & secondary currency\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\tisExpired: {\n\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t{ $gt: [{ $strLenCP: { $ifNull: ['$couponDocs.expirationReason', ''] } }, 0] },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t\t{ $ifNull: ['$couponDocs.duration.end', false] },\n\t\t\t\t\t\t\t\t\t{ $lt: ['$couponDocs.duration.end', now] },\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t\texchangeRate,\n\t\t\t\t\t'couponDocs.value': {\n\t\t\t\t\t\t$switch: {\n\t\t\t\t\t\t\tbranches: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcase: { $eq: ['$couponDocs.couponType', CouponType.REFERRAL_CODE] },\n\t\t\t\t\t\t\t\t\tthen: referralSettings?.receiver?.value ?? 0,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdefault: '$couponDocs.value',\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t'couponDocs.valueType': {\n\t\t\t\t\t\t$switch: {\n\t\t\t\t\t\t\tbranches: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcase: { $eq: ['$couponDocs.couponType', CouponType.REFERRAL_CODE] },\n\t\t\t\t\t\t\t\t\tthen: referralSettings?.receiver?.valueType ?? 0,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdefault: '$couponDocs.valueType',\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t'couponDocs.secondaryCurrencyValue': {\n\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t{ $eq: ['$couponDocs.valueType', ValueType.PERCENTAGE] },\n\t\t\t\t\t\t\tnull,\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$switch: {\n\t\t\t\t\t\t\t\t\tbranches: [\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tcase: { $eq: ['$couponDocs.couponType', CouponType.REFERRAL_CODE] },\n\t\t\t\t\t\t\t\t\t\t\tthen: secondaryReferralReceiverValue,\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\tdefault: getRoundSecondaryCurrencyStage({\n\t\t\t\t\t\t\t\t\t\t$multiply: ['$couponDocs.value', exchangeRate],\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t\t'couponDocs.secondaryCurrencyMaxDiscountLimit': {\n\t\t\t\t\t\t$switch: {\n\t\t\t\t\t\t\tbranches: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcase: { $eq: ['$couponDocs.couponType', CouponType.REFERRAL_CODE] },\n\t\t\t\t\t\t\t\t\tthen: null,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdefault: getRoundSecondaryCurrencyStage({\n\t\t\t\t\t\t\t\t$multiply: ['$couponDocs.maxDiscountLimit', exchangeRate],\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t'couponDocs.secondaryCurrencyMinOrderValue': {\n\t\t\t\t\t\t$switch: {\n\t\t\t\t\t\t\tbranches: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcase: { $eq: ['$couponDocs.couponType', CouponType.REFERRAL_CODE] },\n\t\t\t\t\t\t\t\t\tthen: secondaryReferralReceiverMinOrderAmount,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdefault: getRoundSecondaryCurrencyStage({\n\t\t\t\t\t\t\t\t$multiply: ['$couponDocs.minOrderValue', exchangeRate],\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Classify category\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\tcategory: {\n\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t'$isExpired',\n\t\t\t\t\t\t\t'expired',\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\t\t{ $gt: ['$usedCount', 0] },\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t\t\t\t\t\t\t{ $not: ['$couponDocs.orderLimitPerUser'] },\n\t\t\t\t\t\t\t\t\t\t\t\t\t{ $lt: ['$usedCount', '$couponDocs.orderLimitPerUser'] },\n\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t'available',\n\t\t\t\t\t\t\t\t\t\t\t'used',\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t'available',\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Group into arrays\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: '$_id',\n\t\t\t\t\tavailable: {\n\t\t\t\t\t\t$push: { $cond: [{ $eq: ['$category', 'available'] }, '$couponDocs', '$$REMOVE'] },\n\t\t\t\t\t},\n\t\t\t\t\tused: {\n\t\t\t\t\t\t$push: { $cond: [{ $eq: ['$category', 'used'] }, '$couponDocs', '$$REMOVE'] },\n\t\t\t\t\t},\n\t\t\t\t\texpired: {\n\t\t\t\t\t\t$push: { $cond: [{ $eq: ['$category', 'expired'] }, '$couponDocs', '$$REMOVE'] },\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t{ $project: { _id: 0 } },\n\t\t];\n\n\t\tconst [results] = await this.userModel.aggregate(pipeline);\n\n\t\tif (!results) {\n\t\t\treturn {\n\t\t\t\tavailable: [],\n\t\t\t\tused: [],\n\t\t\t\texpired: [],\n\t\t\t};\n\t\t}\n\n\t\tconst populatedResults = await this.userModel.populate(results, {\n\t\t\tpath: 'available.referringUser',\n\t\t\tmodel: this.coreUserModel,\n\t\t\tselect: { _id: 0, name: 1 },\n\t\t});\n\n\t\treturn populatedResults;\n\t}\n\n\tpublic addLabelsToAllCoupons(coupons: any): any {\n\t\treturn {\n\t\t\tavailable: this.addLabelsToCoupons(coupons.available).reverse(), // Just to make newly added coupon at the top\n\t\t\tused: this.addLabelsToCoupons(coupons.used).reverse(), // Just to make newly added coupon at the top\n\t\t\texpired: this.addLabelsToCoupons(coupons.expired).reverse(), // Just to make newly added coupon at the top\n\t\t};\n\t}\n\n\tpublic addLabelsToCoupons(coupons: any[]): any[] {\n\t\tconst resultsWithLabels = coupons.map(coupon => {\n\t\t\t// couponOfferLabel\n\t\t\tconst valuePart =\n\t\t\t\tcoupon.valueType === 'fixed'\n\t\t\t\t\t? this.formatCurrency(coupon.value, coupon.secondaryCurrencyValue)\n\t\t\t\t\t: `${coupon.value}%`;\n\n\t\t\tconst maxDiscountPart =\n\t\t\t\tcoupon.isMaxDiscountLimitEnabled && coupon.maxDiscountLimit > 0\n\t\t\t\t\t? ` up to ${this.formatCurrency(coupon.maxDiscountLimit, coupon.secondaryCurrencyMaxDiscountLimit)}`\n\t\t\t\t\t: '';\n\n\t\t\tconst influencerPart = coupon.referringUser ? ` (Referred by ${coupon.referringUser.name})` : '';\n\n\t\t\tcoupon.couponOfferLabel = `Save ${valuePart}${maxDiscountPart}${influencerPart}`;\n\n\t\t\t// couponValidityLabel\n\t\t\tlet validityParts: string[] = [];\n\n\t\t\t// 1. Minimum Order\n\t\t\tif (\n\t\t\t\tcoupon.couponType === CouponType.REFERRAL_CODE\n\t\t\t) {\n\t\t\t\tvalidityParts.push(\n\t\t\t\t\t`Minimum order ${this.formatCurrency(null, coupon.secondaryCurrencyMinOrderValue)}`\n\t\t\t\t);\n\t\t\t} else if (coupon.isMinOrderValueEnabled && coupon.minOrderValue > 0) {\n\t\t\t\tvalidityParts.push(\n\t\t\t\t\t`Minimum order ${this.formatCurrency(coupon.minOrderValue, coupon.secondaryCurrencyMinOrderValue)}`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// 2. Order Limit\n\t\t\tif (coupon.isOrderLimitPerUserEnabled && coupon.orderLimitPerUser > 0) {\n\t\t\t\tvalidityParts.push(`Applied on ${coupon.orderLimitPerUser} order`);\n\t\t\t}\n\n\t\t\t// if(coupon.couponType === CouponType.REFERRAL_CODE || coupon.couponType === CouponType.REFERRAL_REWARD){\n\t\t\t// \tvalidityParts = [];\n\t\t\t// \tvalidityParts.push(`Applied on 1 Order`);\n\t\t\t// }\n\n\t\t\tcoupon.couponValidityLabel = validityParts.join('. ');\n\n\t\t\treturn coupon;\n\t\t});\n\n\t\treturn resultsWithLabels;\n\t}\n\n\tprivate formatCurrency(value: any, secondaryCurrencyValue: any) {\n\t\t// Use coma separator for thousands and round secondary currency value\n\t\tconst formatterUSD = new Intl.NumberFormat('en-US', {\n\t\t\tminimumFractionDigits: 0,\n\t\t\tmaximumFractionDigits: 2,\n\t\t});\n\n\t\tconst formatterLBP = new Intl.NumberFormat('en-US', {\n\t\t\tminimumFractionDigits: 0,\n\t\t});\n\n\t\tif (secondaryCurrencyValue !== null && secondaryCurrencyValue !== undefined) {\n\t\t\treturn 'LBP ' + formatterLBP.format(Number(secondaryCurrencyValue));\n\t\t}\n\n\t\treturn '$' + formatterUSD.format(Number(value ?? 0));\n\t}\n\n\tprivate filterShopCoupons(shop: mongoose.Types.ObjectId, itemType?: ItemType): mongoose.PipelineStage {\n\t\treturn {\n\t\t\t$match: {\n\t\t\t\t$expr: {\n\t\t\t\t\t$or: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{ $eq: ['$couponDocs.couponType', CouponType.INDIVIDUAL_STORE] },\n\t\t\t\t\t\t\t\t{ $eq: ['$couponDocs.shop._id', shop] },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{ $eq: ['$couponDocs.couponType', CouponType.SHOPS_CATEGORIES] },\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t\t\t\t{ $eq: ['$couponDocs.itemType', itemType] },\n\t\t\t\t\t\t\t\t\t\t{ $in: [shop, { $ifNull: ['$couponDocs.shops', []] }] },\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$not: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t$in: ['$couponDocs.couponType', [CouponType.INDIVIDUAL_STORE, CouponType.SHOPS_CATEGORIES]],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t}\n\n\tprivate filterFirstOrderOnlyCoupons(shop?: mongoose.Types.ObjectId): mongoose.PipelineStage[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\thasOrderedFromStore: {\n\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t{ $eq: ['$couponDocs.couponType', CouponType.INDIVIDUAL_STORE] },\n\t\t\t\t\t\t\t{ $in: [shop ?? '$couponDocs.shop._id', '$orderedShops'] },\n\t\t\t\t\t\t\tfalse,\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t\thasUnusedShopInCategory: {\n\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t{ $eq: ['$couponDocs.couponType', CouponType.SHOPS_CATEGORIES] },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\t\t{ $ifNull: [shop, false] },\n\t\t\t\t\t\t\t\t\t{ $in: [shop, '$orderedShops'] },\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$eq: [\n\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t$size: { $setDifference: [{ $ifNull: ['$couponDocs.shops', []] }, '$orderedShops'] },\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tfalse,\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\t$match: {\n\t\t\t\t\t$expr: {\n\t\t\t\t\t\t$not: {\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t$in: ['$couponDocs.couponType', [CouponType.INDIVIDUAL_STORE, CouponType.SHOPS_CATEGORIES]],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{ $eq: ['$couponDocs.firstOrderOnly', true] },\n\t\t\t\t\t\t\t\t{ $or: ['$hasOrderedFromStore', '$hasUnusedShopInCategory'] },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\t}\n}\n"]}
1
+ {"version":3,"file":"CouponRepository.js","sourceRoot":"/","sources":["modules/coupon/services/patterns/repository/CouponRepository.ts"],"names":[],"mappings":";;;AAAA,sEAQmD;AAGnD,gHAAkH;AAClH,4DAO2C;AAC3C,oEAAwE;AACxE,gEAA6E;AAC7E,8DAA2D;AAE3D,MAAa,gBAAgB;IACX,KAAK,CAA+C;IACpD,SAAS,CAA2C;IACpD,qBAAqB,CAAiE;IAC/F,cAAc,GAA+C,IAAI,CAAC;IAE1E;QACC,IAAI,CAAC,KAAK,GAAG,oBAA2D,CAAC;QACzE,IAAI,CAAC,SAAS,GAAG,kBAAqD,CAAC;QACvE,IAAI,CAAC,qBAAqB,GAAG,6BACI,CAAC;IACnC,CAAC;IAED,IAAY,aAAa;QACxB,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;YAC1B,IAAI,CAAC,cAAc,GAAG,IAAA,yBAAgB,GAAE,CAAC;QAC1C,CAAC;QACD,OAAO,IAAI,CAAC,cAAc,CAAC;IAC5B,CAAC;IAEM,KAAK,CAAC,cAAc,CAC1B,IAAyC,EACzC,IAAyC,EACzC,MAA2C,EAC3C,QAA8B;QAE9B,MAAM,CAAC,YAAY,EAAE,gBAAgB,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;YAC1D,IAAA,0BAAe,GAAE;YACjB,IAAI,CAAC,qBAAqB,CAAC,OAAO,EAAE;SACpC,CAAC,CAAC;QAEH,MAAM,8BAA8B,GACnC,gBAAgB,EAAE,QAAQ,EAAE,SAAS,IAAI,gBAAS,CAAC,UAAU;YAC5D,CAAC,C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AAmB;4BACnB,KAAK;yBACL;qBACD;oBACD,uBAAuB,EAAE;wBACxB,KAAK,EAAE;4BACN,EAAE,GAAG,EAAE,CAAC,wBAAwB,EAAE,iBAAU,CAAC,gBAAgB,CAAC,EAAE;4BAChE,wBAAwB;4BACxB,KAAK;yBACL;qBACD;iBACD;aACD;YACD;gBACC,MAAM,EAAE;oBACP,KAAK,EAAE;wBACN,IAAI,EAAE;4BACL,IAAI,EAAE;gCACL;oCACC,GAAG,EAAE,CAAC,wBAAwB,EAAE,CAAC,iBAAU,CAAC,gBAAgB,EAAE,iBAAU,CAAC,gBAAgB,CAAC,CAAC;iCAC3F;gCACD,EAAE,GAAG,EAAE,CAAC,4BAA4B,EAAE,IAAI,CAAC,EAAE;gCAC7C,EAAE,GAAG,EAAE,CAAC,sBAAsB,EAAE,0BAA0B,CAAC,EAAE;6BAC7D;yBACD;qBACD;iBACD;aACD;SACD,CAAC;IACH,CAAC;CACD;AA3oBD,4CA2oBC","sourcesContent":["import {\n\tCoupon,\n\tCouponModel,\n\tgetCoreUserModel,\n\tReferralSetting,\n\tReferralSettingModel,\n\tUser,\n\tUserModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport { SoftDeleteModel } from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport { mongoose } from '@typegoose/typegoose';\nimport { getRoundSecondaryCurrencyStage } from '@lyxa.ai/core/dist/utilities/pipelines/common-pipeline-functions';\nimport {\n\tCouponType,\n\tItemType,\n\tRegularOrderStatus,\n\tServiceBookingStatus,\n\tStatus,\n\tValueType,\n} from '@lyxa.ai/core/dist/utilities/enum';\nimport { getExchangeRate } from '@lyxa.ai/core/dist/utilities/currency';\nimport { roundSecondaryCurrency } from '@lyxa.ai/core/dist/utilities/shared';\nimport { dayjs } from '@lyxa.ai/core/dist/utilities/dayjs';\n\nexport class CouponRepository {\n\tprivate readonly model: typeof CouponModel & SoftDeleteModel<Coupon>;\n\tprivate readonly userModel: typeof UserModel & SoftDeleteModel<User>;\n\tprivate readonly referralSettingsModel: typeof ReferralSettingModel & SoftDeleteModel<ReferralSetting>;\n\tprivate _coreUserModel: ReturnType<typeof getCoreUserModel> | null = null;\n\n\tconstructor() {\n\t\tthis.model = CouponModel as typeof CouponModel & SoftDeleteModel<Coupon>;\n\t\tthis.userModel = UserModel as typeof UserModel & SoftDeleteModel<User>;\n\t\tthis.referralSettingsModel = ReferralSettingModel as typeof ReferralSettingModel &\n\t\t\tSoftDeleteModel<ReferralSetting>;\n\t}\n\n\tprivate get coreUserModel() {\n\t\tif (!this._coreUserModel) {\n\t\t\tthis._coreUserModel = getCoreUserModel();\n\t\t}\n\t\treturn this._coreUserModel;\n\t}\n\n\tpublic async getUserCoupons(\n\t\tuser: mongoose.Types.ObjectId | undefined,\n\t\tshop: mongoose.Types.ObjectId | undefined,\n\t\tvendor: mongoose.Types.ObjectId | undefined,\n\t\titemType: ItemType | undefined\n\t): Promise<any> {\n\t\tconst [exchangeRate, referralSettings] = await Promise.all([\n\t\t\tgetExchangeRate(),\n\t\t\tthis.referralSettingsModel.findOne(),\n\t\t]);\n\n\t\tconst secondaryReferralReceiverValue =\n\t\t\treferralSettings?.receiver?.valueType == ValueType.PERCENTAGE\n\t\t\t\t? null\n\t\t\t\t: roundSecondaryCurrency((referralSettings?.receiver?.value ?? 0) * exchangeRate);\n\n\t\tconst secondaryReferralReceiverMinOrderAmount = roundSecondaryCurrency(\n\t\t\t(referralSettings?.receiver?.minimumOrderValue ?? 0) * exchangeRate\n\t\t);\n\n\t\tconst now = dayjs().toDate();\n\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\t// Match the user\n\t\t\t{\n\t\t\t\t$match: { _id: user },\n\t\t\t},\n\n\t\t\t// Lookup coupons with nested referral/influencer/shop\n\t\t\t{\n\t\t\t\t$lookup: {\n\t\t\t\t\tfrom: 'coupons',\n\t\t\t\t\tlocalField: 'coupons',\n\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\tas: 'couponDocs',\n\t\t\t\t\tpipeline: [\n\t\t\t\t\t\t// Active and started\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t$expr: {\n\t\t\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t\t\t{ $eq: [{ $ifNull: ['$status', Status.ACTIVE] }, Status.ACTIVE] },\n\t\t\t\t\t\t\t\t\t\t{ $lte: [{ $ifNull: ['$duration.start', now] }, now] },\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t...(shop || vendor\n\t\t\t\t\t\t\t\t\t? {}\n\t\t\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t\t$nor: [\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ couponType: CouponType.INDIVIDUAL_STORE },\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ couponType: CouponType.SHOPS_CATEGORIES },\n\t\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ couponType: { $ne: CouponType.CUSTOM_COUPON } },\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ $or: [{ shops: { $exists: false } }, { shops: { $size: 0 } }] },\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ $or: [{ vendors: { $exists: false } }, { vendors: { $size: 0 } }] },\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{ $or: [{ itemTypes: { $exists: false } }, { itemTypes: { $size: 0 } }] },\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t// Lookup referralUser and influencer\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\tfrom: 'users',\n\t\t\t\t\t\t\t\tlocalField: 'referralUser',\n\t\t\t\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\t\t\t\tas: 'referralUser',\n\t\t\t\t\t\t\t\tpipeline: [{ $project: { coreUser: 1 } }],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\tfrom: 'users',\n\t\t\t\t\t\t\t\tlocalField: 'influencer',\n\t\t\t\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\t\t\t\tas: 'influencer',\n\t\t\t\t\t\t\t\tpipeline: [{ $project: { coreUser: 1 } }],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t// Lookup shop\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\tfrom: 'shops',\n\t\t\t\t\t\t\t\tlocalField: 'shop',\n\t\t\t\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\t\t\t\tas: 'shop',\n\t\t\t\t\t\t\t\tpipeline: [{ $project: { name: 1 } }],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t// Lookup vendor\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t\t\tfrom: 'vendors',\n\t\t\t\t\t\t\t\tlocalField: 'vendor',\n\t\t\t\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\t\t\t\tas: 'vendor',\n\t\t\t\t\t\t\t\tpipeline: [{ $project: { name: 1 } }],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ $unwind: { path: '$shop', preserveNullAndEmptyArrays: true } },\n\t\t\t\t\t\t{ $unwind: { path: '$vendor', preserveNullAndEmptyArrays: true } },\n\t\t\t\t\t\t{ $unwind: { path: '$referralUser', preserveNullAndEmptyArrays: true } },\n\t\t\t\t\t\t{ $unwind: { path: '$influencer', preserveNullAndEmptyArrays: true } },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$addFields: {\n\t\t\t\t\t\t\t\treferringUser: { $ifNull: ['$referralUser.coreUser', '$influencer.coreUser'] },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ $project: { referralUser: 0, influencer: 0 } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Lookup orders once and compute:\n\t\t\t// - total usage per coupon\n\t\t\t// - all shops user ordered from\n\t\t\t{\n\t\t\t\t$lookup: {\n\t\t\t\t\tfrom: 'orders',\n\t\t\t\t\tlet: { userId: '$_id' },\n\t\t\t\t\tpipeline: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t$expr: { $eq: ['$user', '$$userId'] },\n\t\t\t\t\t\t\t\tstatus: { $ne: RegularOrderStatus.CANCELLED },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$facet: {\n\t\t\t\t\t\t\t\t// ordered shops for first-order logic\n\t\t\t\t\t\t\t\torderedShops: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t\t\t\t\t_id: null,\n\t\t\t\t\t\t\t\t\t\t\tshops: { $addToSet: '$shop' },\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\n\t\t\t\t\t\t\t\t// coupon usage WITH SNAPSHOT\n\t\t\t\t\t\t\t\tcouponUsage: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t\t\t\t'adjustedFinance.coupon.coupon': { $exists: true },\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t\t\t\t\t_id: '$adjustedFinance.coupon.coupon',\n\t\t\t\t\t\t\t\t\t\t\tsnap: { $first: '$adjustedFinance.coupon' },\n\t\t\t\t\t\t\t\t\t\t\tcount: { $sum: 1 },\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tas: 'orderStats',\n\t\t\t\t},\n\t\t\t},\n\t\t\t// Lookup service bookings once and compute vendor-side first-order/usage data.\n\t\t\t{\n\t\t\t\t$lookup: {\n\t\t\t\t\tfrom: 'serviceBookings',\n\t\t\t\t\tlet: { userId: '$_id' },\n\t\t\t\t\tpipeline: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t$expr: { $eq: ['$user', '$$userId'] },\n\t\t\t\t\t\t\t\tstatus: { $ne: ServiceBookingStatus.CANCELLED },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t$facet: {\n\t\t\t\t\t\t\t\torderedVendors: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t\t\t\t\t_id: null,\n\t\t\t\t\t\t\t\t\t\t\tvendors: { $addToSet: '$vendor' },\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tcouponUsage: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t\t\t\t\t'adjustedFinance.coupon.coupon': { $exists: true },\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t\t\t\t\t_id: '$adjustedFinance.coupon.coupon',\n\t\t\t\t\t\t\t\t\t\t\tsnap: { $first: '$adjustedFinance.coupon' },\n\t\t\t\t\t\t\t\t\t\t\tcount: { $sum: 1 },\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tas: 'serviceBookingStats',\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\torderedShops: {\n\t\t\t\t\t\t$ifNull: [{ $arrayElemAt: ['$orderStats.orderedShops.shops', 0] }, []],\n\t\t\t\t\t},\n\t\t\t\t\torderedVendors: {\n\t\t\t\t\t\t$ifNull: [{ $arrayElemAt: ['$serviceBookingStats.orderedVendors.vendors', 0] }, []],\n\t\t\t\t\t},\n\t\t\t\t\tcouponUsage: {\n\t\t\t\t\t\t$concatArrays: [\n\t\t\t\t\t\t\t{ $ifNull: [{ $arrayElemAt: ['$orderStats.couponUsage', 0] }, []] },\n\t\t\t\t\t\t\t{ $ifNull: [{ $arrayElemAt: ['$serviceBookingStats.couponUsage', 0] }, []] },\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Unwind coupons to classify individually\n\t\t\t{ $unwind: '$couponDocs' },\n\n\t\t\t// Filter coupons by shop/vendor/itemType\n\t\t\t...(shop || vendor ? this.filterShopVendorCoupons(shop, vendor, itemType) : []),\n\n\t\t\t// First-order-only coupon filter\n\t\t\t...this.filterFirstOrderOnlyCoupons({ shop, vendor }),\n\n\t\t\t// Compute usage count per coupon\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\tusageData: {\n\t\t\t\t\t\t$arrayElemAt: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$filter: {\n\t\t\t\t\t\t\t\t\tinput: '$couponUsage',\n\t\t\t\t\t\t\t\t\tas: 'cu',\n\t\t\t\t\t\t\t\t\tcond: { $eq: ['$$cu._id', '$couponDocs._id'] },\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\tusedCount: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$map: {\n\t\t\t\t\t\t\t\tinput: {\n\t\t\t\t\t\t\t\t\t$filter: {\n\t\t\t\t\t\t\t\t\t\tinput: '$couponUsage',\n\t\t\t\t\t\t\t\t\t\tas: 'cu',\n\t\t\t\t\t\t\t\t\t\tcond: { $eq: ['$$cu._id', '$couponDocs._id'] },\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tas: 'cu',\n\t\t\t\t\t\t\t\tin: { $ifNull: ['$$cu.count', 0] },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Expiration & secondary currency\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\tisExpired: {\n\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t{ $gt: [{ $strLenCP: { $ifNull: ['$couponDocs.expirationReason', ''] } }, 0] },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t\t{ $ifNull: ['$couponDocs.duration.end', false] },\n\t\t\t\t\t\t\t\t\t{ $lt: ['$couponDocs.duration.end', now] },\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t\texchangeRate,\n\t\t\t\t\t'couponDocs.value': {\n\t\t\t\t\t\t$switch: {\n\t\t\t\t\t\t\tbranches: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcase: { $eq: ['$couponDocs.couponType', CouponType.REFERRAL_CODE] },\n\t\t\t\t\t\t\t\t\tthen: referralSettings?.receiver?.value ?? 0,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdefault: '$couponDocs.value',\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t'couponDocs.valueType': {\n\t\t\t\t\t\t$switch: {\n\t\t\t\t\t\t\tbranches: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcase: { $eq: ['$couponDocs.couponType', CouponType.REFERRAL_CODE] },\n\t\t\t\t\t\t\t\t\tthen: referralSettings?.receiver?.valueType ?? 0,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdefault: '$couponDocs.valueType',\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t'couponDocs.secondaryCurrencyValue': {\n\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t{ $eq: ['$couponDocs.valueType', ValueType.PERCENTAGE] },\n\t\t\t\t\t\t\tnull,\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$switch: {\n\t\t\t\t\t\t\t\t\tbranches: [\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tcase: { $eq: ['$couponDocs.couponType', CouponType.REFERRAL_CODE] },\n\t\t\t\t\t\t\t\t\t\t\tthen: secondaryReferralReceiverValue,\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\tdefault: getRoundSecondaryCurrencyStage({\n\t\t\t\t\t\t\t\t\t\t$multiply: ['$couponDocs.value', exchangeRate],\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t\t'couponDocs.secondaryCurrencyMaxDiscountLimit': {\n\t\t\t\t\t\t$switch: {\n\t\t\t\t\t\t\tbranches: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcase: { $eq: ['$couponDocs.couponType', CouponType.REFERRAL_CODE] },\n\t\t\t\t\t\t\t\t\tthen: null,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdefault: getRoundSecondaryCurrencyStage({\n\t\t\t\t\t\t\t\t$multiply: ['$couponDocs.maxDiscountLimit', exchangeRate],\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t'couponDocs.secondaryCurrencyMinOrderValue': {\n\t\t\t\t\t\t$switch: {\n\t\t\t\t\t\t\tbranches: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcase: { $eq: ['$couponDocs.couponType', CouponType.REFERRAL_CODE] },\n\t\t\t\t\t\t\t\t\tthen: secondaryReferralReceiverMinOrderAmount,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tdefault: getRoundSecondaryCurrencyStage({\n\t\t\t\t\t\t\t\t$multiply: ['$couponDocs.minOrderValue', exchangeRate],\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Classify category\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\tcategory: {\n\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t'$isExpired',\n\t\t\t\t\t\t\t'expired',\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\t\t{ $gt: ['$usedCount', 0] },\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t\t\t\t\t\t\t{ $not: ['$couponDocs.orderLimitPerUser'] },\n\t\t\t\t\t\t\t\t\t\t\t\t\t{ $lt: ['$usedCount', '$couponDocs.orderLimitPerUser'] },\n\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t'available',\n\t\t\t\t\t\t\t\t\t\t\t'used',\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t'available',\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Group into arrays\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: '$_id',\n\t\t\t\t\tavailable: {\n\t\t\t\t\t\t$push: { $cond: [{ $eq: ['$category', 'available'] }, '$couponDocs', '$$REMOVE'] },\n\t\t\t\t\t},\n\t\t\t\t\tused: {\n\t\t\t\t\t\t$push: { $cond: [{ $eq: ['$category', 'used'] }, '$couponDocs', '$$REMOVE'] },\n\t\t\t\t\t},\n\t\t\t\t\texpired: {\n\t\t\t\t\t\t$push: { $cond: [{ $eq: ['$category', 'expired'] }, '$couponDocs', '$$REMOVE'] },\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t{ $project: { _id: 0 } },\n\t\t];\n\n\t\tconst [results] = await this.userModel.aggregate(pipeline);\n\n\t\tif (!results) {\n\t\t\treturn {\n\t\t\t\tavailable: [],\n\t\t\t\tused: [],\n\t\t\t\texpired: [],\n\t\t\t};\n\t\t}\n\n\t\tconst populatedResults = await this.userModel.populate(results, {\n\t\t\tpath: 'available.referringUser',\n\t\t\tmodel: this.coreUserModel,\n\t\t\tselect: { _id: 0, name: 1 },\n\t\t});\n\n\t\treturn populatedResults;\n\t}\n\n\tpublic addLabelsToAllCoupons(coupons: any): any {\n\t\treturn {\n\t\t\tavailable: this.addLabelsToCoupons(coupons.available).reverse(), // Just to make newly added coupon at the top\n\t\t\tused: this.addLabelsToCoupons(coupons.used).reverse(), // Just to make newly added coupon at the top\n\t\t\texpired: this.addLabelsToCoupons(coupons.expired).reverse(), // Just to make newly added coupon at the top\n\t\t};\n\t}\n\n\tpublic addLabelsToCoupons(coupons: any[]): any[] {\n\t\tconst resultsWithLabels = coupons.map(coupon => {\n\t\t\t// couponOfferLabel\n\t\t\tconst valuePart =\n\t\t\t\tcoupon.valueType === 'fixed'\n\t\t\t\t\t? this.formatCurrency(coupon.value, coupon.secondaryCurrencyValue)\n\t\t\t\t\t: `${coupon.value}%`;\n\n\t\t\tconst maxDiscountPart =\n\t\t\t\tcoupon.isMaxDiscountLimitEnabled && coupon.maxDiscountLimit > 0\n\t\t\t\t\t? ` up to ${this.formatCurrency(coupon.maxDiscountLimit, coupon.secondaryCurrencyMaxDiscountLimit)}`\n\t\t\t\t\t: '';\n\n\t\t\tconst influencerPart = coupon.referringUser ? ` (Referred by ${coupon.referringUser.name})` : '';\n\n\t\t\tcoupon.couponOfferLabel = `Save ${valuePart}${maxDiscountPart}${influencerPart}`;\n\n\t\t\t// couponValidityLabel\n\t\t\tlet validityParts: string[] = [];\n\n\t\t\t// 1. Minimum Order\n\t\t\tif (coupon.couponType === CouponType.REFERRAL_CODE) {\n\t\t\t\tvalidityParts.push(\n\t\t\t\t\t`Minimum order ${this.formatCurrency(null, coupon.secondaryCurrencyMinOrderValue)}`\n\t\t\t\t);\n\t\t\t} else if (coupon.isMinOrderValueEnabled && coupon.minOrderValue > 0) {\n\t\t\t\tvalidityParts.push(\n\t\t\t\t\t`Minimum order ${this.formatCurrency(coupon.minOrderValue, coupon.secondaryCurrencyMinOrderValue)}`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// 2. Order Limit\n\t\t\tif (coupon.isOrderLimitPerUserEnabled && coupon.orderLimitPerUser > 0) {\n\t\t\t\tvalidityParts.push(`Applied on ${coupon.orderLimitPerUser} order`);\n\t\t\t}\n\n\t\t\t// if(coupon.couponType === CouponType.REFERRAL_CODE || coupon.couponType === CouponType.REFERRAL_REWARD){\n\t\t\t// \tvalidityParts = [];\n\t\t\t// \tvalidityParts.push(`Applied on 1 Order`);\n\t\t\t// }\n\n\t\t\tcoupon.couponValidityLabel = validityParts.join('. ');\n\n\t\t\treturn coupon;\n\t\t});\n\n\t\treturn resultsWithLabels;\n\t}\n\n\tprivate formatCurrency(value: any, secondaryCurrencyValue: any) {\n\t\t// Use coma separator for thousands and round secondary currency value\n\t\tconst formatterUSD = new Intl.NumberFormat('en-US', {\n\t\t\tminimumFractionDigits: 0,\n\t\t\tmaximumFractionDigits: 2,\n\t\t});\n\n\t\tconst formatterLBP = new Intl.NumberFormat('en-US', {\n\t\t\tminimumFractionDigits: 0,\n\t\t});\n\n\t\tif (secondaryCurrencyValue !== null && secondaryCurrencyValue !== undefined) {\n\t\t\treturn 'LBP ' + formatterLBP.format(Number(secondaryCurrencyValue));\n\t\t}\n\n\t\treturn '$' + formatterUSD.format(Number(value ?? 0));\n\t}\n\n\tprivate filterShopVendorCoupons(\n\t\tshop?: mongoose.Types.ObjectId,\n\t\tvendor?: mongoose.Types.ObjectId,\n\t\titemType?: ItemType\n\t): mongoose.PipelineStage[] {\n\t\treturn [\n\t\t\t{\n\t\t\t\t$match: {\n\t\t\t\t\t$expr: {\n\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t\t{ $eq: ['$couponDocs.couponType', CouponType.INDIVIDUAL_STORE] },\n\t\t\t\t\t\t\t\t\tshop\n\t\t\t\t\t\t\t\t\t\t? { $eq: ['$couponDocs.shop._id', shop] }\n\t\t\t\t\t\t\t\t\t\t: { $eq: ['$couponDocs.vendor._id', vendor] },\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t\t{ $eq: ['$couponDocs.couponType', CouponType.SHOPS_CATEGORIES] },\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t\t\t\t\t{ $in: [itemType, { $ifNull: ['$couponDocs.itemTypes', []] }] },\n\t\t\t\t\t\t\t\t\t\t\t...(shop ? [{ $in: [shop, { $ifNull: ['$couponDocs.shops', []] }] }] : []),\n\t\t\t\t\t\t\t\t\t\t\t...(vendor ? [{ $in: [vendor, { $ifNull: ['$couponDocs.vendors', []] }] }] : []),\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$not: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$in: [\n\t\t\t\t\t\t\t\t\t\t\t'$couponDocs.couponType',\n\t\t\t\t\t\t\t\t\t\t\t[CouponType.INDIVIDUAL_STORE, CouponType.SHOPS_CATEGORIES],\n\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\t}\n\n\tprivate filterFirstOrderOnlyCoupons(data: {\n\t\tshop?: mongoose.Types.ObjectId;\n\t\tvendor?: mongoose.Types.ObjectId;\n\t}): mongoose.PipelineStage[] {\n\t\tconst { shop, vendor } = data;\n\t\tconst hasOrderedFromStore = vendor\n\t\t\t? { $in: [vendor, '$orderedVendors'] }\n\t\t\t: shop\n\t\t\t\t? { $in: [shop, '$orderedShops'] }\n\t\t\t\t: {\n\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t{ $in: ['$couponDocs.shop._id', '$orderedShops'] },\n\t\t\t\t\t\t\t{ $in: ['$couponDocs.vendor._id', '$orderedVendors'] },\n\t\t\t\t\t\t],\n\t\t\t\t\t};\n\t\tconst hasUnusedStoreInCategory = vendor\n\t\t\t? { $in: [vendor, '$orderedVendors'] }\n\t\t\t: shop\n\t\t\t\t? { $in: [shop, '$orderedShops'] }\n\t\t\t\t: {\n\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$eq: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$size: {\n\t\t\t\t\t\t\t\t\t\t\t$setDifference: [{ $ifNull: ['$couponDocs.shops', []] }, '$orderedShops'],\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t$eq: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t$size: {\n\t\t\t\t\t\t\t\t\t\t\t$setDifference: [{ $ifNull: ['$couponDocs.vendors', []] }, '$orderedVendors'],\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t};\n\n\t\treturn [\n\t\t\t{\n\t\t\t\t$addFields: {\n\t\t\t\t\thasOrderedFromStore: {\n\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t{ $eq: ['$couponDocs.couponType', CouponType.INDIVIDUAL_STORE] },\n\t\t\t\t\t\t\thasOrderedFromStore,\n\t\t\t\t\t\t\tfalse,\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t\thasUnusedShopInCategory: {\n\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t{ $eq: ['$couponDocs.couponType', CouponType.SHOPS_CATEGORIES] },\n\t\t\t\t\t\t\thasUnusedStoreInCategory,\n\t\t\t\t\t\t\tfalse,\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\t$match: {\n\t\t\t\t\t$expr: {\n\t\t\t\t\t\t$not: {\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t$in: ['$couponDocs.couponType', [CouponType.INDIVIDUAL_STORE, CouponType.SHOPS_CATEGORIES]],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{ $eq: ['$couponDocs.firstOrderOnly', true] },\n\t\t\t\t\t\t\t\t{ $or: ['$hasOrderedFromStore', '$hasUnusedShopInCategory'] },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"referral-reward-coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/referral-reward-coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAEN,oBAAoB,IAAI,KAAK,EAU7B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EACN,yBAAyB,EACzB,6BAA6B,IAAI,SAAS,EAC1C,6BAA6B,IAAI,SAAS,EAC1C,iBAAiB,EACjB,MAAM,6BAA6B,CAAC;AAQrC,qBACa,2BAA2B;IACvC,OAAO,CAAC,KAAK,CAA4D;IACzE,OAAO,CAAC,WAAW,CAA+C;IAClE,OAAO,CAAC,aAAa,CAAiE;IACtF,OAAO,CAAC,SAAS,CAA2C;IAC5D,OAAO,CAAC,UAAU,CAA2D;;IAehE,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IASrD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAuB9E,OAAO,CAAC,YAAY;YASN,kBAAkB;IAanB,4BAA4B,CAAC,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ;IAmE7D,cAAc,CAC1B,IAAI,EAAE,iBAAiB,EACvB,cAAc,EAAE,YAAY,CAAC,KAAK,CAAC,GACjC,OAAO,CAAC,yBAAyB,CAAC;YAiEvB,mBAAmB;CAUjC;AAED,eAAO,MAAM,2BAA2B,6BAA6C,CAAC"}
1
+ {"version":3,"file":"referral-reward-coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/referral-reward-coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAEN,oBAAoB,IAAI,KAAK,EAU7B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EACN,yBAAyB,EACzB,6BAA6B,IAAI,SAAS,EAC1C,6BAA6B,IAAI,SAAS,EAC1C,iBAAiB,EACjB,MAAM,6BAA6B,CAAC;AAUrC,qBACa,2BAA2B;IACvC,OAAO,CAAC,KAAK,CAA4D;IACzE,OAAO,CAAC,WAAW,CAA+C;IAClE,OAAO,CAAC,aAAa,CAAiE;IACtF,OAAO,CAAC,SAAS,CAA2C;IAC5D,OAAO,CAAC,UAAU,CAA2D;;IAehE,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IASrD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAuB9E,OAAO,CAAC,YAAY;YASN,kBAAkB;IAanB,4BAA4B,CAAC,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ;IAyE7D,cAAc,CAC1B,IAAI,EAAE,iBAAiB,EACvB,cAAc,EAAE,YAAY,CAAC,KAAK,CAAC,GACjC,OAAO,CAAC,yBAAyB,CAAC;YAmEvB,mBAAmB;CAUjC;AAED,eAAO,MAAM,2BAA2B,6BAA6C,CAAC"}
@@ -49,7 +49,9 @@ const models_1 = require("@lyxa.ai/core/dist/libraries/mongo/models/");
49
49
  const enum_1 = require("@lyxa.ai/core/dist/utilities/enum");
50
50
  const currency_1 = require("@lyxa.ai/core/dist/utilities/currency");
51
51
  const shared_1 = require("@lyxa.ai/core/dist/utilities/shared");
52
- const core_1 = require("@lyxa.ai/core");
52
+ const notification_1 = require("@lyxa.ai/core/dist/libraries/notification");
53
+ const event_1 = require("@lyxa.ai/core/dist/libraries/event");
54
+ const enum_2 = require("@lyxa.ai/core/dist/utilities/enum");
53
55
  let ReferralRewardCouponService = class ReferralRewardCouponService {
54
56
  model;
55
57
  couponModel;
@@ -142,9 +144,18 @@ let ReferralRewardCouponService = class ReferralRewardCouponService {
142
144
  isTotalOrderLimitEnabled: true,
143
145
  });
144
146
  await this.userModel.findByIdAndUpdate(user, { $push: { coupons: referralRewardCoupon._id } }, { new: true, runValidators: true });
145
- const dispatcher = (0, core_1.getLibraries)().getMicroServiceDispatcher();
146
- const notificationRouter = await dispatcher.getMicroService('notification');
147
- await notificationRouter.notificationRouter.sendPushNotificationForCoupon.mutate(referralRewardCoupon._id);
147
+ await (0, event_1.publishEvent)(new notification_1.NotificationEvent({
148
+ title: 'Referral reward received 🙌',
149
+ body: 'Nice one! Your referral reward is ready to use. Thank you for sharing Lyxa 💛',
150
+ data: {
151
+ saveNotification: true,
152
+ accountType: enum_2.NotificationAccountType.USER,
153
+ recipientId: user._id,
154
+ isGlobal: false,
155
+ isClickable: true,
156
+ clickType: enum_2.ClickType.CLICKABLE_COUPON,
157
+ },
158
+ }));
148
159
  }
149
160
  async validateCoupon(data, couponDocument) {
150
161
  if (couponDocument.isMinOrderValueEnabled &&
@@ -188,6 +199,8 @@ let ReferralRewardCouponService = class ReferralRewardCouponService {
188
199
  secondaryCompanyCut: secondaryDiscountAmount,
189
200
  shopCut: 0,
190
201
  secondaryShopCut: 0,
202
+ vendorCut: 0,
203
+ secondaryVendorCut: 0,
191
204
  },
192
205
  };
193
206
  }
@@ -1 +1 @@
1
- {"version":3,"file":"referral-reward-coupon.service.js","sourceRoot":"/","sources":["modules/coupon/services/referral-reward-coupon.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AAEzC,uEAYoD;AAQpD,4DAAkF;AAClF,oEAA2E;AAC3E,gEAAgG;AAChG,wCAA6C;AAItC,IAAM,2BAA2B,GAAjC,MAAM,2BAA2B;IAC/B,KAAK,CAA4D;IACjE,WAAW,CAA+C;IAC1D,aAAa,CAAiE;IAC9E,SAAS,CAA2C;IACpD,UAAU,CAA2D;IAE7E;QACC,IAAI,CAAC,KAAK,GAAG,kCAAsF,CAAC;QACpG,IAAI,CAAC,WAAW,GAAG,oBAA2D,CAAC;QAC/E,IAAI,CAAC,aAAa,GAAG,6BACY,CAAC;QAClC,IAAI,CAAC,SAAS,GAAG,kBAAqD,CAAC;QACvE,IAAI,CAAC,UAAU,GAAG,0BAA6E,CAAC;IACjG,CAAC;IAMM,KAAK,CAAC,MAAM,CAAC,IAAe;QAClC,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtC,CAAC;IAOM,KAAK,CAAC,MAAM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,2BAA2B,GAAG,uBAAuB,CAC1D;YACD,WAAW,EAAE;gBACZ,UAAU,EAAE,cAAc;gBAC1B,mBAAmB,EAAE,uBAAuB;gBAC5C,OAAO,EAAE,CAAC;gBACV,gBAAgB,EAAE,CAAC;aACnB;SACD,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,mBAAmB;QAChC,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAC7D,IAAI,CAAC,eAAe,EAAE,CAAC;YACtB,MAAM,IAAI,kBAAS,CAAC;gBACnB,IAAI,EAAE,WAAW;gBACjB,OAAO,EAAE,4BAA4B;aACrC,CAAC,CAAC;QACJ,CAAC;QACD,OAAO,eAAe,CAAC;IACxB,CAAC;CACD,CAAA;AA3NY,kEAA2B;sCAA3B,2BAA2B;IADvC,IAAA,gBAAO,GAAE;;GACG,2BAA2B,CA2NvC;AAEY,QAAA,2BAA2B,GAAG,gBAAS,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType, mongoose } from '@typegoose/typegoose';\nimport {\n\tReferralRewardCouponModel,\n\tReferralRewardCoupon as Model,\n\tReferralSetting,\n\tReferralSettingModel,\n\tRegularOrder,\n\tUser,\n\tUserModel,\n\tRegularOrderModel,\n\tCouponModel,\n\tCoupon,\n\tReferralCodeCoupon,\n} from '@lyxa.ai/core/dist/libraries/mongo/models/';\nimport {\n\tCouponValidationOutputDTO,\n\tCreateReferralRewardCouponDTO as CreateDTO,\n\tUpdateReferralRewardCouponDTO as UpdateDTO,\n\tValidateCouponDTO,\n} from '@modules/coupon/validations';\nimport { SoftDeleteModel } from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport { CouponType, Status, ValueType } from '@lyxa.ai/core/dist/utilities/enum';\nimport { convertToSecondary } from '@lyxa.ai/core/dist/utilities/currency';\nimport { roundBaseCurrency, roundSecondaryCurrency } from '@lyxa.ai/core/dist/utilities/shared';\nimport { getLibraries } from '@lyxa.ai/core';\nimport { NotificationMicroserviceRouter } from '@lyxa.ai/notification';\n\n@Service()\nexport class ReferralRewardCouponService {\n\tprivate model: typeof ReferralRewardCouponModel & SoftDeleteModel<Model>;\n\tprivate couponModel: typeof CouponModel & SoftDeleteModel<Coupon>;\n\tprivate settingsModel: typeof ReferralSettingModel & SoftDeleteModel<ReferralSetting>;\n\tprivate userModel: typeof UserModel & SoftDeleteModel<User>;\n\tprivate orderModel: typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\n\tconstructor() {\n\t\tthis.model = ReferralRewardCouponModel as typeof ReferralRewardCouponModel & SoftDeleteModel<Model>;\n\t\tthis.couponModel = CouponModel as typeof CouponModel & SoftDeleteModel<Coupon>;\n\t\tthis.settingsModel = ReferralSettingModel as typeof ReferralSettingModel &\n\t\t\tSoftDeleteModel<ReferralSetting>;\n\t\tthis.userModel = UserModel as typeof UserModel & SoftDeleteModel<User>;\n\t\tthis.orderModel = RegularOrderModel as typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\t}\n\n\t/**\n\t * Create a new record\n\t * @param data Data for the new record\n\t */\n\tpublic async create(data: CreateDTO): Promise<DocumentType<Model>> {\n\t\treturn await this.model.create(data);\n\t}\n\n\t/**\n\t * Update record\n\t * @param id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(id: string, data: UpdateDTO): Promise<DocumentType<Model>> {\n\t\tconst record = await this.model.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id: id,\n\t\t\t\tdeletedAt: null,\n\t\t\t},\n\t\t\tdata,\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\tif (!record) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: `Record not found`,\n\t\t\t});\n\t\t}\n\n\t\treturn record;\n\t}\n\n\tprivate generateCode(length = 6): string {\n\t\tconst chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';\n\t\tlet result = '';\n\t\tfor (let i = 0; i < length; i++) {\n\t\t\tresult += chars.charAt(Math.floor(Math.random() * chars.length));\n\t\t}\n\t\treturn result;\n\t}\n\n\tprivate async generateCouponCode(): Promise<string> {\n\t\tlet code: string = 'SAMPLE';\n\t\tlet exists = true;\n\n\t\twhile (exists) {\n\t\t\tcode = this.generateCode();\n\t\t\texists = (await this.model.exists({ code: code })) ? true : false;\n\t\t}\n\n\t\treturn code;\n\t}\n\n\t// give referral reward to user\n\tpublic async giveReferralRewardToReferrer(orderId: mongoose.Types.ObjectId) {\n\t\tconst order = (await this.orderModel.findById(orderId)) as DocumentType<RegularOrder>;\n\n\t\tif (!order) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (!order.adjustedFinance?.coupon?.coupon?._id) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst orderCoupon = await this.couponModel.findById(order.adjustedFinance?.coupon?.coupon?._id);\n\n\t\tif (!orderCoupon || orderCoupon.couponType !== CouponType.REFERRAL_CODE) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst user = (orderCoupon as DocumentType<ReferralCodeCoupon>).referralUser;\n\n\t\tconst referralCouponSettings = await this.getReferralSettings();\n\n\t\tif (!referralCouponSettings.sender) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst referralRewardCoupon = await this.create({\n\t\t\tcode: await this.generateCouponCode(),\n\t\t\tstatus: Status.ACTIVE,\n\t\t\tcouponType: CouponType.REFERRAL_REWARD,\n\t\t\tduration: {\n\t\t\t\tstart: new Date(),\n\t\t\t},\n\t\t\tforNewUserOnly: false,\n\t\t\tuser: user._id,\n\t\t\tminOrderValue: referralCouponSettings.sender?.minimumOrderValue ?? null,\n\t\t\tvalue: referralCouponSettings.sender?.value,\n\t\t\tvalueType: referralCouponSettings.sender?.valueType,\n\t\t\treferralCodeUsedBy: order.user as mongoose.Types.ObjectId,\n\t\t\treferralCodeUsedOnOrder: order._id,\n\t\t\texpirationReason: null,\n\t\t\tmaxDiscountLimit: null,\n\t\t\torderLimitPerUser: 1,\n\t\t\tspendLimit: null,\n\t\t\ttotalOrderLimit: 1,\n\t\t\tisMinOrderValueEnabled: !!referralCouponSettings.sender?.minimumOrderValue,\n\t\t\tisMaxDiscountLimitEnabled: false,\n\t\t\tisOrderLimitPerUserEnabled: true,\n\t\t\tisSpendLimitEnabled: false,\n\t\t\tisTotalOrderLimitEnabled: true,\n\t\t});\n\n\t\tawait this.userModel.findByIdAndUpdate(\n\t\t\tuser,\n\t\t\t{ $push: { coupons: referralRewardCoupon._id } },\n\t\t\t{ new: true, runValidators: true }\n\t\t);\n\n\t\t// Send Notification\n\t\tconst dispatcher = getLibraries().getMicroServiceDispatcher();\n\t\tconst notificationRouter =\n\t\t\tawait dispatcher.getMicroService<NotificationMicroserviceRouter>('notification');\n\n\t\tawait notificationRouter.notificationRouter.sendPushNotificationForCoupon.mutate(\n\t\t\treferralRewardCoupon._id\n\t\t);\n\t}\n\n\tpublic async validateCoupon(\n\t\tdata: ValidateCouponDTO,\n\t\tcouponDocument: DocumentType<Model>\n\t): Promise<CouponValidationOutputDTO> {\n\t\tif (\n\t\t\tcouponDocument.isMinOrderValueEnabled &&\n\t\t\tcouponDocument.minOrderValue &&\n\t\t\tcouponDocument.minOrderValue > data.amountBeforeCoupon\n\t\t) {\n\t\t\t// throw new TRPCError({\n\t\t\t// \tcode: 'BAD_REQUEST',\n\t\t\t// \tmessage: 'Amount is lower than the minimum order amount',\n\t\t\t// });\n\t\t\treturn {\n\t\t\t\tisValid: false,\n\t\t\t\tmessage: `Minimum order amount ${couponDocument.minOrderValue}$ is not met!`,\n\t\t\t};\n\t\t}\n\n\t\tif (couponDocument.user._id.toString() != data.user?.toString()) {\n\t\t\treturn {\n\t\t\t\tisValid: false,\n\t\t\t\tmessage: 'This reward is not for you',\n\t\t\t};\n\t\t}\n\n\t\tlet discountAmount =\n\t\t\tcouponDocument.valueType == ValueType.FIXED\n\t\t\t\t? couponDocument.value\n\t\t\t\t: roundBaseCurrency((data.amountBeforeCoupon * couponDocument.value) / 100);\n\n\t\tlet secondaryDiscountAmount =\n\t\t\tcouponDocument.valueType == ValueType.FIXED\n\t\t\t\t? await convertToSecondary(couponDocument.value)\n\t\t\t\t: roundSecondaryCurrency((data.secondaryAmountBeforeCoupon * couponDocument.value) / 100);\n\n\t\tif (couponDocument.isMaxDiscountLimitEnabled && couponDocument.maxDiscountLimit) {\n\t\t\tdiscountAmount = Math.min(discountAmount, couponDocument.maxDiscountLimit);\n\t\t\tsecondaryDiscountAmount = Math.min(\n\t\t\t\tsecondaryDiscountAmount,\n\t\t\t\tawait convertToSecondary(couponDocument.maxDiscountLimit)\n\t\t\t);\n\t\t}\n\n\t\tif (discountAmount > data.amountBeforeCoupon) {\n\t\t\tdiscountAmount = data.amountBeforeCoupon;\n\t\t}\n\t\tif (secondaryDiscountAmount > data.secondaryAmountBeforeCoupon) {\n\t\t\tsecondaryDiscountAmount = data.secondaryAmountBeforeCoupon;\n\t\t}\n\n\t\treturn {\n\t\t\tisValid: true,\n\t\t\tdiscountAmount,\n\t\t\tsecondaryDiscountAmount,\n\t\t\tamountAfterCoupon: roundBaseCurrency(data.amountBeforeCoupon - discountAmount),\n\t\t\tsecondaryAmountAfterCoupon: roundSecondaryCurrency(\n\t\t\t\tdata.secondaryAmountBeforeCoupon - secondaryDiscountAmount\n\t\t\t),\n\t\t\tdiscountCut: {\n\t\t\t\tcompanyCut: discountAmount,\n\t\t\t\tsecondaryCompanyCut: secondaryDiscountAmount,\n\t\t\t\tshopCut: 0,\n\t\t\t\tsecondaryShopCut: 0,\n\t\t\t},\n\t\t};\n\t}\n\n\tprivate async getReferralSettings(): Promise<DocumentType<ReferralSetting>> {\n\t\tconst referralSetting = await this.settingsModel.findOne({});\n\t\tif (!referralSetting) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'Referral setting not found',\n\t\t\t});\n\t\t}\n\t\treturn referralSetting;\n\t}\n}\n\nexport const referralRewardCouponService = Container.get(ReferralRewardCouponService);\n"]}
1
+ {"version":3,"file":"referral-reward-coupon.service.js","sourceRoot":"/","sources":["modules/coupon/services/referral-reward-coupon.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AAEzC,uEAYoD;AAQpD,4DAAkF;AAClF,oEAA2E;AAC3E,gEAAgG;AAEhG,4EAA8E;AAC9E,8DAAkE;AAClE,4DAAuF;AAGhF,IAAM,2BAA2B,GAAjC,MAAM,2BAA2B;IAC/B,KAAK,CAA4D;IACjE,WAAW,CAA+C;IAC1D,aAAa,CAAiE;IAC9E,SAAS,CAA2C;IACpD,UAAU,CAA2D;IAE7E;QACC,IAAI,CAAC,KAAK,GAAG,kCAAsF,CAAC;QACpG,IAAI,CAAC,WAAW,GAAG,oBAA2D,CAAC;QAC/E,IAAI,CAAC,aAAa,GAAG,6BACY,CAAC;QAClC,IAAI,CAAC,SAAS,GAAG,kBAAqD,CAAC;QACvE,IAAI,CAAC,UAAU,GAAG,0BAA6E,CAAC;IACjG,CAAC;IAMM,KAAK,CAAC,MAAM,CAAC,IAAe;QAClC,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtC,CAAC;IAOM,KAAK,CAAC,MAAM,CAAC,EAAU,EAAE,IAAe;QAC9C,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAC/C;YACC,GAAG,EAAE,EAAE;YACP,SAAS,EAAE,IAAI;SACf,EACD,IAAI,EACJ;YACC,GAAG,EAAE,IAAI;YACT,aAAa,EAAE,IAAI;SACnB,CACD,CAAC;QAEF,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,IAAI,kBAAS,CAAC;gBACnB,IAAI,E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;aACrB;SACD,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,mBAAmB;QAChC,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAC7D,IAAI,CAAC,eAAe,EAAE,CAAC;YACtB,MAAM,IAAI,kBAAS,CAAC;gBACnB,IAAI,EAAE,WAAW;gBACjB,OAAO,EAAE,4BAA4B;aACrC,CAAC,CAAC;QACJ,CAAC;QACD,OAAO,eAAe,CAAC;IACxB,CAAC;CACD,CAAA;AAnOY,kEAA2B;sCAA3B,2BAA2B;IADvC,IAAA,gBAAO,GAAE;;GACG,2BAA2B,CAmOvC;AAEY,QAAA,2BAA2B,GAAG,gBAAS,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType, mongoose } from '@typegoose/typegoose';\nimport {\n\tReferralRewardCouponModel,\n\tReferralRewardCoupon as Model,\n\tReferralSetting,\n\tReferralSettingModel,\n\tRegularOrder,\n\tUser,\n\tUserModel,\n\tRegularOrderModel,\n\tCouponModel,\n\tCoupon,\n\tReferralCodeCoupon,\n} from '@lyxa.ai/core/dist/libraries/mongo/models/';\nimport {\n\tCouponValidationOutputDTO,\n\tCreateReferralRewardCouponDTO as CreateDTO,\n\tUpdateReferralRewardCouponDTO as UpdateDTO,\n\tValidateCouponDTO,\n} from '@modules/coupon/validations';\nimport { SoftDeleteModel } from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport { CouponType, Status, ValueType } from '@lyxa.ai/core/dist/utilities/enum';\nimport { convertToSecondary } from '@lyxa.ai/core/dist/utilities/currency';\nimport { roundBaseCurrency, roundSecondaryCurrency } from '@lyxa.ai/core/dist/utilities/shared';\nimport { getLibraries } from '@lyxa.ai/core';\nimport { NotificationEvent } from '@lyxa.ai/core/dist/libraries/notification';\nimport { publishEvent } from '@lyxa.ai/core/dist/libraries/event';\nimport { ClickType, NotificationAccountType } from '@lyxa.ai/core/dist/utilities/enum';\n\n@Service()\nexport class ReferralRewardCouponService {\n\tprivate model: typeof ReferralRewardCouponModel & SoftDeleteModel<Model>;\n\tprivate couponModel: typeof CouponModel & SoftDeleteModel<Coupon>;\n\tprivate settingsModel: typeof ReferralSettingModel & SoftDeleteModel<ReferralSetting>;\n\tprivate userModel: typeof UserModel & SoftDeleteModel<User>;\n\tprivate orderModel: typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\n\tconstructor() {\n\t\tthis.model = ReferralRewardCouponModel as typeof ReferralRewardCouponModel & SoftDeleteModel<Model>;\n\t\tthis.couponModel = CouponModel as typeof CouponModel & SoftDeleteModel<Coupon>;\n\t\tthis.settingsModel = ReferralSettingModel as typeof ReferralSettingModel &\n\t\t\tSoftDeleteModel<ReferralSetting>;\n\t\tthis.userModel = UserModel as typeof UserModel & SoftDeleteModel<User>;\n\t\tthis.orderModel = RegularOrderModel as typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\t}\n\n\t/**\n\t * Create a new record\n\t * @param data Data for the new record\n\t */\n\tpublic async create(data: CreateDTO): Promise<DocumentType<Model>> {\n\t\treturn await this.model.create(data);\n\t}\n\n\t/**\n\t * Update record\n\t * @param id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(id: string, data: UpdateDTO): Promise<DocumentType<Model>> {\n\t\tconst record = await this.model.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id: id,\n\t\t\t\tdeletedAt: null,\n\t\t\t},\n\t\t\tdata,\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\tif (!record) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: `Record not found`,\n\t\t\t});\n\t\t}\n\n\t\treturn record;\n\t}\n\n\tprivate generateCode(length = 6): string {\n\t\tconst chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';\n\t\tlet result = '';\n\t\tfor (let i = 0; i < length; i++) {\n\t\t\tresult += chars.charAt(Math.floor(Math.random() * chars.length));\n\t\t}\n\t\treturn result;\n\t}\n\n\tprivate async generateCouponCode(): Promise<string> {\n\t\tlet code: string = 'SAMPLE';\n\t\tlet exists = true;\n\n\t\twhile (exists) {\n\t\t\tcode = this.generateCode();\n\t\t\texists = (await this.model.exists({ code: code })) ? true : false;\n\t\t}\n\n\t\treturn code;\n\t}\n\n\t// give referral reward to user\n\tpublic async giveReferralRewardToReferrer(orderId: mongoose.Types.ObjectId) {\n\t\tconst order = (await this.orderModel.findById(orderId)) as DocumentType<RegularOrder>;\n\n\t\tif (!order) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (!order.adjustedFinance?.coupon?.coupon?._id) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst orderCoupon = await this.couponModel.findById(order.adjustedFinance?.coupon?.coupon?._id);\n\n\t\tif (!orderCoupon || orderCoupon.couponType !== CouponType.REFERRAL_CODE) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst user = (orderCoupon as DocumentType<ReferralCodeCoupon>).referralUser;\n\n\t\tconst referralCouponSettings = await this.getReferralSettings();\n\n\t\tif (!referralCouponSettings.sender) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst referralRewardCoupon = await this.create({\n\t\t\tcode: await this.generateCouponCode(),\n\t\t\tstatus: Status.ACTIVE,\n\t\t\tcouponType: CouponType.REFERRAL_REWARD,\n\t\t\tduration: {\n\t\t\t\tstart: new Date(),\n\t\t\t},\n\t\t\tforNewUserOnly: false,\n\t\t\tuser: user._id,\n\t\t\tminOrderValue: referralCouponSettings.sender?.minimumOrderValue ?? null,\n\t\t\tvalue: referralCouponSettings.sender?.value,\n\t\t\tvalueType: referralCouponSettings.sender?.valueType,\n\t\t\treferralCodeUsedBy: order.user as mongoose.Types.ObjectId,\n\t\t\treferralCodeUsedOnOrder: order._id,\n\t\t\texpirationReason: null,\n\t\t\tmaxDiscountLimit: null,\n\t\t\torderLimitPerUser: 1,\n\t\t\tspendLimit: null,\n\t\t\ttotalOrderLimit: 1,\n\t\t\tisMinOrderValueEnabled: !!referralCouponSettings.sender?.minimumOrderValue,\n\t\t\tisMaxDiscountLimitEnabled: false,\n\t\t\tisOrderLimitPerUserEnabled: true,\n\t\t\tisSpendLimitEnabled: false,\n\t\t\tisTotalOrderLimitEnabled: true,\n\t\t});\n\n\t\tawait this.userModel.findByIdAndUpdate(\n\t\t\tuser,\n\t\t\t{ $push: { coupons: referralRewardCoupon._id } },\n\t\t\t{ new: true, runValidators: true }\n\t\t);\n\n\t\tawait publishEvent(\n\t\t\tnew NotificationEvent({\n\t\t\t\ttitle: 'Referral reward received 🙌',\n\t\t\t\tbody: 'Nice one! Your referral reward is ready to use. Thank you for sharing Lyxa 💛',\n\t\t\t\tdata: {\n\t\t\t\t\tsaveNotification: true,\n\t\t\t\t\taccountType: NotificationAccountType.USER,\n\t\t\t\t\trecipientId: user._id,\n\t\t\t\t\tisGlobal: false,\n\t\t\t\t\tisClickable: true,\n\t\t\t\t\tclickType: ClickType.CLICKABLE_COUPON,\n\t\t\t\t},\n\t\t\t})\n\t\t);\n\t}\n\n\tpublic async validateCoupon(\n\t\tdata: ValidateCouponDTO,\n\t\tcouponDocument: DocumentType<Model>\n\t): Promise<CouponValidationOutputDTO> {\n\t\tif (\n\t\t\tcouponDocument.isMinOrderValueEnabled &&\n\t\t\tcouponDocument.minOrderValue &&\n\t\t\tcouponDocument.minOrderValue > data.amountBeforeCoupon\n\t\t) {\n\t\t\t// throw new TRPCError({\n\t\t\t// \tcode: 'BAD_REQUEST',\n\t\t\t// \tmessage: 'Amount is lower than the minimum order amount',\n\t\t\t// });\n\t\t\treturn {\n\t\t\t\tisValid: false,\n\t\t\t\tmessage: `Minimum order amount ${couponDocument.minOrderValue}$ is not met!`,\n\t\t\t};\n\t\t}\n\n\t\tif (couponDocument.user._id.toString() != data.user?.toString()) {\n\t\t\treturn {\n\t\t\t\tisValid: false,\n\t\t\t\tmessage: 'This reward is not for you',\n\t\t\t};\n\t\t}\n\n\t\tlet discountAmount =\n\t\t\tcouponDocument.valueType == ValueType.FIXED\n\t\t\t\t? couponDocument.value\n\t\t\t\t: roundBaseCurrency((data.amountBeforeCoupon * couponDocument.value) / 100);\n\n\t\tlet secondaryDiscountAmount =\n\t\t\tcouponDocument.valueType == ValueType.FIXED\n\t\t\t\t? await convertToSecondary(couponDocument.value)\n\t\t\t\t: roundSecondaryCurrency((data.secondaryAmountBeforeCoupon * couponDocument.value) / 100);\n\n\t\tif (couponDocument.isMaxDiscountLimitEnabled && couponDocument.maxDiscountLimit) {\n\t\t\tdiscountAmount = Math.min(discountAmount, couponDocument.maxDiscountLimit);\n\t\t\tsecondaryDiscountAmount = Math.min(\n\t\t\t\tsecondaryDiscountAmount,\n\t\t\t\tawait convertToSecondary(couponDocument.maxDiscountLimit)\n\t\t\t);\n\t\t}\n\n\t\tif (discountAmount > data.amountBeforeCoupon) {\n\t\t\tdiscountAmount = data.amountBeforeCoupon;\n\t\t}\n\t\tif (secondaryDiscountAmount > data.secondaryAmountBeforeCoupon) {\n\t\t\tsecondaryDiscountAmount = data.secondaryAmountBeforeCoupon;\n\t\t}\n\n\t\treturn {\n\t\t\tisValid: true,\n\t\t\tdiscountAmount,\n\t\t\tsecondaryDiscountAmount,\n\t\t\tamountAfterCoupon: roundBaseCurrency(data.amountBeforeCoupon - discountAmount),\n\t\t\tsecondaryAmountAfterCoupon: roundSecondaryCurrency(\n\t\t\t\tdata.secondaryAmountBeforeCoupon - secondaryDiscountAmount\n\t\t\t),\n\t\t\tdiscountCut: {\n\t\t\t\tcompanyCut: discountAmount,\n\t\t\t\tsecondaryCompanyCut: secondaryDiscountAmount,\n\t\t\t\tshopCut: 0,\n\t\t\t\tsecondaryShopCut: 0,\n\t\t\t\tvendorCut: 0,\n\t\t\t\tsecondaryVendorCut: 0,\n\t\t\t},\n\t\t};\n\t}\n\n\tprivate async getReferralSettings(): Promise<DocumentType<ReferralSetting>> {\n\t\tconst referralSetting = await this.settingsModel.findOne({});\n\t\tif (!referralSetting) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'Referral setting not found',\n\t\t\t});\n\t\t}\n\t\treturn referralSetting;\n\t}\n}\n\nexport const referralRewardCouponService = Container.get(ReferralRewardCouponService);\n"]}