@lyxa.ai/marketing 1.0.16 → 1.0.17
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/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 +39 -67
- package/dist/lib/modules/coupon/services/coupon.service.js.map +1 -1
- package/dist/lib/modules/coupon/services/custom-coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/custom-coupon.service.js +5 -4
- package/dist/lib/modules/coupon/services/custom-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 +5 -4
- 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 +5 -4
- 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 +5 -4
- package/dist/lib/modules/coupon/services/individual-user-coupon.service.js.map +1 -1
- package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.d.ts +0 -1
- package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.js +12 -22
- package/dist/lib/modules/coupon/services/patterns/repository/CouponRepository.js.map +1 -1
- package/dist/lib/modules/coupon/services/referral-code-coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/referral-code-coupon.service.js +5 -4
- package/dist/lib/modules/coupon/services/referral-code-coupon.service.js.map +1 -1
- package/dist/lib/modules/coupon/services/referral-reward-coupon.service.d.ts.map +1 -1
- package/dist/lib/modules/coupon/services/referral-reward-coupon.service.js +5 -4
- package/dist/lib/modules/coupon/services/referral-reward-coupon.service.js.map +1 -1
- package/dist/lib/modules/marketing/validations/buy1-get1-marketing.validation.d.ts.map +1 -1
- package/dist/lib/modules/marketing/validations/buy1-get1-marketing.validation.js +11 -4
- package/dist/lib/modules/marketing/validations/buy1-get1-marketing.validation.js.map +1 -1
- package/dist/lib/modules/punch-marketing-history/services/punch-marketing-history.service.d.ts.map +1 -1
- package/dist/lib/modules/punch-marketing-history/services/punch-marketing-history.service.js +5 -4
- package/dist/lib/modules/punch-marketing-history/services/punch-marketing-history.service.js.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/custom-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/patterns/repository/CouponRepository.d.ts +0 -1
- package/dist/types/modules/coupon/services/patterns/repository/CouponRepository.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/referral-code-coupon.service.d.ts.map +1 -1
- package/dist/types/modules/coupon/services/referral-reward-coupon.service.d.ts.map +1 -1
- package/dist/types/modules/marketing/validations/buy1-get1-marketing.validation.d.ts.map +1 -1
- package/dist/types/modules/punch-marketing-history/services/punch-marketing-history.service.d.ts.map +1 -1
- package/package.json +2 -2
|
@@ -2,6 +2,7 @@ import { DocumentType, mongoose } from '@typegoose/typegoose';
|
|
|
2
2
|
import { Coupon as Model } from '@lyxa.ai/core/dist/libraries/mongo/models';
|
|
3
3
|
import { AllCouponsValidationDTO, CouponValidationOutputDTO, ValidateCouponDTO, AllCouponsDTO } from '../../coupon/validations';
|
|
4
4
|
import { FilterDTO, PaginatedResponse, DeleteDTO, ShareableLinkDTO } from '@lyxa.ai/core/dist/utilities/validation';
|
|
5
|
+
import { ModelType } from '@typegoose/typegoose/lib/types';
|
|
5
6
|
export declare class CouponService {
|
|
6
7
|
private model;
|
|
7
8
|
private userModel;
|
|
@@ -10,7 +11,7 @@ export declare class CouponService {
|
|
|
10
11
|
constructor();
|
|
11
12
|
create(data: AllCouponsValidationDTO): Promise<DocumentType<Model>>;
|
|
12
13
|
findById(id: string | mongoose.Types.ObjectId): Promise<DocumentType<Model>>;
|
|
13
|
-
find(data?: Partial<FilterDTO>): Promise<PaginatedResponse<
|
|
14
|
+
find(data?: Partial<FilterDTO>): Promise<PaginatedResponse<DocumentType<ModelType<Model>>>>;
|
|
14
15
|
update(id: string, data: AllCouponsValidationDTO): Promise<DocumentType<Model>>;
|
|
15
16
|
deleteById(data: DeleteDTO): Promise<string>;
|
|
16
17
|
getUserCoupons(user: mongoose.Types.ObjectId | undefined, page?: number, size?: number): Promise<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAgC,MAAM,sBAAsB,CAAC;AAC5F,OAAO,EAMN,MAAM,IAAI,KAAK,EAOf,MAAM,2CAA2C,CAAC;AACnD,OAAO,EACN,uBAAuB,EAIvB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,MAAM,6BAA6B,CAAC;AAErC,OAAO,EACN,SAAS,EACT,iBAAiB,EACjB,SAAS,EACT,gBAAgB,EAChB,MAAM,yCAAyC,CAAC;
|
|
1
|
+
{"version":3,"file":"coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAgC,MAAM,sBAAsB,CAAC;AAC5F,OAAO,EAMN,MAAM,IAAI,KAAK,EAOf,MAAM,2CAA2C,CAAC;AACnD,OAAO,EACN,uBAAuB,EAIvB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CAAC;KACpC,CAAC;IAKW,cAAc,CAAC,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,yBAAyB,CAAC;IAkExF,OAAO,CAAC,eAAe;YAeT,kBAAkB;YAiBlB,cAAc;YAQd,sBAAsB;YAUtB,kBAAkB;YA8ClB,gBAAgB;YAWhB,uBAAuB;YAqCvB,kBAAkB;YAelB,0BAA0B;YAW1B,oBAAoB;YAepB,4BAA4B;YAW5B,YAAY;IAK1B,OAAO,CAAC,eAAe;IAOV,eAAe,CAAC,IAAI,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;YA8B3E,2BAA2B;YAoB3B,6BAA6B;IASpC,gBAAgB,GAAU,MAAM,MAAM,KAAG,OAAO,CAAC,gBAAgB,CAAC,CAUvE;IAEW,iBAAiB,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAC;CAsHpG;AAED,eAAO,MAAM,aAAa,eAA+B,CAAC"}
|
|
@@ -158,70 +158,17 @@ let CouponService = class CouponService {
|
|
|
158
158
|
}
|
|
159
159
|
async find(data) {
|
|
160
160
|
try {
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
createdAt: 1,
|
|
161
|
+
const paginate = (0, pagination_1.createRawDocumentPaginatorObject)(this.model);
|
|
162
|
+
const options = {
|
|
163
|
+
page: data?.page,
|
|
164
|
+
size: data?.size,
|
|
165
|
+
sort: data?.sort,
|
|
166
|
+
query: { ...data?.query, updatedVersion: { $exists: false } },
|
|
167
|
+
select: data?.select,
|
|
168
|
+
populate: data?.populate,
|
|
170
169
|
};
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (query.status === 'expired') {
|
|
174
|
-
isExpiredField = true;
|
|
175
|
-
delete query.status;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
const pipeline = [
|
|
179
|
-
{ $match: { ...query, deletedAt: null } },
|
|
180
|
-
{
|
|
181
|
-
$lookup: {
|
|
182
|
-
from: 'admins',
|
|
183
|
-
localField: 'createdBy',
|
|
184
|
-
foreignField: '_id',
|
|
185
|
-
as: 'createdBy',
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
{ $unwind: { path: '$createdBy', preserveNullAndEmptyArrays: true } },
|
|
189
|
-
{
|
|
190
|
-
$project: select,
|
|
191
|
-
},
|
|
192
|
-
{
|
|
193
|
-
$addFields: {
|
|
194
|
-
isExpired: {
|
|
195
|
-
$cond: {
|
|
196
|
-
if: {
|
|
197
|
-
$or: [{ $lt: ['$duration.end', new Date()] }, { $ifNull: ['$expirationReason', false] }],
|
|
198
|
-
},
|
|
199
|
-
then: true,
|
|
200
|
-
else: false,
|
|
201
|
-
},
|
|
202
|
-
},
|
|
203
|
-
},
|
|
204
|
-
},
|
|
205
|
-
{
|
|
206
|
-
$match: isExpiredField
|
|
207
|
-
? { isExpired: true }
|
|
208
|
-
: query.status === 'active'
|
|
209
|
-
? { isExpired: false }
|
|
210
|
-
: {},
|
|
211
|
-
},
|
|
212
|
-
];
|
|
213
|
-
if (data?.search?.searchKey) {
|
|
214
|
-
const searchKey = (0, normalize_1.normalizeName)(data.search.searchKey);
|
|
215
|
-
const regex = new RegExp(searchKey, 'i');
|
|
216
|
-
const fields = data.search.searchFields?.length ? data.search.searchFields : ['code'];
|
|
217
|
-
pipeline.push({
|
|
218
|
-
$match: {
|
|
219
|
-
$or: fields.map(field => ({ [field]: { $regex: regex } })),
|
|
220
|
-
},
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
const results = await (0, pagination_1.paginateAggregation)(this.model, pipeline, data?.page, data?.size, data?.sort);
|
|
224
|
-
return results;
|
|
170
|
+
const paginatedResult = await paginate(options);
|
|
171
|
+
return paginatedResult;
|
|
225
172
|
}
|
|
226
173
|
catch (err) {
|
|
227
174
|
throw new server_1.TRPCError({
|
|
@@ -600,7 +547,7 @@ let CouponService = class CouponService {
|
|
|
600
547
|
try {
|
|
601
548
|
let query = data?.query || {};
|
|
602
549
|
if (!query.user) {
|
|
603
|
-
(0, error_common_1.badRequestError)(
|
|
550
|
+
(0, error_common_1.badRequestError)('User ID is required in query');
|
|
604
551
|
}
|
|
605
552
|
const select = data?.select || {
|
|
606
553
|
_id: 1,
|
|
@@ -618,6 +565,20 @@ let CouponService = class CouponService {
|
|
|
618
565
|
delete query.status;
|
|
619
566
|
}
|
|
620
567
|
}
|
|
568
|
+
if (query.createdAt) {
|
|
569
|
+
if ("$gte" in query.createdAt) {
|
|
570
|
+
query.createdAt.$gte = new Date(query.createdAt.$gte);
|
|
571
|
+
}
|
|
572
|
+
if ("$lte" in query.createdAt) {
|
|
573
|
+
query.createdAt.$lte = new Date(query.createdAt.$lte);
|
|
574
|
+
}
|
|
575
|
+
if ("$gt" in query.createdAt) {
|
|
576
|
+
query.createdAt.$gt = new Date(query.createdAt.$gt);
|
|
577
|
+
}
|
|
578
|
+
if ("$lt" in query.createdAt) {
|
|
579
|
+
query.createdAt.$lt = new Date(query.createdAt.$lt);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
621
582
|
const user = await this.userModel.findById(query.user).select('coupons');
|
|
622
583
|
query._id = { $in: user?.coupons ?? [] };
|
|
623
584
|
delete query.user;
|
|
@@ -632,9 +593,6 @@ let CouponService = class CouponService {
|
|
|
632
593
|
},
|
|
633
594
|
},
|
|
634
595
|
{ $unwind: { path: '$createdBy', preserveNullAndEmptyArrays: true } },
|
|
635
|
-
{
|
|
636
|
-
$project: select,
|
|
637
|
-
},
|
|
638
596
|
{
|
|
639
597
|
$addFields: {
|
|
640
598
|
isExpired: {
|
|
@@ -655,6 +613,20 @@ let CouponService = class CouponService {
|
|
|
655
613
|
? { isExpired: false }
|
|
656
614
|
: {},
|
|
657
615
|
},
|
|
616
|
+
{
|
|
617
|
+
$addFields: {
|
|
618
|
+
status: {
|
|
619
|
+
$cond: {
|
|
620
|
+
if: { $eq: ['$isExpired', true] },
|
|
621
|
+
then: 'expired',
|
|
622
|
+
else: '$status',
|
|
623
|
+
},
|
|
624
|
+
},
|
|
625
|
+
},
|
|
626
|
+
},
|
|
627
|
+
{
|
|
628
|
+
$project: select,
|
|
629
|
+
},
|
|
658
630
|
];
|
|
659
631
|
if (data?.search?.searchKey) {
|
|
660
632
|
const searchKey = (0, normalize_1.normalizeName)(data.search.searchKey);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coupon.service.js","sourceRoot":"/","sources":["modules/coupon/services/coupon.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AAEzC,sEAamD;AAiBnD,wEAIiD;AAEjD,wBAOW;AACX,4DAA2G;AAE3G,wEAAyF;AACzF,wCAA6C;AAE7C,6EAA0E;AAC1E,sEAAuE;AACvE,4EAA4E;AAGrE,IAAM,aAAa,GAAnB,MAAM,aAAa;IACjB,KAAK,CAA8C;IACnD,SAAS,CAA2C;IACpD,UAAU,CAA2D;IACrE,mBAAmB,CAA2E;IAEtG;QACC,IAAI,CAAC,KAAK,GAAG,oBAA0D,CAAC;QACxE,IAAI,CAAC,SAAS,GAAG,kBAAqD,CAAC;QACvE,IAAI,CAAC,UAAU,GAAG,0BAA6E,CAAC;QAChG,IAAI,CAAC,mBAAmB,GAAG,kCACW,CAAC;IACxC,CAAC;IAMM,KAAK,CAAC,MAAM,CAAC,IAA6B;QAChD,IAAI,MAA2B,CAAC;QAEhC,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;YAC5C,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,SAAS,EAAE,IAAI;SACf,CAAC,CAAC;QAEH,IAAI,WAAW,EAAE,CAAC;YACjB,MAAM,IAAI,kBAAS,CAAC;gB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,CAAC,CAAC,MAAM,CAAC,CAAC;gBAEtF,QAAQ,CAAC,IAAI,CAAC;oBACb,MAAM,EAAE;wBACP,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;qBAC1D;iBACD,CAAC,CAAC;YACJ,CAAC;YAGD,MAAM,OAAO,GAAG,MAAM,IAAA,gCAAmB,EAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;YAEpG,OAAO,OAAsD,CAAC;QAC/D,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,MAAM,IAAI,kBAAS,CAAC;gBACnB,IAAI,EAAE,uBAAuB;gBAC7B,OAAO,EAAE,QAAQ,GAAG,EAAE;aACtB,CAAC,CAAC;QACJ,CAAC;IACF,CAAC;CACD,CAAA;AAl0BY,sCAAa;wBAAb,aAAa;IADzB,IAAA,gBAAO,GAAE;;GACG,aAAa,CAk0BzB;AAEY,QAAA,aAAa,GAAG,gBAAS,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType, mongoose, queryMethod, ReturnModelType } from '@typegoose/typegoose';\nimport {\n\tCouponModel,\n\tCustomCoupon,\n\tGlobalCoupon,\n\tIndividualStoreCoupon,\n\tIndividualUserCoupon,\n\tCoupon as Model,\n\tReferralRewardCoupon,\n\tReferralRewardCouponModel,\n\tRegularOrder,\n\tRegularOrderModel,\n\tUser,\n\tUserModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport {\n\tAllCouponsValidationDTO,\n\tAllCouponsUpdateDTO,\n\tGetUserCouponDTO,\n\tcouponFields,\n\tCouponValidationOutputDTO,\n\tValidateCouponDTO,\n\tAllCouponsDTO,\n} from '@modules/coupon/validations';\nimport { SoftDeleteModel } from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport {\n\tFilterDTO,\n\tPaginatedResponse,\n\tDeleteDTO,\n\tShareableLinkDTO,\n} from '@lyxa.ai/core/dist/utilities/validation';\nimport {\n\tcreateRawDocumentPaginatorObject,\n\tpaginateAggregation,\n\tPaginatorOptions,\n} from '@lyxa.ai/core/dist/utilities/pagination';\nimport { ModelType } from '@typegoose/typegoose/lib/types';\nimport {\n\tcustomCouponService,\n\tglobalCouponService,\n\tindividualStoreCouponService,\n\tindividualUserCouponService,\n\treferralCodeCouponService,\n\treferralRewardCouponService,\n} from '.';\nimport { CouponExpirationReason, CouponType, RegularOrderStatus } from '@lyxa.ai/core/dist/utilities/enum';\nimport { BaseCoupon } from '@lyxa.ai/core/dist/libraries/mongo/models/base/base-coupon.model';\nimport { punchMarketingHistoryService } from '@modules/punch-marketing-history/services';\nimport { getLibraries } from '@lyxa.ai/core';\nimport { NotificationMicroserviceRouter } from '@lyxa.ai/notification';\nimport { CouponRepository } from './patterns/repository/CouponRepository';\nimport { normalizeName } from '@lyxa.ai/core/dist/utilities/normalize';\nimport { badRequestError } from '@lyxa.ai/core/dist/utilities/error-common';\n\n@Service()\nexport class CouponService {\n\tprivate model: typeof CouponModel & SoftDeleteModel<Model>;\n\tprivate userModel: typeof UserModel & SoftDeleteModel<User>;\n\tprivate orderModel: typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\tprivate referralRewardModel: typeof ReferralRewardCouponModel & SoftDeleteModel<ReferralRewardCoupon>;\n\n\tconstructor() {\n\t\tthis.model = CouponModel as typeof CouponModel & SoftDeleteModel<Model>;\n\t\tthis.userModel = UserModel as typeof UserModel & SoftDeleteModel<User>;\n\t\tthis.orderModel = RegularOrderModel as typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\t\tthis.referralRewardModel = ReferralRewardCouponModel as typeof ReferralRewardCouponModel &\n\t\t\tSoftDeleteModel<ReferralRewardCoupon>;\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: AllCouponsValidationDTO): Promise<DocumentType<Model>> {\n\t\tlet coupon: DocumentType<Model>;\n\n\t\tconst checkCoupon = await this.model.findOne({\n\t\t\tcode: data.code,\n\t\t\tdeletedAt: null,\n\t\t});\n\n\t\tif (checkCoupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'CONFLICT',\n\t\t\t\tmessage: 'Coupon code already exists',\n\t\t\t});\n\t\t}\n\n\t\tswitch (data.couponType) {\n\t\t\tcase CouponType.GLOBAL: {\n\t\t\t\tcoupon = await globalCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst globalCoupon = coupon as DocumentType<GlobalCoupon>;\n\t\t\t\t\tif (!globalCoupon.forNewUserOnly) {\n\t\t\t\t\t\tawait this.assignCouponToMultipleUser({}, coupon._id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase CouponType.REFERRAL_CODE: {\n\t\t\t\tcoupon = await referralCodeCouponService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase CouponType.REFERRAL_REWARD: {\n\t\t\t\tcoupon = await referralRewardCouponService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase CouponType.INDIVIDUAL_STORE: {\n\t\t\t\tcoupon = await individualStoreCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst individualStoreCoupon = coupon as DocumentType<IndividualStoreCoupon>;\n\t\t\t\t\tif (!individualStoreCoupon.forNewUserOnly) {\n\t\t\t\t\t\tawait this.assignCouponToMultipleUser({}, coupon._id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase CouponType.INDIVIDUAL_USER: {\n\t\t\t\tcoupon = await individualUserCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst individualUserCoupon = coupon as DocumentType<IndividualUserCoupon>;\n\t\t\t\t\tconst users = individualUserCoupon.users;\n\t\t\t\t\tawait this.assignCouponToMultipleUser({ _id: { $in: users } }, coupon._id);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase CouponType.CUSTOM_COUPON: {\n\t\t\t\tcoupon = await customCouponService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'BAD_REQUEST',\n\t\t\t\t\tmessage: 'Coupon type is not valid',\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (!coupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'INTERNAL_SERVER_ERROR',\n\t\t\t\tmessage: 'Could not create coupon',\n\t\t\t});\n\t\t}\n\n\t\t// Send Notification\n\t\tconst dispatcher = getLibraries().getMicroServiceDispatcher();\n\t\tconst notificationRouter =\n\t\t\tawait dispatcher.getMicroService<NotificationMicroserviceRouter>('notification');\n\n\t\tawait notificationRouter.notificationRouter.sendPushNotificationForCoupon.mutate(coupon._id);\n\n\t\treturn coupon;\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(id: string | mongoose.Types.ObjectId): Promise<DocumentType<Model>> {\n\t\tconst result = await this.model\n\t\t\t.findOne({\n\t\t\t\t_id: id,\n\t\t\t\tdeletedAt: null,\n\t\t\t})\n\t\t\t.populate([\n\t\t\t\t{\n\t\t\t\t\tpath: 'previousVersions',\n\t\t\t\t},\n\t\t\t]);\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<AllCouponsDTO>> {\n\t\ttry {\n\t\t\tlet query = data?.query || {};\n\t\t\tconst select = data?.select || {\n\t\t\t\t_id: 1,\n\t\t\t\t'createdBy.name': 1,\n\t\t\t\t'duration.start': 1,\n\t\t\t\t'duration.end': 1,\n\t\t\t\tcode: 1,\n\t\t\t\tstatus: 1,\n\t\t\t\tcreatedAt: 1,\n\t\t\t};\n\n\t\t\tlet isExpiredField = null;\n\n\t\t\tif (query.status) {\n\t\t\t\tif (query.status === 'expired') {\n\t\t\t\t\tisExpiredField = true;\n\t\t\t\t\tdelete query.status;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst pipeline: any[] = [\n\t\t\t\t{ $match: { ...query, deletedAt: null } }, // Filter by search query\n\t\t\t\t{\n\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t// INNER JOIN on categories\n\t\t\t\t\t\tfrom: 'admins', // Mongoose auto-pluralizes collection names\n\t\t\t\t\t\tlocalField: 'createdBy',\n\t\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\t\tas: 'createdBy',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $unwind: { path: '$createdBy', preserveNullAndEmptyArrays: true } }, // Flatten the array\n\t\t\t\t{\n\t\t\t\t\t$project: select,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$addFields: {\n\t\t\t\t\t\tisExpired: {\n\t\t\t\t\t\t\t$cond: {\n\t\t\t\t\t\t\t\tif: {\n\t\t\t\t\t\t\t\t\t$or: [{ $lt: ['$duration.end', new Date()] }, { $ifNull: ['$expirationReason', false] }],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tthen: true,\n\t\t\t\t\t\t\t\telse: false,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$match: isExpiredField\n\t\t\t\t\t\t? { isExpired: true }\n\t\t\t\t\t\t: query.status === 'active'\n\t\t\t\t\t\t\t? { isExpired: false }\n\t\t\t\t\t\t\t: {},\n\t\t\t\t},\n\t\t\t];\n\n\t\t\tif (data?.search?.searchKey) {\n\t\t\t\tconst searchKey = normalizeName(data.search.searchKey);\n\t\t\t\tconst regex = new RegExp(searchKey, 'i'); // case-insensitive search\n\t\t\t\tconst fields = data.search.searchFields?.length ? data.search.searchFields : ['code']; // default field(s) to search in\n\n\t\t\t\tpipeline.push({\n\t\t\t\t\t$match: {\n\t\t\t\t\t\t$or: fields.map(field => ({ [field]: { $regex: regex } })),\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// Execute the aggregation with pagination and sorting\n\t\t\tconst results = await paginateAggregation(this.model, pipeline, data?.page, data?.size, data?.sort);\n\n\t\t\treturn results as unknown as PaginatedResponse<AllCouponsDTO>;\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 old coupon\n\t * @param data Data of the updated coupon\n\t */\n\tpublic async update(id: string, data: AllCouponsValidationDTO): Promise<DocumentType<Model>> {\n\t\tlet coupon: DocumentType<Model> | null;\n\n\t\tconst [oldCoupon, oldOrders, checkCouponCode] = await Promise.all([\n\t\t\tthis.model.findById(id),\n\t\t\tthis.orderModel\n\t\t\t\t.find({\n\t\t\t\t\t'finance.coupon.coupon': id,\n\t\t\t\t})\n\t\t\t\t.select(['user', 'finance']),\n\t\t\tthis.model.findOne({\n\t\t\t\tcode: data.code,\n\t\t\t\t_id: { $ne: id },\n\t\t\t}),\n\t\t]);\n\n\t\tif (!oldCoupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: `Coupon not found`,\n\t\t\t});\n\t\t}\n\n\t\tif (checkCouponCode) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'CONFLICT',\n\t\t\t\tmessage: `Coupon code already exists`,\n\t\t\t});\n\t\t}\n\n\t\tconst oldCouponUsers = oldOrders.map(order => order.user);\n\n\t\tswitch (data.couponType) {\n\t\t\tcase CouponType.GLOBAL: {\n\t\t\t\tcoupon = await globalCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst globalCoupon = coupon as DocumentType<GlobalCoupon>;\n\t\t\t\t\tif (!globalCoupon.forNewUserOnly) {\n\t\t\t\t\t\tawait this.exchangeCouponForMultipleUser(\n\t\t\t\t\t\t\t{ _id: { $nin: oldCouponUsers } },\n\t\t\t\t\t\t\toldCoupon._id,\n\t\t\t\t\t\t\tcoupon._id\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tawait this.removeCouponFromMultipleUser({ _id: { $nin: oldCouponUsers } }, oldCoupon._id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase CouponType.INDIVIDUAL_STORE: {\n\t\t\t\tcoupon = await individualStoreCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst individualStoreCoupon = coupon as DocumentType<IndividualStoreCoupon>;\n\t\t\t\t\tif (!individualStoreCoupon.forNewUserOnly) {\n\t\t\t\t\t\tawait this.exchangeCouponForMultipleUser(\n\t\t\t\t\t\t\t{ _id: { $nin: oldCouponUsers } },\n\t\t\t\t\t\t\toldCoupon._id,\n\t\t\t\t\t\t\tcoupon._id\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tawait this.removeCouponFromMultipleUser({ _id: { $nin: oldCouponUsers } }, oldCoupon._id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase CouponType.INDIVIDUAL_USER: {\n\t\t\t\tcoupon = await individualUserCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst individualUserCoupon = coupon as DocumentType<IndividualUserCoupon>;\n\t\t\t\t\tawait this.removeCouponFromMultipleUser({ _id: { $nin: oldCouponUsers } }, oldCoupon._id);\n\t\t\t\t\tif (!individualUserCoupon.forNewUserOnly) {\n\t\t\t\t\t\tawait this.assignCouponToMultipleUser(\n\t\t\t\t\t\t\t{ _id: { $in: individualUserCoupon.users } },\n\t\t\t\t\t\t\toldCoupon._id\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase CouponType.CUSTOM_COUPON: {\n\t\t\t\tcoupon = await customCouponService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'BAD_REQUEST',\n\t\t\t\t\tmessage: `Invalid coupon type`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (!coupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'BAD_REQUEST',\n\t\t\t\tmessage: `Could create updated coupon`,\n\t\t\t});\n\t\t}\n\n\t\t// add previous version to new coupon\n\t\tconst previousVersions = oldCoupon?.previousVersions || [];\n\t\tpreviousVersions.push(oldCoupon._id);\n\n\t\tconst updatedCoupon = await this.model.findByIdAndUpdate(\n\t\t\tcoupon._id,\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tpreviousVersions,\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\tif (!updatedCoupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'INTERNAL_SERVER_ERROR',\n\t\t\t\tmessage: `Could not set previous versions of coupon`,\n\t\t\t});\n\t\t}\n\t\t// update old coupon\n\t\tconst updatedOldCoupon = await this.model.findByIdAndUpdate(\n\t\t\toldCoupon._id,\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tupdatedVersion: coupon._id,\n\t\t\t\t},\n\t\t\t},\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\treturn updatedCoupon;\n\t}\n\n\t/**\n\t * Delete record\n\t * @param data Data for delete\n\t */\n\tpublic async deleteById(data: DeleteDTO): Promise<string> {\n\t\tconst { _id, softDelete } = data;\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: 'NOT_FOUND',\n\t\t\t\tmessage: 'Record not found',\n\t\t\t});\n\t\t}\n\n\t\t// remove coupons from users' coupon lists\n\t\tthis.removeCouponFromMultipleUser({}, _id);\n\n\t\treturn 'Record deleted successfully';\n\t}\n\n\t/**\n\t * Get User Coupons\n\t * @param data User and pagination data\n\t */\n\tpublic async getUserCoupons(\n\t\tuser: mongoose.Types.ObjectId | undefined,\n\t\tpage: number = 0,\n\t\tsize: number = 0\n\t): Promise<{\n\t\tavailable: Array<DocumentType<Model>>;\n\t\tused: Array<DocumentType<Model>>;\n\t\texpired: Array<DocumentType<Model>>;\n\t}> {\n\t\tconst couponRepository = new CouponRepository();\n\t\treturn await couponRepository.getUserCoupons(user, page, size);\n\t}\n\n\tpublic async validateCoupon(data: ValidateCouponDTO): Promise<CouponValidationOutputDTO> {\n\t\tif (!data.coupon) {\n\t\t\treturn await punchMarketingHistoryService.getPunchDiscount(data);\n\t\t}\n\n\t\tconst [user, coupon] = await Promise.all([\n\t\t\tthis.userModel.findById(data.user),\n\t\t\tthis.model.findOne({ code: data.coupon }),\n\t\t]);\n\n\t\tif (!user) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'User not found',\n\t\t\t});\n\t\t}\n\n\t\tif (!coupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'Coupon not found',\n\t\t\t});\n\t\t}\n\n\t\tif (coupon.couponType == CouponType.REFERRAL_CODE) {\n\t\t\treturn await referralCodeCouponService.validateCoupon(data);\n\t\t}\n\n\t\tconst baseCoupon = coupon as DocumentType<BaseCoupon>;\n\n\t\tconst isValid = await this.validateBaseCoupon(baseCoupon, user._id, data.amountBeforeCoupon);\n\n\t\tif (!isValid) {\n\t\t\treturn {\n\t\t\t\tisValid,\n\t\t\t};\n\t\t}\n\n\t\tswitch (coupon.couponType) {\n\t\t\tcase CouponType.REFERRAL_REWARD:\n\t\t\t\treturn await referralRewardCouponService.validateCoupon(\n\t\t\t\t\tdata,\n\t\t\t\t\tcoupon as DocumentType<ReferralRewardCoupon>\n\t\t\t\t);\n\t\t\tcase CouponType.GLOBAL:\n\t\t\t\treturn await globalCouponService.validateCoupon(data, coupon as DocumentType<GlobalCoupon>);\n\t\t\tcase CouponType.INDIVIDUAL_STORE:\n\t\t\t\treturn await individualStoreCouponService.validateCoupon(\n\t\t\t\t\tdata,\n\t\t\t\t\tcoupon as DocumentType<IndividualStoreCoupon>\n\t\t\t\t);\n\t\t\tcase CouponType.INDIVIDUAL_USER:\n\t\t\t\treturn await individualUserCouponService.validateCoupon(\n\t\t\t\t\tdata,\n\t\t\t\t\tcoupon as DocumentType<IndividualUserCoupon>\n\t\t\t\t);\n\t\t\tcase CouponType.CUSTOM_COUPON:\n\t\t\t\treturn await customCouponService.validateCoupon(data, coupon as DocumentType<CustomCoupon>);\n\t\t\tdefault:\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'NOT_IMPLEMENTED',\n\t\t\t\t\tmessage: `Validation not implemented for ${coupon.couponType} coupons`,\n\t\t\t\t});\n\t\t}\n\t}\n\n\tprivate isCouponExpired(coupon: DocumentType<BaseCoupon>): Boolean {\n\t\t// TODO: Add an event listener to ensure the coupon expiration reason is updated after an order is placed\n\t\tif (coupon.expirationReason || (coupon?.duration?.end && new Date(coupon?.duration?.end) < new Date())) {\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t *\n\t * @param coupon Base coupon document\n\t * @param user ID of the user trying to user the coupon\n\t * @param orderAmount Order subtotal\n\t * @returns True if coupon is valid\n\t */\n\tprivate async validateBaseCoupon(\n\t\tcoupon: DocumentType<BaseCoupon>,\n\t\tuser: mongoose.Types.ObjectId,\n\t\torderAmount: number\n\t): Promise<boolean> {\n\t\tconst invalidConditions = [\n\t\t\tthis.isCouponExpired(coupon), // check coupon expired\n\t\t\tcoupon.duration.start > new Date(), // check coupon start\n\t\t\tcoupon.minOrderValue && orderAmount < coupon.minOrderValue, // check minimum order value\n\t\t\tcoupon.forNewUserOnly && !(await this.checkIfNewUser(user)), // check if new user\n\t\t\tcoupon.orderLimitPerUser &&\n\t\t\t\t(await this.getCouponOrdersForUser(user, coupon._id)) >= coupon.orderLimitPerUser, // check order limit for user\n\t\t];\n\n\t\treturn invalidConditions.every(value => value == false);\n\t}\n\n\tprivate async checkIfNewUser(user: mongoose.Types.ObjectId): Promise<boolean> {\n\t\tconst order = await this.orderModel.findOne({\n\t\t\tuser,\n\t\t\tstatus: { ne: RegularOrderStatus.CANCELLED },\n\t\t});\n\t\treturn order ? false : true;\n\t}\n\n\tprivate async getCouponOrdersForUser(\n\t\tuser: mongoose.Types.ObjectId,\n\t\tcoupon: mongoose.Types.ObjectId\n\t): Promise<number> {\n\t\treturn await this.orderModel.countDocuments({\n\t\t\tstatus: { $ne: RegularOrderStatus.CANCELLED },\n\t\t\t'finance.coupon.coupon': coupon,\n\t\t});\n\t}\n\n\tprivate async updateCouponExpiry(coupon: DocumentType<BaseCoupon>) {\n\t\tif (coupon.totalOrderLimit) {\n\t\t\t// total order limit\n\t\t\tconst orderCount = await this.orderModel.countDocuments({\n\t\t\t\tstatus: { $ne: RegularOrderStatus.CANCELLED },\n\t\t\t\t'finance.coupon.coupon': coupon._id,\n\t\t\t});\n\n\t\t\tif (orderCount > coupon.totalOrderLimit) {\n\t\t\t\tawait this.model.findByIdAndUpdate(\n\t\t\t\t\tcoupon._id,\n\t\t\t\t\t{\n\t\t\t\t\t\t$set: {\n\t\t\t\t\t\t\texpirationReason: CouponExpirationReason.TOTAL_ORDER_LIMIT,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\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\t\tif (coupon.spendLimit) {\n\t\t\t// spend limit\n\n\t\t\tconst { totalCouponDiscount, totalSecondaryCouponDiscount } = await this.getCouponDiscountTotals(\n\t\t\t\tcoupon._id\n\t\t\t);\n\n\t\t\tif (totalCouponDiscount >= coupon.spendLimit) {\n\t\t\t\tawait this.model.findByIdAndUpdate(\n\t\t\t\t\tcoupon._id,\n\t\t\t\t\t{\n\t\t\t\t\t\t$set: {\n\t\t\t\t\t\t\texpirationReason: CouponExpirationReason.SPEND_LIMIT,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\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\t}\n\tprivate async findCouponByCode(code: string): Promise<DocumentType<Model> | null> {\n\t\tconst coupon = await this.model.findOne({ code: code });\n\t\tif (!coupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'Coupon not found',\n\t\t\t});\n\t\t}\n\t\treturn coupon;\n\t}\n\n\tprivate async getCouponDiscountTotals(coupon: mongoose.Types.ObjectId) {\n\t\tconst pipeline = [\n\t\t\t{\n\t\t\t\t$match: {\n\t\t\t\t\tstatus: { $ne: RegularOrderStatus.CANCELLED },\n\t\t\t\t\t'finance.coupon.coupon': coupon,\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: null,\n\t\t\t\t\ttotalCouponDiscount: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$toDouble: {\n\t\t\t\t\t\t\t\t$ifNull: ['$finance.pricing.couponDiscount', 0],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\ttotalSecondaryCouponDiscount: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$toDouble: {\n\t\t\t\t\t\t\t\t$ifNull: ['$finance.pricing.secondaryCouponDiscount', 0],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\n\t\tconst result = await this.orderModel.aggregate(pipeline);\n\n\t\treturn {\n\t\t\ttotalCouponDiscount: result[0]?.totalCouponDiscount ?? 0,\n\t\t\ttotalSecondaryCouponDiscount: result[0]?.totalSecondaryCouponDiscount ?? 0,\n\t\t};\n\t}\n\n\tprivate async assignCouponToUser(user: mongoose.Types.ObjectId, coupon: mongoose.Types.ObjectId) {\n\t\tconst updatedUser = await this.userModel.findByIdAndUpdate(\n\t\t\tuser,\n\t\t\t{\n\t\t\t\t$push: {\n\t\t\t\t\tcoupons: coupon,\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\t}\n\n\tprivate async assignCouponToMultipleUser(\n\t\tquery: mongoose.FilterQuery<User>,\n\t\tcoupon: mongoose.Types.ObjectId\n\t) {\n\t\tconst updatedUsers = await this.userModel.updateMany(query, {\n\t\t\t$push: {\n\t\t\t\tcoupons: coupon,\n\t\t\t},\n\t\t});\n\t}\n\n\tprivate async removeCouponFromUser(user: mongoose.Types.ObjectId, coupon: mongoose.Types.ObjectId) {\n\t\tconst updatedUser = await this.userModel.findByIdAndUpdate(\n\t\t\tuser,\n\t\t\t{\n\t\t\t\t$pull: {\n\t\t\t\t\tcoupons: coupon,\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\t}\n\n\tprivate async removeCouponFromMultipleUser(\n\t\tquery: mongoose.FilterQuery<User>,\n\t\tcoupon: mongoose.Types.ObjectId\n\t) {\n\t\tconst updatedUsers = await this.userModel.updateMany(query, {\n\t\t\t$pull: {\n\t\t\t\tcoupons: coupon,\n\t\t\t},\n\t\t});\n\t}\n\n\tprivate async newUserCheck(userId: mongoose.Types.ObjectId): Promise<boolean> {\n\t\tconst user = await this.userModel.findById(userId).select('orderCompleted');\n\t\treturn (user?.orderCompleted ?? 0) < 1;\n\t}\n\n\tprivate throwBadRequest(message: string): never {\n\t\tthrow new TRPCError({\n\t\t\tcode: 'BAD_REQUEST',\n\t\t\tmessage,\n\t\t});\n\t}\n\n\tpublic async addCouponToUser(user: any, couponCode: string): Promise<DocumentType<Model>> {\n\t\tconst coupon = await this.findCouponByCode(couponCode);\n\t\tif (!coupon) {\n\t\t\tthis.throwBadRequest('Coupon not found');\n\t\t}\n\n\t\tconst typedCoupon = coupon as DocumentType<BaseCoupon>;\n\n\t\tif (this.isCouponExpired(typedCoupon)) {\n\t\t\tthis.throwBadRequest('This coupon has expired and cannot be added');\n\t\t}\n\n\t\tconst isNewUser = await this.newUserCheck(user);\n\t\tif (typedCoupon.forNewUserOnly && !isNewUser) {\n\t\t\tthis.throwBadRequest('This coupon is valid for new users only');\n\t\t}\n\n\t\tconst alreadyApplied = await this.hasUserAlreadyAppliedCoupon(user, typedCoupon._id);\n\t\tif (alreadyApplied) {\n\t\t\tthis.throwBadRequest('Coupon already applied to user');\n\t\t}\n\t\tawait this.userModel.findByIdAndUpdate(\n\t\t\tuser,\n\t\t\t{ $push: { coupons: typedCoupon._id } },\n\t\t\t{ new: true, runValidators: true }\n\t\t);\n\n\t\treturn coupon;\n\t}\n\n\tprivate async hasUserAlreadyAppliedCoupon(\n\t\tuserId: mongoose.Types.ObjectId,\n\t\tcouponId: mongoose.Types.ObjectId\n\t): Promise<boolean> {\n\t\tconst user = await this.userModel.findById(userId).select('coupons');\n\t\tif (!user) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'User not found',\n\t\t\t});\n\t\t}\n\n\t\treturn (\n\t\t\tuser.coupons?.some((c: any) => {\n\t\t\t\tconst id = (c?._id ?? c)?.toString();\n\t\t\t\treturn id === couponId.toString();\n\t\t\t}) ?? false\n\t\t);\n\t}\n\n\tprivate async exchangeCouponForMultipleUser(\n\t\tquery: mongoose.FilterQuery<User>,\n\t\toldCoupon: mongoose.Types.ObjectId,\n\t\tnewCoupon: mongoose.Types.ObjectId\n\t) {\n\t\tawait this.removeCouponFromMultipleUser(query, oldCoupon);\n\t\tawait this.assignCouponToMultipleUser(query, newCoupon);\n\t}\n\n\tpublic getShareableLink = async (code: string): Promise<ShareableLinkDTO> => {\n\t\tconst secretsService = getLibraries().getSecretsService();\n\t\tconst deepLinkUrl = (await secretsService.getKey('DEEP_LINK_URL')) ?? 'https://link.lyxa.ai';\n\n\t\tconst link = `${deepLinkUrl}/invite/${code}`;\n\n\t\tconst message = `Download Lyxa and explore the wide variety of offerings and benefits! Use my code at checkout to claim a special coupon : ${code}.\n\t\t${link}`;\n\n\t\treturn { link, message };\n\t};\n\n\tpublic async getUserCouponList(data?: Partial<FilterDTO>): Promise<PaginatedResponse<AllCouponsDTO>> {\n\t\ttry {\n\t\t\tlet query = data?.query || {};\n\n\t\t\tif (!query.user) {\n\t\t\t\tbadRequestError(\"User ID is required in query\");\n\t\t\t}\n\n\t\t\tconst select = data?.select || {\n\t\t\t\t_id: 1,\n\t\t\t\t'createdBy.name': 1,\n\t\t\t\t'duration.start': 1,\n\t\t\t\t'duration.end': 1,\n\t\t\t\tcode: 1,\n\t\t\t\tstatus: 1,\n\t\t\t\tcreatedAt: 1,\n\t\t\t};\n\n\t\t\tlet isExpiredField = null;\n\n\t\t\tif (query.status) {\n\t\t\t\tif (query.status === 'expired') {\n\t\t\t\t\tisExpiredField = true;\n\t\t\t\t\tdelete query.status;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst user = await this.userModel.findById(query.user).select('coupons');\n\t\t\tquery._id = { $in: user?.coupons ?? [] };\n\t\t\tdelete query.user; // remove user from query to avoid issues in aggregation match stage\n\n\t\t\tconst pipeline: any[] = [\n\t\t\t\t{ $match: { ...query, deletedAt: null } }, // Filter by search query\n\t\t\t\t{\n\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t// INNER JOIN on categories\n\t\t\t\t\t\tfrom: 'admins', // Mongoose auto-pluralizes collection names\n\t\t\t\t\t\tlocalField: 'createdBy',\n\t\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\t\tas: 'createdBy',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $unwind: { path: '$createdBy', preserveNullAndEmptyArrays: true } }, // Flatten the array\n\t\t\t\t{\n\t\t\t\t\t$project: select,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$addFields: {\n\t\t\t\t\t\tisExpired: {\n\t\t\t\t\t\t\t$cond: {\n\t\t\t\t\t\t\t\tif: {\n\t\t\t\t\t\t\t\t\t$or: [{ $lt: ['$duration.end', new Date()] }, { $ifNull: ['$expirationReason', false] }],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tthen: true,\n\t\t\t\t\t\t\t\telse: false,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$match: isExpiredField\n\t\t\t\t\t\t? { isExpired: true }\n\t\t\t\t\t\t: query.status === 'active'\n\t\t\t\t\t\t\t? { isExpired: false }\n\t\t\t\t\t\t\t: {},\n\t\t\t\t},\n\t\t\t];\n\n\t\t\tif (data?.search?.searchKey) {\n\t\t\t\tconst searchKey = normalizeName(data.search.searchKey);\n\t\t\t\tconst regex = new RegExp(searchKey, 'i'); // case-insensitive search\n\t\t\t\tconst fields = data.search.searchFields?.length ? data.search.searchFields : ['code']; // default field(s) to search in\n\n\t\t\t\tpipeline.push({\n\t\t\t\t\t$match: {\n\t\t\t\t\t\t$or: fields.map(field => ({ [field]: { $regex: regex } })),\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// Execute the aggregation with pagination and sorting\n\t\t\tconst results = await paginateAggregation(this.model, pipeline, data?.page, data?.size, data?.sort);\n\n\t\t\treturn results as unknown as PaginatedResponse<AllCouponsDTO>;\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\nexport const couponService = Container.get(CouponService);\n"]}
|
|
1
|
+
{"version":3,"file":"coupon.service.js","sourceRoot":"/","sources":["modules/coupon/services/coupon.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AAEzC,sEAamD;AAiBnD,wEAIiD;AAEjD,wBAOW;AACX,4DAA2G;AAE3G,wEAAyF;AACzF,wCAA6C;AAE7C,6EAA0E;AAC1E,sEAAuE;AACvE,4EAA4E;AAGrE,IAAM,aAAa,GAAnB,MAAM,aAAa;IACjB,KAAK,CAA8C;IACnD,SAAS,CAA2C;IACpD,UAAU,CAA2D;IACrE,mBAAmB,CAA2E;IAEtG;QACC,IAAI,CAAC,KAAK,GAAG,oBAA0D,CAAC;QACxE,IAAI,CAAC,SAAS,GAAG,kBAAqD,CAAC;QACvE,IAAI,CAAC,UAAU,GAAG,0BAA6E,CAAC;QAChG,IAAI,CAAC,mBAAmB,GAAG,kCACW,CAAC;IACxC,CAAC;IAMM,KAAK,CAAC,MAAM,CAAC,IAA6B;QAChD,IAAI,MAA2B,CAAC;QAEhC,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;YAC5C,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,SAAS,EAAE,IAAI;SACf,CA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BzB;AAEY,QAAA,aAAa,GAAG,gBAAS,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType, mongoose, queryMethod, ReturnModelType } from '@typegoose/typegoose';\nimport {\n\tCouponModel,\n\tCustomCoupon,\n\tGlobalCoupon,\n\tIndividualStoreCoupon,\n\tIndividualUserCoupon,\n\tCoupon as Model,\n\tReferralRewardCoupon,\n\tReferralRewardCouponModel,\n\tRegularOrder,\n\tRegularOrderModel,\n\tUser,\n\tUserModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models';\nimport {\n\tAllCouponsValidationDTO,\n\tAllCouponsUpdateDTO,\n\tGetUserCouponDTO,\n\tcouponFields,\n\tCouponValidationOutputDTO,\n\tValidateCouponDTO,\n\tAllCouponsDTO,\n} from '@modules/coupon/validations';\nimport { SoftDeleteModel } from '@lyxa.ai/core/dist/libraries/mongo/plugins/soft-delete-plugin';\nimport {\n\tFilterDTO,\n\tPaginatedResponse,\n\tDeleteDTO,\n\tShareableLinkDTO,\n} from '@lyxa.ai/core/dist/utilities/validation';\nimport {\n\tcreateRawDocumentPaginatorObject,\n\tpaginateAggregation,\n\tPaginatorOptions,\n} from '@lyxa.ai/core/dist/utilities/pagination';\nimport { ModelType } from '@typegoose/typegoose/lib/types';\nimport {\n\tcustomCouponService,\n\tglobalCouponService,\n\tindividualStoreCouponService,\n\tindividualUserCouponService,\n\treferralCodeCouponService,\n\treferralRewardCouponService,\n} from '.';\nimport { CouponExpirationReason, CouponType, RegularOrderStatus } from '@lyxa.ai/core/dist/utilities/enum';\nimport { BaseCoupon } from '@lyxa.ai/core/dist/libraries/mongo/models/base/base-coupon.model';\nimport { punchMarketingHistoryService } from '@modules/punch-marketing-history/services';\nimport { getLibraries } from '@lyxa.ai/core';\nimport { NotificationMicroserviceRouter } from '@lyxa.ai/notification';\nimport { CouponRepository } from './patterns/repository/CouponRepository';\nimport { normalizeName } from '@lyxa.ai/core/dist/utilities/normalize';\nimport { badRequestError } from '@lyxa.ai/core/dist/utilities/error-common';\n\n@Service()\nexport class CouponService {\n\tprivate model: typeof CouponModel & SoftDeleteModel<Model>;\n\tprivate userModel: typeof UserModel & SoftDeleteModel<User>;\n\tprivate orderModel: typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\tprivate referralRewardModel: typeof ReferralRewardCouponModel & SoftDeleteModel<ReferralRewardCoupon>;\n\n\tconstructor() {\n\t\tthis.model = CouponModel as typeof CouponModel & SoftDeleteModel<Model>;\n\t\tthis.userModel = UserModel as typeof UserModel & SoftDeleteModel<User>;\n\t\tthis.orderModel = RegularOrderModel as typeof RegularOrderModel & SoftDeleteModel<RegularOrder>;\n\t\tthis.referralRewardModel = ReferralRewardCouponModel as typeof ReferralRewardCouponModel &\n\t\t\tSoftDeleteModel<ReferralRewardCoupon>;\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: AllCouponsValidationDTO): Promise<DocumentType<Model>> {\n\t\tlet coupon: DocumentType<Model>;\n\n\t\tconst checkCoupon = await this.model.findOne({\n\t\t\tcode: data.code,\n\t\t\tdeletedAt: null,\n\t\t});\n\n\t\tif (checkCoupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'CONFLICT',\n\t\t\t\tmessage: 'Coupon code already exists',\n\t\t\t});\n\t\t}\n\n\t\tswitch (data.couponType) {\n\t\t\tcase CouponType.GLOBAL: {\n\t\t\t\tcoupon = await globalCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst globalCoupon = coupon as DocumentType<GlobalCoupon>;\n\t\t\t\t\tif (!globalCoupon.forNewUserOnly) {\n\t\t\t\t\t\tawait this.assignCouponToMultipleUser({}, coupon._id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase CouponType.REFERRAL_CODE: {\n\t\t\t\tcoupon = await referralCodeCouponService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase CouponType.REFERRAL_REWARD: {\n\t\t\t\tcoupon = await referralRewardCouponService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase CouponType.INDIVIDUAL_STORE: {\n\t\t\t\tcoupon = await individualStoreCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst individualStoreCoupon = coupon as DocumentType<IndividualStoreCoupon>;\n\t\t\t\t\tif (!individualStoreCoupon.forNewUserOnly) {\n\t\t\t\t\t\tawait this.assignCouponToMultipleUser({}, coupon._id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase CouponType.INDIVIDUAL_USER: {\n\t\t\t\tcoupon = await individualUserCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst individualUserCoupon = coupon as DocumentType<IndividualUserCoupon>;\n\t\t\t\t\tconst users = individualUserCoupon.users;\n\t\t\t\t\tawait this.assignCouponToMultipleUser({ _id: { $in: users } }, coupon._id);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase CouponType.CUSTOM_COUPON: {\n\t\t\t\tcoupon = await customCouponService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'BAD_REQUEST',\n\t\t\t\t\tmessage: 'Coupon type is not valid',\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (!coupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'INTERNAL_SERVER_ERROR',\n\t\t\t\tmessage: 'Could not create coupon',\n\t\t\t});\n\t\t}\n\n\t\t// Send Notification\n\t\tconst dispatcher = getLibraries().getMicroServiceDispatcher();\n\t\tconst notificationRouter =\n\t\t\tawait dispatcher.getMicroService<NotificationMicroserviceRouter>('notification');\n\n\t\tawait notificationRouter.notificationRouter.sendPushNotificationForCoupon.mutate(coupon._id);\n\n\t\treturn coupon;\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(id: string | mongoose.Types.ObjectId): Promise<DocumentType<Model>> {\n\t\tconst result = await this.model\n\t\t\t.findOne({\n\t\t\t\t_id: id,\n\t\t\t\tdeletedAt: null,\n\t\t\t})\n\t\t\t.populate([\n\t\t\t\t{\n\t\t\t\t\tpath: 'previousVersions',\n\t\t\t\t},\n\t\t\t]);\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, updatedVersion: { $exists: false } },\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 old coupon\n\t * @param data Data of the updated coupon\n\t */\n\tpublic async update(id: string, data: AllCouponsValidationDTO): Promise<DocumentType<Model>> {\n\t\tlet coupon: DocumentType<Model> | null;\n\n\t\tconst [oldCoupon, oldOrders, checkCouponCode] = await Promise.all([\n\t\t\tthis.model.findById(id),\n\t\t\tthis.orderModel\n\t\t\t\t.find({\n\t\t\t\t\t'finance.coupon.coupon': id,\n\t\t\t\t})\n\t\t\t\t.select(['user', 'finance']),\n\t\t\tthis.model.findOne({\n\t\t\t\tcode: data.code,\n\t\t\t\t_id: { $ne: id },\n\t\t\t}),\n\t\t]);\n\n\t\tif (!oldCoupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: `Coupon not found`,\n\t\t\t});\n\t\t}\n\n\t\tif (checkCouponCode) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'CONFLICT',\n\t\t\t\tmessage: `Coupon code already exists`,\n\t\t\t});\n\t\t}\n\n\t\tconst oldCouponUsers = oldOrders.map(order => order.user);\n\n\t\tswitch (data.couponType) {\n\t\t\tcase CouponType.GLOBAL: {\n\t\t\t\tcoupon = await globalCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst globalCoupon = coupon as DocumentType<GlobalCoupon>;\n\t\t\t\t\tif (!globalCoupon.forNewUserOnly) {\n\t\t\t\t\t\tawait this.exchangeCouponForMultipleUser(\n\t\t\t\t\t\t\t{ _id: { $nin: oldCouponUsers } },\n\t\t\t\t\t\t\toldCoupon._id,\n\t\t\t\t\t\t\tcoupon._id\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tawait this.removeCouponFromMultipleUser({ _id: { $nin: oldCouponUsers } }, oldCoupon._id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase CouponType.INDIVIDUAL_STORE: {\n\t\t\t\tcoupon = await individualStoreCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst individualStoreCoupon = coupon as DocumentType<IndividualStoreCoupon>;\n\t\t\t\t\tif (!individualStoreCoupon.forNewUserOnly) {\n\t\t\t\t\t\tawait this.exchangeCouponForMultipleUser(\n\t\t\t\t\t\t\t{ _id: { $nin: oldCouponUsers } },\n\t\t\t\t\t\t\toldCoupon._id,\n\t\t\t\t\t\t\tcoupon._id\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tawait this.removeCouponFromMultipleUser({ _id: { $nin: oldCouponUsers } }, oldCoupon._id);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase CouponType.INDIVIDUAL_USER: {\n\t\t\t\tcoupon = await individualUserCouponService.create(data);\n\t\t\t\tif (coupon) {\n\t\t\t\t\tconst individualUserCoupon = coupon as DocumentType<IndividualUserCoupon>;\n\t\t\t\t\tawait this.removeCouponFromMultipleUser({ _id: { $nin: oldCouponUsers } }, oldCoupon._id);\n\t\t\t\t\tif (!individualUserCoupon.forNewUserOnly) {\n\t\t\t\t\t\tawait this.assignCouponToMultipleUser(\n\t\t\t\t\t\t\t{ _id: { $in: individualUserCoupon.users } },\n\t\t\t\t\t\t\toldCoupon._id\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase CouponType.CUSTOM_COUPON: {\n\t\t\t\tcoupon = await customCouponService.create(data);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'BAD_REQUEST',\n\t\t\t\t\tmessage: `Invalid coupon type`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (!coupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'BAD_REQUEST',\n\t\t\t\tmessage: `Could create updated coupon`,\n\t\t\t});\n\t\t}\n\n\t\t// add previous version to new coupon\n\t\tconst previousVersions = oldCoupon?.previousVersions || [];\n\t\tpreviousVersions.push(oldCoupon._id);\n\n\t\tconst updatedCoupon = await this.model.findByIdAndUpdate(\n\t\t\tcoupon._id,\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tpreviousVersions,\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\tif (!updatedCoupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'INTERNAL_SERVER_ERROR',\n\t\t\t\tmessage: `Could not set previous versions of coupon`,\n\t\t\t});\n\t\t}\n\t\t// update old coupon\n\t\tconst updatedOldCoupon = await this.model.findByIdAndUpdate(\n\t\t\toldCoupon._id,\n\t\t\t{\n\t\t\t\t$set: {\n\t\t\t\t\tupdatedVersion: coupon._id,\n\t\t\t\t},\n\t\t\t},\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\treturn updatedCoupon;\n\t}\n\n\t/**\n\t * Delete record\n\t * @param data Data for delete\n\t */\n\tpublic async deleteById(data: DeleteDTO): Promise<string> {\n\t\tconst { _id, softDelete } = data;\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: 'NOT_FOUND',\n\t\t\t\tmessage: 'Record not found',\n\t\t\t});\n\t\t}\n\n\t\t// remove coupons from users' coupon lists\n\t\tthis.removeCouponFromMultipleUser({}, _id);\n\n\t\treturn 'Record deleted successfully';\n\t}\n\n\t/**\n\t * Get User Coupons\n\t * @param data User and pagination data\n\t */\n\tpublic async getUserCoupons(\n\t\tuser: mongoose.Types.ObjectId | undefined,\n\t\tpage: number = 0,\n\t\tsize: number = 0\n\t): Promise<{\n\t\tavailable: Array<DocumentType<Model>>;\n\t\tused: Array<DocumentType<Model>>;\n\t\texpired: Array<DocumentType<Model>>;\n\t}> {\n\t\tconst couponRepository = new CouponRepository();\n\t\treturn await couponRepository.getUserCoupons(user, page, size);\n\t}\n\n\tpublic async validateCoupon(data: ValidateCouponDTO): Promise<CouponValidationOutputDTO> {\n\t\tif (!data.coupon) {\n\t\t\treturn await punchMarketingHistoryService.getPunchDiscount(data);\n\t\t}\n\n\t\tconst [user, coupon] = await Promise.all([\n\t\t\tthis.userModel.findById(data.user),\n\t\t\tthis.model.findOne({ code: data.coupon }),\n\t\t]);\n\n\t\tif (!user) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'User not found',\n\t\t\t});\n\t\t}\n\n\t\tif (!coupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'Coupon not found',\n\t\t\t});\n\t\t}\n\n\t\tif (coupon.couponType == CouponType.REFERRAL_CODE) {\n\t\t\treturn await referralCodeCouponService.validateCoupon(data);\n\t\t}\n\n\t\tconst baseCoupon = coupon as DocumentType<BaseCoupon>;\n\n\t\tconst isValid = await this.validateBaseCoupon(baseCoupon, user._id, data.amountBeforeCoupon);\n\n\t\tif (!isValid) {\n\t\t\treturn {\n\t\t\t\tisValid,\n\t\t\t};\n\t\t}\n\n\t\tswitch (coupon.couponType) {\n\t\t\tcase CouponType.REFERRAL_REWARD:\n\t\t\t\treturn await referralRewardCouponService.validateCoupon(\n\t\t\t\t\tdata,\n\t\t\t\t\tcoupon as DocumentType<ReferralRewardCoupon>\n\t\t\t\t);\n\t\t\tcase CouponType.GLOBAL:\n\t\t\t\treturn await globalCouponService.validateCoupon(data, coupon as DocumentType<GlobalCoupon>);\n\t\t\tcase CouponType.INDIVIDUAL_STORE:\n\t\t\t\treturn await individualStoreCouponService.validateCoupon(\n\t\t\t\t\tdata,\n\t\t\t\t\tcoupon as DocumentType<IndividualStoreCoupon>\n\t\t\t\t);\n\t\t\tcase CouponType.INDIVIDUAL_USER:\n\t\t\t\treturn await individualUserCouponService.validateCoupon(\n\t\t\t\t\tdata,\n\t\t\t\t\tcoupon as DocumentType<IndividualUserCoupon>\n\t\t\t\t);\n\t\t\tcase CouponType.CUSTOM_COUPON:\n\t\t\t\treturn await customCouponService.validateCoupon(data, coupon as DocumentType<CustomCoupon>);\n\t\t\tdefault:\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'NOT_IMPLEMENTED',\n\t\t\t\t\tmessage: `Validation not implemented for ${coupon.couponType} coupons`,\n\t\t\t\t});\n\t\t}\n\t}\n\n\tprivate isCouponExpired(coupon: DocumentType<BaseCoupon>): Boolean {\n\t\t// TODO: Add an event listener to ensure the coupon expiration reason is updated after an order is placed\n\t\tif (coupon.expirationReason || (coupon?.duration?.end && new Date(coupon?.duration?.end) < new Date())) {\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t *\n\t * @param coupon Base coupon document\n\t * @param user ID of the user trying to user the coupon\n\t * @param orderAmount Order subtotal\n\t * @returns True if coupon is valid\n\t */\n\tprivate async validateBaseCoupon(\n\t\tcoupon: DocumentType<BaseCoupon>,\n\t\tuser: mongoose.Types.ObjectId,\n\t\torderAmount: number\n\t): Promise<boolean> {\n\t\tconst invalidConditions = [\n\t\t\tthis.isCouponExpired(coupon), // check coupon expired\n\t\t\tcoupon.duration.start > new Date(), // check coupon start\n\t\t\tcoupon.minOrderValue && orderAmount < coupon.minOrderValue, // check minimum order value\n\t\t\tcoupon.forNewUserOnly && !(await this.checkIfNewUser(user)), // check if new user\n\t\t\tcoupon.orderLimitPerUser &&\n\t\t\t\t(await this.getCouponOrdersForUser(user, coupon._id)) >= coupon.orderLimitPerUser, // check order limit for user\n\t\t];\n\n\t\treturn invalidConditions.every(value => value == false);\n\t}\n\n\tprivate async checkIfNewUser(user: mongoose.Types.ObjectId): Promise<boolean> {\n\t\tconst order = await this.orderModel.findOne({\n\t\t\tuser,\n\t\t\tstatus: { ne: RegularOrderStatus.CANCELLED },\n\t\t});\n\t\treturn order ? false : true;\n\t}\n\n\tprivate async getCouponOrdersForUser(\n\t\tuser: mongoose.Types.ObjectId,\n\t\tcoupon: mongoose.Types.ObjectId\n\t): Promise<number> {\n\t\treturn await this.orderModel.countDocuments({\n\t\t\tstatus: { $ne: RegularOrderStatus.CANCELLED },\n\t\t\t'finance.coupon.coupon': coupon,\n\t\t});\n\t}\n\n\tprivate async updateCouponExpiry(coupon: DocumentType<BaseCoupon>) {\n\t\tif (coupon.totalOrderLimit) {\n\t\t\t// total order limit\n\t\t\tconst orderCount = await this.orderModel.countDocuments({\n\t\t\t\tstatus: { $ne: RegularOrderStatus.CANCELLED },\n\t\t\t\t'finance.coupon.coupon': coupon._id,\n\t\t\t});\n\n\t\t\tif (orderCount > coupon.totalOrderLimit) {\n\t\t\t\tawait this.model.findByIdAndUpdate(\n\t\t\t\t\tcoupon._id,\n\t\t\t\t\t{\n\t\t\t\t\t\t$set: {\n\t\t\t\t\t\t\texpirationReason: CouponExpirationReason.TOTAL_ORDER_LIMIT,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\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\t\tif (coupon.spendLimit) {\n\t\t\t// spend limit\n\n\t\t\tconst { totalCouponDiscount, totalSecondaryCouponDiscount } = await this.getCouponDiscountTotals(\n\t\t\t\tcoupon._id\n\t\t\t);\n\n\t\t\tif (totalCouponDiscount >= coupon.spendLimit) {\n\t\t\t\tawait this.model.findByIdAndUpdate(\n\t\t\t\t\tcoupon._id,\n\t\t\t\t\t{\n\t\t\t\t\t\t$set: {\n\t\t\t\t\t\t\texpirationReason: CouponExpirationReason.SPEND_LIMIT,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\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\t}\n\tprivate async findCouponByCode(code: string): Promise<DocumentType<Model> | null> {\n\t\tconst coupon = await this.model.findOne({ code: code });\n\t\tif (!coupon) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'Coupon not found',\n\t\t\t});\n\t\t}\n\t\treturn coupon;\n\t}\n\n\tprivate async getCouponDiscountTotals(coupon: mongoose.Types.ObjectId) {\n\t\tconst pipeline = [\n\t\t\t{\n\t\t\t\t$match: {\n\t\t\t\t\tstatus: { $ne: RegularOrderStatus.CANCELLED },\n\t\t\t\t\t'finance.coupon.coupon': coupon,\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\t$group: {\n\t\t\t\t\t_id: null,\n\t\t\t\t\ttotalCouponDiscount: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$toDouble: {\n\t\t\t\t\t\t\t\t$ifNull: ['$finance.pricing.couponDiscount', 0],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\ttotalSecondaryCouponDiscount: {\n\t\t\t\t\t\t$sum: {\n\t\t\t\t\t\t\t$toDouble: {\n\t\t\t\t\t\t\t\t$ifNull: ['$finance.pricing.secondaryCouponDiscount', 0],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t];\n\n\t\tconst result = await this.orderModel.aggregate(pipeline);\n\n\t\treturn {\n\t\t\ttotalCouponDiscount: result[0]?.totalCouponDiscount ?? 0,\n\t\t\ttotalSecondaryCouponDiscount: result[0]?.totalSecondaryCouponDiscount ?? 0,\n\t\t};\n\t}\n\n\tprivate async assignCouponToUser(user: mongoose.Types.ObjectId, coupon: mongoose.Types.ObjectId) {\n\t\tconst updatedUser = await this.userModel.findByIdAndUpdate(\n\t\t\tuser,\n\t\t\t{\n\t\t\t\t$push: {\n\t\t\t\t\tcoupons: coupon,\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\t}\n\n\tprivate async assignCouponToMultipleUser(\n\t\tquery: mongoose.FilterQuery<User>,\n\t\tcoupon: mongoose.Types.ObjectId\n\t) {\n\t\tconst updatedUsers = await this.userModel.updateMany(query, {\n\t\t\t$push: {\n\t\t\t\tcoupons: coupon,\n\t\t\t},\n\t\t});\n\t}\n\n\tprivate async removeCouponFromUser(user: mongoose.Types.ObjectId, coupon: mongoose.Types.ObjectId) {\n\t\tconst updatedUser = await this.userModel.findByIdAndUpdate(\n\t\t\tuser,\n\t\t\t{\n\t\t\t\t$pull: {\n\t\t\t\t\tcoupons: coupon,\n\t\t\t\t},\n\t\t\t},\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\t}\n\n\tprivate async removeCouponFromMultipleUser(\n\t\tquery: mongoose.FilterQuery<User>,\n\t\tcoupon: mongoose.Types.ObjectId\n\t) {\n\t\tconst updatedUsers = await this.userModel.updateMany(query, {\n\t\t\t$pull: {\n\t\t\t\tcoupons: coupon,\n\t\t\t},\n\t\t});\n\t}\n\n\tprivate async newUserCheck(userId: mongoose.Types.ObjectId): Promise<boolean> {\n\t\tconst user = await this.userModel.findById(userId).select('orderCompleted');\n\t\treturn (user?.orderCompleted ?? 0) < 1;\n\t}\n\n\tprivate throwBadRequest(message: string): never {\n\t\tthrow new TRPCError({\n\t\t\tcode: 'BAD_REQUEST',\n\t\t\tmessage,\n\t\t});\n\t}\n\n\tpublic async addCouponToUser(user: any, couponCode: string): Promise<DocumentType<Model>> {\n\t\tconst coupon = await this.findCouponByCode(couponCode);\n\t\tif (!coupon) {\n\t\t\tthis.throwBadRequest('Coupon not found');\n\t\t}\n\n\t\tconst typedCoupon = coupon as DocumentType<BaseCoupon>;\n\n\t\tif (this.isCouponExpired(typedCoupon)) {\n\t\t\tthis.throwBadRequest('This coupon has expired and cannot be added');\n\t\t}\n\n\t\tconst isNewUser = await this.newUserCheck(user);\n\t\tif (typedCoupon.forNewUserOnly && !isNewUser) {\n\t\t\tthis.throwBadRequest('This coupon is valid for new users only');\n\t\t}\n\n\t\tconst alreadyApplied = await this.hasUserAlreadyAppliedCoupon(user, typedCoupon._id);\n\t\tif (alreadyApplied) {\n\t\t\tthis.throwBadRequest('Coupon already applied to user');\n\t\t}\n\t\tawait this.userModel.findByIdAndUpdate(\n\t\t\tuser,\n\t\t\t{ $push: { coupons: typedCoupon._id } },\n\t\t\t{ new: true, runValidators: true }\n\t\t);\n\n\t\treturn coupon;\n\t}\n\n\tprivate async hasUserAlreadyAppliedCoupon(\n\t\tuserId: mongoose.Types.ObjectId,\n\t\tcouponId: mongoose.Types.ObjectId\n\t): Promise<boolean> {\n\t\tconst user = await this.userModel.findById(userId).select('coupons');\n\t\tif (!user) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: 'User not found',\n\t\t\t});\n\t\t}\n\n\t\treturn (\n\t\t\tuser.coupons?.some((c: any) => {\n\t\t\t\tconst id = (c?._id ?? c)?.toString();\n\t\t\t\treturn id === couponId.toString();\n\t\t\t}) ?? false\n\t\t);\n\t}\n\n\tprivate async exchangeCouponForMultipleUser(\n\t\tquery: mongoose.FilterQuery<User>,\n\t\toldCoupon: mongoose.Types.ObjectId,\n\t\tnewCoupon: mongoose.Types.ObjectId\n\t) {\n\t\tawait this.removeCouponFromMultipleUser(query, oldCoupon);\n\t\tawait this.assignCouponToMultipleUser(query, newCoupon);\n\t}\n\n\tpublic getShareableLink = async (code: string): Promise<ShareableLinkDTO> => {\n\t\tconst secretsService = getLibraries().getSecretsService();\n\t\tconst deepLinkUrl = (await secretsService.getKey('DEEP_LINK_URL')) ?? 'https://link.lyxa.ai';\n\n\t\tconst link = `${deepLinkUrl}/invite/${code}`;\n\n\t\tconst message = `Download Lyxa and explore the wide variety of offerings and benefits! Use my code at checkout to claim a special coupon : ${code}.\n\t\t${link}`;\n\n\t\treturn { link, message };\n\t};\n\n\tpublic async getUserCouponList(data?: Partial<FilterDTO>): Promise<PaginatedResponse<AllCouponsDTO>> {\n\t\ttry {\n\t\t\tlet query = data?.query || {};\n\n\t\t\tif (!query.user) {\n\t\t\t\tbadRequestError('User ID is required in query');\n\t\t\t}\n\n\t\t\tconst select = data?.select || {\n\t\t\t\t_id: 1,\n\t\t\t\t'createdBy.name': 1,\n\t\t\t\t'duration.start': 1,\n\t\t\t\t'duration.end': 1,\n\t\t\t\tcode: 1,\n\t\t\t\tstatus: 1,\n\t\t\t\tcreatedAt: 1,\n\t\t\t};\n\n\t\t\tlet isExpiredField = null;\n\n\t\t\tif (query.status) {\n\t\t\t\tif (query.status === 'expired') {\n\t\t\t\t\tisExpiredField = true;\n\t\t\t\t\tdelete query.status;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (query.createdAt) {\n\t\t\t\t// convert to date range\n\t\t\t\tif (\"$gte\" in query.createdAt) {\n\t\t\t\t\tquery.createdAt.$gte = new Date(query.createdAt.$gte);\n\t\t\t\t}\n\t\t\t\tif (\"$lte\" in query.createdAt) {\n\t\t\t\t\tquery.createdAt.$lte = new Date(query.createdAt.$lte);\n\t\t\t\t}\n\t\t\t\tif (\"$gt\" in query.createdAt) {\n\t\t\t\t\tquery.createdAt.$gt = new Date(query.createdAt.$gt);\n\t\t\t\t}\n\t\t\t\tif (\"$lt\" in query.createdAt) {\n\t\t\t\t\tquery.createdAt.$lt = new Date(query.createdAt.$lt);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst user = await this.userModel.findById(query.user).select('coupons');\n\t\t\tquery._id = { $in: user?.coupons ?? [] };\n\t\t\tdelete query.user; // remove user from query to avoid issues in aggregation match stage\n\n\t\t\tconst pipeline: any[] = [\n\t\t\t\t{ $match: { ...query, deletedAt: null } }, // Filter by search query\n\t\t\t\t{\n\t\t\t\t\t$lookup: {\n\t\t\t\t\t\t// INNER JOIN on categories\n\t\t\t\t\t\tfrom: 'admins', // Mongoose auto-pluralizes collection names\n\t\t\t\t\t\tlocalField: 'createdBy',\n\t\t\t\t\t\tforeignField: '_id',\n\t\t\t\t\t\tas: 'createdBy',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ $unwind: { path: '$createdBy', preserveNullAndEmptyArrays: true } }, // Flatten the array\n\t\t\t\t{\n\t\t\t\t\t$addFields: {\n\t\t\t\t\t\tisExpired: {\n\t\t\t\t\t\t\t$cond: {\n\t\t\t\t\t\t\t\tif: {\n\t\t\t\t\t\t\t\t\t$or: [{ $lt: ['$duration.end', new Date()] }, { $ifNull: ['$expirationReason', false] }],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tthen: true,\n\t\t\t\t\t\t\t\telse: false,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$match: isExpiredField\n\t\t\t\t\t\t? { isExpired: true }\n\t\t\t\t\t\t: query.status === 'active'\n\t\t\t\t\t\t\t? { isExpired: false }\n\t\t\t\t\t\t\t: {},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$addFields: {\n\t\t\t\t\t\tstatus: {\n\t\t\t\t\t\t\t$cond: {\n\t\t\t\t\t\t\t\tif: { $eq: ['$isExpired', true] },\n\t\t\t\t\t\t\t\tthen: 'expired',\n\t\t\t\t\t\t\t\telse: '$status',\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t$project: select,\n\t\t\t\t},\n\t\t\t];\n\n\t\t\tif (data?.search?.searchKey) {\n\t\t\t\tconst searchKey = normalizeName(data.search.searchKey);\n\t\t\t\tconst regex = new RegExp(searchKey, 'i'); // case-insensitive search\n\t\t\t\tconst fields = data.search.searchFields?.length ? data.search.searchFields : ['code']; // default field(s) to search in\n\n\t\t\t\tpipeline.push({\n\t\t\t\t\t$match: {\n\t\t\t\t\t\t$or: fields.map(field => ({ [field]: { $regex: regex } })),\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// Execute the aggregation with pagination and sorting\n\t\t\tconst results = await paginateAggregation(this.model, pipeline, data?.page, data?.size, data?.sort);\n\n\t\t\treturn results as unknown as PaginatedResponse<AllCouponsDTO>;\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\nexport const couponService = Container.get(CouponService);\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom-coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/custom-coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAqB,YAAY,IAAI,KAAK,EAAE,MAAM,4CAA4C,CAAC;AACtG,OAAO,EACN,yBAAyB,EACzB,qBAAqB,IAAI,SAAS,EAClC,qBAAqB,IAAI,SAAS,EAClC,iBAAiB,EACjB,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"custom-coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/custom-coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAqB,YAAY,IAAI,KAAK,EAAE,MAAM,4CAA4C,CAAC;AACtG,OAAO,EACN,yBAAyB,EACzB,qBAAqB,IAAI,SAAS,EAClC,qBAAqB,IAAI,SAAS,EAClC,iBAAiB,EACjB,MAAM,6BAA6B,CAAC;AAKrC,qBACa,mBAAmB;IAC/B,OAAO,CAAC,KAAK,CAAC;;IAUD,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IASrD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAuBjE,cAAc,CAC1B,IAAI,EAAE,iBAAiB,EACvB,cAAc,EAAE,YAAY,CAAC,KAAK,CAAC,GACjC,OAAO,CAAC,yBAAyB,CAAC;CA2BrC;AAED,eAAO,MAAM,mBAAmB,qBAAqC,CAAC"}
|
|
@@ -47,6 +47,7 @@ const typedi_1 = __importStar(require("typedi"));
|
|
|
47
47
|
const server_1 = require("@trpc/server");
|
|
48
48
|
const models_1 = require("@lyxa.ai/core/dist/libraries/mongo/models/");
|
|
49
49
|
const currency_1 = require("@lyxa.ai/core/dist/utilities/currency");
|
|
50
|
+
const shared_1 = require("@lyxa.ai/core/dist/utilities/shared");
|
|
50
51
|
const enum_1 = require("@lyxa.ai/core/dist/utilities/enum");
|
|
51
52
|
let CustomCouponService = class CustomCouponService {
|
|
52
53
|
model;
|
|
@@ -75,16 +76,16 @@ let CustomCouponService = class CustomCouponService {
|
|
|
75
76
|
async validateCoupon(data, couponDocument) {
|
|
76
77
|
const discountAmount = couponDocument.valueType == enum_1.ValueType.FIXED
|
|
77
78
|
? couponDocument.value
|
|
78
|
-
: (data.amountBeforeCoupon * couponDocument.value) / 100;
|
|
79
|
+
: (0, shared_1.roundBaseCurrency)((data.amountBeforeCoupon * couponDocument.value) / 100);
|
|
79
80
|
const secondaryDiscountAmount = couponDocument.valueType == enum_1.ValueType.FIXED
|
|
80
81
|
? await (0, currency_1.convertToSecondary)(couponDocument.value)
|
|
81
|
-
:
|
|
82
|
+
: (0, shared_1.roundSecondaryCurrency)((data.secondaryAmountBeforeCoupon * couponDocument.value) / 100);
|
|
82
83
|
return {
|
|
83
84
|
isValid: true,
|
|
84
85
|
discountAmount,
|
|
85
86
|
secondaryDiscountAmount,
|
|
86
|
-
amountAfterCoupon: data.amountBeforeCoupon - discountAmount,
|
|
87
|
-
secondaryAmountAfterCoupon: data.secondaryAmountBeforeCoupon - secondaryDiscountAmount,
|
|
87
|
+
amountAfterCoupon: (0, shared_1.roundBaseCurrency)(data.amountBeforeCoupon - discountAmount),
|
|
88
|
+
secondaryAmountAfterCoupon: (0, shared_1.roundSecondaryCurrency)(data.secondaryAmountBeforeCoupon - secondaryDiscountAmount),
|
|
88
89
|
discountCut: {
|
|
89
90
|
companyCut: discountAmount,
|
|
90
91
|
secondaryCompanyCut: secondaryDiscountAmount,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom-coupon.service.js","sourceRoot":"/","sources":["modules/coupon/services/custom-coupon.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AAEzC,uEAAsG;AAOtG,
|
|
1
|
+
{"version":3,"file":"custom-coupon.service.js","sourceRoot":"/","sources":["modules/coupon/services/custom-coupon.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AAEzC,uEAAsG;AAOtG,oEAA2E;AAC3E,gEAAgG;AAChG,4DAA8D;AAGvD,IAAM,mBAAmB,GAAzB,MAAM,mBAAmB;IACvB,KAAK,CAAC;IAEd;QACC,IAAI,CAAC,KAAK,GAAG,0BAAiB,CAAC;IAChC,CAAC;IAMM,KAAK,CAAC,MAAM,CAAC,IAAe;QAClC,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtC,CAAC;IAOM,KAAK,CAAC,MAAM,CAAC,EAAU,EAAE,IAAe;QAC9C,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAC/C;YACC,GAAG,EAAE,EAAE;YACP,SAAS,EAAE,IAAI;SACf,EACD,IAAI,EACJ;YACC,GAAG,EAAE,IAAI;YACT,aAAa,EAAE,IAAI;SACnB,CACD,CAAC;QAEF,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,IAAI,kBAAS,CAAC;gBACnB,IAAI,EAAE,WAAW;gBACjB,OAAO,EAAE,kBAAkB;aAC3B,CAAC,CAAC;QACJ,CAAC;QAED,OAAO,MAAM,CAAC;IACf,CAAC;IAEM,KAAK,CAAC,cAAc,CAC1B,IAAuB,EACvB,cAAmC;QAEnC,MAAM,cAAc,GACnB,cAAc,CAAC,SAAS,IAAI,gBAAS,CAAC,KAAK;YAC1C,CAAC,CAAC,cAAc,CAAC,KAAK;YACtB,CAAC,CAAC,IAAA,0BAAiB,EAAC,CAAC,IAAI,CAAC,kBAAkB,GAAG,cAAc,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC;QAE9E,MAAM,uBAAuB,GAC5B,cAAc,CAAC,SAAS,IAAI,gBAAS,CAAC,KAAK;YAC1C,CAAC,CAAC,MAAM,IAAA,6BAAkB,EAAC,cAAc,CAAC,KAAK,CAAC;YAChD,CAAC,CAAC,IAAA,+BAAsB,EAAC,CAAC,IAAI,CAAC,2BAA2B,GAAG,cAAc,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC;QAE5F,OAAO;YACN,OAAO,EAAE,IAAI;YACb,cAAc;YACd,uBAAuB;YACvB,iBAAiB,EAAE,IAAA,0BAAiB,EAAC,IAAI,CAAC,kBAAkB,GAAG,cAAc,CAAC;YAC9E,0BAA0B,EAAE,IAAA,+BAAsB,EACjD,IAAI,CAAC,2BAA2B,GAAG,uBAAuB,CAC1D;YACD,WAAW,EAAE;gBACZ,UAAU,EAAE,cAAc;gBAC1B,mBAAmB,EAAE,uBAAuB;gBAC5C,OAAO,EAAE,CAAC;gBACV,gBAAgB,EAAE,CAAC;aACnB;SACD,CAAC;IACH,CAAC;CACD,CAAA;AAzEY,kDAAmB;8BAAnB,mBAAmB;IAD/B,IAAA,gBAAO,GAAE;;GACG,mBAAmB,CAyE/B;AAEY,QAAA,mBAAmB,GAAG,gBAAS,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType } from '@typegoose/typegoose';\nimport { CustomCouponModel, CustomCoupon as Model } from '@lyxa.ai/core/dist/libraries/mongo/models/';\nimport {\n\tCouponValidationOutputDTO,\n\tCreateCustomCouponDTO as CreateDTO,\n\tUpdateCustomCouponDTO as UpdateDTO,\n\tValidateCouponDTO,\n} from '@modules/coupon/validations';\nimport { convertToSecondary } from '@lyxa.ai/core/dist/utilities/currency';\nimport { roundBaseCurrency, roundSecondaryCurrency } from '@lyxa.ai/core/dist/utilities/shared';\nimport { ValueType } from '@lyxa.ai/core/dist/utilities/enum';\n\n@Service()\nexport class CustomCouponService {\n\tprivate model;\n\n\tconstructor() {\n\t\tthis.model = CustomCouponModel;\n\t}\n\n\t/**\n\t * Create a new record\n\t * @param data Data for the new record\n\t */\n\tpublic async create(data: CreateDTO): Promise<DocumentType<Model>> {\n\t\treturn await this.model.create(data);\n\t}\n\n\t/**\n\t * Update record\n\t * @param id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(id: string, data: UpdateDTO): Promise<DocumentType<Model>> {\n\t\tconst record = await this.model.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id: id,\n\t\t\t\tdeletedAt: null,\n\t\t\t},\n\t\t\tdata,\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\tif (!record) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: `Record not found`,\n\t\t\t});\n\t\t}\n\n\t\treturn record;\n\t}\n\n\tpublic async validateCoupon(\n\t\tdata: ValidateCouponDTO,\n\t\tcouponDocument: DocumentType<Model>\n\t): Promise<CouponValidationOutputDTO> {\n\t\tconst discountAmount =\n\t\t\tcouponDocument.valueType == ValueType.FIXED\n\t\t\t\t? couponDocument.value\n\t\t\t\t: roundBaseCurrency((data.amountBeforeCoupon * couponDocument.value) / 100);\n\n\t\tconst secondaryDiscountAmount =\n\t\t\tcouponDocument.valueType == ValueType.FIXED\n\t\t\t\t? await convertToSecondary(couponDocument.value)\n\t\t\t\t: roundSecondaryCurrency((data.secondaryAmountBeforeCoupon * couponDocument.value) / 100);\n\n\t\treturn {\n\t\t\tisValid: true,\n\t\t\tdiscountAmount,\n\t\t\tsecondaryDiscountAmount,\n\t\t\tamountAfterCoupon: roundBaseCurrency(data.amountBeforeCoupon - discountAmount),\n\t\t\tsecondaryAmountAfterCoupon: roundSecondaryCurrency(\n\t\t\t\tdata.secondaryAmountBeforeCoupon - secondaryDiscountAmount\n\t\t\t),\n\t\t\tdiscountCut: {\n\t\t\t\tcompanyCut: discountAmount,\n\t\t\t\tsecondaryCompanyCut: secondaryDiscountAmount,\n\t\t\t\tshopCut: 0,\n\t\t\t\tsecondaryShopCut: 0,\n\t\t\t},\n\t\t};\n\t}\n}\n\nexport const customCouponService = Container.get(CustomCouponService);\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global-coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/global-coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAqB,YAAY,IAAI,KAAK,EAAE,MAAM,4CAA4C,CAAC;AACtG,OAAO,EACN,yBAAyB,EACzB,qBAAqB,IAAI,SAAS,EAClC,qBAAqB,IAAI,SAAS,EAClC,iBAAiB,EACjB,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"global-coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/global-coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAqB,YAAY,IAAI,KAAK,EAAE,MAAM,4CAA4C,CAAC;AACtG,OAAO,EACN,yBAAyB,EACzB,qBAAqB,IAAI,SAAS,EAClC,qBAAqB,IAAI,SAAS,EAClC,iBAAiB,EACjB,MAAM,6BAA6B,CAAC;AAKrC,qBACa,mBAAmB;IAC/B,OAAO,CAAC,KAAK,CAAC;;IAUD,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IA6BrD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAsBjE,cAAc,CAC1B,IAAI,EAAE,iBAAiB,EACvB,cAAc,EAAE,YAAY,CAAC,KAAK,CAAC,GACjC,OAAO,CAAC,yBAAyB,CAAC;CA2BrC;AAED,eAAO,MAAM,mBAAmB,qBAAqC,CAAC"}
|
|
@@ -48,6 +48,7 @@ const server_1 = require("@trpc/server");
|
|
|
48
48
|
const models_1 = require("@lyxa.ai/core/dist/libraries/mongo/models/");
|
|
49
49
|
const enum_1 = require("@lyxa.ai/core/dist/utilities/enum");
|
|
50
50
|
const currency_1 = require("@lyxa.ai/core/dist/utilities/currency");
|
|
51
|
+
const shared_1 = require("@lyxa.ai/core/dist/utilities/shared");
|
|
51
52
|
let GlobalCouponService = class GlobalCouponService {
|
|
52
53
|
model;
|
|
53
54
|
constructor() {
|
|
@@ -92,16 +93,16 @@ let GlobalCouponService = class GlobalCouponService {
|
|
|
92
93
|
async validateCoupon(data, couponDocument) {
|
|
93
94
|
const discountAmount = couponDocument.valueType == enum_1.ValueType.FIXED
|
|
94
95
|
? couponDocument.value
|
|
95
|
-
: (data.amountBeforeCoupon * couponDocument.value) / 100;
|
|
96
|
+
: (0, shared_1.roundBaseCurrency)((data.amountBeforeCoupon * couponDocument.value) / 100);
|
|
96
97
|
const secondaryDiscountAmount = couponDocument.valueType == enum_1.ValueType.FIXED
|
|
97
98
|
? await (0, currency_1.convertToSecondary)(couponDocument.value)
|
|
98
|
-
:
|
|
99
|
+
: (0, shared_1.roundSecondaryCurrency)((data.secondaryAmountBeforeCoupon * couponDocument.value) / 100);
|
|
99
100
|
return {
|
|
100
101
|
isValid: true,
|
|
101
102
|
discountAmount,
|
|
102
103
|
secondaryDiscountAmount,
|
|
103
|
-
amountAfterCoupon: data.amountBeforeCoupon - discountAmount,
|
|
104
|
-
secondaryAmountAfterCoupon: data.secondaryAmountBeforeCoupon - secondaryDiscountAmount,
|
|
104
|
+
amountAfterCoupon: (0, shared_1.roundBaseCurrency)(data.amountBeforeCoupon - discountAmount),
|
|
105
|
+
secondaryAmountAfterCoupon: (0, shared_1.roundSecondaryCurrency)(data.secondaryAmountBeforeCoupon - secondaryDiscountAmount),
|
|
105
106
|
discountCut: {
|
|
106
107
|
companyCut: discountAmount,
|
|
107
108
|
secondaryCompanyCut: secondaryDiscountAmount,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global-coupon.service.js","sourceRoot":"/","sources":["modules/coupon/services/global-coupon.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AAEzC,uEAAsG;AAOtG,
|
|
1
|
+
{"version":3,"file":"global-coupon.service.js","sourceRoot":"/","sources":["modules/coupon/services/global-coupon.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AAEzC,uEAAsG;AAOtG,4DAA0E;AAC1E,oEAA2E;AAC3E,gEAAgG;AAGzF,IAAM,mBAAmB,GAAzB,MAAM,mBAAmB;IACvB,KAAK,CAAC;IAEd;QACC,IAAI,CAAC,KAAK,GAAG,0BAAiB,CAAC;IAChC,CAAC;IAMM,KAAK,CAAC,MAAM,CAAC,IAAe;QAClC,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACzB,MAAM,WAAW,GAAG,IAAI,IAAI,EAAE,CAAC;YAC/B,MAAM,6BAA6B,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;gBAC9D,UAAU,EAAE,iBAAU,CAAC,MAAM;gBAC7B,cAAc,EAAE,IAAI;gBACpB,SAAS,EAAE,IAAI;gBACf,gBAAgB,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE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CAAC,IAAI,CAAC,2BAA2B,GAAG,cAAc,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC;QAE5F,OAAO;YACN,OAAO,EAAE,IAAI;YACb,cAAc;YACd,uBAAuB;YACvB,iBAAiB,EAAE,IAAA,0BAAiB,EAAC,IAAI,CAAC,kBAAkB,GAAG,cAAc,CAAC;YAC9E,0BAA0B,EAAE,IAAA,+BAAsB,EACjD,IAAI,CAAC,2BAA2B,GAAG,uBAAuB,CAC1D;YACD,WAAW,EAAE;gBACZ,UAAU,EAAE,cAAc;gBAC1B,mBAAmB,EAAE,uBAAuB;gBAC5C,OAAO,EAAE,CAAC;gBACV,gBAAgB,EAAE,CAAC;aACnB;SACD,CAAC;IACH,CAAC;CACD,CAAA;AA5FY,kDAAmB;8BAAnB,mBAAmB;IAD/B,IAAA,gBAAO,GAAE;;GACG,mBAAmB,CA4F/B;AAEY,QAAA,mBAAmB,GAAG,gBAAS,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType } from '@typegoose/typegoose';\nimport { GlobalCouponModel, GlobalCoupon as Model } from '@lyxa.ai/core/dist/libraries/mongo/models/';\nimport {\n\tCouponValidationOutputDTO,\n\tCreateGlobalCouponDTO as CreateDTO,\n\tUpdateGlobalCouponDTO as UpdateDTO,\n\tValidateCouponDTO,\n} from '@modules/coupon/validations';\nimport { CouponType, ValueType } from '@lyxa.ai/core/dist/utilities/enum';\nimport { convertToSecondary } from '@lyxa.ai/core/dist/utilities/currency';\nimport { roundBaseCurrency, roundSecondaryCurrency } from '@lyxa.ai/core/dist/utilities/shared';\n\n@Service()\nexport class GlobalCouponService {\n\tprivate model;\n\n\tconstructor() {\n\t\tthis.model = GlobalCouponModel;\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\tif (data.forNewUserOnly) {\n\t\t\tconst currentDate = new Date();\n\t\t\tconst alreadyActiveForNewUserCoupon = await this.model.findOne({\n\t\t\t\tcouponType: CouponType.GLOBAL,\n\t\t\t\tforNewUserOnly: true,\n\t\t\t\tdeletedAt: null,\n\t\t\t\texpirationReason: { $exists: false },\n\t\t\t\t'duration.end': { $gte: currentDate },\n\t\t\t});\n\n\t\t\tif (alreadyActiveForNewUserCoupon) {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: 'CONFLICT',\n\t\t\t\t\tmessage: 'New User Coupon is already active.',\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tconst coupon = this.model.create(data);\n\n\t\treturn coupon;\n\t}\n\n\t/**\n\t * Update record\n\t * @param id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(id: string, data: UpdateDTO): Promise<DocumentType<Model>> {\n\t\tconst record = await this.model.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id: id,\n\t\t\t\tdeletedAt: null,\n\t\t\t},\n\t\t\tdata,\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\tif (!record) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: `Record not found`,\n\t\t\t});\n\t\t}\n\n\t\treturn record;\n\t}\n\tpublic async validateCoupon(\n\t\tdata: ValidateCouponDTO,\n\t\tcouponDocument: DocumentType<Model>\n\t): Promise<CouponValidationOutputDTO> {\n\t\tconst discountAmount =\n\t\t\tcouponDocument.valueType == ValueType.FIXED\n\t\t\t\t? couponDocument.value\n\t\t\t\t: roundBaseCurrency((data.amountBeforeCoupon * couponDocument.value) / 100);\n\n\t\tconst secondaryDiscountAmount =\n\t\t\tcouponDocument.valueType == ValueType.FIXED\n\t\t\t\t? await convertToSecondary(couponDocument.value)\n\t\t\t\t: roundSecondaryCurrency((data.secondaryAmountBeforeCoupon * couponDocument.value) / 100);\n\n\t\treturn {\n\t\t\tisValid: true,\n\t\t\tdiscountAmount,\n\t\t\tsecondaryDiscountAmount,\n\t\t\tamountAfterCoupon: roundBaseCurrency(data.amountBeforeCoupon - discountAmount),\n\t\t\tsecondaryAmountAfterCoupon: roundSecondaryCurrency(\n\t\t\t\tdata.secondaryAmountBeforeCoupon - secondaryDiscountAmount\n\t\t\t),\n\t\t\tdiscountCut: {\n\t\t\t\tcompanyCut: discountAmount,\n\t\t\t\tsecondaryCompanyCut: secondaryDiscountAmount,\n\t\t\t\tshopCut: 0,\n\t\t\t\tsecondaryShopCut: 0,\n\t\t\t},\n\t\t};\n\t}\n}\n\nexport const globalCouponService = Container.get(GlobalCouponService);\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"individual-store-coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/individual-store-coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAY,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAEN,qBAAqB,IAAI,KAAK,
|
|
1
|
+
{"version":3,"file":"individual-store-coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/individual-store-coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAY,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAEN,qBAAqB,IAAI,KAAK,EAE9B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EACN,yBAAyB,EACzB,8BAA8B,IAAI,SAAS,EAC3C,8BAA8B,IAAI,SAAS,EAC3C,iBAAiB,EACjB,MAAM,6BAA6B,CAAC;AAKrC,qBACa,4BAA4B;IACxC,OAAO,CAAC,KAAK,CAAC;IACd,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAmB;;IAWzC,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAWrD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAuBjE,cAAc,CAC1B,IAAI,EAAE,iBAAiB,EACvB,cAAc,EAAE,YAAY,CAAC,KAAK,CAAC,GACjC,OAAO,CAAC,yBAAyB,CAAC;CA4CrC;AAED,eAAO,MAAM,4BAA4B,8BAA8C,CAAC"}
|
|
@@ -48,6 +48,7 @@ const server_1 = require("@trpc/server");
|
|
|
48
48
|
const typegoose_1 = require("@typegoose/typegoose");
|
|
49
49
|
const models_1 = require("@lyxa.ai/core/dist/libraries/mongo/models/");
|
|
50
50
|
const currency_1 = require("@lyxa.ai/core/dist/utilities/currency");
|
|
51
|
+
const shared_1 = require("@lyxa.ai/core/dist/utilities/shared");
|
|
51
52
|
const enum_1 = require("@lyxa.ai/core/dist/utilities/enum");
|
|
52
53
|
let IndividualStoreCouponService = class IndividualStoreCouponService {
|
|
53
54
|
model;
|
|
@@ -91,17 +92,17 @@ let IndividualStoreCouponService = class IndividualStoreCouponService {
|
|
|
91
92
|
}
|
|
92
93
|
const discountAmount = couponDocument.valueType == enum_1.ValueType.FIXED
|
|
93
94
|
? couponDocument.value
|
|
94
|
-
: (data.amountBeforeCoupon * couponDocument.value) / 100;
|
|
95
|
+
: (0, shared_1.roundBaseCurrency)((data.amountBeforeCoupon * couponDocument.value) / 100);
|
|
95
96
|
const secondaryDiscountAmount = couponDocument.valueType == enum_1.ValueType.FIXED
|
|
96
97
|
? await (0, currency_1.convertToSecondary)(couponDocument.value)
|
|
97
|
-
:
|
|
98
|
+
: (0, shared_1.roundSecondaryCurrency)((data.secondaryAmountBeforeCoupon * couponDocument.value) / 100);
|
|
98
99
|
const isShopCoverExpenses = couponDocument.isShopCoverExpenses;
|
|
99
100
|
return {
|
|
100
101
|
isValid: true,
|
|
101
102
|
discountAmount,
|
|
102
103
|
secondaryDiscountAmount,
|
|
103
|
-
amountAfterCoupon: data.amountBeforeCoupon - discountAmount,
|
|
104
|
-
secondaryAmountAfterCoupon: data.secondaryAmountBeforeCoupon - secondaryDiscountAmount,
|
|
104
|
+
amountAfterCoupon: (0, shared_1.roundBaseCurrency)(data.amountBeforeCoupon - discountAmount),
|
|
105
|
+
secondaryAmountAfterCoupon: (0, shared_1.roundSecondaryCurrency)(data.secondaryAmountBeforeCoupon - secondaryDiscountAmount),
|
|
105
106
|
discountCut: {
|
|
106
107
|
companyCut: isShopCoverExpenses ? 0 : discountAmount,
|
|
107
108
|
secondaryCompanyCut: isShopCoverExpenses ? 0 : secondaryDiscountAmount,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"individual-store-coupon.service.js","sourceRoot":"/","sources":["modules/coupon/services/individual-store-coupon.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AACzC,oDAA8D;AAC9D,
|
|
1
|
+
{"version":3,"file":"individual-store-coupon.service.js","sourceRoot":"/","sources":["modules/coupon/services/individual-store-coupon.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAA4C;AAC5C,yCAAyC;AACzC,oDAA8D;AAC9D,uEAIoD;AAOpD,oEAA2E;AAC3E,gEAAgG;AAChG,4DAA8D;AAGvD,IAAM,4BAA4B,GAAlC,MAAM,4BAA4B;IAChC,KAAK,CAAC;IACG,kBAAkB,CAAmB;IAEtD;QACC,IAAI,CAAC,KAAK,GAAG,mCAA0B,CAAC;QACxC,IAAI,CAAC,kBAAkB,GAAG,kBAA6B,CAAC;IACzD,CAAC;IAMM,KAAK,CAAC,MAAM,CAAC,IAAe;QAClC,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAEvC,OAAO,MAAM,CAAC;IACf,CAAC;IAOM,KAAK,CAAC,MAAM,CAAC,EAAU,EAAE,IAAe;QAC9C,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC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C1C,CAAC,CAAC,MAAM,IAAA,6BAAkB,EAAC,cAAc,CAAC,KAAK,CAAC;YAChD,CAAC,CAAC,IAAA,+BAAsB,EAAC,CAAC,IAAI,CAAC,2BAA2B,GAAG,cAAc,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC;QAE5F,MAAM,mBAAmB,GAAG,cAAc,CAAC,mBAAmB,CAAC;QAE/D,OAAO;YACN,OAAO,EAAE,IAAI;YACb,cAAc;YACd,uBAAuB;YACvB,iBAAiB,EAAE,IAAA,0BAAiB,EAAC,IAAI,CAAC,kBAAkB,GAAG,cAAc,CAAC;YAC9E,0BAA0B,EAAE,IAAA,+BAAsB,EACjD,IAAI,CAAC,2BAA2B,GAAG,uBAAuB,CAC1D;YACD,WAAW,EAAE;gBACZ,UAAU,EAAE,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,cAAc;gBACpD,mBAAmB,EAAE,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,uBAAuB;gBACtE,OAAO,EAAE,mBAAmB,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;gBACjD,gBAAgB,EAAE,mBAAmB,CAAC,CAAC,CAAC,uBAAuB,CAAC,CAAC,CAAC,CAAC;aACnE;SACD,CAAC;IACH,CAAC;CACD,CAAA;AA9FY,oEAA4B;uCAA5B,4BAA4B;IADxC,IAAA,gBAAO,GAAE;;GACG,4BAA4B,CA8FxC;AAEY,QAAA,4BAA4B,GAAG,gBAAS,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC","sourcesContent":["import Container, { Service } from 'typedi';\nimport { TRPCError } from '@trpc/server';\nimport { DocumentType, mongoose } from '@typegoose/typegoose';\nimport {\n\tIndividualStoreCouponModel,\n\tIndividualStoreCoupon as Model,\n\tShopModel,\n} from '@lyxa.ai/core/dist/libraries/mongo/models/';\nimport {\n\tCouponValidationOutputDTO,\n\tCreateIndividualStoreCouponDTO as CreateDTO,\n\tUpdateIndividualStoreCouponDTO as UpdateDTO,\n\tValidateCouponDTO,\n} from '@modules/coupon/validations';\nimport { convertToSecondary } from '@lyxa.ai/core/dist/utilities/currency';\nimport { roundBaseCurrency, roundSecondaryCurrency } from '@lyxa.ai/core/dist/utilities/shared';\nimport { ValueType } from '@lyxa.ai/core/dist/utilities/enum';\n\n@Service()\nexport class IndividualStoreCouponService {\n\tprivate model;\n\tprivate readonly shopModelForCoupon: typeof ShopModel;\n\n\tconstructor() {\n\t\tthis.model = IndividualStoreCouponModel;\n\t\tthis.shopModelForCoupon = ShopModel as typeof ShopModel;\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 coupon = this.model.create(data);\n\n\t\treturn coupon;\n\t}\n\n\t/**\n\t * Update record\n\t * @param id ID of the record\n\t * @param data Data to update\n\t */\n\tpublic async update(id: string, data: UpdateDTO): Promise<DocumentType<Model>> {\n\t\tconst record = await this.model.findOneAndUpdate(\n\t\t\t{\n\t\t\t\t_id: id,\n\t\t\t\tdeletedAt: null,\n\t\t\t},\n\t\t\tdata,\n\t\t\t{\n\t\t\t\tnew: true,\n\t\t\t\trunValidators: true,\n\t\t\t}\n\t\t);\n\n\t\tif (!record) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'NOT_FOUND',\n\t\t\t\tmessage: `Record not found`,\n\t\t\t});\n\t\t}\n\n\t\treturn record;\n\t}\n\n\tpublic async validateCoupon(\n\t\tdata: ValidateCouponDTO,\n\t\tcouponDocument: DocumentType<Model>\n\t): Promise<CouponValidationOutputDTO> {\n\t\tconst shopId = new mongoose.Types.ObjectId(data.shop);\n\t\tconst shop = await this.shopModelForCoupon.findOne({ _id: shopId, deletedAt: null });\n\t\tif (!shop) {\n\t\t\treturn {\n\t\t\t\tisValid: false,\n\t\t\t};\n\t\t}\n\n\t\t// Check if the coupon is applicable to the shop and item-type\n\t\tif (!couponDocument.shops?.includes(shopId) && !couponDocument.itemTypes?.includes(shop.itemType)) {\n\t\t\treturn {\n\t\t\t\tisValid: false,\n\t\t\t};\n\t\t}\n\n\t\tconst discountAmount =\n\t\t\tcouponDocument.valueType == ValueType.FIXED\n\t\t\t\t? couponDocument.value\n\t\t\t\t: roundBaseCurrency((data.amountBeforeCoupon * couponDocument.value) / 100);\n\n\t\tconst secondaryDiscountAmount =\n\t\t\tcouponDocument.valueType == ValueType.FIXED\n\t\t\t\t? await convertToSecondary(couponDocument.value)\n\t\t\t\t: roundSecondaryCurrency((data.secondaryAmountBeforeCoupon * couponDocument.value) / 100);\n\n\t\tconst isShopCoverExpenses = couponDocument.isShopCoverExpenses;\n\n\t\treturn {\n\t\t\tisValid: true,\n\t\t\tdiscountAmount,\n\t\t\tsecondaryDiscountAmount,\n\t\t\tamountAfterCoupon: roundBaseCurrency(data.amountBeforeCoupon - discountAmount),\n\t\t\tsecondaryAmountAfterCoupon: roundSecondaryCurrency(\n\t\t\t\tdata.secondaryAmountBeforeCoupon - secondaryDiscountAmount\n\t\t\t),\n\t\t\tdiscountCut: {\n\t\t\t\tcompanyCut: isShopCoverExpenses ? 0 : discountAmount,\n\t\t\t\tsecondaryCompanyCut: isShopCoverExpenses ? 0 : secondaryDiscountAmount,\n\t\t\t\tshopCut: isShopCoverExpenses ? discountAmount : 0,\n\t\t\t\tsecondaryShopCut: isShopCoverExpenses ? secondaryDiscountAmount : 0,\n\t\t\t},\n\t\t};\n\t}\n}\n\nexport const individualStoreCouponService = Container.get(IndividualStoreCouponService);\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"individual-user-coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/individual-user-coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAY,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAEN,oBAAoB,IAAI,KAAK,EAC7B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EACN,yBAAyB,EACzB,6BAA6B,IAAI,SAAS,EAC1C,6BAA6B,IAAI,SAAS,EAC1C,iBAAiB,EACjB,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"individual-user-coupon.service.d.ts","sourceRoot":"/","sources":["modules/coupon/services/individual-user-coupon.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAY,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAEN,oBAAoB,IAAI,KAAK,EAC7B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EACN,yBAAyB,EACzB,6BAA6B,IAAI,SAAS,EAC1C,6BAA6B,IAAI,SAAS,EAC1C,iBAAiB,EACjB,MAAM,6BAA6B,CAAC;AAKrC,qBACa,2BAA2B;IACvC,OAAO,CAAC,KAAK,CAAC;;IAUD,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAWrD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IA2BjE,cAAc,CAC1B,IAAI,EAAE,iBAAiB,EACvB,cAAc,EAAE,YAAY,CAAC,KAAK,CAAC,GACjC,OAAO,CAAC,yBAAyB,CAAC;CAkCrC;AAED,eAAO,MAAM,2BAA2B,6BAA6C,CAAC"}
|