@lyxa.ai/marketing 1.0.41 → 1.0.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/event/event-initializations.d.ts.map +1 -1
- package/dist/lib/event/event-initializations.js +2 -0
- package/dist/lib/event/event-initializations.js.map +1 -1
- package/dist/lib/event/event-listeners/event-listeners.d.ts.map +1 -1
- package/dist/lib/event/event-listeners/event-listeners.js.map +1 -1
- package/dist/lib/index.d.ts +643 -0
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +6 -6
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/modules/coupon/routers/coupon.router.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/coupon.service.d.ts +1 -0
- package/dist/lib/modules/coupon/services/coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/coupon.service.js +61 -40
- package/dist/lib/modules/coupon/services/coupon.service.js.map +1 -1
- package/dist/lib/modules/coupon/services/helper.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/helper.service.js +11 -6
- package/dist/lib/modules/coupon/services/helper.service.js.map +1 -1
- package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.js +108 -149
- package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.js.map +1 -1
- package/dist/lib/modules/coupon/services/referral-code-coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/referral-code-coupon.service.js +1 -1
- package/dist/lib/modules/coupon/services/referral-code-coupon.service.js.map +1 -1
- package/dist/lib/modules/coupon/services/referral-reward-coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/referral-reward-coupon.service.js +5 -1
- package/dist/lib/modules/coupon/services/referral-reward-coupon.service.js.map +1 -1
- package/dist/lib/modules/deal-setting/routers/deal-setting.router.d.ts.map +1 -1
- package/dist/lib/modules/featured-setting/routers/featured-setting.router.d.ts.map +1 -1
- package/dist/lib/modules/limited-time-offer/campaign/campaign.router.d.ts +339 -0
- package/dist/lib/modules/limited-time-offer/campaign/campaign.router.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/campaign.router.js +77 -0
- package/dist/lib/modules/limited-time-offer/campaign/campaign.router.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/index.d.ts +4 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/index.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/index.js +24 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/index.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.producer.d.ts +12 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.producer.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.producer.js +29 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.producer.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.worker.d.ts +8 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.worker.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.worker.js +79 -0
- package/dist/lib/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.worker.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/limited-time-offer-campaign.validation.d.ts +851 -0
- package/dist/lib/modules/limited-time-offer/campaign/limited-time-offer-campaign.validation.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/limited-time-offer-campaign.validation.js +100 -0
- package/dist/lib/modules/limited-time-offer/campaign/limited-time-offer-campaign.validation.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-lifecycle.service.d.ts +11 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-lifecycle.service.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-lifecycle.service.js +75 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-lifecycle.service.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-scheduler.service.d.ts +9 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-scheduler.service.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-scheduler.service.js +42 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-scheduler.service.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign.service.d.ts +78 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign.service.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign.service.js +308 -0
- package/dist/lib/modules/limited-time-offer/campaign/services/limited-time-offer-campaign.service.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer-config.service.d.ts +12 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer-config.service.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer-config.service.js +94 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer-config.service.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer-config.validation.d.ts +189 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer-config.validation.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer-config.validation.js +24 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer-config.validation.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer.router.d.ts +122 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer.router.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer.router.js +37 -0
- package/dist/lib/modules/limited-time-offer/config/limited-time-offer.router.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/limited-time-offer.router.d.ts +644 -0
- package/dist/lib/modules/limited-time-offer/limited-time-offer.router.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/limited-time-offer.router.js +13 -0
- package/dist/lib/modules/limited-time-offer/limited-time-offer.router.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/participant/limited-time-offer-participant.router.d.ts +170 -0
- package/dist/lib/modules/limited-time-offer/participant/limited-time-offer-participant.router.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/participant/limited-time-offer-participant.router.js +51 -0
- package/dist/lib/modules/limited-time-offer/participant/limited-time-offer-participant.router.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/participant/limited-time-offer-participant.validation.d.ts +603 -0
- package/dist/lib/modules/limited-time-offer/participant/limited-time-offer-participant.validation.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/participant/limited-time-offer-participant.validation.js +85 -0
- package/dist/lib/modules/limited-time-offer/participant/limited-time-offer-participant.validation.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/participant/services/limited-time-offer-participant-marketing.service.d.ts +12 -0
- package/dist/lib/modules/limited-time-offer/participant/services/limited-time-offer-participant-marketing.service.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/participant/services/limited-time-offer-participant-marketing.service.js +98 -0
- package/dist/lib/modules/limited-time-offer/participant/services/limited-time-offer-participant-marketing.service.js.map +1 -0
- package/dist/lib/modules/limited-time-offer/participant/services/limited-time-offer-participant.service.d.ts +21 -0
- package/dist/lib/modules/limited-time-offer/participant/services/limited-time-offer-participant.service.d.ts.map +1 -0
- package/dist/lib/modules/limited-time-offer/participant/services/limited-time-offer-participant.service.js +133 -0
- package/dist/lib/modules/limited-time-offer/participant/services/limited-time-offer-participant.service.js.map +1 -0
- package/dist/lib/modules/marketing/patterns/repository/MarketingMetricsRepository.d.ts +2 -1
- package/dist/lib/modules/marketing/patterns/repository/MarketingMetricsRepository.d.ts.map +1 -1
- package/dist/lib/modules/marketing/patterns/repository/MarketingMetricsRepository.js +23 -15
- package/dist/lib/modules/marketing/patterns/repository/MarketingMetricsRepository.js.map +1 -1
- package/dist/lib/modules/marketing/routers/marketing.router.d.ts.map +1 -1
- package/dist/lib/modules/marketing/routers/marketing.router.js.map +1 -1
- package/dist/lib/modules/marketing/services/buy1-get1-marketing.service.d.ts +0 -2
- package/dist/lib/modules/marketing/services/buy1-get1-marketing.service.d.ts.map +1 -1
- package/dist/lib/modules/marketing/services/buy1-get1-marketing.service.js +18 -39
- package/dist/lib/modules/marketing/services/buy1-get1-marketing.service.js.map +1 -1
- package/dist/lib/modules/marketing/services/discount-marketing.service.d.ts +0 -3
- package/dist/lib/modules/marketing/services/discount-marketing.service.d.ts.map +1 -1
- package/dist/lib/modules/marketing/services/discount-marketing.service.js +26 -52
- package/dist/lib/modules/marketing/services/discount-marketing.service.js.map +1 -1
- package/dist/lib/modules/marketing/services/marketing.service.d.ts +1 -0
- package/dist/lib/modules/marketing/services/marketing.service.d.ts.map +1 -1
- package/dist/lib/modules/marketing/services/marketing.service.js +55 -25
- package/dist/lib/modules/marketing/services/marketing.service.js.map +1 -1
- package/dist/lib/modules/marketing/services/product-marketing.service.d.ts +9 -2
- package/dist/lib/modules/marketing/services/product-marketing.service.d.ts.map +1 -1
- package/dist/lib/modules/marketing/services/product-marketing.service.js +132 -7
- package/dist/lib/modules/marketing/services/product-marketing.service.js.map +1 -1
- package/dist/lib/modules/marketing/services/synchronize-product-marketing.service.d.ts.map +1 -1
- package/dist/lib/modules/marketing/services/synchronize-product-marketing.service.js +30 -24
- package/dist/lib/modules/marketing/services/synchronize-product-marketing.service.js.map +1 -1
- package/dist/lib/modules/marketing/transformers/buy1-get1-marketing.transformer.d.ts +3 -0
- package/dist/lib/modules/marketing/transformers/buy1-get1-marketing.transformer.d.ts.map +1 -1
- package/dist/lib/modules/marketing/transformers/discount-marketing.transformer.d.ts +3 -0
- package/dist/lib/modules/marketing/transformers/discount-marketing.transformer.d.ts.map +1 -1
- package/dist/lib/modules/marketing/transformers/product-marketing.transformer.d.ts +1 -0
- package/dist/lib/modules/marketing/transformers/product-marketing.transformer.d.ts.map +1 -1
- package/dist/lib/modules/marketing/validations/buy1-get1-marketing.validation.d.ts +30 -0
- package/dist/lib/modules/marketing/validations/buy1-get1-marketing.validation.d.ts.map +1 -1
- package/dist/lib/modules/marketing/validations/discount-marketing.validation.d.ts +45 -9
- package/dist/lib/modules/marketing/validations/discount-marketing.validation.d.ts.map +1 -1
- package/dist/lib/modules/marketing/validations/product-marketing.validation.d.ts +77 -0
- package/dist/lib/modules/marketing/validations/product-marketing.validation.d.ts.map +1 -1
- package/dist/lib/modules/marketing/validations/product-marketing.validation.js +20 -1
- package/dist/lib/modules/marketing/validations/product-marketing.validation.js.map +1 -1
- package/dist/lib/modules/punch-marketing-history/routers/punch-marketing-history.router.d.ts.map +1 -1
- package/dist/types/event/event-initializations.d.ts.map +1 -1
- package/dist/types/event/event-listeners/event-listeners.d.ts.map +1 -1
- package/dist/types/index.d.ts +643 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modules/coupon/routers/coupon.router.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/coupon.service.d.ts +1 -0
- package/dist/types/modules/coupon/services/coupon.service.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/helper.service.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/patterns/repository/CouponRepository.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/referral-code-coupon.service.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/referral-reward-coupon.service.d.ts.map +1 -1
- package/dist/types/modules/deal-setting/routers/deal-setting.router.d.ts.map +1 -1
- package/dist/types/modules/featured-setting/routers/featured-setting.router.d.ts.map +1 -1
- package/dist/types/modules/limited-time-offer/campaign/campaign.router.d.ts +339 -0
- package/dist/types/modules/limited-time-offer/campaign/campaign.router.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/campaign/jobs/index.d.ts +4 -0
- package/dist/types/modules/limited-time-offer/campaign/jobs/index.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.producer.d.ts +12 -0
- package/dist/types/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.producer.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.worker.d.ts +8 -0
- package/dist/types/modules/limited-time-offer/campaign/jobs/limited-time-offer-campaign.worker.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/campaign/limited-time-offer-campaign.validation.d.ts +851 -0
- package/dist/types/modules/limited-time-offer/campaign/limited-time-offer-campaign.validation.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-lifecycle.service.d.ts +11 -0
- package/dist/types/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-lifecycle.service.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-scheduler.service.d.ts +9 -0
- package/dist/types/modules/limited-time-offer/campaign/services/limited-time-offer-campaign-scheduler.service.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/campaign/services/limited-time-offer-campaign.service.d.ts +78 -0
- package/dist/types/modules/limited-time-offer/campaign/services/limited-time-offer-campaign.service.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/config/limited-time-offer-config.service.d.ts +12 -0
- package/dist/types/modules/limited-time-offer/config/limited-time-offer-config.service.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/config/limited-time-offer-config.validation.d.ts +189 -0
- package/dist/types/modules/limited-time-offer/config/limited-time-offer-config.validation.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/config/limited-time-offer.router.d.ts +122 -0
- package/dist/types/modules/limited-time-offer/config/limited-time-offer.router.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/limited-time-offer.router.d.ts +644 -0
- package/dist/types/modules/limited-time-offer/limited-time-offer.router.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/participant/limited-time-offer-participant.router.d.ts +170 -0
- package/dist/types/modules/limited-time-offer/participant/limited-time-offer-participant.router.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/participant/limited-time-offer-participant.service.d.ts +1 -0
- package/dist/types/modules/limited-time-offer/participant/limited-time-offer-participant.service.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/participant/limited-time-offer-participant.validation.d.ts +603 -0
- package/dist/types/modules/limited-time-offer/participant/limited-time-offer-participant.validation.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/participant/services/limited-time-offer-participant-marketing.service.d.ts +12 -0
- package/dist/types/modules/limited-time-offer/participant/services/limited-time-offer-participant-marketing.service.d.ts.map +1 -0
- package/dist/types/modules/limited-time-offer/participant/services/limited-time-offer-participant.service.d.ts +21 -0
- package/dist/types/modules/limited-time-offer/participant/services/limited-time-offer-participant.service.d.ts.map +1 -0
- package/dist/types/modules/marketing/patterns/repository/MarketingMetricsRepository.d.ts +2 -1
- package/dist/types/modules/marketing/patterns/repository/MarketingMetricsRepository.d.ts.map +1 -1
- package/dist/types/modules/marketing/routers/marketing.router.d.ts.map +1 -1
- package/dist/types/modules/marketing/services/buy1-get1-marketing.service.d.ts +0 -2
- package/dist/types/modules/marketing/services/buy1-get1-marketing.service.d.ts.map +1 -1
- package/dist/types/modules/marketing/services/discount-marketing.service.d.ts +0 -3
- package/dist/types/modules/marketing/services/discount-marketing.service.d.ts.map +1 -1
- package/dist/types/modules/marketing/services/marketing.service.d.ts +1 -0
- package/dist/types/modules/marketing/services/marketing.service.d.ts.map +1 -1
- package/dist/types/modules/marketing/services/product-marketing.service.d.ts +13 -2
- package/dist/types/modules/marketing/services/product-marketing.service.d.ts.map +1 -1
- package/dist/types/modules/marketing/services/synchronize-product-marketing.service.d.ts.map +1 -1
- package/dist/types/modules/marketing/transformers/buy1-get1-marketing.transformer.d.ts +3 -0
- package/dist/types/modules/marketing/transformers/buy1-get1-marketing.transformer.d.ts.map +1 -1
- package/dist/types/modules/marketing/transformers/discount-marketing.transformer.d.ts +3 -0
- package/dist/types/modules/marketing/transformers/discount-marketing.transformer.d.ts.map +1 -1
- package/dist/types/modules/marketing/transformers/product-marketing.transformer.d.ts +1 -0
- package/dist/types/modules/marketing/transformers/product-marketing.transformer.d.ts.map +1 -1
- package/dist/types/modules/marketing/validations/buy1-get1-marketing.validation.d.ts +30 -0
- package/dist/types/modules/marketing/validations/buy1-get1-marketing.validation.d.ts.map +1 -1
- package/dist/types/modules/marketing/validations/discount-marketing.validation.d.ts +45 -9
- package/dist/types/modules/marketing/validations/discount-marketing.validation.d.ts.map +1 -1
- package/dist/types/modules/marketing/validations/product-marketing.validation.d.ts +77 -0
- package/dist/types/modules/marketing/validations/product-marketing.validation.d.ts.map +1 -1
- package/dist/types/modules/punch-marketing-history/routers/punch-marketing-history.router.d.ts.map +1 -1
- package/package.json +2 -2
- package/dist/lib/events/event-initializations.d.ts +0 -2
- package/dist/lib/events/event-initializations.d.ts.map +0 -1
- package/dist/lib/events/event-initializations.js +0 -5
- package/dist/lib/events/event-initializations.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limited-time-offer-participant.validation.js","sourceRoot":"/","sources":["modules/limited-time-offer/participant/limited-time-offer-participant.validation.ts"],"names":[],"mappings":";;;AAAA,6BAAsB;AACtB,wEAAiG;AAEjG,IAAY,YAKX;AALD,WAAY,YAAY;IACpB,6BAAa,CAAA;IACb,uCAAuB,CAAA;IACvB,6BAAa,CAAA;IACb,iCAAiB,CAAA;AACrB,CAAC,EALW,YAAY,4BAAZ,YAAY,QAKvB;AAEY,QAAA,qBAAqB,GAAG,OAAC;KACjC,MAAM,CAAC;IACJ,YAAY,EAAE,OAAC,CAAC,OAAO,EAAE;IACzB,QAAQ,EAAE,OAAC,CAAC,KAAK,CAAC,0BAAa,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC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EDULED = 'SCHEDULED',\n LIVE = 'LIVE',\n JOINED = 'JOINED',\n}\n\nexport const JoinLtoCampaignSchema = z\n .object({\n isEntireMenu: z.boolean(),\n products: z.array(ZodValidation.objectId()).default([]),\n discountPercentage: ZodValidation.number('Discount percentage'),\n maxDiscountCap: ZodValidation.number('Max discount cap'),\n isOrderCapacityEnabled: z.boolean(),\n maxOrderCapacity: ZodValidation.number('Max order capacity').optional(),\n })\n .superRefine((d, ctx) => {\n if (!d.isEntireMenu && d.products.length === 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: 'At least one product must be selected',\n path: ['products']\n });\n }\n if (d.isOrderCapacityEnabled && !d.maxOrderCapacity) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: 'Max order capacity is required when capacity is enabled',\n path: ['maxOrderCapacity']\n });\n }\n if (d.isOrderCapacityEnabled && d.maxOrderCapacity !== undefined && d.maxOrderCapacity <= 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: 'Max order capacity must be greater than 0',\n path: ['maxOrderCapacity']\n });\n }\n });\n\nconst ParticipantSchema = z.object({\n _id: ZodValidation.objectId(),\n campaign: ZodValidation.objectId(),\n shop: ZodValidation.objectId(),\n joinedAt: z.date(),\n products: z.array(ZodValidation.objectId()).default([]),\n isEntireMenu: z.boolean(),\n discountPercentage: ZodValidation.number('Discount percentage').optional(),\n maxDiscountCap: ZodValidation.number('Max discount cap').optional(),\n isOrderCapacityEnabled: z.boolean(),\n maxOrderCapacity: ZodValidation.number('Max order capacity').optional(),\n isWithdrawn: z.boolean(),\n withdrawnAt: z.date().nullable().optional(),\n createdAt: z.date().nullable().optional(),\n updatedAt: z.date().nullable().optional(),\n});\n\nconst CampaignSummarySchema = z.object({\n _id: ZodValidation.objectId(),\n title: ZodValidation.string('Title').optional(),\n subTitle: ZodValidation.string('Subtitle').optional(),\n startDate: z.date().nullable().optional(),\n endDate: z.date().nullable().optional(),\n discountOptions: z.array(ZodValidation.number('Discount option')).default([]),\n maxDiscountCaps: z.array(ZodValidation.number('Max discount cap')).default([]),\n});\n\nconst ParticipantSummarySchema = z.object({\n discountPercentage: ZodValidation.number('Discount percentage').optional(),\n maxDiscountCap: ZodValidation.number('Max discount cap').optional(),\n joinedAt: z.date(),\n isEntireMenu: z.boolean(),\n isOrderCapacityEnabled: z.boolean(),\n maxOrderCapacity: ZodValidation.number('Max order capacity').optional(),\n});\n\nconst ParticipantStatusSchema = z.object({\n campaignState: z.nativeEnum(LtoCardState),\n campaign: CampaignSummarySchema.optional(),\n participant: ParticipantSummarySchema.optional(),\n});\n\nexport const ParticipantResponseSchema = createResponseSchema(ParticipantSchema);\nexport const ParticipantStatusResponseSchema = createResponseSchema(ParticipantStatusSchema);\n\nexport type JoinLtoCampaignDTO = DTO<typeof JoinLtoCampaignSchema>;\nexport type JoinLtoCampaignInputDTO = JoinLtoCampaignDTO;\nexport type ParticipantStatusDTO = DTO<typeof ParticipantStatusSchema>;\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { mongoose } from '@typegoose/typegoose';
|
|
2
|
+
import { JoinLtoCampaignDTO } from '../limited-time-offer-participant.validation';
|
|
3
|
+
import { LimitedTimeOfferCampaign } from '@lyxa.ai/core/dist/libraries/mongo/models';
|
|
4
|
+
export declare class LtoParticipantMarketingService {
|
|
5
|
+
applyMarketing(shopId: mongoose.Types.ObjectId, campaign: LimitedTimeOfferCampaign & {
|
|
6
|
+
_id: mongoose.Types.ObjectId;
|
|
7
|
+
}, dto: JoinLtoCampaignDTO): Promise<mongoose.Types.ObjectId>;
|
|
8
|
+
removeMarketing(marketingId: mongoose.Types.ObjectId): Promise<void>;
|
|
9
|
+
removeAllCampaignMarketings(campaignId: mongoose.Types.ObjectId): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export declare const ltoParticipantMarketingService: LtoParticipantMarketingService;
|
|
12
|
+
//# sourceMappingURL=limited-time-offer-participant-marketing.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limited-time-offer-participant-marketing.service.d.ts","sourceRoot":"/","sources":["modules/limited-time-offer/participant/services/limited-time-offer-participant-marketing.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,QAAQ,EAAC,MAAM,sBAAsB,CAAC;AAG9C,OAAO,EAAC,kBAAkB,EAAC,MAAM,8CAA8C,CAAC;AAChF,OAAO,EAAC,wBAAwB,EAAmC,MAAM,2CAA2C,CAAC;AAGrH,qBACa,8BAA8B;IAE1B,cAAc,CACvB,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAC/B,QAAQ,EAAE,wBAAwB,GAAG;QAAE,GAAG,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAA;KAAE,EACrE,GAAG,EAAE,kBAAkB,GACxB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC;IA6BtB,eAAe,CAAC,WAAW,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAIpE,2BAA2B,CAAC,UAAU,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;CAgB/F;AAED,eAAO,MAAM,8BAA8B,gCAAgD,CAAC"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.ltoParticipantMarketingService = exports.LtoParticipantMarketingService = void 0;
|
|
43
|
+
const typedi_1 = __importStar(require("typedi"));
|
|
44
|
+
const services_1 = require("../../../marketing/services");
|
|
45
|
+
const models_1 = require("@lyxa.ai/core/dist/libraries/mongo/models");
|
|
46
|
+
const enum_1 = require("@lyxa.ai/core/dist/utilities/enum");
|
|
47
|
+
let LtoParticipantMarketingService = class LtoParticipantMarketingService {
|
|
48
|
+
async applyMarketing(shopId, campaign, dto) {
|
|
49
|
+
const marketing = await services_1.marketingService.create({
|
|
50
|
+
shop: shopId,
|
|
51
|
+
marketingType: enum_1.MarketingType.DISCOUNT,
|
|
52
|
+
isAppliedByCompany: true,
|
|
53
|
+
duration: { start: campaign.startDate, end: campaign.endDate },
|
|
54
|
+
status: enum_1.MarketingStatus.ACTIVE,
|
|
55
|
+
isEntireMenu: dto.isEntireMenu,
|
|
56
|
+
value: dto.isEntireMenu ? dto.discountPercentage : undefined,
|
|
57
|
+
valueType: dto.isEntireMenu ? enum_1.ValueType.PERCENTAGE : undefined,
|
|
58
|
+
products: dto.isEntireMenu
|
|
59
|
+
? []
|
|
60
|
+
: dto.products.map(productId => ({
|
|
61
|
+
product: productId,
|
|
62
|
+
value: dto.discountPercentage,
|
|
63
|
+
valueType: enum_1.ValueType.PERCENTAGE,
|
|
64
|
+
})),
|
|
65
|
+
maxDiscountPerOrder: dto.maxDiscountCap,
|
|
66
|
+
isMaxDiscountPerOrderEnabled: dto.maxDiscountCap > 0,
|
|
67
|
+
categories: [],
|
|
68
|
+
excludeProducts: [],
|
|
69
|
+
spendLimit: 0,
|
|
70
|
+
isSpendLimitEnabled: false,
|
|
71
|
+
isOnlyForSubscriber: false,
|
|
72
|
+
});
|
|
73
|
+
return marketing._id;
|
|
74
|
+
}
|
|
75
|
+
async removeMarketing(marketingId) {
|
|
76
|
+
await services_1.marketingService.deleteById({ _id: marketingId, softDelete: true });
|
|
77
|
+
}
|
|
78
|
+
async removeAllCampaignMarketings(campaignId) {
|
|
79
|
+
const participants = await models_1.LimitedTimeOfferParticipantModel
|
|
80
|
+
.find({
|
|
81
|
+
campaign: campaignId,
|
|
82
|
+
isWithdrawn: false,
|
|
83
|
+
marketing: { $exists: true, $ne: null },
|
|
84
|
+
})
|
|
85
|
+
.select('marketing')
|
|
86
|
+
.lean();
|
|
87
|
+
const marketingIds = participants
|
|
88
|
+
.map(p => p.marketing)
|
|
89
|
+
.filter(Boolean);
|
|
90
|
+
await services_1.marketingService.deleteManyByIds(marketingIds);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
exports.LtoParticipantMarketingService = LtoParticipantMarketingService;
|
|
94
|
+
exports.LtoParticipantMarketingService = LtoParticipantMarketingService = __decorate([
|
|
95
|
+
(0, typedi_1.Service)()
|
|
96
|
+
], LtoParticipantMarketingService);
|
|
97
|
+
exports.ltoParticipantMarketingService = typedi_1.default.get(LtoParticipantMarketingService);
|
|
98
|
+
//# sourceMappingURL=limited-time-offer-participant-marketing.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limited-time-offer-participant-marketing.service.js","sourceRoot":"/","sources":["modules/limited-time-offer/participant/services/limited-time-offer-participant-marketing.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA0C;AAG1C,0DAA6D;AAE7D,sEAAqH;AACrH,4DAA4F;AAGrF,IAAM,8BAA8B,GAApC,MAAM,8BAA8B;IAEhC,KAAK,CAAC,cAAc,CACvB,MAA+B,EAC/B,QAAqE,EACrE,GAAuB;QAEvB,MAAM,SAAS,GAAG,MAAM,2BAAgB,CAAC,MAAM,CAAC;YAC5C,IAAI,EAAE,MAAM;YACZ,aAAa,EAAE,oBAAa,CAAC,QAAQ;YACrC,kBAAkB,EAAE,IAAI;YACxB,QAAQ,EAAE,EAAC,KAAK,EAAE,QAAQ,CAAC,SAAS,EAAE,GAAG,EAAE,QAAQ,CAAC,OAAO,EAAC;YAC5D,MAAM,EAAE,sBAAe,CAAC,MAAM;YAC9B,YAAY,EAAE,GAAG,CAAC,YAAY;YAC9B,KAAK,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,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AM,CAAC,OAAO,CAAC,CAAC;QAErB,MAAM,2BAAgB,CAAC,eAAe,CAAC,YAAY,CAAC,CAAC;IACzD,CAAC;CACJ,CAAA;AAvDY,wEAA8B;yCAA9B,8BAA8B;IAD1C,IAAA,gBAAO,GAAE;GACG,8BAA8B,CAuD1C;AAEY,QAAA,8BAA8B,GAAG,gBAAS,CAAC,GAAG,CAAC,8BAA8B,CAAC,CAAC","sourcesContent":["import Container, {Service} from 'typedi';\nimport {mongoose} from '@typegoose/typegoose';\n\nimport {marketingService} from '@modules/marketing/services';\nimport {JoinLtoCampaignDTO} from '../limited-time-offer-participant.validation';\nimport {LimitedTimeOfferCampaign, LimitedTimeOfferParticipantModel} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport {MarketingStatus, MarketingType, ValueType} from '@lyxa.ai/core/dist/utilities/enum';\n\n@Service()\nexport class LtoParticipantMarketingService {\n\n public async applyMarketing(\n shopId: mongoose.Types.ObjectId,\n campaign: LimitedTimeOfferCampaign & { _id: mongoose.Types.ObjectId },\n dto: JoinLtoCampaignDTO,\n ): Promise<mongoose.Types.ObjectId> {\n const marketing = await marketingService.create({\n shop: shopId,\n marketingType: MarketingType.DISCOUNT,\n isAppliedByCompany: true,\n duration: {start: campaign.startDate, end: campaign.endDate},\n status: MarketingStatus.ACTIVE,\n isEntireMenu: dto.isEntireMenu,\n value: dto.isEntireMenu ? dto.discountPercentage : undefined,\n valueType: dto.isEntireMenu ? ValueType.PERCENTAGE : undefined,\n products: dto.isEntireMenu\n ? []\n : dto.products.map(productId => ({\n product: productId,\n value: dto.discountPercentage,\n valueType: ValueType.PERCENTAGE,\n })),\n maxDiscountPerOrder: dto.maxDiscountCap,\n isMaxDiscountPerOrderEnabled: dto.maxDiscountCap > 0,\n categories: [],\n excludeProducts: [],\n spendLimit: 0,\n isSpendLimitEnabled: false,\n isOnlyForSubscriber: false,\n });\n\n return marketing._id;\n }\n\n public async removeMarketing(marketingId: mongoose.Types.ObjectId): Promise<void> {\n await marketingService.deleteById({_id: marketingId, softDelete: true});\n }\n\n public async removeAllCampaignMarketings(campaignId: mongoose.Types.ObjectId): Promise<void> {\n const participants = await LimitedTimeOfferParticipantModel\n .find({\n campaign: campaignId,\n isWithdrawn: false,\n marketing: {$exists: true, $ne: null},\n })\n .select('marketing')\n .lean();\n\n const marketingIds = participants\n .map(p => p.marketing as mongoose.Types.ObjectId)\n .filter(Boolean);\n\n await marketingService.deleteManyByIds(marketingIds);\n }\n}\n\nexport const ltoParticipantMarketingService = Container.get(LtoParticipantMarketingService);\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { LimitedTimeOfferParticipant as Model } from '@lyxa.ai/core/dist/libraries/mongo/models';
|
|
2
|
+
import { JoinLtoCampaignDTO, ParticipantStatusDTO } from '../limited-time-offer-participant.validation';
|
|
3
|
+
import { mongoose } from '@typegoose/typegoose';
|
|
4
|
+
export declare class LimitedTimeOfferParticipantService {
|
|
5
|
+
private model;
|
|
6
|
+
private campaignService;
|
|
7
|
+
private marketingService;
|
|
8
|
+
constructor();
|
|
9
|
+
join(shopId: mongoose.Types.ObjectId, dto: JoinLtoCampaignDTO): Promise<Model & {
|
|
10
|
+
_id: mongoose.Types.ObjectId;
|
|
11
|
+
} & {
|
|
12
|
+
__v: number;
|
|
13
|
+
}>;
|
|
14
|
+
withdraw(shopId: mongoose.Types.ObjectId): Promise<void>;
|
|
15
|
+
getStatus(shopId: mongoose.Types.ObjectId): Promise<ParticipantStatusDTO>;
|
|
16
|
+
private fetchActiveCampaignOrThrow;
|
|
17
|
+
private assertNotAlreadyJoined;
|
|
18
|
+
private validateDiscountSelections;
|
|
19
|
+
}
|
|
20
|
+
export declare const ltoParticipantService: LimitedTimeOfferParticipantService;
|
|
21
|
+
//# sourceMappingURL=limited-time-offer-participant.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limited-time-offer-participant.service.d.ts","sourceRoot":"/","sources":["modules/limited-time-offer/participant/services/limited-time-offer-participant.service.ts"],"names":[],"mappings":"AACA,OAAO,EAEH,2BAA2B,IAAI,KAAK,EAEvC,MAAM,2CAA2C,CAAC;AAMnD,OAAO,EAAC,kBAAkB,EAAgB,oBAAoB,EAAC,MAAM,8CAA8C,CAAC;AACpH,OAAO,EAAC,QAAQ,EAAC,MAAM,sBAAsB,CAAC;AAE9C,qBACa,kCAAkC;IAC3C,OAAO,CAAC,KAAK,CAAmE;IAChF,OAAO,CAAC,eAAe,CAAqB;IAC5C,OAAO,CAAC,gBAAgB,CAAiC;;IAQ5C,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,EAAE,kBAAkB;;;;;IAyB7D,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAkBxD,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,oBAAoB,CAAC;YAmBxE,0BAA0B;YAQ1B,sBAAsB;IAKpC,OAAO,CAAC,0BAA0B;CAQrC;AAED,eAAO,MAAM,qBAAqB,oCAAoD,CAAC"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
42
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.ltoParticipantService = exports.LimitedTimeOfferParticipantService = void 0;
|
|
46
|
+
const typedi_1 = __importStar(require("typedi"));
|
|
47
|
+
const models_1 = require("@lyxa.ai/core/dist/libraries/mongo/models");
|
|
48
|
+
const enum_1 = require("@lyxa.ai/core/dist/utilities/enum");
|
|
49
|
+
const error_common_1 = require("@lyxa.ai/core/dist/utilities/error-common");
|
|
50
|
+
const limited_time_offer_campaign_service_1 = require("../../../limited-time-offer/campaign/services/limited-time-offer-campaign.service");
|
|
51
|
+
const limited_time_offer_participant_marketing_service_1 = require("./limited-time-offer-participant-marketing.service");
|
|
52
|
+
const limited_time_offer_participant_validation_1 = require("../limited-time-offer-participant.validation");
|
|
53
|
+
let LimitedTimeOfferParticipantService = class LimitedTimeOfferParticipantService {
|
|
54
|
+
model;
|
|
55
|
+
campaignService;
|
|
56
|
+
marketingService;
|
|
57
|
+
constructor() {
|
|
58
|
+
this.model = models_1.LimitedTimeOfferParticipantModel;
|
|
59
|
+
this.campaignService = typedi_1.default.get(limited_time_offer_campaign_service_1.LtoCampaignService);
|
|
60
|
+
this.marketingService = typedi_1.default.get(limited_time_offer_participant_marketing_service_1.LtoParticipantMarketingService);
|
|
61
|
+
}
|
|
62
|
+
async join(shopId, dto) {
|
|
63
|
+
const campaign = await this.fetchActiveCampaignOrThrow();
|
|
64
|
+
await this.assertNotAlreadyJoined(campaign._id, shopId);
|
|
65
|
+
this.validateDiscountSelections(campaign, dto.discountPercentage, dto.maxDiscountCap);
|
|
66
|
+
const marketingId = await this.marketingService.applyMarketing(shopId, campaign, dto);
|
|
67
|
+
const participant = await this.model.create({
|
|
68
|
+
campaign: campaign._id,
|
|
69
|
+
shop: shopId,
|
|
70
|
+
marketing: marketingId,
|
|
71
|
+
joinedAt: new Date(),
|
|
72
|
+
products: dto.products ?? [],
|
|
73
|
+
isEntireMenu: dto.isEntireMenu,
|
|
74
|
+
discountPercentage: dto.discountPercentage,
|
|
75
|
+
maxDiscountCap: dto.maxDiscountCap,
|
|
76
|
+
isOrderCapacityEnabled: dto.isOrderCapacityEnabled,
|
|
77
|
+
maxOrderCapacity: dto.maxOrderCapacity,
|
|
78
|
+
isWithdrawn: false,
|
|
79
|
+
});
|
|
80
|
+
return participant.toObject();
|
|
81
|
+
}
|
|
82
|
+
async withdraw(shopId) {
|
|
83
|
+
const campaign = await this.fetchActiveCampaignOrThrow();
|
|
84
|
+
const participant = await this.model.findOneAndUpdate({ campaign: campaign._id, shop: shopId, isWithdrawn: false }, { $set: { isWithdrawn: true, withdrawnAt: new Date() } }, { new: true });
|
|
85
|
+
if (!participant)
|
|
86
|
+
throw (0, error_common_1.notFoundError)('You have not joined this campaign.');
|
|
87
|
+
if (participant.marketing) {
|
|
88
|
+
await this.marketingService.removeMarketing(participant.marketing);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async getStatus(shopId) {
|
|
92
|
+
const campaign = await this.campaignService.getCurrent();
|
|
93
|
+
if (!campaign)
|
|
94
|
+
return { campaignState: limited_time_offer_participant_validation_1.LtoCardState.NONE };
|
|
95
|
+
if (campaign.status === enum_1.LimitedTimeOfferCampaignStatus.SCHEDULED) {
|
|
96
|
+
return { campaignState: limited_time_offer_participant_validation_1.LtoCardState.SCHEDULED, campaign };
|
|
97
|
+
}
|
|
98
|
+
const participant = await this.model
|
|
99
|
+
.findOne({ campaign: campaign._id, shop: shopId, isWithdrawn: false })
|
|
100
|
+
.select('discountPercentage maxDiscountCap joinedAt isEntireMenu isOrderCapacityEnabled maxOrderCapacity')
|
|
101
|
+
.lean();
|
|
102
|
+
return participant
|
|
103
|
+
? { campaignState: limited_time_offer_participant_validation_1.LtoCardState.JOINED, campaign, participant }
|
|
104
|
+
: { campaignState: limited_time_offer_participant_validation_1.LtoCardState.LIVE, campaign };
|
|
105
|
+
}
|
|
106
|
+
async fetchActiveCampaignOrThrow() {
|
|
107
|
+
const campaign = await this.campaignService.getCurrent();
|
|
108
|
+
if (!campaign || campaign.status !== enum_1.LimitedTimeOfferCampaignStatus.ACTIVE) {
|
|
109
|
+
throw (0, error_common_1.badRequestError)('No active campaign is currently running.');
|
|
110
|
+
}
|
|
111
|
+
return campaign;
|
|
112
|
+
}
|
|
113
|
+
async assertNotAlreadyJoined(campaignId, shopId) {
|
|
114
|
+
const exists = await this.model.exists({ campaign: campaignId, shop: shopId, isWithdrawn: false });
|
|
115
|
+
if (exists)
|
|
116
|
+
throw (0, error_common_1.conflictError)('Your shop has already joined this campaign.');
|
|
117
|
+
}
|
|
118
|
+
validateDiscountSelections(campaign, discountPercentage, maxDiscountCap) {
|
|
119
|
+
if (!campaign.discountOptions.includes(discountPercentage)) {
|
|
120
|
+
throw (0, error_common_1.badRequestError)('Selected discount is not an available option for this campaign.');
|
|
121
|
+
}
|
|
122
|
+
if (!campaign.maxDiscountCaps.includes(maxDiscountCap)) {
|
|
123
|
+
throw (0, error_common_1.badRequestError)('Selected max discount cap is not an available option for this campaign.');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
exports.LimitedTimeOfferParticipantService = LimitedTimeOfferParticipantService;
|
|
128
|
+
exports.LimitedTimeOfferParticipantService = LimitedTimeOfferParticipantService = __decorate([
|
|
129
|
+
(0, typedi_1.Service)(),
|
|
130
|
+
__metadata("design:paramtypes", [])
|
|
131
|
+
], LimitedTimeOfferParticipantService);
|
|
132
|
+
exports.ltoParticipantService = typedi_1.default.get(LimitedTimeOfferParticipantService);
|
|
133
|
+
//# sourceMappingURL=limited-time-offer-participant.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limited-time-offer-participant.service.js","sourceRoot":"/","sources":["modules/limited-time-offer/participant/services/limited-time-offer-participant.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA0C;AAC1C,sEAImD;AAEnD,4DAAiF;AACjF,4EAAwG;AACxG,2IAAqH;AACrH,yHAAkG;AAClG,4GAAoH;AAI7G,IAAM,kCAAkC,GAAxC,MAAM,kCAAkC;IACnC,KAAK,CAAmE;IACxE,eAAe,CAA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CAAC,CAAC","sourcesContent":["import Container, {Service} from 'typedi';\nimport {\n LimitedTimeOfferCampaign,\n LimitedTimeOfferParticipant as Model,\n LimitedTimeOfferParticipantModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport {SoftDeleteModel} from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport {LimitedTimeOfferCampaignStatus} from '@lyxa.ai/core/dist/utilities/enum';\nimport {badRequestError, conflictError, notFoundError} from '@lyxa.ai/core/dist/utilities/error-common';\nimport {LtoCampaignService} from '@modules/limited-time-offer/campaign/services/limited-time-offer-campaign.service';\nimport {LtoParticipantMarketingService} from './limited-time-offer-participant-marketing.service';\nimport {JoinLtoCampaignDTO, LtoCardState, ParticipantStatusDTO} from '../limited-time-offer-participant.validation';\nimport {mongoose} from '@typegoose/typegoose';\n\n@Service()\nexport class LimitedTimeOfferParticipantService {\n private model: typeof LimitedTimeOfferParticipantModel & SoftDeleteModel<Model>;\n private campaignService: LtoCampaignService;\n private marketingService: LtoParticipantMarketingService;\n\n constructor() {\n this.model = LimitedTimeOfferParticipantModel as typeof LimitedTimeOfferParticipantModel & SoftDeleteModel<Model>;\n this.campaignService = Container.get(LtoCampaignService);\n this.marketingService = Container.get(LtoParticipantMarketingService);\n }\n\n public async join(shopId: mongoose.Types.ObjectId, dto: JoinLtoCampaignDTO) {\n const campaign = await this.fetchActiveCampaignOrThrow();\n\n await this.assertNotAlreadyJoined(campaign._id, shopId);\n this.validateDiscountSelections(campaign, dto.discountPercentage, dto.maxDiscountCap);\n\n const marketingId = await this.marketingService.applyMarketing(shopId, campaign, dto);\n\n const participant = await this.model.create({\n campaign: campaign._id,\n shop: shopId,\n marketing: marketingId,\n joinedAt: new Date(),\n products: dto.products ?? [],\n isEntireMenu: dto.isEntireMenu,\n discountPercentage: dto.discountPercentage,\n maxDiscountCap: dto.maxDiscountCap,\n isOrderCapacityEnabled: dto.isOrderCapacityEnabled,\n maxOrderCapacity: dto.maxOrderCapacity,\n isWithdrawn: false,\n });\n\n return participant.toObject();\n }\n\n public async withdraw(shopId: mongoose.Types.ObjectId): Promise<void> {\n const campaign = await this.fetchActiveCampaignOrThrow();\n\n const participant = await this.model.findOneAndUpdate(\n {campaign: campaign._id, shop: shopId, isWithdrawn: false},\n {$set: {isWithdrawn: true, withdrawnAt: new Date()}},\n {new: true},\n );\n\n if (!participant) throw notFoundError('You have not joined this campaign.');\n\n if (participant.marketing) {\n await this.marketingService.removeMarketing(\n participant.marketing as mongoose.Types.ObjectId,\n );\n }\n }\n\n public async getStatus(shopId: mongoose.Types.ObjectId): Promise<ParticipantStatusDTO> {\n const campaign = await this.campaignService.getCurrent();\n\n if (!campaign) return {campaignState: LtoCardState.NONE};\n\n if (campaign.status === LimitedTimeOfferCampaignStatus.SCHEDULED) {\n return {campaignState: LtoCardState.SCHEDULED, campaign};\n }\n\n const participant = await this.model\n .findOne({campaign: campaign._id, shop: shopId, isWithdrawn: false})\n .select('discountPercentage maxDiscountCap joinedAt isEntireMenu isOrderCapacityEnabled maxOrderCapacity')\n .lean();\n\n return participant\n ? {campaignState: LtoCardState.JOINED, campaign, participant}\n : {campaignState: LtoCardState.LIVE, campaign};\n }\n\n private async fetchActiveCampaignOrThrow() {\n const campaign = await this.campaignService.getCurrent();\n if (!campaign || campaign.status !== LimitedTimeOfferCampaignStatus.ACTIVE) {\n throw badRequestError('No active campaign is currently running.');\n }\n return campaign;\n }\n\n private async assertNotAlreadyJoined(campaignId: mongoose.Types.ObjectId, shopId: mongoose.Types.ObjectId): Promise<void> {\n const exists = await this.model.exists({campaign: campaignId, shop: shopId, isWithdrawn: false});\n if (exists) throw conflictError('Your shop has already joined this campaign.');\n }\n\n private validateDiscountSelections(campaign: LimitedTimeOfferCampaign, discountPercentage: number, maxDiscountCap: number): void {\n if (!campaign.discountOptions.includes(discountPercentage)) {\n throw badRequestError('Selected discount is not an available option for this campaign.');\n }\n if (!campaign.maxDiscountCaps.includes(maxDiscountCap)) {\n throw badRequestError('Selected max discount cap is not an available option for this campaign.');\n }\n }\n}\n\nexport const ltoParticipantService = Container.get(LimitedTimeOfferParticipantService);\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DocumentType, mongoose } from '@typegoose/typegoose';
|
|
2
|
-
import { LineItem, RegularOrder } from '@lyxa.ai/core/dist/libraries/mongo/models';
|
|
2
|
+
import { LineItem, Marketing, RegularOrder } from '@lyxa.ai/core/dist/libraries/mongo/models';
|
|
3
3
|
import { MarketingGraphInputDTO, MarketingMetricsDTO, MarketingOrdersGraphOutputDTO } from '../../../marketing/validations';
|
|
4
4
|
import { MarketingType } from '@lyxa.ai/core/dist/utilities/enum';
|
|
5
5
|
import { Duration } from '@lyxa.ai/core/dist/libraries/mongo/models/shared/duration.model';
|
|
@@ -9,6 +9,7 @@ export declare class MarketingMetricsRepository {
|
|
|
9
9
|
constructor();
|
|
10
10
|
calculateMarketingSpendPerOrder(order: DocumentType<RegularOrder> & {
|
|
11
11
|
lineItems: DocumentType<LineItem>[];
|
|
12
|
+
marketings: DocumentType<Marketing>[];
|
|
12
13
|
}): Map<mongoose.Types.ObjectId, number>;
|
|
13
14
|
calculateMarketingSpent(marketingId: mongoose.Types.ObjectId, duration?: Duration): Promise<{
|
|
14
15
|
amount: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MarketingMetricsRepository.d.ts","sourceRoot":"/","sources":["modules/marketing/patterns/repository/MarketingMetricsRepository.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAmB,MAAM,sBAAsB,CAAC;AAC/E,OAAO,EAEN,QAAQ,
|
|
1
|
+
{"version":3,"file":"MarketingMetricsRepository.d.ts","sourceRoot":"/","sources":["modules/marketing/patterns/repository/MarketingMetricsRepository.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAmB,MAAM,sBAAsB,CAAC;AAC/E,OAAO,EAEN,QAAQ,EACR,SAAS,EAGT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CAAC;IAsF3F,mBAAmB,CAC/B,IAAI,EAAE,sBAAsB,GAC1B,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAoL9C;AAED,eAAO,MAAM,0BAA0B,4BAA4C,CAAC"}
|
|
@@ -62,24 +62,28 @@ let MarketingMetricsRepository = class MarketingMetricsRepository {
|
|
|
62
62
|
}
|
|
63
63
|
calculateMarketingSpendPerOrder(order) {
|
|
64
64
|
const spendMap = new Map();
|
|
65
|
+
const marketingOwnerMap = new Map();
|
|
66
|
+
for (const marketing of order.marketings ?? []) {
|
|
67
|
+
marketingOwnerMap.set(marketing._id.toString(), marketing.isAppliedByCompany ? 'COMPANY' : 'SHOP');
|
|
68
|
+
}
|
|
65
69
|
const addSpend = (marketingId, amount) => {
|
|
66
70
|
if (!amount || amount <= 0)
|
|
67
71
|
return;
|
|
68
72
|
spendMap.set(marketingId, (spendMap.get(marketingId) ?? 0) + amount);
|
|
69
73
|
};
|
|
74
|
+
const getRelevantAmount = (marketingId, companyAmount, shopAmount) => {
|
|
75
|
+
const owner = marketingOwnerMap.get(marketingId.toString());
|
|
76
|
+
return owner === 'COMPANY' ? companyAmount : shopAmount;
|
|
77
|
+
};
|
|
70
78
|
for (const lineItem of order.lineItems ?? []) {
|
|
71
79
|
for (const marketingId of lineItem.marketings ?? []) {
|
|
72
80
|
const snap = lineItem.calculationSnap ?? {};
|
|
73
|
-
addSpend(marketingId._id, (Number(snap.itemDiscountByCompany) || 0) +
|
|
74
|
-
(Number(snap.itemDiscountByShop) || 0) +
|
|
75
|
-
(Number(snap.b1g1DiscountByCompany) || 0) +
|
|
76
|
-
(Number(snap.b1g1DiscountByShop) || 0));
|
|
81
|
+
addSpend(marketingId._id, getRelevantAmount(marketingId._id, (Number(snap.itemDiscountByCompany) || 0) + (Number(snap.b1g1DiscountByCompany) || 0), (Number(snap.itemDiscountByShop) || 0) + (Number(snap.b1g1DiscountByShop) || 0)));
|
|
77
82
|
}
|
|
78
83
|
}
|
|
79
84
|
if (order.adjustedFinance?.freeDelivery) {
|
|
80
85
|
for (const marketingId of order.marketings ?? []) {
|
|
81
|
-
addSpend(marketingId._id, (order.adjustedFinance.freeDelivery.companyCut ?? 0)
|
|
82
|
-
(order.adjustedFinance.freeDelivery.shopCut ?? 0));
|
|
86
|
+
addSpend(marketingId._id, getRelevantAmount(marketingId._id, order.adjustedFinance.freeDelivery.companyCut ?? 0, order.adjustedFinance.freeDelivery.shopCut ?? 0));
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
89
|
if (order.adjustedFinance?.pricing?.punchMarketingDiscount) {
|
|
@@ -96,8 +100,8 @@ let MarketingMetricsRepository = class MarketingMetricsRepository {
|
|
|
96
100
|
}
|
|
97
101
|
const calculators = {
|
|
98
102
|
[enum_2.MarketingType.FREE_DELIVERY]: () => this.calculateFreeDeliverySpent(marketingId, duration),
|
|
99
|
-
[enum_2.MarketingType.DISCOUNT]: () => this.calculateItemBasedSpent(marketingId, duration),
|
|
100
|
-
[enum_2.MarketingType.BUY1GET1]: () => this.calculateItemBasedSpent(marketingId, duration),
|
|
103
|
+
[enum_2.MarketingType.DISCOUNT]: () => this.calculateItemBasedSpent(marketingId, marketing.isAppliedByCompany, duration),
|
|
104
|
+
[enum_2.MarketingType.BUY1GET1]: () => this.calculateItemBasedSpent(marketingId, marketing.isAppliedByCompany, duration),
|
|
101
105
|
[enum_2.MarketingType.PUNCH_MARKETING]: () => this.calculatePunchMarketingSpent(marketingId, duration),
|
|
102
106
|
[enum_2.MarketingType.FEATURED]: function () {
|
|
103
107
|
return Promise.resolve(marketing.amount || 0);
|
|
@@ -109,7 +113,16 @@ let MarketingMetricsRepository = class MarketingMetricsRepository {
|
|
|
109
113
|
marketingType: marketing.marketingType,
|
|
110
114
|
};
|
|
111
115
|
}
|
|
112
|
-
async calculateItemBasedSpent(marketingId, duration) {
|
|
116
|
+
async calculateItemBasedSpent(marketingId, isAppliedByAdmin, duration) {
|
|
117
|
+
const addBlock = isAppliedByAdmin
|
|
118
|
+
? [
|
|
119
|
+
{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByCompany', 0] },
|
|
120
|
+
{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByCompany', 0] },
|
|
121
|
+
]
|
|
122
|
+
: [
|
|
123
|
+
{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByShop', 0] },
|
|
124
|
+
{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByShop', 0] },
|
|
125
|
+
];
|
|
113
126
|
const pipeline = [
|
|
114
127
|
this.buildBaseMatch(marketingId, duration),
|
|
115
128
|
{
|
|
@@ -126,12 +139,7 @@ let MarketingMetricsRepository = class MarketingMetricsRepository {
|
|
|
126
139
|
_id: null,
|
|
127
140
|
totalSpent: {
|
|
128
141
|
$sum: {
|
|
129
|
-
$add:
|
|
130
|
-
{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByCompany', 0] },
|
|
131
|
-
{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByShop', 0] },
|
|
132
|
-
{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByCompany', 0] },
|
|
133
|
-
{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByShop', 0] },
|
|
134
|
-
],
|
|
142
|
+
$add: addBlock,
|
|
135
143
|
},
|
|
136
144
|
},
|
|
137
145
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MarketingMetricsRepository.js","sourceRoot":"/","sources":["modules/marketing/patterns/repository/MarketingMetricsRepository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,8DAA2D;AAC3D,oDAA+E;AAC/E,sEAOmD;AAQnD,4DAA6F;AAC7F,qGAAgG;AAChG,4DAAkE;AAClE,4EAA0E;AAE1E,4DAAiE;AACjE,0CAAoD;AAG7C,IAAM,0BAA0B,GAAhC,M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,CAkoBtC;AAEY,QAAA,0BAA0B,GAAG,gBAAS,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { dayjs } from '@lyxa.ai/core/dist/utilities/dayjs';\nimport { DocumentType, mongoose, ReturnModelType } from '@typegoose/typegoose';\nimport {\n\tFeaturedMarketing,\n\tLineItem,\n\tMarketingModel,\n\tMarketing as Model,\n\tRegularOrder,\n\tRegularOrderModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport {\n\tIncreaseDuringDiscountDTO,\n\tMarketingGraphInputDTO,\n\tMarketingMetricsDTO,\n\tMarketingOrdersGraphOutputDTO,\n} from '@modules/marketing/validations';\nimport { SoftDeleteModel } from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport { GraphType, OrderType, RegularOrderStatus } from '@lyxa.ai/core/dist/utilities/enum';\nimport { productMarketingService } from '@modules/marketing/services/product-marketing.service';\nimport { MarketingType } from '@lyxa.ai/core/dist/utilities/enum';\nimport { notFoundError } from '@lyxa.ai/core/dist/utilities/error-common';\nimport { Duration } from '@lyxa.ai/core/dist/libraries/mongo/models/shared/duration.model';\nimport { LyxaTimezone } from '@lyxa.ai/core/dist/utilities/time';\nimport { fillMissingPeriods } from '@utility/utils';\n\n@Service()\nexport class MarketingMetricsRepository {\n\tprivate model: typeof MarketingModel & SoftDeleteModel<Model>;\n\tprivate orderModel: typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\n\tconstructor() {\n\t\tthis.model = MarketingModel as typeof MarketingModel & SoftDeleteModel<Model>;\n\t\tthis.orderModel = RegularOrderModel as typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\t}\n\n\tpublic calculateMarketingSpendPerOrder(\n\t\torder: DocumentType<RegularOrder> & { lineItems: DocumentType<LineItem>[] }\n\t): Map<mongoose.Types.ObjectId, number> {\n\t\tconst spendMap = new Map<mongoose.Types.ObjectId, number>();\n\n\t\tconst addSpend = (marketingId: mongoose.Types.ObjectId, amount: number) => {\n\t\t\tif (!amount || amount <= 0) return;\n\t\t\tspendMap.set(marketingId, (spendMap.get(marketingId) ?? 0) + amount);\n\t\t};\n\n\t\t/**\n\t\t * ITEM-BASED (DISCOUNT / B1G1)\n\t\t */\n\t\tfor (const lineItem of order.lineItems ?? []) {\n\t\t\tfor (const marketingId of lineItem.marketings ?? []) {\n\t\t\t\tconst snap = lineItem.calculationSnap ?? {};\n\t\t\t\taddSpend(\n\t\t\t\t\tmarketingId._id,\n\t\t\t\t\t(Number(snap.itemDiscountByCompany) || 0) +\n\t\t\t\t\t\t(Number(snap.itemDiscountByShop) || 0) +\n\t\t\t\t\t\t(Number(snap.b1g1DiscountByCompany) || 0) +\n\t\t\t\t\t\t(Number(snap.b1g1DiscountByShop) || 0)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * FREE DELIVERY\n\t\t */\n\t\tif (order.adjustedFinance?.freeDelivery) {\n\t\t\tfor (const marketingId of order.marketings ?? []) {\n\t\t\t\taddSpend(\n\t\t\t\t\tmarketingId._id,\n\t\t\t\t\t(order.adjustedFinance.freeDelivery.companyCut ?? 0) +\n\t\t\t\t\t\t(order.adjustedFinance.freeDelivery.shopCut ?? 0)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * PUNCH MARKETING\n\t\t */\n\t\tif (order.adjustedFinance?.pricing?.punchMarketingDiscount) {\n\t\t\tfor (const marketingId of order.marketings ?? []) {\n\t\t\t\taddSpend(marketingId._id, order.adjustedFinance.pricing.punchMarketingDiscount);\n\t\t\t}\n\t\t}\n\n\t\treturn spendMap;\n\t}\n\n\tpublic async calculateMarketingSpent(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tduration?: Duration\n\t): Promise<{ amount: number; marketingType: MarketingType }> {\n\t\tconst marketing = await this.model.findById(marketingId).lean();\n\n\t\tif (!marketing) {\n\t\t\tnotFoundError('Marketing not found');\n\t\t}\n\n\t\tconst calculators: Record<MarketingType, () => Promise<number>> = {\n\t\t\t[MarketingType.FREE_DELIVERY]: () => this.calculateFreeDeliverySpent(marketingId, duration),\n\n\t\t\t[MarketingType.DISCOUNT]: () => this.calculateItemBasedSpent(marketingId, duration),\n\n\t\t\t[MarketingType.BUY1GET1]: () => this.calculateItemBasedSpent(marketingId, duration),\n\n\t\t\t[MarketingType.PUNCH_MARKETING]: () => this.calculatePunchMarketingSpent(marketingId, duration),\n\t\t\t[MarketingType.FEATURED]: function (): Promise<number> {\n\t\t\t\treturn Promise.resolve((marketing as DocumentType<FeaturedMarketing>).amount || 0);\n\t\t\t},\n\t\t};\n\n\t\tconst amount = calculators[marketing.marketingType] ? await calculators[marketing.marketingType]() : 0;\n\n\t\treturn {\n\t\t\tamount,\n\t\t\tmarketingType: marketing.marketingType,\n\t\t};\n\t}\n\n\tprivate async calculateItemBasedSpent(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tduration?: Duration\n\t): Promise<number> {\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\tthis.buildBaseMatch(marketingId, duration),\n\n\t\t\t{\n\t\t\t\t$lookup: {\n\t\t\t\t\tfrom: 'lineItems',\n\t\t\t\t\tlocalField: 'cart',\n\t\t\t\t\tforeignField: 'cart',\n\t\t\t\t\tas: 'lineItems',\n\t\t\t\t},\n\t\t\t},\n\t\t\t{ $unwind: '$lineItems' },\n\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: null,\n\t\t\t\t\ttotalSpent: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$add: [\n\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByCompany', 0] },\n\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByShop', 0] },\n\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByCompany', 0] },\n\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByShop', 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\t\t];\n\n\t\treturn this.runAggregateAndGetTotal(pipeline, 'totalSpent');\n\t}\n\n\tprivate async calculateFreeDeliverySpent(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tduration?: Duration\n\t): Promise<number> {\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\tthis.buildBaseMatch(marketingId, duration),\n\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: null,\n\t\t\t\t\ttotalSpent: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$add: [\n\t\t\t\t\t\t\t\t{ $ifNull: ['$adjustedFinance.freeDelivery.companyCut', 0] },\n\t\t\t\t\t\t\t\t{ $ifNull: ['$adjustedFinance.freeDelivery.shopCut', 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\t\t];\n\n\t\treturn this.runAggregateAndGetTotal(pipeline, 'totalSpent');\n\t}\n\n\tprivate async calculatePunchMarketingSpent(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tduration?: Duration\n\t): Promise<number> {\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\tthis.buildBaseMatch(marketingId, duration),\n\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: null,\n\t\t\t\t\ttotalSpent: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$ifNull: ['$adjustedFinance.pricing.punchMarketingDiscount', 0],\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\n\t\treturn this.runAggregateAndGetTotal(pipeline, 'totalSpent');\n\t}\n\n\tprivate buildBaseMatch(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tduration?: Duration\n\t): mongoose.PipelineStage.Match {\n\t\treturn {\n\t\t\t$match: {\n\t\t\t\tmarketings: marketingId,\n\t\t\t\tstatus: { $ne: RegularOrderStatus.CANCELLED },\n\t\t\t\t...(duration && {\n\t\t\t\t\tprocessedAt: {\n\t\t\t\t\t\t$gte: duration.start,\n\t\t\t\t\t\t$lte: duration.end,\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t},\n\t\t};\n\t}\n\n\tprivate async runAggregateAndGetTotal(pipeline: mongoose.PipelineStage[], field: string): Promise<number> {\n\t\tconst [result] = await this.orderModel.aggregate(pipeline);\n\t\treturn result?.[field] ?? 0;\n\t}\n\n\tpublic async getMetrics(marketingId: mongoose.Types.ObjectId): Promise<MarketingMetricsDTO> {\n\t\tconst marketing = await this.model.findById(marketingId).lean();\n\n\t\tif (!marketing) {\n\t\t\tnotFoundError('Marketing not found');\n\t\t}\n\n\t\t//TODO: fix current end and previous start\n\n\t\tconst currentDuration = { start: marketing.duration.start, end: dayjs().toDate() };\n\t\tconst previousDuration = {\n\t\t\tstart: dayjs(marketing.duration.start).subtract(dayjs().diff(marketing.duration.start)).toDate(),\n\t\t\tend: marketing.duration.start,\n\t\t};\n\n\t\tlet metrics: MarketingMetricsDTO = {};\n\n\t\tif ([MarketingType.DISCOUNT, MarketingType.BUY1GET1].includes(marketing.marketingType)) {\n\t\t\tmetrics.numberOfItems = await this.getMarketingItemCount(marketingId);\n\t\t}\n\n\t\tmetrics.orderIncreaseWithDiscount = await this.calculateOrderIncreaseWithDiscount(\n\t\t\tmarketingId,\n\t\t\tmarketing.shop._id,\n\t\t\tcurrentDuration,\n\t\t\tpreviousDuration\n\t\t);\n\t\tmetrics.customerIncreaseWithDiscount = await this.calculateCustomerIncreaseWithDiscount(\n\t\t\tmarketingId,\n\t\t\tmarketing.shop._id,\n\t\t\tcurrentDuration,\n\t\t\tpreviousDuration\n\t\t);\n\n\t\treturn metrics;\n\t}\n\n\tprivate async getMarketingItemCount(marketingId: mongoose.Types.ObjectId): Promise<number> {\n\t\tconst result = await productMarketingService.countDocuments({ marketing: marketingId });\n\t\treturn result ?? 0;\n\t}\n\n\tprivate async calculateOrderIncreaseWithDiscount(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tshopId: mongoose.Types.ObjectId,\n\t\tcurrentDuration: Duration,\n\t\tpreviousDuration: Duration\n\t): Promise<IncreaseDuringDiscountDTO> {\n\t\tconst [currentOrders, previousOrders] = await Promise.all([\n\t\t\tthis.orderModel.countDocuments({\n\t\t\t\tcreatedAt: {\n\t\t\t\t\t$gte: currentDuration.start,\n\t\t\t\t\t$lte: currentDuration.end,\n\t\t\t\t},\n\t\t\t\tmarketings: marketingId, // marketing must be applied\n\t\t\t\tshop: shopId,\n\t\t\t}),\n\t\t\tthis.orderModel.countDocuments({\n\t\t\t\tcreatedAt: {\n\t\t\t\t\t$gte: previousDuration.start,\n\t\t\t\t\t$lte: previousDuration.end,\n\t\t\t\t},\n\t\t\t\tshop: shopId,\n\t\t\t}),\n\t\t]);\n\n\t\treturn this.calculateIncrease(currentOrders, previousOrders);\n\t}\n\n\tprivate async calculateCustomerIncreaseWithDiscount(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tshopId: mongoose.Types.ObjectId,\n\t\tcurrentDuration: Duration,\n\t\tpreviousDuration: Duration\n\t): Promise<IncreaseDuringDiscountDTO> {\n\t\tconst [currentNewCustomers, previousNewCustomers] = await Promise.all([\n\t\t\tthis.getNewCustomersForShop(shopId, currentDuration, { marketings: marketingId }),\n\t\t\tthis.getNewCustomersForShop(shopId, previousDuration),\n\t\t]);\n\n\t\treturn this.calculateIncrease(currentNewCustomers, previousNewCustomers);\n\t}\n\n\tprivate calculateIncrease(current: number, previous: number): IncreaseDuringDiscountDTO {\n\t\tconst count = current - previous;\n\n\t\tconst percentageIncrease = previous === 0 ? (current > 0 ? 100 : 0) : (count / previous) * 100;\n\n\t\treturn {\n\t\t\tcount,\n\t\t\tpercentageIncrease,\n\t\t};\n\t}\n\n\tprivate async getNewCustomersForShop(\n\t\tshopId: mongoose.Types.ObjectId,\n\t\tduration: Duration,\n\t\textraMatch: Record<string, any> = {}\n\t): Promise<number> {\n\t\t// Customers who ordered during the period\n\t\tconst customersInPeriod = await this.orderModel.distinct('user', {\n\t\t\tcreatedAt: {\n\t\t\t\t$gte: duration.start,\n\t\t\t\t$lte: duration.end,\n\t\t\t},\n\t\t\tshop: shopId,\n\t\t\t...extraMatch,\n\t\t});\n\n\t\tif (customersInPeriod.length === 0) {\n\t\t\treturn 0;\n\t\t}\n\n\t\t// Customers who had ordered from this shop BEFORE the period\n\t\tconst existingCustomers = await this.orderModel.distinct('user', {\n\t\t\tshop: shopId,\n\t\t\tcreatedAt: { $lt: duration.start },\n\t\t\tuser: { $in: customersInPeriod },\n\t\t});\n\n\t\t// New customers = in period − existing\n\t\treturn customersInPeriod.length - existingCustomers.length;\n\t}\n\n\tpublic async getOrdersGraph(data: MarketingGraphInputDTO): Promise<MarketingOrdersGraphOutputDTO> {\n\t\tconst dateFormat = data.type === GraphType.MONTHLY ? '%Y-%m' : '%Y-%m-%d';\n\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\t// Filter only delivered orders within the given date range\n\t\t\tthis.buildBaseMatch(new mongoose.Types.ObjectId(data.marketingId), {\n\t\t\t\tstart: data.startDate,\n\t\t\t\tend: data.endDate,\n\t\t\t}),\n\n\t\t\t// Extract formatted date from 'processedAt' for grouping\n\t\t\t{\n\t\t\t\t$project: {\n\t\t\t\t\tdate: {\n\t\t\t\t\t\t$dateToString: {\n\t\t\t\t\t\t\tformat: dateFormat,\n\t\t\t\t\t\t\tdate: '$processedAt',\n\t\t\t\t\t\t\ttimezone: LyxaTimezone,\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 by formatted date and count orders\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: '$date',\n\t\t\t\t\torders: { $sum: 1 },\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Sort results chronologically\n\t\t\t{\n\t\t\t\t$sort: { _id: 1 },\n\t\t\t},\n\n\t\t\t// Format the response to match output DTO structure\n\t\t\t{\n\t\t\t\t$project: {\n\t\t\t\t\t_id: 0,\n\t\t\t\t\tdate: '$_id',\n\t\t\t\t\torders: 1,\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\n\t\t// Execute aggregation pipeline\n\t\tconst results = await this.orderModel.aggregate(pipeline).allowDiskUse(true).exec();\n\n\t\tconst graphData = fillMissingPeriods(results, data.startDate, data.endDate, data.type, {\n\t\t\torders: 0,\n\t\t});\n\n\t\treturn graphData;\n\t}\n\n\tpublic async getCustomersGraph(data: MarketingGraphInputDTO): Promise<{ date: string; users: number }[]> {\n\t\t// 1️⃣ Get shopId from marketing\n\t\tconst marketing = await this.model.findById(data.marketingId).select('shop').lean();\n\n\t\tif (!marketing) {\n\t\t\tnotFoundError('Marketing not found');\n\t\t}\n\n\t\tconst shopId = marketing.shop._id;\n\n\t\t// 2️⃣ Fetch users who ordered from this shop BEFORE the start date\n\t\tconst oldUsers = await this.orderModel.distinct('user', {\n\t\t\tshop: shopId,\n\t\t\tstatus: RegularOrderStatus.DELIVERED,\n\t\t\tprocessedAt: { $lt: data.startDate },\n\t\t});\n\n\t\tconst dateFormat = data.type === GraphType.MONTHLY ? '%Y-%m' : '%Y-%m-%d';\n\n\t\t// 3️⃣ Aggregation pipeline\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\t// Orders during the period with this marketing\n\t\t\t{\n\t\t\t\t$match: {\n\t\t\t\t\torderType: OrderType.REGULAR,\n\t\t\t\t\tstatus: RegularOrderStatus.DELIVERED,\n\t\t\t\t\tshop: shopId,\n\t\t\t\t\tmarketings: new mongoose.Types.ObjectId(data.marketingId),\n\t\t\t\t\tprocessedAt: { $gte: data.startDate, $lte: data.endDate },\n\t\t\t\t\tuser: { $nin: oldUsers }, // 🚫 exclude old users\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Sort so earliest order per user comes first\n\t\t\t{ $sort: { processedAt: 1 } },\n\n\t\t\t// First order per user (new users only)\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: '$user',\n\t\t\t\t\tfirstOrderDate: { $first: '$processedAt' },\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Format date\n\t\t\t{\n\t\t\t\t$project: {\n\t\t\t\t\tdate: {\n\t\t\t\t\t\t$dateToString: {\n\t\t\t\t\t\t\tformat: dateFormat,\n\t\t\t\t\t\t\tdate: '$firstOrderDate',\n\t\t\t\t\t\t\ttimezone: LyxaTimezone,\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 by date\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: '$date',\n\t\t\t\t\tusers: { $sum: 1 },\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Sort chronologically\n\t\t\t{ $sort: { _id: 1 } },\n\n\t\t\t// Shape response\n\t\t\t{\n\t\t\t\t$project: {\n\t\t\t\t\t_id: 0,\n\t\t\t\t\tdate: '$_id',\n\t\t\t\t\tusers: 1,\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\n\t\tconst results = await this.orderModel.aggregate(pipeline).allowDiskUse(true).exec();\n\n\t\t// 4️⃣ Fill missing periods\n\t\treturn fillMissingPeriods(results, data.startDate, data.endDate, data.type, {\n\t\t\tusers: 0,\n\t\t});\n\t}\n\n\tpublic async getAmountSpentGraph(\n\t\tdata: MarketingGraphInputDTO\n\t): Promise<{ date: string; amount: number }[]> {\n\t\tconst marketing = await this.model.findById(data.marketingId).lean();\n\n\t\tif (!marketing) {\n\t\t\tnotFoundError('Marketing not found');\n\t\t}\n\n\t\tconst marketingId = new mongoose.Types.ObjectId(data.marketingId);\n\t\tconst dateFormat = data.type === GraphType.MONTHLY ? '%Y-%m' : '%Y-%m-%d';\n\n\t\tlet pipeline: mongoose.PipelineStage[] | null = null;\n\n\t\t/**\n\t\t * ITEM-BASED (DISCOUNT / BUY1GET1)\n\t\t */\n\t\tif ([MarketingType.DISCOUNT, MarketingType.BUY1GET1].includes(marketing.marketingType)) {\n\t\t\tpipeline = [\n\t\t\t\tthis.buildBaseMatch(marketingId, {\n\t\t\t\t\tstart: data.startDate,\n\t\t\t\t\tend: data.endDate,\n\t\t\t\t}),\n\t\t\t\t{\n\t\t\t\t\t$lookup: {\n\t\t\t\t\t\tfrom: 'lineItems',\n\t\t\t\t\t\tlocalField: 'cart',\n\t\t\t\t\t\tforeignField: 'cart',\n\t\t\t\t\t\tas: 'lineItems',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $unwind: '$lineItems' },\n\t\t\t\t{\n\t\t\t\t\t$match: {\n\t\t\t\t\t\t'lineItems.marketings': marketingId,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\tdate: {\n\t\t\t\t\t\t\t$dateToString: {\n\t\t\t\t\t\t\t\tformat: dateFormat,\n\t\t\t\t\t\t\t\tdate: '$processedAt',\n\t\t\t\t\t\t\t\ttimezone: LyxaTimezone,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tamount: {\n\t\t\t\t\t\t\t$add: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\t\t\tmarketing.isAppliedByCompany,\n\t\t\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByCompany', 0] },\n\t\t\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByShop', 0] },\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{\n\t\t\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\t\t\tmarketing.isAppliedByCompany,\n\t\t\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByCompany', 0] },\n\t\t\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByShop', 0] },\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\t{\n\t\t\t\t\t$group: {\n\t\t\t\t\t\t_id: '$date',\n\t\t\t\t\t\tamount: { $sum: '$amount' },\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $sort: { _id: 1 } },\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\t_id: 0,\n\t\t\t\t\t\tdate: '$_id',\n\t\t\t\t\t\tamount: 1,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t];\n\t\t} else if (marketing.marketingType === MarketingType.FREE_DELIVERY) {\n\n\t\t/**\n\t\t * FREE DELIVERY\n\t\t */\n\t\t\tpipeline = [\n\t\t\t\tthis.buildBaseMatch(marketingId, {\n\t\t\t\t\tstart: data.startDate,\n\t\t\t\t\tend: data.endDate,\n\t\t\t\t}),\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\tdate: {\n\t\t\t\t\t\t\t$dateToString: {\n\t\t\t\t\t\t\t\tformat: dateFormat,\n\t\t\t\t\t\t\t\tdate: '$processedAt',\n\t\t\t\t\t\t\t\ttimezone: LyxaTimezone,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tamount: {\n\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\tmarketing.isAppliedByCompany,\n\t\t\t\t\t\t\t\t{ $ifNull: ['$adjustedFinance.freeDelivery.companyCut', 0] },\n\t\t\t\t\t\t\t\t{ $ifNull: ['$adjustedFinance.freeDelivery.shopCut', 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\t{\n\t\t\t\t\t$group: {\n\t\t\t\t\t\t_id: '$date',\n\t\t\t\t\t\tamount: { $sum: '$amount' },\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $sort: { _id: 1 } },\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\t_id: 0,\n\t\t\t\t\t\tdate: '$_id',\n\t\t\t\t\t\tamount: 1,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t];\n\t\t} else if (marketing.marketingType === MarketingType.PUNCH_MARKETING) {\n\n\t\t/**\n\t\t * PUNCH MARKETING\n\t\t */\n\t\t\tpipeline = [\n\t\t\t\tthis.buildBaseMatch(marketingId, {\n\t\t\t\t\tstart: data.startDate,\n\t\t\t\t\tend: data.endDate,\n\t\t\t\t}),\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\tdate: {\n\t\t\t\t\t\t\t$dateToString: {\n\t\t\t\t\t\t\t\tformat: dateFormat,\n\t\t\t\t\t\t\t\tdate: '$processedAt',\n\t\t\t\t\t\t\t\ttimezone: LyxaTimezone,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tamount: {\n\t\t\t\t\t\t\t$ifNull: ['$adjustedFinance.pricing.punchMarketingDiscount', 0],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$group: {\n\t\t\t\t\t\t_id: '$date',\n\t\t\t\t\t\tamount: { $sum: '$amount' },\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $sort: { _id: 1 } },\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\t_id: 0,\n\t\t\t\t\t\tdate: '$_id',\n\t\t\t\t\t\tamount: 1,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t];\n\t\t}\n\n\t\t// If marketing type doesn't produce spend graph → return empty graph\n\t\tif (!pipeline) {\n\t\t\treturn fillMissingPeriods([] as { date: string; amount: number }[], data.startDate, data.endDate, data.type, {\n\t\t\t\tamount: 0,\n\t\t\t});\n\t\t}\n\n\t\tconst results = await this.orderModel.aggregate(pipeline).allowDiskUse(true).exec();\n\n\t\treturn fillMissingPeriods(results, data.startDate, data.endDate, data.type, {\n\t\t\tamount: 0,\n\t\t});\n\t}\n}\n\nexport const marketingMetricsRepository = Container.get(MarketingMetricsRepository);\n"]}
|
|
1
|
+
{"version":3,"file":"MarketingMetricsRepository.js","sourceRoot":"/","sources":["modules/marketing/patterns/repository/MarketingMetricsRepository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,8DAA2D;AAC3D,oDAA+E;AAC/E,sEAQmD;AAQnD,4DAA6F;AAC7F,qGAAgG;AAChG,4DAAkE;AAClE,4EAA0E;AAE1E,4DAAiE;AACjE,0CAAoD;AAG7C,IAAM,0BAA0B,GAAhC,MAAM,0BAA0B;IAC9B,KAAK,CAAiD;IACtD,UAAU,CAA2D;IAE7E;QACC,IAAI,CAAC,KAAK,GAAG,uBAAgE,CAAC;QAC9E,IAAI,CAAC,UAAU,GAAG,0BAA6E,CAAC;IACjG,CAAC;IAEM,+BAA+B,CACrC,KAGC;QAED,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAmC,CAAC;QAE5D,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAA8B,CAAC;QAGhE,KAAK,MAAM,SAAS,IAAI,KAAK,CAAC,UAAU,IAAI,EAAE,EAAE,CAAC;YAChD,iBAAiB,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,SAAS,CAAC,kBAAkB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;QACpG,CAAC;QAED,MAAM,QAAQ,GAAG,CAAC,WAAoC,EAAE,MAAc,EAAE,EAAE;YACzE,IAAI,CAAC,MAAM,IAAI,MAAM,IAAI,CAAC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AE,CAAC;SACT,CAAC,CAAC;IACJ,CAAC;CACD,CAAA;AAtqBY,gEAA0B;qCAA1B,0BAA0B;IADtC,IAAA,gBAAO,GAAE;;GACG,0BAA0B,CAsqBtC;AAEY,QAAA,0BAA0B,GAAG,gBAAS,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { dayjs } from '@lyxa.ai/core/dist/utilities/dayjs';\nimport { DocumentType, mongoose, ReturnModelType } from '@typegoose/typegoose';\nimport {\n\tFeaturedMarketing,\n\tLineItem,\n\tMarketing,\n\tMarketingModel,\n\tMarketing as Model,\n\tRegularOrder,\n\tRegularOrderModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport {\n\tIncreaseDuringDiscountDTO,\n\tMarketingGraphInputDTO,\n\tMarketingMetricsDTO,\n\tMarketingOrdersGraphOutputDTO,\n} from '@modules/marketing/validations';\nimport { SoftDeleteModel } from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport { GraphType, OrderType, RegularOrderStatus } from '@lyxa.ai/core/dist/utilities/enum';\nimport { productMarketingService } from '@modules/marketing/services/product-marketing.service';\nimport { MarketingType } from '@lyxa.ai/core/dist/utilities/enum';\nimport { notFoundError } from '@lyxa.ai/core/dist/utilities/error-common';\nimport { Duration } from '@lyxa.ai/core/dist/libraries/mongo/models/shared/duration.model';\nimport { LyxaTimezone } from '@lyxa.ai/core/dist/utilities/time';\nimport { fillMissingPeriods } from '@utility/utils';\n\n@Service()\nexport class MarketingMetricsRepository {\n\tprivate model: typeof MarketingModel & SoftDeleteModel<Model>;\n\tprivate orderModel: typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\n\tconstructor() {\n\t\tthis.model = MarketingModel as typeof MarketingModel & SoftDeleteModel<Model>;\n\t\tthis.orderModel = RegularOrderModel as typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\t}\n\n\tpublic calculateMarketingSpendPerOrder(\n\t\torder: DocumentType<RegularOrder> & {\n\t\t\tlineItems: DocumentType<LineItem>[];\n\t\t\tmarketings: DocumentType<Marketing>[];\n\t\t}\n\t): Map<mongoose.Types.ObjectId, number> {\n\t\tconst spendMap = new Map<mongoose.Types.ObjectId, number>();\n\n\t\tconst marketingOwnerMap = new Map<string, 'COMPANY' | 'SHOP'>();\n\n\t\t// Build owner map\n\t\tfor (const marketing of order.marketings ?? []) {\n\t\t\tmarketingOwnerMap.set(marketing._id.toString(), marketing.isAppliedByCompany ? 'COMPANY' : 'SHOP');\n\t\t}\n\n\t\tconst addSpend = (marketingId: mongoose.Types.ObjectId, amount: number) => {\n\t\t\tif (!amount || amount <= 0) return;\n\t\t\tspendMap.set(marketingId, (spendMap.get(marketingId) ?? 0) + amount);\n\t\t};\n\n\t\tconst getRelevantAmount = (\n\t\t\tmarketingId: mongoose.Types.ObjectId,\n\t\t\tcompanyAmount: number,\n\t\t\tshopAmount: number\n\t\t) => {\n\t\t\tconst owner = marketingOwnerMap.get(marketingId.toString());\n\t\t\treturn owner === 'COMPANY' ? companyAmount : shopAmount;\n\t\t};\n\n\t\t/**\n\t\t * ITEM-BASED\n\t\t */\n\t\tfor (const lineItem of order.lineItems ?? []) {\n\t\t\tfor (const marketingId of lineItem.marketings ?? []) {\n\t\t\t\tconst snap = lineItem.calculationSnap ?? {};\n\n\t\t\t\taddSpend(\n\t\t\t\t\tmarketingId._id,\n\t\t\t\t\tgetRelevantAmount(\n\t\t\t\t\t\tmarketingId._id,\n\t\t\t\t\t\t(Number(snap.itemDiscountByCompany) || 0) + (Number(snap.b1g1DiscountByCompany) || 0),\n\t\t\t\t\t\t(Number(snap.itemDiscountByShop) || 0) + (Number(snap.b1g1DiscountByShop) || 0)\n\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * FREE DELIVERY\n\t\t */\n\t\tif (order.adjustedFinance?.freeDelivery) {\n\t\t\tfor (const marketingId of order.marketings ?? []) {\n\t\t\t\taddSpend(\n\t\t\t\t\tmarketingId._id,\n\t\t\t\t\tgetRelevantAmount(\n\t\t\t\t\t\tmarketingId._id,\n\t\t\t\t\t\torder.adjustedFinance.freeDelivery.companyCut ?? 0,\n\t\t\t\t\t\torder.adjustedFinance.freeDelivery.shopCut ?? 0\n\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\t/**\n\t\t * PUNCH MARKETING\n\t\t */\n\t\tif (order.adjustedFinance?.pricing?.punchMarketingDiscount) {\n\t\t\tfor (const marketingId of order.marketings ?? []) {\n\t\t\t\taddSpend(marketingId._id, order.adjustedFinance.pricing.punchMarketingDiscount);\n\t\t\t}\n\t\t}\n\n\t\treturn spendMap;\n\t}\n\n\tpublic async calculateMarketingSpent(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tduration?: Duration\n\t): Promise<{ amount: number; marketingType: MarketingType }> {\n\t\tconst marketing = await this.model.findById(marketingId).lean();\n\n\t\tif (!marketing) {\n\t\t\tnotFoundError('Marketing not found');\n\t\t}\n\n\t\tconst calculators: Record<MarketingType, () => Promise<number>> = {\n\t\t\t[MarketingType.FREE_DELIVERY]: () => this.calculateFreeDeliverySpent(marketingId, duration),\n\n\t\t\t[MarketingType.DISCOUNT]: () =>\n\t\t\t\tthis.calculateItemBasedSpent(marketingId, marketing.isAppliedByCompany, duration),\n\n\t\t\t[MarketingType.BUY1GET1]: () =>\n\t\t\t\tthis.calculateItemBasedSpent(marketingId, marketing.isAppliedByCompany, duration),\n\n\t\t\t[MarketingType.PUNCH_MARKETING]: () => this.calculatePunchMarketingSpent(marketingId, duration),\n\t\t\t[MarketingType.FEATURED]: function (): Promise<number> {\n\t\t\t\treturn Promise.resolve((marketing as DocumentType<FeaturedMarketing>).amount || 0);\n\t\t\t},\n\t\t};\n\n\t\tconst amount = calculators[marketing.marketingType] ? await calculators[marketing.marketingType]() : 0;\n\n\t\treturn {\n\t\t\tamount,\n\t\t\tmarketingType: marketing.marketingType,\n\t\t};\n\t}\n\n\tprivate async calculateItemBasedSpent(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tisAppliedByAdmin: boolean,\n\t\tduration?: Duration\n\t): Promise<number> {\n\t\tconst addBlock = isAppliedByAdmin\n\t\t\t? [\n\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByCompany', 0] },\n\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByCompany', 0] },\n\t\t\t\t]\n\t\t\t: [\n\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByShop', 0] },\n\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByShop', 0] },\n\t\t\t\t];\n\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\tthis.buildBaseMatch(marketingId, duration),\n\n\t\t\t{\n\t\t\t\t$lookup: {\n\t\t\t\t\tfrom: 'lineItems',\n\t\t\t\t\tlocalField: 'cart',\n\t\t\t\t\tforeignField: 'cart',\n\t\t\t\t\tas: 'lineItems',\n\t\t\t\t},\n\t\t\t},\n\t\t\t{ $unwind: '$lineItems' },\n\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: null,\n\t\t\t\t\ttotalSpent: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$add: addBlock,\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\n\t\treturn this.runAggregateAndGetTotal(pipeline, 'totalSpent');\n\t}\n\n\tprivate async calculateFreeDeliverySpent(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tduration?: Duration\n\t): Promise<number> {\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\tthis.buildBaseMatch(marketingId, duration),\n\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: null,\n\t\t\t\t\ttotalSpent: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$add: [\n\t\t\t\t\t\t\t\t{ $ifNull: ['$adjustedFinance.freeDelivery.companyCut', 0] },\n\t\t\t\t\t\t\t\t{ $ifNull: ['$adjustedFinance.freeDelivery.shopCut', 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\t\t];\n\n\t\treturn this.runAggregateAndGetTotal(pipeline, 'totalSpent');\n\t}\n\n\tprivate async calculatePunchMarketingSpent(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tduration?: Duration\n\t): Promise<number> {\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\tthis.buildBaseMatch(marketingId, duration),\n\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: null,\n\t\t\t\t\ttotalSpent: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$ifNull: ['$adjustedFinance.pricing.punchMarketingDiscount', 0],\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\n\t\treturn this.runAggregateAndGetTotal(pipeline, 'totalSpent');\n\t}\n\n\tprivate buildBaseMatch(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tduration?: Duration\n\t): mongoose.PipelineStage.Match {\n\t\treturn {\n\t\t\t$match: {\n\t\t\t\tmarketings: marketingId,\n\t\t\t\tstatus: { $ne: RegularOrderStatus.CANCELLED },\n\t\t\t\t...(duration && {\n\t\t\t\t\tprocessedAt: {\n\t\t\t\t\t\t$gte: duration.start,\n\t\t\t\t\t\t$lte: duration.end,\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t},\n\t\t};\n\t}\n\n\tprivate async runAggregateAndGetTotal(pipeline: mongoose.PipelineStage[], field: string): Promise<number> {\n\t\tconst [result] = await this.orderModel.aggregate(pipeline);\n\t\treturn result?.[field] ?? 0;\n\t}\n\n\tpublic async getMetrics(marketingId: mongoose.Types.ObjectId): Promise<MarketingMetricsDTO> {\n\t\tconst marketing = await this.model.findById(marketingId).lean();\n\n\t\tif (!marketing) {\n\t\t\tnotFoundError('Marketing not found');\n\t\t}\n\n\t\t//TODO: fix current end and previous start\n\n\t\tconst currentDuration = { start: marketing.duration.start, end: dayjs().toDate() };\n\t\tconst previousDuration = {\n\t\t\tstart: dayjs(marketing.duration.start).subtract(dayjs().diff(marketing.duration.start)).toDate(),\n\t\t\tend: marketing.duration.start,\n\t\t};\n\n\t\tlet metrics: MarketingMetricsDTO = {};\n\n\t\tif ([MarketingType.DISCOUNT, MarketingType.BUY1GET1].includes(marketing.marketingType)) {\n\t\t\tmetrics.numberOfItems = await this.getMarketingItemCount(marketingId);\n\t\t}\n\n\t\tmetrics.orderIncreaseWithDiscount = await this.calculateOrderIncreaseWithDiscount(\n\t\t\tmarketingId,\n\t\t\tmarketing.shop._id,\n\t\t\tcurrentDuration,\n\t\t\tpreviousDuration\n\t\t);\n\t\tmetrics.customerIncreaseWithDiscount = await this.calculateCustomerIncreaseWithDiscount(\n\t\t\tmarketingId,\n\t\t\tmarketing.shop._id,\n\t\t\tcurrentDuration,\n\t\t\tpreviousDuration\n\t\t);\n\n\t\treturn metrics;\n\t}\n\n\tprivate async getMarketingItemCount(marketingId: mongoose.Types.ObjectId): Promise<number> {\n\t\tconst result = await productMarketingService.countDocuments({ marketing: marketingId });\n\t\treturn result ?? 0;\n\t}\n\n\tprivate async calculateOrderIncreaseWithDiscount(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tshopId: mongoose.Types.ObjectId,\n\t\tcurrentDuration: Duration,\n\t\tpreviousDuration: Duration\n\t): Promise<IncreaseDuringDiscountDTO> {\n\t\tconst [currentOrders, previousOrders] = await Promise.all([\n\t\t\tthis.orderModel.countDocuments({\n\t\t\t\tcreatedAt: {\n\t\t\t\t\t$gte: currentDuration.start,\n\t\t\t\t\t$lte: currentDuration.end,\n\t\t\t\t},\n\t\t\t\tmarketings: marketingId, // marketing must be applied\n\t\t\t\tshop: shopId,\n\t\t\t}),\n\t\t\tthis.orderModel.countDocuments({\n\t\t\t\tcreatedAt: {\n\t\t\t\t\t$gte: previousDuration.start,\n\t\t\t\t\t$lte: previousDuration.end,\n\t\t\t\t},\n\t\t\t\tshop: shopId,\n\t\t\t}),\n\t\t]);\n\n\t\treturn this.calculateIncrease(currentOrders, previousOrders);\n\t}\n\n\tprivate async calculateCustomerIncreaseWithDiscount(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tshopId: mongoose.Types.ObjectId,\n\t\tcurrentDuration: Duration,\n\t\tpreviousDuration: Duration\n\t): Promise<IncreaseDuringDiscountDTO> {\n\t\tconst [currentNewCustomers, previousNewCustomers] = await Promise.all([\n\t\t\tthis.getNewCustomersForShop(shopId, currentDuration, { marketings: marketingId }),\n\t\t\tthis.getNewCustomersForShop(shopId, previousDuration),\n\t\t]);\n\n\t\treturn this.calculateIncrease(currentNewCustomers, previousNewCustomers);\n\t}\n\n\tprivate calculateIncrease(current: number, previous: number): IncreaseDuringDiscountDTO {\n\t\tconst count = current - previous;\n\n\t\tconst percentageIncrease = previous === 0 ? (current > 0 ? 100 : 0) : (count / previous) * 100;\n\n\t\treturn {\n\t\t\tcount,\n\t\t\tpercentageIncrease,\n\t\t};\n\t}\n\n\tprivate async getNewCustomersForShop(\n\t\tshopId: mongoose.Types.ObjectId,\n\t\tduration: Duration,\n\t\textraMatch: Record<string, any> = {}\n\t): Promise<number> {\n\t\t// Customers who ordered during the period\n\t\tconst customersInPeriod = await this.orderModel.distinct('user', {\n\t\t\tcreatedAt: {\n\t\t\t\t$gte: duration.start,\n\t\t\t\t$lte: duration.end,\n\t\t\t},\n\t\t\tshop: shopId,\n\t\t\t...extraMatch,\n\t\t});\n\n\t\tif (customersInPeriod.length === 0) {\n\t\t\treturn 0;\n\t\t}\n\n\t\t// Customers who had ordered from this shop BEFORE the period\n\t\tconst existingCustomers = await this.orderModel.distinct('user', {\n\t\t\tshop: shopId,\n\t\t\tcreatedAt: { $lt: duration.start },\n\t\t\tuser: { $in: customersInPeriod },\n\t\t});\n\n\t\t// New customers = in period − existing\n\t\treturn customersInPeriod.length - existingCustomers.length;\n\t}\n\n\tpublic async getOrdersGraph(data: MarketingGraphInputDTO): Promise<MarketingOrdersGraphOutputDTO> {\n\t\tconst dateFormat = data.type === GraphType.MONTHLY ? '%Y-%m' : '%Y-%m-%d';\n\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\t// Filter only delivered orders within the given date range\n\t\t\tthis.buildBaseMatch(new mongoose.Types.ObjectId(data.marketingId), {\n\t\t\t\tstart: data.startDate,\n\t\t\t\tend: data.endDate,\n\t\t\t}),\n\n\t\t\t// Extract formatted date from 'processedAt' for grouping\n\t\t\t{\n\t\t\t\t$project: {\n\t\t\t\t\tdate: {\n\t\t\t\t\t\t$dateToString: {\n\t\t\t\t\t\t\tformat: dateFormat,\n\t\t\t\t\t\t\tdate: '$processedAt',\n\t\t\t\t\t\t\ttimezone: LyxaTimezone,\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 by formatted date and count orders\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: '$date',\n\t\t\t\t\torders: { $sum: 1 },\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Sort results chronologically\n\t\t\t{\n\t\t\t\t$sort: { _id: 1 },\n\t\t\t},\n\n\t\t\t// Format the response to match output DTO structure\n\t\t\t{\n\t\t\t\t$project: {\n\t\t\t\t\t_id: 0,\n\t\t\t\t\tdate: '$_id',\n\t\t\t\t\torders: 1,\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\n\t\t// Execute aggregation pipeline\n\t\tconst results = await this.orderModel.aggregate(pipeline).allowDiskUse(true).exec();\n\n\t\tconst graphData = fillMissingPeriods(results, data.startDate, data.endDate, data.type, {\n\t\t\torders: 0,\n\t\t});\n\n\t\treturn graphData;\n\t}\n\n\tpublic async getCustomersGraph(data: MarketingGraphInputDTO): Promise<{ date: string; users: number }[]> {\n\t\t// 1️⃣ Get shopId from marketing\n\t\tconst marketing = await this.model.findById(data.marketingId).select('shop').lean();\n\n\t\tif (!marketing) {\n\t\t\tnotFoundError('Marketing not found');\n\t\t}\n\n\t\tconst shopId = marketing.shop._id;\n\n\t\t// 2️⃣ Fetch users who ordered from this shop BEFORE the start date\n\t\tconst oldUsers = await this.orderModel.distinct('user', {\n\t\t\tshop: shopId,\n\t\t\tstatus: RegularOrderStatus.DELIVERED,\n\t\t\tprocessedAt: { $lt: data.startDate },\n\t\t});\n\n\t\tconst dateFormat = data.type === GraphType.MONTHLY ? '%Y-%m' : '%Y-%m-%d';\n\n\t\t// 3️⃣ Aggregation pipeline\n\t\tconst pipeline: mongoose.PipelineStage[] = [\n\t\t\t// Orders during the period with this marketing\n\t\t\t{\n\t\t\t\t$match: {\n\t\t\t\t\torderType: OrderType.REGULAR,\n\t\t\t\t\tstatus: RegularOrderStatus.DELIVERED,\n\t\t\t\t\tshop: shopId,\n\t\t\t\t\tmarketings: new mongoose.Types.ObjectId(data.marketingId),\n\t\t\t\t\tprocessedAt: { $gte: data.startDate, $lte: data.endDate },\n\t\t\t\t\tuser: { $nin: oldUsers }, // 🚫 exclude old users\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Sort so earliest order per user comes first\n\t\t\t{ $sort: { processedAt: 1 } },\n\n\t\t\t// First order per user (new users only)\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: '$user',\n\t\t\t\t\tfirstOrderDate: { $first: '$processedAt' },\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Format date\n\t\t\t{\n\t\t\t\t$project: {\n\t\t\t\t\tdate: {\n\t\t\t\t\t\t$dateToString: {\n\t\t\t\t\t\t\tformat: dateFormat,\n\t\t\t\t\t\t\tdate: '$firstOrderDate',\n\t\t\t\t\t\t\ttimezone: LyxaTimezone,\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 by date\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: '$date',\n\t\t\t\t\tusers: { $sum: 1 },\n\t\t\t\t},\n\t\t\t},\n\n\t\t\t// Sort chronologically\n\t\t\t{ $sort: { _id: 1 } },\n\n\t\t\t// Shape response\n\t\t\t{\n\t\t\t\t$project: {\n\t\t\t\t\t_id: 0,\n\t\t\t\t\tdate: '$_id',\n\t\t\t\t\tusers: 1,\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\n\t\tconst results = await this.orderModel.aggregate(pipeline).allowDiskUse(true).exec();\n\n\t\t// 4️⃣ Fill missing periods\n\t\treturn fillMissingPeriods(results, data.startDate, data.endDate, data.type, {\n\t\t\tusers: 0,\n\t\t});\n\t}\n\n\tpublic async getAmountSpentGraph(\n\t\tdata: MarketingGraphInputDTO\n\t): Promise<{ date: string; amount: number }[]> {\n\t\tconst marketing = await this.model.findById(data.marketingId).lean();\n\n\t\tif (!marketing) {\n\t\t\tnotFoundError('Marketing not found');\n\t\t}\n\n\t\tconst marketingId = new mongoose.Types.ObjectId(data.marketingId);\n\t\tconst dateFormat = data.type === GraphType.MONTHLY ? '%Y-%m' : '%Y-%m-%d';\n\n\t\tlet pipeline: mongoose.PipelineStage[] | null = null;\n\n\t\t/**\n\t\t * ITEM-BASED (DISCOUNT / BUY1GET1)\n\t\t */\n\t\tif ([MarketingType.DISCOUNT, MarketingType.BUY1GET1].includes(marketing.marketingType)) {\n\t\t\tpipeline = [\n\t\t\t\tthis.buildBaseMatch(marketingId, {\n\t\t\t\t\tstart: data.startDate,\n\t\t\t\t\tend: data.endDate,\n\t\t\t\t}),\n\t\t\t\t{\n\t\t\t\t\t$lookup: {\n\t\t\t\t\t\tfrom: 'lineItems',\n\t\t\t\t\t\tlocalField: 'cart',\n\t\t\t\t\t\tforeignField: 'cart',\n\t\t\t\t\t\tas: 'lineItems',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $unwind: '$lineItems' },\n\t\t\t\t{\n\t\t\t\t\t$match: {\n\t\t\t\t\t\t'lineItems.marketings': marketingId,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\tdate: {\n\t\t\t\t\t\t\t$dateToString: {\n\t\t\t\t\t\t\t\tformat: dateFormat,\n\t\t\t\t\t\t\t\tdate: '$processedAt',\n\t\t\t\t\t\t\t\ttimezone: LyxaTimezone,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tamount: {\n\t\t\t\t\t\t\t$add: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\t\t\tmarketing.isAppliedByCompany,\n\t\t\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByCompany', 0] },\n\t\t\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.itemDiscountByShop', 0] },\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{\n\t\t\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\t\t\tmarketing.isAppliedByCompany,\n\t\t\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByCompany', 0] },\n\t\t\t\t\t\t\t\t\t\t{ $ifNull: ['$lineItems.calculationSnap.b1g1DiscountByShop', 0] },\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\t{\n\t\t\t\t\t$group: {\n\t\t\t\t\t\t_id: '$date',\n\t\t\t\t\t\tamount: { $sum: '$amount' },\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $sort: { _id: 1 } },\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\t_id: 0,\n\t\t\t\t\t\tdate: '$_id',\n\t\t\t\t\t\tamount: 1,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t];\n\t\t} else if (marketing.marketingType === MarketingType.FREE_DELIVERY) {\n\t\t\t/**\n\t\t\t * FREE DELIVERY\n\t\t\t */\n\t\t\tpipeline = [\n\t\t\t\tthis.buildBaseMatch(marketingId, {\n\t\t\t\t\tstart: data.startDate,\n\t\t\t\t\tend: data.endDate,\n\t\t\t\t}),\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\tdate: {\n\t\t\t\t\t\t\t$dateToString: {\n\t\t\t\t\t\t\t\tformat: dateFormat,\n\t\t\t\t\t\t\t\tdate: '$processedAt',\n\t\t\t\t\t\t\t\ttimezone: LyxaTimezone,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tamount: {\n\t\t\t\t\t\t\t$cond: [\n\t\t\t\t\t\t\t\tmarketing.isAppliedByCompany,\n\t\t\t\t\t\t\t\t{ $ifNull: ['$adjustedFinance.freeDelivery.companyCut', 0] },\n\t\t\t\t\t\t\t\t{ $ifNull: ['$adjustedFinance.freeDelivery.shopCut', 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\t{\n\t\t\t\t\t$group: {\n\t\t\t\t\t\t_id: '$date',\n\t\t\t\t\t\tamount: { $sum: '$amount' },\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $sort: { _id: 1 } },\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\t_id: 0,\n\t\t\t\t\t\tdate: '$_id',\n\t\t\t\t\t\tamount: 1,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t];\n\t\t} else if (marketing.marketingType === MarketingType.PUNCH_MARKETING) {\n\t\t\t/**\n\t\t\t * PUNCH MARKETING\n\t\t\t */\n\t\t\tpipeline = [\n\t\t\t\tthis.buildBaseMatch(marketingId, {\n\t\t\t\t\tstart: data.startDate,\n\t\t\t\t\tend: data.endDate,\n\t\t\t\t}),\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\tdate: {\n\t\t\t\t\t\t\t$dateToString: {\n\t\t\t\t\t\t\t\tformat: dateFormat,\n\t\t\t\t\t\t\t\tdate: '$processedAt',\n\t\t\t\t\t\t\t\ttimezone: LyxaTimezone,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\tamount: {\n\t\t\t\t\t\t\t$ifNull: ['$adjustedFinance.pricing.punchMarketingDiscount', 0],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$group: {\n\t\t\t\t\t\t_id: '$date',\n\t\t\t\t\t\tamount: { $sum: '$amount' },\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $sort: { _id: 1 } },\n\t\t\t\t{\n\t\t\t\t\t$project: {\n\t\t\t\t\t\t_id: 0,\n\t\t\t\t\t\tdate: '$_id',\n\t\t\t\t\t\tamount: 1,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t];\n\t\t}\n\n\t\t// If marketing type doesn't produce spend graph → return empty graph\n\t\tif (!pipeline) {\n\t\t\treturn fillMissingPeriods(\n\t\t\t\t[] as { date: string; amount: number }[],\n\t\t\t\tdata.startDate,\n\t\t\t\tdata.endDate,\n\t\t\t\tdata.type,\n\t\t\t\t{\n\t\t\t\t\tamount: 0,\n\t\t\t\t}\n\t\t\t);\n\t\t}\n\n\t\tconst results = await this.orderModel.aggregate(pipeline).allowDiskUse(true).exec();\n\n\t\treturn fillMissingPeriods(results, data.startDate, data.endDate, data.type, {\n\t\t\tamount: 0,\n\t\t});\n\t}\n}\n\nexport const marketingMetricsRepository = Container.get(MarketingMetricsRepository);\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"marketing.router.d.ts","sourceRoot":"/","sources":["modules/marketing/routers/marketing.router.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAGhD,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"marketing.router.d.ts","sourceRoot":"/","sources":["modules/marketing/routers/marketing.router.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAGhD,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4BAOmB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuI9C,CAAC"}
|