@lyxa.ai/marketing 1.0.29 → 1.0.30
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/index.d.ts +635 -0
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/modules/coupon/routers/coupon.router.d.ts +635 -0
- package/dist/lib/modules/coupon/routers/coupon.router.d.ts.map +1 -1
- package/dist/lib/modules/coupon/routers/coupon.router.js +4 -0
- package/dist/lib/modules/coupon/routers/coupon.router.js.map +1 -1
- package/dist/lib/modules/coupon/services/coupon.service.d.ts +2 -1
- package/dist/lib/modules/coupon/services/coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/coupon.service.js +41 -18
- package/dist/lib/modules/coupon/services/coupon.service.js.map +1 -1
- package/dist/lib/modules/coupon/services/global-coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/global-coupon.service.js +2 -1
- package/dist/lib/modules/coupon/services/global-coupon.service.js.map +1 -1
- package/dist/lib/modules/coupon/services/individual-store-coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/individual-store-coupon.service.js +3 -0
- package/dist/lib/modules/coupon/services/individual-store-coupon.service.js.map +1 -1
- package/dist/lib/modules/coupon/services/individual-user-coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/individual-user-coupon.service.js +1 -0
- package/dist/lib/modules/coupon/services/individual-user-coupon.service.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 +2 -0
- 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 +1 -0
- package/dist/lib/modules/coupon/services/referral-reward-coupon.service.js.map +1 -1
- package/dist/lib/modules/coupon/services/shops-categories-coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/shops-categories-coupon.service.js +3 -0
- package/dist/lib/modules/coupon/services/shops-categories-coupon.service.js.map +1 -1
- package/dist/lib/modules/coupon/validations/all-coupons.validation.d.ts.map +1 -1
- package/dist/lib/modules/coupon/validations/all-coupons.validation.js.map +1 -1
- package/dist/lib/modules/coupon/validations/coupon.validation.d.ts +21 -0
- package/dist/lib/modules/coupon/validations/coupon.validation.d.ts.map +1 -1
- package/dist/lib/modules/coupon/validations/coupon.validation.js +10 -4
- package/dist/lib/modules/coupon/validations/coupon.validation.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 +5 -8
- package/dist/lib/modules/marketing/routers/marketing.router.js.map +1 -1
- 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 +11 -3
- 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.map +1 -1
- package/dist/lib/modules/marketing/services/discount-marketing.service.js +11 -3
- 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 +23 -3
- package/dist/lib/modules/marketing/services/marketing.service.js.map +1 -1
- package/dist/types/index.d.ts +635 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modules/coupon/routers/coupon.router.d.ts +635 -0
- package/dist/types/modules/coupon/routers/coupon.router.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/coupon.service.d.ts +2 -1
- package/dist/types/modules/coupon/services/coupon.service.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/global-coupon.service.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/individual-store-coupon.service.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/individual-user-coupon.service.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/coupon/services/shops-categories-coupon.service.d.ts.map +1 -1
- package/dist/types/modules/coupon/validations/all-coupons.validation.d.ts.map +1 -1
- package/dist/types/modules/coupon/validations/coupon.validation.d.ts +21 -0
- package/dist/types/modules/coupon/validations/coupon.validation.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.map +1 -1
- 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/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coupon.validation.js","sourceRoot":"/","sources":["modules/coupon/validations/coupon.validation.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,iGAImE;AACnE,4DAA0G;AAC1G,iGAGmE;AACnE,8DAA2D;AAK9C,QAAA,YAAY,GAAG;IAC3B,IAAI,EAAE,iCAAa,CAAC,MAAM,CAAC,MAAM,EAAE;QAClC,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,MAAM,CAAC,aAAa,CAAC,EAAE,OAAO,EAAE,+BAA+B,EAAE;QACvF,SAAS,EAAE,CAAC;QACZ,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,6BAA6B;QACzC,UAAU,EAAE,6BAA6B;KACzC,CAAC;IACF,iBAAiB,EAAE,iCAAa,CAAC,MAAM,CAAC,mBAAmB,CAAC;SAC1D,GAAG,CAAC,CAAC,CAAC;SACN,GAAG,CAAC,CAAC,CAAC;SACN,QAAQ,EAAE;SACV,QAAQ,EAAE;SACV,OAAO,CAAC,IAAI,CAAC;IACf,0BAA0B,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CAAkC;QAC9C,KAAK,EAAE;YACN,OAAO,EAAE,aAAa;YACtB,OAAO,EAAE,2CAA2C;SACpD;KACD,CAAC;CACF,CAAC,CAAC;AAEU,QAAA,wBAAwB,GAAG,OAAC,CAAC,MAAM,CAAC;IAChD,GAAG,EAAE,iCAAa,CAAC,sBAAsB;IACzC,MAAM,EAAE,iCAAa,CAAC,QAAQ,CAAC,aAAM,EAAE,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,aAAM,CAAC,MAAM,CAAC;CAClF,CAAC,CAAC;AAEU,QAAA,8BAA8B,GAAG,IAAA,wCAAoB,EAAC,oCAA4B,CAAC,CAAC","sourcesContent":["import { z } from 'zod';\nimport {\n\tZodValidation,\n\tcreateSchemaBuilder,\n\tDTO,\n} from '@lyxa.ai/core/dist/utilities/validation/global-validation';\nimport { CouponExpirationReason, CouponType, Status, ValueType } from '@lyxa.ai/core/dist/utilities/enum';\nimport {\n\tcreateResponseSchema,\n\tDurationSchema,\n} from '@lyxa.ai/core/dist/utilities/validation/common-validation';\nimport { dayjs } from '@lyxa.ai/core/dist/utilities/dayjs';\n\n/**\n * Define the base parent schema fields\n */\nexport const couponFields = {\n\tcode: ZodValidation.string('Code', {\n\t\tregex: { pattern: new RegExp('^[A-Z0-9]+$'), message: 'Coupon name must be uppercase' },\n\t\tmaxLength: 6,\n\t\tminLength: 6,\n\t\tminMessage: 'Coupon must be 6 characters',\n\t\tmaxMessage: 'Coupon must be 6 characters',\n\t}),\n\torderLimitPerUser: ZodValidation.number('OrderLimitPerUser')\n\t\t.min(1)\n\t\t.max(5)\n\t\t.optional()\n\t\t.nullable()\n\t\t.default(null),\n\tisOrderLimitPerUserEnabled: ZodValidation.boolean('Is order limit per user enabled').optional().default(false),\n\tcreatedBy: ZodValidation.objectId('Created by').optional(),\n\tisExpired: ZodValidation.boolean('Is expired').optional(),\n};\n\nexport const baseFields = {\n\t...couponFields,\n\tvalueType: ZodValidation.enumType(ValueType, 'Value type'),\n\tvalue: ZodValidation.number('Value'),\n\tsecondaryCurrencyValue: ZodValidation.number('Secondary currency value').optional(),\n\tmaxDiscountLimit: ZodValidation.number('Max discount limit').optional().nullable().default(null),\n\tsecondaryCurrencyMaxDiscountLimit: ZodValidation.number('Secondary currency max discount limit').optional(),\n\tisMaxDiscountLimitEnabled: ZodValidation.boolean('Is max discount limit enabled').optional().default(false),\n\tduration: DurationSchema.transform(duration => {\n\t\tduration.start = dayjs(duration.start).startOf('day').toDate();\n\t\tduration.end = dayjs(duration.end).endOf('day').toDate();\n\t\treturn duration;\n\t}),\n\tspendLimit: ZodValidation.number('Spend limit').optional().nullable().default(null),\n\tisSpendLimitEnabled: ZodValidation.boolean('Is spend limit enabled').optional().default(false),\n\ttotalOrderLimit: ZodValidation.number('Total order limit').optional().nullable().default(null),\n\tisTotalOrderLimitEnabled: ZodValidation.boolean('Is total order limit enabled').optional().default(false),\n\tminOrderValue: ZodValidation.number('Min order value').nullable().default(null),\n\tsecondaryCurrencyMinOrderValue: ZodValidation.number('Secondary currency min order value').optional(),\n\tisMinOrderValueEnabled: ZodValidation.boolean('Is min order value enabled').optional().default(false),\n\tforNewUserOnly: ZodValidation.boolean('ForNewUserOnly').optional().default(false),\n\tstatus: ZodValidation.enumType(Status, 'Status').optional().default(Status.ACTIVE),\n\texpirationReason: ZodValidation.enumType(CouponExpirationReason, 'Expiration reason')\n\t\t.optional()\n\t\t.nullable()\n\t\t.default(null),\n\treferringUser: z\n\t\t.object({\n\t\t\tname: ZodValidation.string('Referring user name').optional(),\n\t\t})\n\t\t.optional(),\n};\n\n/**\n * Create schema builder for Parent entity\n * - Includes timestamps\n * - Excludes 'createdAt' from update operations\n */\nconst schemaBuilder = createSchemaBuilder(baseFields);\n\n/**\n * Get all schemas for the Parent entity\n */\nconst {\n\tBaseSchema: CouponValidationSchema,\n\tEntitySchema: CouponSchema,\n\tIdSchema: CouponIdSchema,\n\tUpdateSchema: UpdateCouponSchema,\n} = schemaBuilder.getAllSchemas({ createdAt: true });\n\n/**\n * Export schemas\n */\nexport const GetUserCouponsResponseSchema = z.object({\n\tavailable: z.array(CouponSchema),\n\tused: z.array(CouponSchema),\n\texpired: z.array(CouponSchema),\n});\n\nexport {\n\tCouponValidationSchema,\n\tCouponSchema,\n\tCouponIdSchema as CouponIdValidationSchema,\n\tUpdateCouponSchema,\n};\n\n/**\n * Schema for getting user coupons\n */\nexport const GetUserCouponSchema = z.object({\n\tuser: ZodValidation.objectId('User').optional(),\n\tshop: ZodValidation.objectId('Shop').optional(),\n\tpage: ZodValidation.number('Page').optional().default(0),\n\tsize: ZodValidation.number('Page size').optional().default(100),\n});\n\n/**\n * Schema for validating coupons\n */\nexport const ValidateCouponSchema = z.object({\n\tuser: ZodValidation.objectIdOrStringSchema,\n\tshop: ZodValidation.objectIdOrStringSchema,\n\tcoupon: ZodValidation.string('Coupon').optional(),\n\tamountBeforeCoupon: ZodValidation.number('Amount before coupon'),\n\tsecondaryAmountBeforeCoupon: ZodValidation.number('Secondary amount before coupon'),\n});\n\nconst discountCutSchema = z.object({\n\tcompanyCut: ZodValidation.number('companyCut').optional(),\n\tsecondaryCompanyCut: ZodValidation.number('secondaryCompanyCut').optional(),\n\tshopCut: ZodValidation.number('shopCut').optional(),\n\tsecondaryShopCut: ZodValidation.number('secondaryShopCut').optional(),\n});\n\n/**\n * Schema for coupon validation output\n */\nexport const CouponValidationOutputSchema = z.object({\n\tisValid: ZodValidation.boolean('IsValid').default(false),\n\tamountAfterCoupon: ZodValidation.number('Amount after coupon').optional(),\n\tsecondaryAmountAfterCoupon: ZodValidation.number('Secondary amount after coupon').optional(),\n\tdiscountAmount: ZodValidation.number('Coupon Discount').optional(),\n\tsecondaryDiscountAmount: ZodValidation.number('Secondary coupon discount').optional(),\n\tdiscountCut: discountCutSchema.optional(),\n});\n\nexport const CouponCodeSchema = z.object({\n\tcouponCode: ZodValidation.string('Coupon code', {\n\t\tminLength: 6,\n\t\tmaxLength: 6,\n\t\tminMessage: 'Coupon code must be 6 characters',\n\t\tmaxMessage: 'Coupon code must be 6 characters',\n\t\tregex: {\n\t\t\tpattern: /^[A-Z0-9]+$/,\n\t\t\tmessage: 'Coupon must be uppercase and alphanumeric',\n\t\t},\n\t}),\n});\n\nexport const UpdateCouponStatusSchema = z.object({\n\t_id: ZodValidation.objectIdOrStringSchema,\n\tstatus: ZodValidation.enumType(Status, 'Status').optional().default(Status.ACTIVE),\n});\n\nexport const CouponValidationResponseSchema = createResponseSchema(CouponValidationOutputSchema);\n\n/**\n * Export DTO types\n */\nexport type CreateCouponDTO = DTO<typeof CouponValidationSchema>;\nexport type CouponDTO = DTO<typeof CouponSchema>;\nexport type UpdateCouponDTO = DTO<typeof UpdateCouponSchema>;\nexport type GetUserCouponDTO = DTO<typeof GetUserCouponSchema>;\nexport type ValidateCouponDTO = DTO<typeof ValidateCouponSchema>;\nexport type CouponValidationOutputDTO = DTO<typeof CouponValidationOutputSchema>;\nexport type GetUserCouponsResponseDTO = DTO<typeof GetUserCouponsResponseSchema>;\nexport type UpdateCouponStatusDTO = DTO<typeof UpdateCouponStatusSchema>;\n"]}
|
|
1
|
+
{"version":3,"file":"coupon.validation.js","sourceRoot":"/","sources":["modules/coupon/validations/coupon.validation.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AACxB,iGAImE;AACnE,4DAA0G;AAC1G,iGAGmE;AACnE,8DAA2D;AAK9C,QAAA,YAAY,GAAG;IAC3B,IAAI,EAAE,iCAAa,CAAC,MAAM,CAAC,MAAM,EAAE;QAClC,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,MAAM,CAAC,aAAa,CAAC,EAAE,OAAO,EAAE,+BAA+B,EAAE;QACvF,SAAS,EAAE,CAAC;QACZ,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,6BAA6B;QACzC,UAAU,EAAE,6BAA6B;KACzC,CAAC;IACF,iBAAiB,EAAE,iCAAa,CAAC,MAAM,CAAC,mBAAmB,CAAC;SAC1D,GAAG,CAAC,CAAC,CAA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,MAAM,CAAC,aAAa,EAAE;QAC/C,SAAS,EAAE,CAAC;QACZ,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,kCAAkC;QAC9C,UAAU,EAAE,kCAAkC;QAC9C,KAAK,EAAE;YACN,OAAO,EAAE,aAAa;YACtB,OAAO,EAAE,2CAA2C;SACpD;KACD,CAAC;CACF,CAAC,CAAC;AAEU,QAAA,wBAAwB,GAAG,OAAC,CAAC,MAAM,CAAC;IAChD,GAAG,EAAE,iCAAa,CAAC,sBAAsB;IACzC,MAAM,EAAE,iCAAa,CAAC,QAAQ,CAAC,aAAM,EAAE,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,aAAM,CAAC,MAAM,CAAC;CAClF,CAAC,CAAC;AAEU,QAAA,sCAAsC,GAAG,OAAC,CAAC,MAAM,CAAC;IAC9D,MAAM,EAAE,iCAAa,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC;CACtD,CAAC,CAAC;AAEU,QAAA,8BAA8B,GAAG,IAAA,wCAAoB,EAAC,oCAA4B,CAAC,CAAC","sourcesContent":["import { z } from 'zod';\nimport {\n\tZodValidation,\n\tcreateSchemaBuilder,\n\tDTO,\n} from '@lyxa.ai/core/dist/utilities/validation/global-validation';\nimport { CouponExpirationReason, CouponType, Status, ValueType } from '@lyxa.ai/core/dist/utilities/enum';\nimport {\n\tcreateResponseSchema,\n\tDurationSchema,\n} from '@lyxa.ai/core/dist/utilities/validation/common-validation';\nimport { dayjs } from '@lyxa.ai/core/dist/utilities/dayjs';\n\n/**\n * Define the base parent schema fields\n */\nexport const couponFields = {\n\tcode: ZodValidation.string('Code', {\n\t\tregex: { pattern: new RegExp('^[A-Z0-9]+$'), message: 'Coupon name must be uppercase' },\n\t\tmaxLength: 6,\n\t\tminLength: 6,\n\t\tminMessage: 'Coupon must be 6 characters',\n\t\tmaxMessage: 'Coupon must be 6 characters',\n\t}),\n\torderLimitPerUser: ZodValidation.number('OrderLimitPerUser')\n\t\t.min(1)\n\t\t.max(5)\n\t\t.optional()\n\t\t.nullable()\n\t\t.default(null),\n\tisOrderLimitPerUserEnabled: ZodValidation.boolean('Is order limit per user enabled')\n\t\t.optional()\n\t\t.default(false),\n\tcreatedBy: ZodValidation.objectId('Created by').optional(),\n\tisExpired: ZodValidation.boolean('Is expired').optional(),\n};\n\nexport const baseFields = {\n\t...couponFields,\n\tvalueType: ZodValidation.enumType(ValueType, 'Value type'),\n\tvalue: ZodValidation.number('Value'),\n\tsecondaryCurrencyValue: ZodValidation.number('Secondary currency value').optional(),\n\tmaxDiscountLimit: ZodValidation.number('Max discount limit').optional().nullable().default(null),\n\tsecondaryCurrencyMaxDiscountLimit: ZodValidation.number('Secondary currency max discount limit').optional(),\n\tisMaxDiscountLimitEnabled: ZodValidation.boolean('Is max discount limit enabled').optional().default(false),\n\tduration: DurationSchema.transform(duration => {\n\t\t// duration.start = dayjs(duration.start).startOf('day').toDate();\n\t\t// duration.end = dayjs(duration.end).endOf('day').toDate();\n\t\tduration.start = dayjs(duration.start).toDate();\n\t\tduration.end = dayjs(duration.end).toDate();\n\t\treturn duration;\n\t}),\n\tspendLimit: ZodValidation.number('Spend limit').optional().nullable().default(null),\n\tisSpendLimitEnabled: ZodValidation.boolean('Is spend limit enabled').optional().default(false),\n\ttotalOrderLimit: ZodValidation.number('Total order limit').optional().nullable().default(null),\n\tisTotalOrderLimitEnabled: ZodValidation.boolean('Is total order limit enabled').optional().default(false),\n\tminOrderValue: ZodValidation.number('Min order value').nullable().default(null),\n\tsecondaryCurrencyMinOrderValue: ZodValidation.number('Secondary currency min order value').optional(),\n\tisMinOrderValueEnabled: ZodValidation.boolean('Is min order value enabled').optional().default(false),\n\tforNewUserOnly: ZodValidation.boolean('ForNewUserOnly').optional().default(false),\n\tstatus: ZodValidation.enumType(Status, 'Status').optional().default(Status.ACTIVE),\n\texpirationReason: ZodValidation.enumType(CouponExpirationReason, 'Expiration reason')\n\t\t.optional()\n\t\t.nullable()\n\t\t.default(null),\n\treferringUser: z\n\t\t.object({\n\t\t\tname: ZodValidation.string('Referring user name').optional(),\n\t\t})\n\t\t.optional(),\n};\n\n/**\n * Create schema builder for Parent entity\n * - Includes timestamps\n * - Excludes 'createdAt' from update operations\n */\nconst schemaBuilder = createSchemaBuilder(baseFields);\n\n/**\n * Get all schemas for the Parent entity\n */\nconst {\n\tBaseSchema: CouponValidationSchema,\n\tEntitySchema: CouponSchema,\n\tIdSchema: CouponIdSchema,\n\tUpdateSchema: UpdateCouponSchema,\n} = schemaBuilder.getAllSchemas({ createdAt: true });\n\n/**\n * Export schemas\n */\nexport const GetUserCouponsResponseSchema = z.object({\n\tavailable: z.array(CouponSchema),\n\tused: z.array(CouponSchema),\n\texpired: z.array(CouponSchema),\n});\n\nexport {\n\tCouponValidationSchema,\n\tCouponSchema,\n\tCouponIdSchema as CouponIdValidationSchema,\n\tUpdateCouponSchema,\n};\n\n/**\n * Schema for getting user coupons\n */\nexport const GetUserCouponSchema = z.object({\n\tuser: ZodValidation.objectId('User').optional(),\n\tshop: ZodValidation.objectId('Shop').optional(),\n\tpage: ZodValidation.number('Page').optional().default(0),\n\tsize: ZodValidation.number('Page size').optional().default(100),\n});\n\n/**\n * Schema for validating coupons\n */\nexport const ValidateCouponSchema = z.object({\n\tuser: ZodValidation.objectIdOrStringSchema,\n\tshop: ZodValidation.objectIdOrStringSchema,\n\tcoupon: ZodValidation.string('Coupon').optional(),\n\tamountBeforeCoupon: ZodValidation.number('Amount before coupon'),\n\tsecondaryAmountBeforeCoupon: ZodValidation.number('Secondary amount before coupon'),\n});\n\nconst discountCutSchema = z.object({\n\tcompanyCut: ZodValidation.number('companyCut').optional(),\n\tsecondaryCompanyCut: ZodValidation.number('secondaryCompanyCut').optional(),\n\tshopCut: ZodValidation.number('shopCut').optional(),\n\tsecondaryShopCut: ZodValidation.number('secondaryShopCut').optional(),\n});\n\n/**\n * Schema for coupon validation output\n */\nexport const CouponValidationOutputSchema = z.object({\n\tisValid: ZodValidation.boolean('IsValid').default(false),\n\tmessage: ZodValidation.string(\"Message\").optional(),\n\tamountAfterCoupon: ZodValidation.number('Amount after coupon').optional(),\n\tsecondaryAmountAfterCoupon: ZodValidation.number('Secondary amount after coupon').optional(),\n\tdiscountAmount: ZodValidation.number('Coupon Discount').optional(),\n\tsecondaryDiscountAmount: ZodValidation.number('Secondary coupon discount').optional(),\n\tdiscountCut: discountCutSchema.optional(),\n});\n\nexport const CouponCodeSchema = z.object({\n\tcouponCode: ZodValidation.string('Coupon code', {\n\t\tminLength: 6,\n\t\tmaxLength: 6,\n\t\tminMessage: 'Coupon code must be 6 characters',\n\t\tmaxMessage: 'Coupon code must be 6 characters',\n\t\tregex: {\n\t\t\tpattern: /^[A-Z0-9]+$/,\n\t\t\tmessage: 'Coupon must be uppercase and alphanumeric',\n\t\t},\n\t}),\n});\n\nexport const UpdateCouponStatusSchema = z.object({\n\t_id: ZodValidation.objectIdOrStringSchema,\n\tstatus: ZodValidation.enumType(Status, 'Status').optional().default(Status.ACTIVE),\n});\n\nexport const CheckExistingNewUserCouponOutputSchema = z.object({\n\texists: ZodValidation.boolean('Exists').default(false),\n});\n\nexport const CouponValidationResponseSchema = createResponseSchema(CouponValidationOutputSchema);\n\n/**\n * Export DTO types\n */\nexport type CreateCouponDTO = DTO<typeof CouponValidationSchema>;\nexport type CouponDTO = DTO<typeof CouponSchema>;\nexport type UpdateCouponDTO = DTO<typeof UpdateCouponSchema>;\nexport type GetUserCouponDTO = DTO<typeof GetUserCouponSchema>;\nexport type ValidateCouponDTO = DTO<typeof ValidateCouponSchema>;\nexport type CouponValidationOutputDTO = DTO<typeof CouponValidationOutputSchema>;\nexport type GetUserCouponsResponseDTO = DTO<typeof GetUserCouponsResponseSchema>;\nexport type UpdateCouponStatusDTO = DTO<typeof UpdateCouponStatusSchema>;\nexport type CheckExistingNewUserCouponOutputDTO = DTO<typeof CheckExistingNewUserCouponOutputSchema>;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"marketing.router.d.ts","sourceRoot":"/","sources":["modules/marketing/routers/marketing.router.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"marketing.router.d.ts","sourceRoot":"/","sources":["modules/marketing/routers/marketing.router.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4BAeZ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2Cf,CAAC"}
|
|
@@ -19,15 +19,11 @@ exports.marketingRouter = (0, trpc_1.createTRPCRouter)({
|
|
|
19
19
|
await services_1.marketingService.create(input, isAdmin ? new typegoose_1.mongoose.Types.ObjectId(ctx.entity?.id) : undefined);
|
|
20
20
|
return (0, response_1.response)('Marketing created successfully');
|
|
21
21
|
})),
|
|
22
|
-
getById: trpc_1.publicProcedure
|
|
23
|
-
.input(validation_1.GetByIdInputSchema)
|
|
24
|
-
.query(async ({ input }) => error_handler_1.ErrorHandler.wrapAsync(async () => {
|
|
22
|
+
getById: trpc_1.publicProcedure.input(validation_1.GetByIdInputSchema).query(async ({ input }) => error_handler_1.ErrorHandler.wrapAsync(async () => {
|
|
25
23
|
const result = await services_1.marketingService.findById(input);
|
|
26
24
|
return (0, response_1.response)('Marketing fetched successfully', result);
|
|
27
25
|
})),
|
|
28
|
-
get: trpc_1.publicProcedure
|
|
29
|
-
.input(validation_1.FilterSchema)
|
|
30
|
-
.query(async ({ input }) => error_handler_1.ErrorHandler.wrapAsync(async () => {
|
|
26
|
+
get: trpc_1.publicProcedure.input(validation_1.FilterSchema).query(async ({ input }) => error_handler_1.ErrorHandler.wrapAsync(async () => {
|
|
31
27
|
const result = await services_1.marketingService.find(input);
|
|
32
28
|
return (0, response_1.response)('Marketing fetched successfully', result);
|
|
33
29
|
})),
|
|
@@ -40,11 +36,12 @@ exports.marketingRouter = (0, trpc_1.createTRPCRouter)({
|
|
|
40
36
|
await services_1.marketingService.update(_id, rest, isAdmin ? new typegoose_1.mongoose.Types.ObjectId(ctx.entity?.id) : undefined);
|
|
41
37
|
return (0, response_1.response)('Marketing updated successfully');
|
|
42
38
|
})),
|
|
43
|
-
deleteById: (0, auth_1.createAuthenticatedProcedure)({ entityTypes: [auth_2.AuthEntityType.CRM] })
|
|
39
|
+
deleteById: (0, auth_1.createAuthenticatedProcedure)({ entityTypes: [auth_2.AuthEntityType.CRM, auth_2.AuthEntityType.SELLER] })
|
|
44
40
|
.input(validation_1.DeleteSchema)
|
|
45
41
|
.output(validations_1.MarketingResponseSchema)
|
|
46
42
|
.mutation(async ({ ctx, input }) => error_handler_1.ErrorHandler.wrapAsync(async () => {
|
|
47
|
-
|
|
43
|
+
const isAdmin = ctx.entity?.type == auth_2.AuthEntityType.CRM;
|
|
44
|
+
await services_1.marketingService.deleteById(input, isAdmin ? new typegoose_1.mongoose.Types.ObjectId(ctx.entity?.id) : undefined);
|
|
48
45
|
return (0, response_1.response)('Marketing deleted successfully');
|
|
49
46
|
})),
|
|
50
47
|
handleNewProductForEntireMenuMarketing: trpc_1.publicProcedure
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"marketing.router.js","sourceRoot":"/","sources":["modules/marketing/routers/marketing.router.ts"],"names":[],"mappings":";;;AAAA,4DAAsF;AACtF,6EAAkG;AAClG,0DAA0E;AAC1E,gEAKwC;AACxC,oEAAiE;AACjE,wEAAyG;AACzG,8EAA0E;AAC1E,4DAAmE;AACnE,oDAAgD;AAEnC,QAAA,eAAe,GAAG,IAAA,uBAAgB,EAAC;IAC/C,MAAM,EAAE,IAAA,mCAA4B,EAAC,EAAE,WAAW,EAAE,CAA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9C,QAAQ,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,CAC7B,4BAAY,CAAC,SAAS,CAAC,KAAK,IAAI,EAAE;QACjC,MAAM,2BAAO,CAAC,sCAAsC,CAAC,KAAK,CAAC,CAAC;QAC5D,OAAO,IAAA,mBAAQ,EAAC,8BAA8B,CAAC,CAAC;IACjD,CAAC,CAAC,CACF;CACF,CAAC,CAAC","sourcesContent":["import { createTRPCRouter, publicProcedure } from '@lyxa.ai/core/dist/libraries/trpc';\nimport { createAuthenticatedProcedure } from '@lyxa.ai/core/dist/libraries/trpc/middlewares/auth';\nimport { marketingService as service } from '@modules/marketing/services';\nimport {\n\tAllMarketingsValidationSchema,\n\tMarketingResponseSchema as ResponseSchema,\n\tAllMarketingsUpdateSchema,\n\tProductEntireMenuMarketingHandlerSchema,\n} from '@modules/marketing/validations';\nimport { response } from '@lyxa.ai/core/dist/utilities/response';\nimport { FilterSchema, DeleteSchema, GetByIdInputSchema } from '@lyxa.ai/core/dist/utilities/validation';\nimport { ErrorHandler } from '@lyxa.ai/core/dist/utilities/error-handler';\nimport { AuthEntityType } from '@lyxa.ai/core/dist/libraries/auth';\nimport { mongoose } from '@typegoose/typegoose';\n\nexport const marketingRouter = createTRPCRouter({\n\tcreate: createAuthenticatedProcedure({ entityTypes: [AuthEntityType.CRM, AuthEntityType.SELLER] })\n\t\t.input(AllMarketingsValidationSchema)\n\t\t.output(ResponseSchema)\n\t\t.mutation(async ({ ctx, input }) =>\n\t\t\tErrorHandler.wrapAsync(async () => {\n\t\t\t\tconst isAdmin = ctx.entity?.type == AuthEntityType.CRM;\n\t\t\t\tawait service.create(input, isAdmin ? new mongoose.Types.ObjectId(ctx.entity?.id) : undefined);\n\t\t\t\treturn response('Marketing created successfully');\n\t\t\t})\n\t\t),\n\n\tgetById: publicProcedure.input(GetByIdInputSchema).query(async ({ input }) =>\n\t\tErrorHandler.wrapAsync(async () => {\n\t\t\tconst result = await service.findById(input);\n\t\t\treturn response('Marketing fetched successfully', result);\n\t\t})\n\t),\n\n\tget: publicProcedure.input(FilterSchema).query(async ({ input }) =>\n\t\tErrorHandler.wrapAsync(async () => {\n\t\t\tconst result = await service.find(input);\n\t\t\treturn response('Marketing fetched successfully', result);\n\t\t})\n\t),\n\n\tupdate: createAuthenticatedProcedure({ entityTypes: [AuthEntityType.CRM, AuthEntityType.SELLER] })\n\t\t.input(AllMarketingsUpdateSchema)\n\t\t.output(ResponseSchema)\n\t\t.mutation(async ({ ctx, input }) =>\n\t\t\tErrorHandler.wrapAsync(async () => {\n\t\t\t\tconst { _id, ...rest } = input;\n\t\t\t\tconst isAdmin = ctx.entity?.type == AuthEntityType.CRM;\n\n\t\t\t\tawait service.update(_id, rest, isAdmin ? new mongoose.Types.ObjectId(ctx.entity?.id) : undefined);\n\t\t\t\treturn response('Marketing updated successfully');\n\t\t\t})\n\t\t),\n\n\tdeleteById: createAuthenticatedProcedure({ entityTypes: [AuthEntityType.CRM, AuthEntityType.SELLER] })\n\t\t.input(DeleteSchema)\n\t\t.output(ResponseSchema)\n\t\t.mutation(async ({ ctx, input }) =>\n\t\t\tErrorHandler.wrapAsync(async () => {\n\t\t\t\tconst isAdmin = ctx.entity?.type == AuthEntityType.CRM;\n\t\t\t\tawait service.deleteById(input, isAdmin ? new mongoose.Types.ObjectId(ctx.entity?.id) : undefined);\n\t\t\t\treturn response('Marketing deleted successfully');\n\t\t\t})\n\t\t),\n\n\thandleNewProductForEntireMenuMarketing: publicProcedure\n\t\t.input(ProductEntireMenuMarketingHandlerSchema)\n\t\t.mutation(async ({ input }) =>\n\t\t\tErrorHandler.wrapAsync(async () => {\n\t\t\t\tawait service.handleNewProductForEntireMenuMarketing(input);\n\t\t\t\treturn response('Product handled successfully');\n\t\t\t})\n\t\t),\n});\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buy1-get1-marketing.service.d.ts","sourceRoot":"/","sources":["modules/marketing/services/buy1-get1-marketing.service.ts"],"names":[],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AAC,QAAQ,EACpC,SAAS,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,GAChC,OAAO,CAAC,IAAI,CAAC;CAOhB;AAED,eAAO,MAAM,wBAAwB,0BAA0C,CAAC"}
|
|
@@ -131,14 +131,18 @@ let Buy1Get1MarketingService = class Buy1Get1MarketingService {
|
|
|
131
131
|
data.isEntireMenu) {
|
|
132
132
|
await _1.productMarketingService.deleteMany({ marketing: _id }, true);
|
|
133
133
|
}
|
|
134
|
-
if (data.products &&
|
|
134
|
+
if (data.products &&
|
|
135
|
+
data.products.length > 0 &&
|
|
136
|
+
(!data.categories || data.categories.length === 0) &&
|
|
137
|
+
!data.isEntireMenu) {
|
|
135
138
|
for (const product of data.products) {
|
|
136
139
|
this.productMarketingsCreate(_id, product.product);
|
|
137
140
|
}
|
|
138
141
|
}
|
|
139
142
|
else if (data.categories &&
|
|
140
143
|
data.categories.length > 0 &&
|
|
141
|
-
(!data.products || data.products.length === 0)
|
|
144
|
+
(!data.products || data.products.length === 0) &&
|
|
145
|
+
!data.isEntireMenu) {
|
|
142
146
|
const categoryWiseProducts = await this.productModel
|
|
143
147
|
.find({
|
|
144
148
|
shopCategory: { $in: data.categories },
|
|
@@ -148,7 +152,11 @@ let Buy1Get1MarketingService = class Buy1Get1MarketingService {
|
|
|
148
152
|
this.productMarketingsCreate(_id, product._id);
|
|
149
153
|
}
|
|
150
154
|
}
|
|
151
|
-
else if (data.categories &&
|
|
155
|
+
else if (data.categories &&
|
|
156
|
+
data.categories.length > 0 &&
|
|
157
|
+
data.products &&
|
|
158
|
+
data.products.length > 0 &&
|
|
159
|
+
!data.isEntireMenu) {
|
|
152
160
|
const categoryWiseProducts = await this.productModel
|
|
153
161
|
.find({
|
|
154
162
|
shopCategory: { $in: data.categories },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buy1-get1-marketing.service.js","sourceRoot":"/","sources":["modules/marketing/services/buy1-get1-marketing.service.ts"],"names":[],"mappings":";;;;;;;;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IAAI;SAChB,CAAC,CAAC;IACJ,CAAC;CACD,CAAA;AAxLY,4DAAwB;mCAAxB,wBAAwB;IADpC,IAAA,gBAAO,GAAE;;GACG,wBAAwB,CAwLpC;AAEY,QAAA,wBAAwB,GAAG,gBAAS,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType, mongoose } from '@typegoose/typegoose';\nimport {\n\tBuy1Get1MarketingModel,\n\tBuy1Get1Marketing as Model,\n\tProductModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models/';\nimport {\n\tCreateBuy1Get1MarketingDTO as CreateDTO,\n\tUpdateBuy1Get1MarketingDTO as UpdateDTO,\n} from '@modules/marketing/validations';\nimport { productMarketingService } from './';\nimport { notFoundError } from '@lyxa.ai/core/dist/utilities/error-common';\n\n@Service()\nexport class Buy1Get1MarketingService {\n\tprivate model: typeof Buy1Get1MarketingModel;\n\tprivate productModel: typeof ProductModel;\n\n\tconstructor() {\n\t\tthis.model = Buy1Get1MarketingModel;\n\t\tthis.productModel = ProductModel;\n\t}\n\n\t/**\n\t * Create a new record\n\t * @param data Data for the new record\n\t */\n\tpublic async create(data: CreateDTO): Promise<DocumentType<Model>> {\n\t\tconst result = await this.model.create(data);\n\n\t\t// Only products\n\t\tif (\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t(!data.categories || data.categories.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\tfor (const product of data.products) {\n\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product.product))) {\n\t\t\t\t\tthis.productMarketingsCreate(result._id, product.product);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\t(!data.products || data.products.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Only categories\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\tthis.productMarketingsCreate(result._id, product._id);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Both products and categories\n\t\t\t// For category\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\tthis.productMarketingsCreate(result._id, product._id);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// For products\n\t\t\tfor (const product of data.products) {\n\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product.product))) {\n\t\t\t\t\tthis.productMarketingsCreate(result._id, product.product);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (data.isEntireMenu) {\n\t\t\t// Entire menu\n\t\t\tthis.productModel\n\t\t\t\t.find({ shop: data.shop })\n\t\t\t\t.select('_id')\n\t\t\t\t.exec()\n\t\t\t\t.then(productDocuments => {\n\t\t\t\t\tfor (const product of productDocuments) {\n\t\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\t\t\tthis.productMarketingsCreate(result._id, product._id);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Update record\n\t * @param _id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(_id: mongoose.Types.ObjectId, data: UpdateDTO): Promise<DocumentType<Model>> {\n\t\tconst { products, ...rest } = data;\n\t\tconst record = await this.model.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id: _id,\n\t\t\t\tdeletedAt: null,\n\t\t\t},\n\t\t\trest,\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\t// Remove all products from product-marketings collection\n\t\tif (\n\t\t\t(data.products && data.products.length > 0) ||\n\t\t\t(data.categories && data.categories.length > 0) ||\n\t\t\tdata.isEntireMenu\n\t\t) {\n\t\t\tawait productMarketingService.deleteMany({ marketing: _id }, true);\n\t\t}\n\n\t\t// Only products\n\t\tif (data.products && data.products.length > 0 && (!data.categories || data.categories.length === 0)) {\n\t\t\tfor (const product of data.products) {\n\t\t\t\tthis.productMarketingsCreate(_id, product.product);\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\t(!data.products || data.products.length === 0)\n\t\t) {\n\t\t\t// Only categories\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tthis.productMarketingsCreate(_id, product._id);\n\t\t\t}\n\t\t} else if (data.categories && data.categories.length > 0 && data.products && data.products.length > 0) {\n\t\t\t// Both products and categories\n\t\t\t// For category\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tthis.productMarketingsCreate(_id, product._id);\n\t\t\t}\n\n\t\t\t// For products\n\t\t\tfor (const product of data.products) {\n\t\t\t\tthis.productMarketingsCreate(_id, product.product);\n\t\t\t}\n\t\t} else if (data.isEntireMenu) {\n\t\t\t// Entire menu\n\t\t\tthis.productModel\n\t\t\t\t.find({ shop: data.shop })\n\t\t\t\t.select('_id')\n\t\t\t\t.exec()\n\t\t\t\t.then(productDocuments => {\n\t\t\t\t\tfor (const product of productDocuments) {\n\t\t\t\t\t\tthis.productMarketingsCreate(_id, product._id);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t}\n\n\t\tif (!record) notFoundError('Record not found');\n\n\t\treturn record;\n\t}\n\n\tpublic async productMarketingsCreate(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tproductId: mongoose.Types.ObjectId\n\t): Promise<void> {\n\t\tproductMarketingService.create({\n\t\t\tmarketing: marketingId,\n\t\t\tproduct: productId,\n\t\t\tisBuy1Get1: true,\n\t\t});\n\t}\n}\n\nexport const buy1Get1MarketingService = Container.get(Buy1Get1MarketingService);\n"]}
|
|
1
|
+
{"version":3,"file":"buy1-get1-marketing.service.js","sourceRoot":"/","sources":["modules/marketing/services/buy1-get1-marketing.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAG5C,uEAIoD;AAKpD,yBAA6C;AAC7C,4EAA0E;AAGnE,IAAM,wBAAwB,GAA9B,MAAM,wBAAwB;IAC5B,KAAK,CAAgC;IACrC,YAAY,CAAsB;IAE1C;QACC,IAAI,CAAC,KAAK,GAAG,+BAAsB,CAAC;QACpC,IAAI,CAAC,YAAY,GAAG,qBAAY,CAAC;IAClC,CAAC;IAMM,KAAK,CAAC,MAAM,CAAC,IAAe;QAClC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAG7C,IACC,IAAI,CAAC,QAAQ;YACb,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC;YACxB,CAAC,CAAC,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,KAAK,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AI,CAAC,uBAAuB,CAAC,GAAG,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;YACpD,CAAC;QACF,CAAC;aAAM,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YAE9B,IAAI,CAAC,YAAY;iBACf,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC;iBACzB,MAAM,CAAC,KAAK,CAAC;iBACb,IAAI,EAAE;iBACN,IAAI,CAAC,gBAAgB,CAAC,EAAE;gBACxB,KAAK,MAAM,OAAO,IAAI,gBAAgB,EAAE,CAAC;oBACxC,IAAI,CAAC,uBAAuB,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;gBAChD,CAAC;YACF,CAAC,CAAC,CAAC;QACL,CAAC;QAED,IAAI,CAAC,MAAM;YAAE,IAAA,4BAAa,EAAC,kBAAkB,CAAC,CAAC;QAE/C,OAAO,MAAM,CAAC;IACf,CAAC;IAEM,KAAK,CAAC,uBAAuB,CACnC,WAAoC,EACpC,SAAkC;QAElC,0BAAuB,CAAC,MAAM,CAAC;YAC9B,SAAS,EAAE,WAAW;YACtB,OAAO,EAAE,SAAS;YAClB,UAAU,EAAE,IAAI;SAChB,CAAC,CAAC;IACJ,CAAC;CACD,CAAA;AApMY,4DAAwB;mCAAxB,wBAAwB;IADpC,IAAA,gBAAO,GAAE;;GACG,wBAAwB,CAoMpC;AAEY,QAAA,wBAAwB,GAAG,gBAAS,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType, mongoose } from '@typegoose/typegoose';\nimport {\n\tBuy1Get1MarketingModel,\n\tBuy1Get1Marketing as Model,\n\tProductModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models/';\nimport {\n\tCreateBuy1Get1MarketingDTO as CreateDTO,\n\tUpdateBuy1Get1MarketingDTO as UpdateDTO,\n} from '@modules/marketing/validations';\nimport { productMarketingService } from './';\nimport { notFoundError } from '@lyxa.ai/core/dist/utilities/error-common';\n\n@Service()\nexport class Buy1Get1MarketingService {\n\tprivate model: typeof Buy1Get1MarketingModel;\n\tprivate productModel: typeof ProductModel;\n\n\tconstructor() {\n\t\tthis.model = Buy1Get1MarketingModel;\n\t\tthis.productModel = ProductModel;\n\t}\n\n\t/**\n\t * Create a new record\n\t * @param data Data for the new record\n\t */\n\tpublic async create(data: CreateDTO): Promise<DocumentType<Model>> {\n\t\tconst result = await this.model.create(data);\n\n\t\t// Only products\n\t\tif (\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t(!data.categories || data.categories.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\tfor (const product of data.products) {\n\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product.product))) {\n\t\t\t\t\tthis.productMarketingsCreate(result._id, product.product);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\t(!data.products || data.products.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Only categories\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\tthis.productMarketingsCreate(result._id, product._id);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Both products and categories\n\t\t\t// For category\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\tthis.productMarketingsCreate(result._id, product._id);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// For products\n\t\t\tfor (const product of data.products) {\n\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product.product))) {\n\t\t\t\t\tthis.productMarketingsCreate(result._id, product.product);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (data.isEntireMenu) {\n\t\t\t// Entire menu\n\t\t\tthis.productModel\n\t\t\t\t.find({ shop: data.shop })\n\t\t\t\t.select('_id')\n\t\t\t\t.exec()\n\t\t\t\t.then(productDocuments => {\n\t\t\t\t\tfor (const product of productDocuments) {\n\t\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\t\t\tthis.productMarketingsCreate(result._id, product._id);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Update record\n\t * @param _id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(_id: mongoose.Types.ObjectId, data: UpdateDTO): Promise<DocumentType<Model>> {\n\t\tconst { products, ...rest } = data;\n\t\tconst record = await this.model.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id: _id,\n\t\t\t\tdeletedAt: null,\n\t\t\t},\n\t\t\trest,\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\t// Remove all products from product-marketings collection\n\t\tif (\n\t\t\t(data.products && data.products.length > 0) ||\n\t\t\t(data.categories && data.categories.length > 0) ||\n\t\t\tdata.isEntireMenu\n\t\t) {\n\t\t\tawait productMarketingService.deleteMany({ marketing: _id }, true);\n\t\t}\n\n\t\t// Only products\n\t\tif (\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t(!data.categories || data.categories.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\tfor (const product of data.products) {\n\t\t\t\tthis.productMarketingsCreate(_id, product.product);\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\t(!data.products || data.products.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Only categories\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tthis.productMarketingsCreate(_id, product._id);\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Both products and categories\n\t\t\t// For category\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tthis.productMarketingsCreate(_id, product._id);\n\t\t\t}\n\n\t\t\t// For products\n\t\t\tfor (const product of data.products) {\n\t\t\t\tthis.productMarketingsCreate(_id, product.product);\n\t\t\t}\n\t\t} else if (data.isEntireMenu) {\n\t\t\t// Entire menu\n\t\t\tthis.productModel\n\t\t\t\t.find({ shop: data.shop })\n\t\t\t\t.select('_id')\n\t\t\t\t.exec()\n\t\t\t\t.then(productDocuments => {\n\t\t\t\t\tfor (const product of productDocuments) {\n\t\t\t\t\t\tthis.productMarketingsCreate(_id, product._id);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t}\n\n\t\tif (!record) notFoundError('Record not found');\n\n\t\treturn record;\n\t}\n\n\tpublic async productMarketingsCreate(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tproductId: mongoose.Types.ObjectId\n\t): Promise<void> {\n\t\tproductMarketingService.create({\n\t\t\tmarketing: marketingId,\n\t\t\tproduct: productId,\n\t\t\tisBuy1Get1: true,\n\t\t});\n\t}\n}\n\nexport const buy1Get1MarketingService = Container.get(Buy1Get1MarketingService);\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discount-marketing.service.d.ts","sourceRoot":"/","sources":["modules/marketing/services/discount-marketing.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAEN,iBAAiB,IAAI,KAAK,EAE1B,MAAM,2CAA2C,CAAC;AACnD,OAAO,EACN,0BAA0B,IAAI,SAAS,EACvC,0BAA0B,IAAI,SAAS,EACvC,MAAM,gCAAgC,CAAC;AAExC,OAAO,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AAG9D,qBACa,wBAAwB;IACpC,OAAO,CAAC,KAAK,CAAgC;IAC7C,OAAO,CAAC,YAAY,CAAsB;;IAW7B,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAmIrD,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"discount-marketing.service.d.ts","sourceRoot":"/","sources":["modules/marketing/services/discount-marketing.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAEN,iBAAiB,IAAI,KAAK,EAE1B,MAAM,2CAA2C,CAAC;AACnD,OAAO,EACN,0BAA0B,IAAI,SAAS,EACvC,0BAA0B,IAAI,SAAS,EACvC,MAAM,gCAAgC,CAAC;AAExC,OAAO,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AAG9D,qBACa,wBAAwB;IACpC,OAAO,CAAC,KAAK,CAAgC;IAC7C,OAAO,CAAC,YAAY,CAAsB;;IAW7B,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAmIrD,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAiInF,uBAAuB,CACnC,WAAW,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,EACpC,SAAS,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAClC,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,SAAS,GAClB,OAAO,CAAC,IAAI,CAAC;CAQhB;AAED,eAAO,MAAM,wBAAwB,0BAA0C,CAAC"}
|
|
@@ -161,7 +161,10 @@ let DiscountMarketingService = class DiscountMarketingService {
|
|
|
161
161
|
data.isEntireMenu) {
|
|
162
162
|
await _1.productMarketingService.deleteMany({ marketing: _id }, true);
|
|
163
163
|
}
|
|
164
|
-
if (data.products &&
|
|
164
|
+
if (data.products &&
|
|
165
|
+
data.products.length > 0 &&
|
|
166
|
+
(!data.categories || data.categories.length === 0) &&
|
|
167
|
+
!data.isEntireMenu) {
|
|
165
168
|
for (const product of data.products) {
|
|
166
169
|
if (product.valueType !== undefined && product.valueType !== null) {
|
|
167
170
|
await this.productMarketingsCreate(_id, product.product, Number(product.value), product.valueType);
|
|
@@ -174,7 +177,8 @@ let DiscountMarketingService = class DiscountMarketingService {
|
|
|
174
177
|
}
|
|
175
178
|
else if (data.categories &&
|
|
176
179
|
data.categories.length > 0 &&
|
|
177
|
-
(!data.products || data.products.length === 0)
|
|
180
|
+
(!data.products || data.products.length === 0) &&
|
|
181
|
+
!data.isEntireMenu) {
|
|
178
182
|
const categoryWiseProducts = await this.productModel
|
|
179
183
|
.find({
|
|
180
184
|
shopCategory: { $in: data.categories },
|
|
@@ -189,7 +193,11 @@ let DiscountMarketingService = class DiscountMarketingService {
|
|
|
189
193
|
}
|
|
190
194
|
}
|
|
191
195
|
}
|
|
192
|
-
else if (data.categories &&
|
|
196
|
+
else if (data.categories &&
|
|
197
|
+
data.categories.length > 0 &&
|
|
198
|
+
data.products &&
|
|
199
|
+
data.products.length > 0 &&
|
|
200
|
+
!data.isEntireMenu) {
|
|
193
201
|
const categoryWiseProducts = await this.productModel
|
|
194
202
|
.find({
|
|
195
203
|
shopCategory: { $in: data.categories },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discount-marketing.service.js","sourceRoot":"/","sources":["modules/marketing/services/discount-marketing.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAE5C,sEAImD;AAKnD,yBAA6C;AAE7C,4EAA2F;AAGpF,IAAM,wBAAwB,GAA9B,MAAM,wBAAwB;IAC5B,KAAK,CAAgC;IACrC,YAAY,CAAsB;IAE1C;QACC,IAAI,CAAC,KAAK,GAAG,+BAAsB,CAAC;QACpC,IAAI,CAAC,YAAY,GAAG,qBAAY,CAAC;IAClC,CAAC;IAMM,KAAK,CAAC,MAAM,CAAC,IAAe;QAClC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAG7C,IACC,IAAI,CAAC,QAAQ;YACb,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC;YACxB,CAAC,CAAC,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC,CAAC;YAClD,CAAC,IAAI,CAAC,YAAY,EACjB,CAAC;YACF,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACrC,IAAI,OAAO,CAA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{ Service } from 'typedi';\nimport { DocumentType, mongoose } from '@typegoose/typegoose';\nimport {\n\tDiscountMarketingModel,\n\tDiscountMarketing as Model,\n\tProductModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport {\n\tCreateDiscountMarketingDTO as CreateDTO,\n\tUpdateDiscountMarketingDTO as UpdateDTO,\n} from '@modules/marketing/validations';\nimport { productMarketingService } from './';\nimport { ValueType } from '@lyxa.ai/core/dist/utilities/enum';\nimport { badRequestError, notFoundError } from '@lyxa.ai/core/dist/utilities/error-common';\n\n@Service()\nexport class DiscountMarketingService {\n\tprivate model: typeof DiscountMarketingModel;\n\tprivate productModel: typeof ProductModel;\n\n\tconstructor() {\n\t\tthis.model = DiscountMarketingModel;\n\t\tthis.productModel = ProductModel;\n\t}\n\n\t/**\n\t * Create a new record\n\t * @param data Data for the new record\n\t */\n\tpublic async create(data: CreateDTO): Promise<DocumentType<Model>> {\n\t\tconst result = await this.model.create(data);\n\n\t\t// Only products\n\t\tif (\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t(!data.categories || data.categories.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\tfor (const product of data.products) {\n\t\t\t\tif (product.valueType !== undefined && product.valueType !== null) {\n\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product.product))) {\n\t\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t\tresult._id,\n\t\t\t\t\t\t\tproduct.product,\n\t\t\t\t\t\t\tNumber(product.value),\n\t\t\t\t\t\t\tproduct.valueType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Product discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\t(!data.products || data.products.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Only categories\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (data.categoryValueType !== undefined && data.categoryValueType !== null) {\n\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t\tresult._id,\n\t\t\t\t\t\t\tproduct._id,\n\t\t\t\t\t\t\tNumber(data.categoryValue),\n\t\t\t\t\t\t\tdata.categoryValueType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Category discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Both products and categories\n\t\t\t// For category\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (data.categoryValueType !== undefined && data.categoryValueType !== null) {\n\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t\tresult._id,\n\t\t\t\t\t\t\tproduct._id,\n\t\t\t\t\t\t\tNumber(data.categoryValue),\n\t\t\t\t\t\t\tdata.categoryValueType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Category discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// For products\n\t\t\tfor (const product of data.products) {\n\t\t\t\t// Ignore the product which is already added by category\n\t\t\t\tconst isProductAddedByCategory = categoryWiseProducts.find(\n\t\t\t\t\tp => p._id.toString() === product.product.toString()\n\t\t\t\t);\n\t\t\t\tif (isProductAddedByCategory) continue;\n\n\t\t\t\tif (product.valueType !== undefined && product.valueType !== null) {\n\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product.product))) {\n\t\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t\tresult._id,\n\t\t\t\t\t\t\tproduct.product,\n\t\t\t\t\t\t\tNumber(product.value),\n\t\t\t\t\t\t\tproduct.valueType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Product discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (data.isEntireMenu) {\n\t\t\t// Entire menu\n\t\t\tthis.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshop: data.shop,\n\t\t\t\t})\n\t\t\t\t.select('_id')\n\t\t\t\t.exec()\n\t\t\t\t.then(productDocuments => {\n\t\t\t\t\tfor (const product of productDocuments) {\n\t\t\t\t\t\tif (data.valueType !== undefined) {\n\t\t\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\t\t\t\tthis.productMarketingsCreate(result._id, product._id, Number(data.value), data.valueType);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tbadRequestError('Discount value & value-type is required for entire menu');\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Update record\n\t * @param _id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(_id: mongoose.Types.ObjectId, data: UpdateDTO): Promise<DocumentType<Model>> {\n\t\tconst { categories, products, ...rest } = data;\n\t\tconst record = await this.model.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id: _id,\n\t\t\t\tdeletedAt: null,\n\t\t\t},\n\t\t\t{ categories, ...rest },\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\t// Remove all products from product-marketings collection\n\t\tif (\n\t\t\t(data.products && data.products.length > 0) ||\n\t\t\t(data.categories && data.categories.length > 0) ||\n\t\t\tdata.isEntireMenu\n\t\t) {\n\t\t\tawait productMarketingService.deleteMany({ marketing: _id }, true);\n\t\t}\n\n\t\t// Only products\n\t\tif (data.products && data.products.length > 0 && (!data.categories || data.categories.length === 0)) {\n\t\t\tfor (const product of data.products) {\n\t\t\t\tif (product.valueType !== undefined && product.valueType !== null) {\n\t\t\t\t\tawait this.productMarketingsCreate(_id, product.product, Number(product.value), product.valueType);\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Product discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t\tconsole.log('Step 6.1');\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\t(!data.products || data.products.length === 0)\n\t\t) {\n\t\t\t// Only categories\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (data.categoryValueType !== undefined && data.categoryValueType !== null) {\n\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t_id,\n\t\t\t\t\t\tproduct._id,\n\t\t\t\t\t\tNumber(data.categoryValue),\n\t\t\t\t\t\tdata.categoryValueType\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Category discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (data.categories && data.categories.length > 0 && data.products && data.products.length > 0) {\n\t\t\t// Both products and categories\n\t\t\t// For category\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (data.categoryValueType !== undefined && data.categoryValueType !== null) {\n\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t_id,\n\t\t\t\t\t\tproduct._id,\n\t\t\t\t\t\tNumber(data.categoryValue),\n\t\t\t\t\t\tdata.categoryValueType\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Category discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// For products\n\t\t\tfor (const product of data.products) {\n\t\t\t\t// Ignore the product which is already added by category\n\t\t\t\tconst isProductAddedByCategory = categoryWiseProducts.find(\n\t\t\t\t\tp => p._id.toString() === product.product.toString()\n\t\t\t\t);\n\t\t\t\tif (isProductAddedByCategory) continue;\n\n\t\t\t\tif (product.valueType !== undefined && product.valueType !== null) {\n\t\t\t\t\tawait this.productMarketingsCreate(_id, product.product, Number(product.value), product.valueType);\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Product discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (data.isEntireMenu) {\n\t\t\t// Entire menu\n\t\t\tthis.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshop: data.shop,\n\t\t\t\t})\n\t\t\t\t.select('_id')\n\t\t\t\t.exec()\n\t\t\t\t.then(productDocuments => {\n\t\t\t\t\tfor (const product of productDocuments) {\n\t\t\t\t\t\tif (data.valueType !== undefined) {\n\t\t\t\t\t\t\tthis.productMarketingsCreate(_id, product._id, Number(data.value), data.valueType);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tbadRequestError('Discount value & value-type is required for entire menu');\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t}\n\n\t\tif (!record) notFoundError('Record not found');\n\n\t\treturn record;\n\t}\n\n\tpublic async productMarketingsCreate(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tproductId: mongoose.Types.ObjectId,\n\t\tvalue: number,\n\t\tvalueType: ValueType\n\t): Promise<void> {\n\t\tproductMarketingService.create({\n\t\t\tmarketing: marketingId,\n\t\t\tproduct: productId,\n\t\t\tvalue: value,\n\t\t\tvalueType: valueType,\n\t\t});\n\t}\n}\n\nexport const discountMarketingService = Container.get(DiscountMarketingService);\n"]}
|
|
1
|
+
{"version":3,"file":"discount-marketing.service.js","sourceRoot":"/","sources":["modules/marketing/services/discount-marketing.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAE5C,sEAImD;AAKnD,yBAA6C;AAE7C,4EAA2F;AAGpF,IAAM,wBAAwB,GAA9B,MAAM,wBAAwB;IAC5B,KAAK,CAAgC;IACrC,YAAY,CAAsB;IAE1C;QACC,IAAI,CAAC,KAAK,GAAG,+BAAsB,CAAC;QACpC,IAAI,CAAC,YAAY,GAAG,qBAAY,CAAC;IAClC,CAAC;IAMM,KAAK,CAAC,MAAM,CAAC,IAAe;QAClC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAG7C,IACC,IAAI,CAAC,QAAQ;YACb,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC;YACxB,CAAC,CAAC,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC,CAAC;YAClD,CAAC,IAAI,CAAC,YAAY,EACjB,CAAC;YACF,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACrC,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,IAAI,OAAO,CAAC,SAAS,KAAK,IAAI,EAAE,CAAC;oBACnE,IAAI,CAAC,IAAI,CAAC,eAAe,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,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ACF,CAAC;aAAM,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YAE9B,IAAI,CAAC,YAAY;iBACf,IAAI,CAAC;gBACL,IAAI,EAAE,IAAI,CAAC,IAAI;aACf,CAAC;iBACD,MAAM,CAAC,KAAK,CAAC;iBACb,IAAI,EAAE;iBACN,IAAI,CAAC,gBAAgB,CAAC,EAAE;gBACxB,KAAK,MAAM,OAAO,IAAI,gBAAgB,EAAE,CAAC;oBACxC,IAAI,IAAI,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;wBAClC,IAAI,CAAC,uBAAuB,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;oBACpF,CAAC;yBAAM,CAAC;wBACP,IAAA,8BAAe,EAAC,yDAAyD,CAAC,CAAC;oBAC5E,CAAC;gBACF,CAAC;YACF,CAAC,CAAC,CAAC;QACL,CAAC;QAED,IAAI,CAAC,MAAM;YAAE,IAAA,4BAAa,EAAC,kBAAkB,CAAC,CAAC;QAE/C,OAAO,MAAM,CAAC;IACf,CAAC;IAEM,KAAK,CAAC,uBAAuB,CACnC,WAAoC,EACpC,SAAkC,EAClC,KAAa,EACb,SAAoB;QAEpB,0BAAuB,CAAC,MAAM,CAAC;YAC9B,SAAS,EAAE,WAAW;YACtB,OAAO,EAAE,SAAS;YAClB,KAAK,EAAE,KAAK;YACZ,SAAS,EAAE,SAAS;SACpB,CAAC,CAAC;IACJ,CAAC;CACD,CAAA;AA9RY,4DAAwB;mCAAxB,wBAAwB;IADpC,IAAA,gBAAO,GAAE;;GACG,wBAAwB,CA8RpC;AAEY,QAAA,wBAAwB,GAAG,gBAAS,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { DocumentType, mongoose } from '@typegoose/typegoose';\nimport {\n\tDiscountMarketingModel,\n\tDiscountMarketing as Model,\n\tProductModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport {\n\tCreateDiscountMarketingDTO as CreateDTO,\n\tUpdateDiscountMarketingDTO as UpdateDTO,\n} from '@modules/marketing/validations';\nimport { productMarketingService } from './';\nimport { ValueType } from '@lyxa.ai/core/dist/utilities/enum';\nimport { badRequestError, notFoundError } from '@lyxa.ai/core/dist/utilities/error-common';\n\n@Service()\nexport class DiscountMarketingService {\n\tprivate model: typeof DiscountMarketingModel;\n\tprivate productModel: typeof ProductModel;\n\n\tconstructor() {\n\t\tthis.model = DiscountMarketingModel;\n\t\tthis.productModel = ProductModel;\n\t}\n\n\t/**\n\t * Create a new record\n\t * @param data Data for the new record\n\t */\n\tpublic async create(data: CreateDTO): Promise<DocumentType<Model>> {\n\t\tconst result = await this.model.create(data);\n\n\t\t// Only products\n\t\tif (\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t(!data.categories || data.categories.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\tfor (const product of data.products) {\n\t\t\t\tif (product.valueType !== undefined && product.valueType !== null) {\n\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product.product))) {\n\t\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t\tresult._id,\n\t\t\t\t\t\t\tproduct.product,\n\t\t\t\t\t\t\tNumber(product.value),\n\t\t\t\t\t\t\tproduct.valueType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Product discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\t(!data.products || data.products.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Only categories\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (data.categoryValueType !== undefined && data.categoryValueType !== null) {\n\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t\tresult._id,\n\t\t\t\t\t\t\tproduct._id,\n\t\t\t\t\t\t\tNumber(data.categoryValue),\n\t\t\t\t\t\t\tdata.categoryValueType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Category discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Both products and categories\n\t\t\t// For category\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (data.categoryValueType !== undefined && data.categoryValueType !== null) {\n\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t\tresult._id,\n\t\t\t\t\t\t\tproduct._id,\n\t\t\t\t\t\t\tNumber(data.categoryValue),\n\t\t\t\t\t\t\tdata.categoryValueType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Category discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// For products\n\t\t\tfor (const product of data.products) {\n\t\t\t\t// Ignore the product which is already added by category\n\t\t\t\tconst isProductAddedByCategory = categoryWiseProducts.find(\n\t\t\t\t\tp => p._id.toString() === product.product.toString()\n\t\t\t\t);\n\t\t\t\tif (isProductAddedByCategory) continue;\n\n\t\t\t\tif (product.valueType !== undefined && product.valueType !== null) {\n\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product.product))) {\n\t\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t\tresult._id,\n\t\t\t\t\t\t\tproduct.product,\n\t\t\t\t\t\t\tNumber(product.value),\n\t\t\t\t\t\t\tproduct.valueType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Product discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (data.isEntireMenu) {\n\t\t\t// Entire menu\n\t\t\tthis.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshop: data.shop,\n\t\t\t\t})\n\t\t\t\t.select('_id')\n\t\t\t\t.exec()\n\t\t\t\t.then(productDocuments => {\n\t\t\t\t\tfor (const product of productDocuments) {\n\t\t\t\t\t\tif (data.valueType !== undefined) {\n\t\t\t\t\t\t\tif (!data.excludeProducts || !data.excludeProducts.some(p => p.equals(product._id))) {\n\t\t\t\t\t\t\t\tthis.productMarketingsCreate(result._id, product._id, Number(data.value), data.valueType);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tbadRequestError('Discount value & value-type is required for entire menu');\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Update record\n\t * @param _id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(_id: mongoose.Types.ObjectId, data: UpdateDTO): Promise<DocumentType<Model>> {\n\t\tconst { categories, products, ...rest } = data;\n\t\tconst record = await this.model.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id: _id,\n\t\t\t\tdeletedAt: null,\n\t\t\t},\n\t\t\t{ categories, ...rest },\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\t// Remove all products from product-marketings collection\n\t\tif (\n\t\t\t(data.products && data.products.length > 0) ||\n\t\t\t(data.categories && data.categories.length > 0) ||\n\t\t\tdata.isEntireMenu\n\t\t) {\n\t\t\tawait productMarketingService.deleteMany({ marketing: _id }, true);\n\t\t}\n\n\t\t// Only products\n\t\tif (\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t(!data.categories || data.categories.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\tfor (const product of data.products) {\n\t\t\t\tif (product.valueType !== undefined && product.valueType !== null) {\n\t\t\t\t\tawait this.productMarketingsCreate(_id, product.product, Number(product.value), product.valueType);\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Product discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t\tconsole.log('Step 6.1');\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\t(!data.products || data.products.length === 0) &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Only categories\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (data.categoryValueType !== undefined && data.categoryValueType !== null) {\n\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t_id,\n\t\t\t\t\t\tproduct._id,\n\t\t\t\t\t\tNumber(data.categoryValue),\n\t\t\t\t\t\tdata.categoryValueType\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Category discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (\n\t\t\tdata.categories &&\n\t\t\tdata.categories.length > 0 &&\n\t\t\tdata.products &&\n\t\t\tdata.products.length > 0 &&\n\t\t\t!data.isEntireMenu\n\t\t) {\n\t\t\t// Both products and categories\n\t\t\t// For category\n\t\t\tconst categoryWiseProducts = await this.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshopCategory: { $in: data.categories },\n\t\t\t\t})\n\t\t\t\t.lean();\n\n\t\t\tfor (const product of categoryWiseProducts) {\n\t\t\t\tif (data.categoryValueType !== undefined && data.categoryValueType !== null) {\n\t\t\t\t\tawait this.productMarketingsCreate(\n\t\t\t\t\t\t_id,\n\t\t\t\t\t\tproduct._id,\n\t\t\t\t\t\tNumber(data.categoryValue),\n\t\t\t\t\t\tdata.categoryValueType\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Category discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// For products\n\t\t\tfor (const product of data.products) {\n\t\t\t\t// Ignore the product which is already added by category\n\t\t\t\tconst isProductAddedByCategory = categoryWiseProducts.find(\n\t\t\t\t\tp => p._id.toString() === product.product.toString()\n\t\t\t\t);\n\t\t\t\tif (isProductAddedByCategory) continue;\n\n\t\t\t\tif (product.valueType !== undefined && product.valueType !== null) {\n\t\t\t\t\tawait this.productMarketingsCreate(_id, product.product, Number(product.value), product.valueType);\n\t\t\t\t} else {\n\t\t\t\t\tbadRequestError('Product discount value & value-type is required');\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (data.isEntireMenu) {\n\t\t\t// Entire menu\n\t\t\tthis.productModel\n\t\t\t\t.find({\n\t\t\t\t\tshop: data.shop,\n\t\t\t\t})\n\t\t\t\t.select('_id')\n\t\t\t\t.exec()\n\t\t\t\t.then(productDocuments => {\n\t\t\t\t\tfor (const product of productDocuments) {\n\t\t\t\t\t\tif (data.valueType !== undefined) {\n\t\t\t\t\t\t\tthis.productMarketingsCreate(_id, product._id, Number(data.value), data.valueType);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tbadRequestError('Discount value & value-type is required for entire menu');\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t}\n\n\t\tif (!record) notFoundError('Record not found');\n\n\t\treturn record;\n\t}\n\n\tpublic async productMarketingsCreate(\n\t\tmarketingId: mongoose.Types.ObjectId,\n\t\tproductId: mongoose.Types.ObjectId,\n\t\tvalue: number,\n\t\tvalueType: ValueType\n\t): Promise<void> {\n\t\tproductMarketingService.create({\n\t\t\tmarketing: marketingId,\n\t\t\tproduct: productId,\n\t\t\tvalue: value,\n\t\t\tvalueType: valueType,\n\t\t});\n\t}\n}\n\nexport const discountMarketingService = Container.get(DiscountMarketingService);\n"]}
|
|
@@ -10,6 +10,7 @@ export declare class MarketingService {
|
|
|
10
10
|
findById(data: GetByIdInputDTO): Promise<DocumentType<Model>>;
|
|
11
11
|
find(data?: Partial<FilterDTO>): Promise<PaginatedResponse<DocumentType<ModelType<Model>>>>;
|
|
12
12
|
update(_id: mongoose.Types.ObjectId, data: UpdateDTO, admin?: mongoose.Types.ObjectId): Promise<DocumentType<Model>>;
|
|
13
|
+
private verifyUnpauseConditions;
|
|
13
14
|
private logMarketingAdminAction;
|
|
14
15
|
deleteById(data: DeleteDTO, admin?: mongoose.Types.ObjectId): Promise<string>;
|
|
15
16
|
handleNewProductForEntireMenuMarketing(data: ProductEntireMenuMarketingHandlerDTO): Promise<string>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"marketing.service.d.ts","sourceRoot":"/","sources":["modules/marketing/services/marketing.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAmB,MAAM,sBAAsB,CAAC;AAC/E,OAAO,
|
|
1
|
+
{"version":3,"file":"marketing.service.d.ts","sourceRoot":"/","sources":["modules/marketing/services/marketing.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAmB,MAAM,sBAAsB,CAAC;AAC/E,OAAO,EAIN,SAAS,IAAI,KAAK,EAClB,MAAM,2CAA2C,CAAC;AACnD,OAAO,EAEN,0BAA0B,EAE1B,oCAAoC,EACpC,kBAAkB,IAAI,SAAS,EAC/B,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACN,SAAS,EACT,iBAAiB,EACjB,SAAS,EACT,eAAe,EACf,MAAM,yCAAyC,CAAC;AAGjD,OAAO,EAAE,SAAS,EAAE,MAAM,gCAAgC,CAAC;AAuB3D,qBACa,gBAAgB;IAC5B,OAAO,CAAC,KAAK,CAAiD;;IAUjD,MAAM,CAClB,IAAI,EAAE,0BAA0B,EAChC,KAAK,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,GAC7B,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IA+GlB,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAsB7D,IAAI,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC,iBAAiB,CAAC,YAAY,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IA0B3F,MAAM,CAClB,GAAG,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAC5B,IAAI,EAAE,SAAS,EACf,KAAK,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,GAC7B,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;YAkGjB,uBAAuB;YAevB,uBAAuB;IAmBxB,UAAU,CAAC,IAAI,EAAE,SAAS,EAAE,KAAK,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC;IAgC7E,sCAAsC,CAAC,IAAI,EAAE,oCAAoC;CAgC9F;AAED,eAAO,MAAM,gBAAgB,kBAAkC,CAAC"}
|
|
@@ -189,6 +189,11 @@ let MarketingService = class MarketingService {
|
|
|
189
189
|
if (!oldMarketing) {
|
|
190
190
|
(0, error_common_1.notFoundError)('Could not find marketing');
|
|
191
191
|
}
|
|
192
|
+
if (data.status &&
|
|
193
|
+
data.status === enum_1.MarketingStatus.ACTIVE &&
|
|
194
|
+
oldMarketing.status === enum_1.MarketingStatus.PAUSED) {
|
|
195
|
+
await this.verifyUnpauseConditions(_id);
|
|
196
|
+
}
|
|
192
197
|
switch (oldMarketing.marketingType) {
|
|
193
198
|
case enum_2.MarketingType.DISCOUNT: {
|
|
194
199
|
marketing = await _1.discountMarketingService.update(_id, data);
|
|
@@ -229,11 +234,11 @@ let MarketingService = class MarketingService {
|
|
|
229
234
|
if (admin) {
|
|
230
235
|
const allChanges = (0, events_common_methods_1.getChangedFields)(oldMarketing, marketing);
|
|
231
236
|
const relevantChanges = (0, events_common_methods_1.filterRelevantChanges)(allChanges, data);
|
|
232
|
-
if (Object.keys(relevantChanges).includes('
|
|
233
|
-
if (data.
|
|
237
|
+
if (Object.keys(relevantChanges).includes('status')) {
|
|
238
|
+
if (data.status == enum_1.MarketingStatus.ACTIVE) {
|
|
234
239
|
this.logMarketingAdminAction(admin, enum_1.AdminLogsUpdatedFieldsType.CONTINUED_MARKETING_PROMOTIONS, oldMarketing, marketing);
|
|
235
240
|
}
|
|
236
|
-
else {
|
|
241
|
+
else if (data.status == enum_1.MarketingStatus.PAUSED) {
|
|
237
242
|
this.logMarketingAdminAction(admin, enum_1.AdminLogsUpdatedFieldsType.PAUSED_MARKETING_PROMOTIONS, oldMarketing, marketing);
|
|
238
243
|
}
|
|
239
244
|
}
|
|
@@ -241,6 +246,19 @@ let MarketingService = class MarketingService {
|
|
|
241
246
|
}
|
|
242
247
|
return marketing;
|
|
243
248
|
}
|
|
249
|
+
async verifyUnpauseConditions(marketingId) {
|
|
250
|
+
const marketing = await this.model.findOne({ _id: marketingId, deletedAt: null }).populate('products');
|
|
251
|
+
if (!marketing)
|
|
252
|
+
return;
|
|
253
|
+
if ([enum_2.MarketingType.BUY1GET1, enum_2.MarketingType.DISCOUNT].includes(marketing.marketingType)) {
|
|
254
|
+
const typedMarketing = marketing;
|
|
255
|
+
const productIds = typedMarketing.products.map((product) => product.product);
|
|
256
|
+
const isAvailable = await _1.productMarketingService.checkProductMarketingAvailability(productIds);
|
|
257
|
+
if (!isAvailable) {
|
|
258
|
+
(0, error_common_1.conflictError)("You can't activate discount and B1G1 on the same items at the same time");
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
244
262
|
async logMarketingAdminAction(admin, actionType, oldDocument, newDocument) {
|
|
245
263
|
const adminActivityLogsEvent = new admin_logs_event_1.AdminActivityLogsEvent({
|
|
246
264
|
admin,
|
|
@@ -259,6 +277,8 @@ let MarketingService = class MarketingService {
|
|
|
259
277
|
message: 'Record not found',
|
|
260
278
|
});
|
|
261
279
|
}
|
|
280
|
+
record.status = enum_1.MarketingStatus.DELETED;
|
|
281
|
+
await record.save();
|
|
262
282
|
const result = await this.model.deleteRecord({ _id, deletedAt: null }, softDelete);
|
|
263
283
|
if (!result) {
|
|
264
284
|
throw new server_1.TRPCError({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"marketing.service.js","sourceRoot":"/","sources":["modules/marketing/services/marketing.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AAEzC,sEAKmD;AAenD,wEAA6G;AAE7G,8FAA6G;AAC7G,iGAAoG;AACpG,4DAAgG;AAChG,8DAAkE;AAGlE,wBAOW;AACX,4DAAkE;AAClE,4EAImD;AAG5C,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IACpB,KAAK,CAAiD;IAE9D;QACC,IAAI,CAAC,KAAK,GAAG,uBAAgE,CAAC;IAC/E,CAAC;IAMM,KAAK,CAAC,MAAM,CAClB,IAAgC,EAChC,KAA+B;QAE/B,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;QAEvB,MAAM,cAAc,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;YAC/C,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,aAAa,EAAE,IAAI,CAAC,aAAa;YACjC,cAAc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,iBAAqD,CAAC,SAAS,CAAC;gBACnG,CAAC;gBACD,MAAM,MAAM,GAAG,MAAM,0BAAuB,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;gBAE1E,OAAO,CAAC,GAAG,CAAC,8BAA8B,MAAM,EAAE,CAAC,CAAC;YACrD,CAAC;QACF,CAAC;QAED,OAAO,8BAA8B,CAAC;IACvC,CAAC;CACD,CAAA;AA1VY,4CAAgB;2BAAhB,gBAAgB;IAD5B,IAAA,gBAAO,GAAE;;GACG,gBAAgB,CA0V5B;AAEY,QAAA,gBAAgB,GAAG,gBAAS,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType, mongoose, ReturnModelType } from '@typegoose/typegoose';\nimport {\n\tDiscountMarketing,\n\tMarketingModel,\n\tMarketing as Model,\n\tPunchMarketingHistory,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport {\n\tAllMarketingsDTO,\n\tAllMarketingsValidationDTO,\n\tCreateProductMarketingDTO,\n\tProductEntireMenuMarketingHandlerDTO,\n\tUpdateMarketingDTO as UpdateDTO,\n} from '@modules/marketing/validations';\nimport {\n\tFilterDTO,\n\tPaginatedResponse,\n\tDeleteDTO,\n\tGetByIdInputDTO,\n} from '@lyxa.ai/core/dist/utilities/validation';\nimport { SoftDeleteModel } from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport { createRawDocumentPaginatorObject, PaginatorOptions } from '@lyxa.ai/core/dist/utilities/pagination';\nimport { ModelType } from '@typegoose/typegoose/lib/types';\nimport { filterRelevantChanges, getChangedFields } from '@lyxa.ai/core/dist/utilities/events-common-methods';\nimport { AdminActivityLogsEvent } from '@lyxa.ai/core/dist/libraries/event/events/admin-logs-event';\nimport { AdminLogsUpdatedFieldsType, MarketingStatus } from '@lyxa.ai/core/dist/utilities/enum';\nimport { publishEvent } from '@lyxa.ai/core/dist/libraries/event';\nimport { BaseEvent } from '@lyxa.ai/core/dist/libraries/event/BaseEvent';\n\nimport {\n\tbuy1Get1MarketingService,\n\tdiscountMarketingService,\n\tfeaturedMarketingService,\n\tfreeDeliveryMarketingService,\n\tproductMarketingService,\n\tpunchMarketingService,\n} from '.';\nimport { MarketingType } from '@lyxa.ai/core/dist/utilities/enum';\nimport {\n\tinternalServerError,\n\tnotFoundError,\n\tunauthorizedError,\n} from '@lyxa.ai/core/dist/utilities/error-common';\n\n@Service()\nexport class MarketingService {\n\tprivate model: typeof MarketingModel & SoftDeleteModel<Model>;\n\n\tconstructor() {\n\t\tthis.model = MarketingModel as typeof MarketingModel & SoftDeleteModel<Model>;\n\t}\n\n\t/**\n\t * Create a new record\n\t * @param data Data for the new record\n\t */\n\tpublic async create(\n\t\tdata: AllMarketingsValidationDTO,\n\t\tadmin?: mongoose.Types.ObjectId\n\t): Promise<DocumentType<Model>> {\n\t\tconst now = new Date();\n\n\t\tconst existingRecord = await this.model.findOne({\n\t\t\tshop: data.shop,\n\t\t\tmarketingType: data.marketingType,\n\t\t\t'duration.end': { $gt: new Date() },\n\t\t\tstatus: MarketingStatus.ACTIVE,\n\t\t});\n\t\tif (existingRecord) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'CONFLICT',\n\t\t\t\tmessage: `This shop already has a ${data.marketingType} marketing`,\n\t\t\t});\n\t\t}\n\n\t\tif (data.isEntireMenu) {\n\t\t\tconst existingMarketing = await this.model.findOne({\n\t\t\t\tshop: data.shop,\n\t\t\t\t'duration.start': { $lt: now },\n\t\t\t\t'duration.end': { $gt: now },\n\t\t\t\tstatus: MarketingStatus.ACTIVE,\n\t\t\t\tmarketingType: { $in: [MarketingType.BUY1GET1, MarketingType.DISCOUNT] },\n\t\t\t});\n\t\t\tif (existingMarketing) {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'CONFLICT',\n\t\t\t\t\tmessage: `Cannot create marketing for entire menu while another product based marketing exists for the shop`,\n\t\t\t\t});\n\t\t\t}\n\t\t} else if (data.products && data.products.length > 0) {\n\t\t\tconst productIds = data.products.map((product: CreateProductMarketingDTO) => product.product);\n\n\t\t\tconst hasDuplicates = new Set(productIds.map(String)).size !== productIds.length;\n\n\t\t\tif (hasDuplicates) {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'BAD_REQUEST',\n\t\t\t\t\tmessage: `Product list contains duplicate products`,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst isAvailable = await productMarketingService.checkProductMarketingAvailability(productIds);\n\n\t\t\tif (!isAvailable) {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'CONFLICT',\n\t\t\t\t\tmessage: `Product list contains products which already have marketing applied`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (admin) {\n\t\t\tdata.admin = admin;\n\t\t}\n\n\t\tlet marketing: AllMarketingsDTO;\n\n\t\tswitch (data.marketingType) {\n\t\t\tcase MarketingType.DISCOUNT: {\n\t\t\t\tmarketing = await discountMarketingService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.BUY1GET1: {\n\t\t\t\tmarketing = await buy1Get1MarketingService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.FEATURED: {\n\t\t\t\tif (!admin) {\n\t\t\t\t\tthrow new TRPCError({\n\t\t\t\t\t\tcode: 'UNAUTHORIZED',\n\t\t\t\t\t\tmessage: `Only admin is allowed to create ${data.marketingType} marketing`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tmarketing = await featuredMarketingService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.FREE_DELIVERY: {\n\t\t\t\tmarketing = await freeDeliveryMarketingService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.PUNCH_MARKETING: {\n\t\t\t\tmarketing = await punchMarketingService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\tmarketing = await this.model.create(data, admin);\n\t\t\t}\n\t\t}\n\n\t\tif (admin) {\n\t\t\tthis.logMarketingAdminAction(\n\t\t\t\tadmin,\n\t\t\t\tAdminLogsUpdatedFieldsType.ACTIVATED_MARKETING_PROMOTIONS,\n\t\t\t\t'',\n\t\t\t\tmarketing\n\t\t\t);\n\t\t}\n\n\t\treturn marketing;\n\t}\n\n\t/**\n\t * Get record by ID\n\t * @param id ID of the record\n\t */\n\tpublic async findById(data: GetByIdInputDTO): Promise<DocumentType<Model>> {\n\t\tconst select = data.select || '';\n\t\tconst populate = data.populate || [];\n\t\tconst result = await this.model\n\t\t\t.findOne({\n\t\t\t\t_id: data._id,\n\t\t\t})\n\t\t\t.select(select)\n\t\t\t.populate(populate);\n\t\tif (!result) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'Record not found',\n\t\t\t});\n\t\t}\n\t\treturn result;\n\t}\n\n\t/**\n\t * Get records\n\t * @param query Query to find records\n\t */\n\tpublic async find(data?: Partial<FilterDTO>): Promise<PaginatedResponse<DocumentType<ModelType<Model>>>> {\n\t\ttry {\n\t\t\tconst paginate = createRawDocumentPaginatorObject(this.model as ReturnModelType<typeof this.model>);\n\t\t\tconst options: PaginatorOptions<typeof Model> = {\n\t\t\t\tpage: data?.page,\n\t\t\t\tsize: data?.size,\n\t\t\t\tsort: data?.sort,\n\t\t\t\tquery: data?.query,\n\t\t\t\tselect: data?.select,\n\t\t\t\tpopulate: data?.populate,\n\t\t\t};\n\t\t\tconst paginatedResult = await paginate(options);\n\t\t\treturn paginatedResult;\n\t\t} catch (err) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'INTERNAL_SERVER_ERROR',\n\t\t\t\tmessage: `err: ${err}`,\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Update record\n\t * @param id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(\n\t\t_id: mongoose.Types.ObjectId,\n\t\tdata: UpdateDTO,\n\t\tadmin?: mongoose.Types.ObjectId\n\t): Promise<DocumentType<Model>> {\n\t\tlet marketing: AllMarketingsDTO;\n\n\t\tconst oldMarketing = await this.model.findById(_id);\n\n\t\tif (!oldMarketing) {\n\t\t\tnotFoundError('Could not find marketing');\n\t\t}\n\n\t\tswitch (oldMarketing.marketingType) {\n\t\t\tcase MarketingType.DISCOUNT: {\n\t\t\t\tmarketing = await discountMarketingService.update(_id, data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.BUY1GET1: {\n\t\t\t\tmarketing = await buy1Get1MarketingService.update(_id, data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.FEATURED: {\n\t\t\t\tif (!admin) {\n\t\t\t\t\tunauthorizedError(`Only admin is allowed to update ${data.marketingType} marketing`);\n\t\t\t\t}\n\t\t\t\tmarketing = await featuredMarketingService.update(_id, data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.FREE_DELIVERY: {\n\t\t\t\tmarketing = await freeDeliveryMarketingService.update(_id, data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.PUNCH_MARKETING: {\n\t\t\t\tmarketing = await punchMarketingService.update(_id, data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\tmarketing = await this.model.findOneAndUpdate(\n\t\t\t\t\t{\n\t\t\t\t\t\t_id: _id,\n\t\t\t\t\t\tdeletedAt: null,\n\t\t\t\t\t},\n\t\t\t\t\tdata,\n\t\t\t\t\t{\n\t\t\t\t\t\tnew: true,\n\t\t\t\t\t\trunValidators: true,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tif (!marketing) {\n\t\t\tinternalServerError('Could not update marketing');\n\t\t}\n\n\t\tif (admin) {\n\t\t\tconst allChanges = getChangedFields(oldMarketing, marketing);\n\t\t\tconst relevantChanges = filterRelevantChanges(allChanges, data);\n\n\t\t\tif (Object.keys(relevantChanges).includes('pausedAt')) {\n\t\t\t\tif (data.pausedAt == null) {\n\t\t\t\t\tthis.logMarketingAdminAction(\n\t\t\t\t\t\tadmin,\n\t\t\t\t\t\tAdminLogsUpdatedFieldsType.CONTINUED_MARKETING_PROMOTIONS,\n\t\t\t\t\t\toldMarketing,\n\t\t\t\t\t\tmarketing\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tthis.logMarketingAdminAction(\n\t\t\t\t\t\tadmin,\n\t\t\t\t\t\tAdminLogsUpdatedFieldsType.PAUSED_MARKETING_PROMOTIONS,\n\t\t\t\t\t\toldMarketing,\n\t\t\t\t\t\tmarketing\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis.logMarketingAdminAction(\n\t\t\t\tadmin,\n\t\t\t\tAdminLogsUpdatedFieldsType.EDITED_MARKETING_PROMOTIONS,\n\t\t\t\toldMarketing,\n\t\t\t\tmarketing\n\t\t\t);\n\t\t}\n\n\t\treturn marketing;\n\t}\n\n\tprivate async logMarketingAdminAction(\n\t\tadmin: mongoose.Types.ObjectId,\n\t\tactionType: AdminLogsUpdatedFieldsType,\n\t\toldDocument?: any,\n\t\tnewDocument?: any\n\t) {\n\t\tconst adminActivityLogsEvent: BaseEvent<Record<string, unknown>> = new AdminActivityLogsEvent({\n\t\t\tadmin,\n\t\t\tupdatedField: actionType,\n\t\t\toldDocument: oldDocument ?? '',\n\t\t\tnewDocument: newDocument ?? '',\n\t\t});\n\t\tawait publishEvent(adminActivityLogsEvent);\n\t}\n\n\t/**\n\t * Delete record\n\t * @param data Data for delete\n\t */\n\tpublic async deleteById(data: DeleteDTO, admin?: mongoose.Types.ObjectId): Promise<string> {\n\t\tconst { _id, softDelete } = data;\n\n\t\tconst record = await this.model.findById(_id);\n\n\t\tif (!record) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'Record not found',\n\t\t\t});\n\t\t}\n\t\tconst result = await this.model.deleteRecord({ _id, deletedAt: null }, softDelete);\n\t\tif (!result) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'INTERNAL_SERVER_ERROR',\n\t\t\t\tmessage: 'Unable to delete marketing',\n\t\t\t});\n\t\t}\n\n\t\tif (admin) {\n\t\t\tthis.logMarketingAdminAction(admin, AdminLogsUpdatedFieldsType.DELETED_MARKETING_PROMOTIONS, record);\n\t\t}\n\n\t\tproductMarketingService.deleteMany({ marketing: data._id }, data.softDelete);\n\n\t\treturn 'Record deleted successfully';\n\t}\n\n\tpublic async handleNewProductForEntireMenuMarketing(data: ProductEntireMenuMarketingHandlerDTO) {\n\t\tconst existingMarketing = await this.model.findOne({\n\t\t\tshop: data.shop,\n\t\t\tmarketingType: { $in: [MarketingType.BUY1GET1, MarketingType.DISCOUNT] },\n\t\t\t'duration.end': { $gt: new Date() },\n\t\t\tstatus: MarketingStatus.ACTIVE,\n\t\t\tisEntireMenu: true,\n\t\t});\n\n\t\t// check if existing marketing\n\t\tif (existingMarketing) {\n\t\t\tconst isAvailable = await productMarketingService.checkProductMarketingAvailability([data.product]);\n\t\t\tif (isAvailable) {\n\t\t\t\tconst productMarketingData: CreateProductMarketingDTO = {\n\t\t\t\t\tproduct: data.product,\n\t\t\t\t\tmarketing: existingMarketing._id,\n\t\t\t\t};\n\t\t\t\tif (existingMarketing.marketingType == MarketingType.BUY1GET1) {\n\t\t\t\t\tproductMarketingData.isBuy1Get1 = true;\n\t\t\t\t}\n\t\t\t\tif (existingMarketing.marketingType == MarketingType.DISCOUNT) {\n\t\t\t\t\tproductMarketingData.value = (existingMarketing as DocumentType<DiscountMarketing>).value;\n\t\t\t\t\tproductMarketingData.valueType = (existingMarketing as DocumentType<DiscountMarketing>).valueType;\n\t\t\t\t}\n\t\t\t\tconst result = await productMarketingService.create(productMarketingData);\n\n\t\t\t\tconsole.log(`Created product marketing: ${result}`);\n\t\t\t}\n\t\t}\n\n\t\treturn 'Product handled successfully';\n\t}\n}\n\nexport const marketingService = Container.get(MarketingService);\n"]}
|
|
1
|
+
{"version":3,"file":"marketing.service.js","sourceRoot":"/","sources":["modules/marketing/services/marketing.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AAEzC,sEAKmD;AAenD,wEAA6G;AAE7G,8FAA6G;AAC7G,iGAAoG;AACpG,4DAAgG;AAChG,8DAAkE;AAGlE,wBAOW;AACX,4DAAkE;AAClE,4EAKmD;AAG5C,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IACpB,KAAK,CAAiD;IAE9D;QACC,IAAI,CAAC,KAAK,GAAG,uBAAgE,CAAC;IAC/E,CAAC;IAMM,KAAK,CAAC,MAAM,CAClB,IAAgC,EAChC,KAA+B;QAE/B,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;QAEvB,MAAM,cAAc,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;YAC/C,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,aAAa,EAAE,IAAI,CAAC,aAAa;YACjC,cAAc,EAAE,EAAE,GAAG,EAAE,IAAI,IAAI,EAAE,EAAE;YACnC,MAAM,EAAE,sBAAe,CAAC,MAAM;SAC9B,CAAC,CAAC;QACH,IAAI,cAAc,EAAE,CAAC;YACpB,MAAM,IAAI,kBAAS,CAAC;gBACnB,IAAI,EAAE,UAAU;gBAChB,OAAO,EAAE,2BAA2B,IAAI,CAAC,aAAa,YAAY;aAClE,CAAC,CAAC;QACJ,CAAC;QAED,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACvB,MAAM,iBAAiB,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;gBAClD,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,gBAAgB,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE;gBAC9B,cAAc,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,EAAE,IAAI;SAClB,CAAC,CAAC;QAGH,IAAI,iBAAiB,EAAE,CAAC;YACvB,MAAM,WAAW,GAAG,MAAM,0BAAuB,CAAC,iCAAiC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;YACpG,IAAI,WAAW,EAAE,CAAC;gBACjB,MAAM,oBAAoB,GAA8B;oBACvD,OAAO,EAAE,IAAI,CAAC,OAAO;oBACrB,SAAS,EAAE,iBAAiB,CAAC,GAAG;iBAChC,CAAC;gBACF,IAAI,iBAAiB,CAAC,aAAa,IAAI,oBAAa,CAAC,QAAQ,EAAE,CAAC;oBAC/D,oBAAoB,CAAC,UAAU,GAAG,IAAI,CAAC;gBACxC,CAAC;gBACD,IAAI,iBAAiB,CAAC,aAAa,IAAI,oBAAa,CAAC,QAAQ,EAAE,CAAC;oBAC/D,oBAAoB,CAAC,KAAK,GAAI,iBAAqD,CAAC,KAAK,CAAC;oBAC1F,oBAAoB,CAAC,SAAS,GAAI,iBAAqD,CAAC,SAAS,CAAC;gBACnG,CAAC;gBACD,MAAM,MAAM,GAAG,MAAM,0BAAuB,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;gBAE1E,OAAO,CAAC,GAAG,CAAC,8BAA8B,MAAM,EAAE,CAAC,CAAC;YACrD,CAAC;QACF,CAAC;QAED,OAAO,8BAA8B,CAAC;IACvC,CAAC;CACD,CAAA;AArXY,4CAAgB;2BAAhB,gBAAgB;IAD5B,IAAA,gBAAO,GAAE;;GACG,gBAAgB,CAqX5B;AAEY,QAAA,gBAAgB,GAAG,gBAAS,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType, mongoose, ReturnModelType } from '@typegoose/typegoose';\nimport {\n\tBuy1Get1Marketing,\n\tDiscountMarketing,\n\tMarketingModel,\n\tMarketing as Model,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport {\n\tAllMarketingsDTO,\n\tAllMarketingsValidationDTO,\n\tCreateProductMarketingDTO,\n\tProductEntireMenuMarketingHandlerDTO,\n\tUpdateMarketingDTO as UpdateDTO,\n} from '@modules/marketing/validations';\nimport {\n\tFilterDTO,\n\tPaginatedResponse,\n\tDeleteDTO,\n\tGetByIdInputDTO,\n} from '@lyxa.ai/core/dist/utilities/validation';\nimport { SoftDeleteModel } from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport { createRawDocumentPaginatorObject, PaginatorOptions } from '@lyxa.ai/core/dist/utilities/pagination';\nimport { ModelType } from '@typegoose/typegoose/lib/types';\nimport { filterRelevantChanges, getChangedFields } from '@lyxa.ai/core/dist/utilities/events-common-methods';\nimport { AdminActivityLogsEvent } from '@lyxa.ai/core/dist/libraries/event/events/admin-logs-event';\nimport { AdminLogsUpdatedFieldsType, MarketingStatus } from '@lyxa.ai/core/dist/utilities/enum';\nimport { publishEvent } from '@lyxa.ai/core/dist/libraries/event';\nimport { BaseEvent } from '@lyxa.ai/core/dist/libraries/event/BaseEvent';\n\nimport {\n\tbuy1Get1MarketingService,\n\tdiscountMarketingService,\n\tfeaturedMarketingService,\n\tfreeDeliveryMarketingService,\n\tproductMarketingService,\n\tpunchMarketingService,\n} from '.';\nimport { MarketingType } from '@lyxa.ai/core/dist/utilities/enum';\nimport {\n\tconflictError,\n\tinternalServerError,\n\tnotFoundError,\n\tunauthorizedError,\n} from '@lyxa.ai/core/dist/utilities/error-common';\n\n@Service()\nexport class MarketingService {\n\tprivate model: typeof MarketingModel & SoftDeleteModel<Model>;\n\n\tconstructor() {\n\t\tthis.model = MarketingModel as typeof MarketingModel & SoftDeleteModel<Model>;\n\t}\n\n\t/**\n\t * Create a new record\n\t * @param data Data for the new record\n\t */\n\tpublic async create(\n\t\tdata: AllMarketingsValidationDTO,\n\t\tadmin?: mongoose.Types.ObjectId\n\t): Promise<DocumentType<Model>> {\n\t\tconst now = new Date();\n\n\t\tconst existingRecord = await this.model.findOne({\n\t\t\tshop: data.shop,\n\t\t\tmarketingType: data.marketingType,\n\t\t\t'duration.end': { $gt: new Date() },\n\t\t\tstatus: MarketingStatus.ACTIVE,\n\t\t});\n\t\tif (existingRecord) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'CONFLICT',\n\t\t\t\tmessage: `This shop already has a ${data.marketingType} marketing`,\n\t\t\t});\n\t\t}\n\n\t\tif (data.isEntireMenu) {\n\t\t\tconst existingMarketing = await this.model.findOne({\n\t\t\t\tshop: data.shop,\n\t\t\t\t'duration.start': { $lt: now },\n\t\t\t\t'duration.end': { $gt: now },\n\t\t\t\tstatus: MarketingStatus.ACTIVE,\n\t\t\t\tmarketingType: { $in: [MarketingType.BUY1GET1, MarketingType.DISCOUNT] },\n\t\t\t});\n\t\t\tif (existingMarketing) {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'CONFLICT',\n\t\t\t\t\tmessage: `Cannot create marketing for entire menu while another product based marketing exists for the shop`,\n\t\t\t\t});\n\t\t\t}\n\t\t} else if (data.products && data.products.length > 0) {\n\t\t\tconst productIds = data.products.map((product: CreateProductMarketingDTO) => product.product);\n\n\t\t\tconst hasDuplicates = new Set(productIds.map(String)).size !== productIds.length;\n\n\t\t\tif (hasDuplicates) {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'BAD_REQUEST',\n\t\t\t\t\tmessage: `Product list contains duplicate products`,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst isAvailable = await productMarketingService.checkProductMarketingAvailability(productIds);\n\n\t\t\tif (!isAvailable) {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'CONFLICT',\n\t\t\t\t\tmessage: `Product list contains products which already have marketing applied`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (admin) {\n\t\t\tdata.admin = admin;\n\t\t}\n\n\t\tlet marketing: AllMarketingsDTO;\n\n\t\tswitch (data.marketingType) {\n\t\t\tcase MarketingType.DISCOUNT: {\n\t\t\t\tmarketing = await discountMarketingService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.BUY1GET1: {\n\t\t\t\tmarketing = await buy1Get1MarketingService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.FEATURED: {\n\t\t\t\tif (!admin) {\n\t\t\t\t\tthrow new TRPCError({\n\t\t\t\t\t\tcode: 'UNAUTHORIZED',\n\t\t\t\t\t\tmessage: `Only admin is allowed to create ${data.marketingType} marketing`,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tmarketing = await featuredMarketingService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.FREE_DELIVERY: {\n\t\t\t\tmarketing = await freeDeliveryMarketingService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.PUNCH_MARKETING: {\n\t\t\t\tmarketing = await punchMarketingService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\tmarketing = await this.model.create(data, admin);\n\t\t\t}\n\t\t}\n\n\t\tif (admin) {\n\t\t\tthis.logMarketingAdminAction(\n\t\t\t\tadmin,\n\t\t\t\tAdminLogsUpdatedFieldsType.ACTIVATED_MARKETING_PROMOTIONS,\n\t\t\t\t'',\n\t\t\t\tmarketing\n\t\t\t);\n\t\t}\n\n\t\treturn marketing;\n\t}\n\n\t/**\n\t * Get record by ID\n\t * @param id ID of the record\n\t */\n\tpublic async findById(data: GetByIdInputDTO): Promise<DocumentType<Model>> {\n\t\tconst select = data.select || '';\n\t\tconst populate = data.populate || [];\n\t\tconst result = await this.model\n\t\t\t.findOne({\n\t\t\t\t_id: data._id,\n\t\t\t})\n\t\t\t.select(select)\n\t\t\t.populate(populate);\n\t\tif (!result) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'Record not found',\n\t\t\t});\n\t\t}\n\t\treturn result;\n\t}\n\n\t/**\n\t * Get records\n\t * @param query Query to find records\n\t */\n\tpublic async find(data?: Partial<FilterDTO>): Promise<PaginatedResponse<DocumentType<ModelType<Model>>>> {\n\t\ttry {\n\t\t\tconst paginate = createRawDocumentPaginatorObject(this.model as ReturnModelType<typeof this.model>);\n\t\t\tconst options: PaginatorOptions<typeof Model> = {\n\t\t\t\tpage: data?.page,\n\t\t\t\tsize: data?.size,\n\t\t\t\tsort: data?.sort,\n\t\t\t\tquery: data?.query,\n\t\t\t\tselect: data?.select,\n\t\t\t\tpopulate: data?.populate,\n\t\t\t};\n\t\t\tconst paginatedResult = await paginate(options);\n\t\t\treturn paginatedResult;\n\t\t} catch (err) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'INTERNAL_SERVER_ERROR',\n\t\t\t\tmessage: `err: ${err}`,\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Update record\n\t * @param id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(\n\t\t_id: mongoose.Types.ObjectId,\n\t\tdata: UpdateDTO,\n\t\tadmin?: mongoose.Types.ObjectId\n\t): Promise<DocumentType<Model>> {\n\t\tlet marketing: AllMarketingsDTO;\n\n\t\tconst oldMarketing = await this.model.findById(_id);\n\n\t\tif (!oldMarketing) {\n\t\t\tnotFoundError('Could not find marketing');\n\t\t}\n\n\t\tif (\n\t\t\tdata.status &&\n\t\t\tdata.status === MarketingStatus.ACTIVE &&\n\t\t\toldMarketing.status === MarketingStatus.PAUSED\n\t\t) {\n\t\t\tawait this.verifyUnpauseConditions(_id);\n\t\t}\n\n\t\tswitch (oldMarketing.marketingType) {\n\t\t\tcase MarketingType.DISCOUNT: {\n\t\t\t\tmarketing = await discountMarketingService.update(_id, data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.BUY1GET1: {\n\t\t\t\tmarketing = await buy1Get1MarketingService.update(_id, data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.FEATURED: {\n\t\t\t\tif (!admin) {\n\t\t\t\t\tunauthorizedError(`Only admin is allowed to update ${data.marketingType} marketing`);\n\t\t\t\t}\n\t\t\t\tmarketing = await featuredMarketingService.update(_id, data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.FREE_DELIVERY: {\n\t\t\t\tmarketing = await freeDeliveryMarketingService.update(_id, data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase MarketingType.PUNCH_MARKETING: {\n\t\t\t\tmarketing = await punchMarketingService.update(_id, data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\tmarketing = await this.model.findOneAndUpdate(\n\t\t\t\t\t{\n\t\t\t\t\t\t_id: _id,\n\t\t\t\t\t\tdeletedAt: null,\n\t\t\t\t\t},\n\t\t\t\t\tdata,\n\t\t\t\t\t{\n\t\t\t\t\t\tnew: true,\n\t\t\t\t\t\trunValidators: true,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tif (!marketing) {\n\t\t\tinternalServerError('Could not update marketing');\n\t\t}\n\n\t\tif (admin) {\n\t\t\tconst allChanges = getChangedFields(oldMarketing, marketing);\n\t\t\tconst relevantChanges = filterRelevantChanges(allChanges, data);\n\n\t\t\tif (Object.keys(relevantChanges).includes('status')) {\n\t\t\t\tif (data.status == MarketingStatus.ACTIVE) {\n\t\t\t\t\tthis.logMarketingAdminAction(\n\t\t\t\t\t\tadmin,\n\t\t\t\t\t\tAdminLogsUpdatedFieldsType.CONTINUED_MARKETING_PROMOTIONS,\n\t\t\t\t\t\toldMarketing,\n\t\t\t\t\t\tmarketing\n\t\t\t\t\t);\n\t\t\t\t} else if (data.status == MarketingStatus.PAUSED) {\n\t\t\t\t\tthis.logMarketingAdminAction(\n\t\t\t\t\t\tadmin,\n\t\t\t\t\t\tAdminLogsUpdatedFieldsType.PAUSED_MARKETING_PROMOTIONS,\n\t\t\t\t\t\toldMarketing,\n\t\t\t\t\t\tmarketing\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis.logMarketingAdminAction(\n\t\t\t\tadmin,\n\t\t\t\tAdminLogsUpdatedFieldsType.EDITED_MARKETING_PROMOTIONS,\n\t\t\t\toldMarketing,\n\t\t\t\tmarketing\n\t\t\t);\n\t\t}\n\n\t\treturn marketing;\n\t}\n\n\tprivate async verifyUnpauseConditions(marketingId: mongoose.Types.ObjectId): Promise<void> {\n\t\tconst marketing = await this.model.findOne({ _id: marketingId, deletedAt: null }).populate('products');\n\t\tif (!marketing) return;\n\n\t\tif ([MarketingType.BUY1GET1, MarketingType.DISCOUNT].includes(marketing.marketingType)) {\n\t\t\tconst typedMarketing = marketing as DocumentType<DiscountMarketing | Buy1Get1Marketing>;\n\t\t\tconst productIds = typedMarketing.products.map((product: any) => product.product);\n\t\t\tconst isAvailable = await productMarketingService.checkProductMarketingAvailability(productIds);\n\n\t\t\tif (!isAvailable) {\n\t\t\t\tconflictError(\"You can't activate discount and B1G1 on the same items at the same time\");\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async logMarketingAdminAction(\n\t\tadmin: mongoose.Types.ObjectId,\n\t\tactionType: AdminLogsUpdatedFieldsType,\n\t\toldDocument?: any,\n\t\tnewDocument?: any\n\t) {\n\t\tconst adminActivityLogsEvent: BaseEvent<Record<string, unknown>> = new AdminActivityLogsEvent({\n\t\t\tadmin,\n\t\t\tupdatedField: actionType,\n\t\t\toldDocument: oldDocument ?? '',\n\t\t\tnewDocument: newDocument ?? '',\n\t\t});\n\t\tawait publishEvent(adminActivityLogsEvent);\n\t}\n\n\t/**\n\t * Delete record\n\t * @param data Data for delete\n\t */\n\tpublic async deleteById(data: DeleteDTO, admin?: mongoose.Types.ObjectId): Promise<string> {\n\t\tconst { _id, softDelete } = data;\n\n\t\tconst record = await this.model.findById(_id);\n\n\t\tif (!record) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'Record not found',\n\t\t\t});\n\t\t}\n\n\t\trecord.status = MarketingStatus.DELETED;\n\t\tawait record.save();\n\n\t\tconst result = await this.model.deleteRecord({ _id, deletedAt: null }, softDelete);\n\t\tif (!result) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'INTERNAL_SERVER_ERROR',\n\t\t\t\tmessage: 'Unable to delete marketing',\n\t\t\t});\n\t\t}\n\n\t\tif (admin) {\n\t\t\tthis.logMarketingAdminAction(admin, AdminLogsUpdatedFieldsType.DELETED_MARKETING_PROMOTIONS, record);\n\t\t}\n\n\t\tproductMarketingService.deleteMany({ marketing: data._id }, data.softDelete);\n\n\t\treturn 'Record deleted successfully';\n\t}\n\n\tpublic async handleNewProductForEntireMenuMarketing(data: ProductEntireMenuMarketingHandlerDTO) {\n\t\tconst existingMarketing = await this.model.findOne({\n\t\t\tshop: data.shop,\n\t\t\tmarketingType: { $in: [MarketingType.BUY1GET1, MarketingType.DISCOUNT] },\n\t\t\t'duration.end': { $gt: new Date() },\n\t\t\tstatus: MarketingStatus.ACTIVE,\n\t\t\tisEntireMenu: true,\n\t\t});\n\n\t\t// check if existing marketing\n\t\tif (existingMarketing) {\n\t\t\tconst isAvailable = await productMarketingService.checkProductMarketingAvailability([data.product]);\n\t\t\tif (isAvailable) {\n\t\t\t\tconst productMarketingData: CreateProductMarketingDTO = {\n\t\t\t\t\tproduct: data.product,\n\t\t\t\t\tmarketing: existingMarketing._id,\n\t\t\t\t};\n\t\t\t\tif (existingMarketing.marketingType == MarketingType.BUY1GET1) {\n\t\t\t\t\tproductMarketingData.isBuy1Get1 = true;\n\t\t\t\t}\n\t\t\t\tif (existingMarketing.marketingType == MarketingType.DISCOUNT) {\n\t\t\t\t\tproductMarketingData.value = (existingMarketing as DocumentType<DiscountMarketing>).value;\n\t\t\t\t\tproductMarketingData.valueType = (existingMarketing as DocumentType<DiscountMarketing>).valueType;\n\t\t\t\t}\n\t\t\t\tconst result = await productMarketingService.create(productMarketingData);\n\n\t\t\t\tconsole.log(`Created product marketing: ${result}`);\n\t\t\t}\n\t\t}\n\n\t\treturn 'Product handled successfully';\n\t}\n}\n\nexport const marketingService = Container.get(MarketingService);\n"]}
|