Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { Coupon, CouponDocument } from '../../database/schemas/coupon.schema'; @Injectable() export class CouponsService { constructor( @InjectModel(Coupon.name) private couponModel: Model<CouponDocument>, ) {} async findAll(filters: { retailer?: string; category?: string; page: number; limit: number; }): Promise<{ coupons: CouponDocument[]; total: number; page: number; totalPages: number; }> { const query: any = { status: 'active' }; Iif (filters.retailer) { // Would need to populate retailer and filter by name // For now, assume retailer is the ObjectId query.retailerId = filters.retailer; } Iif (filters.category) { query['conditions.categories'] = filters.category; } const skip = (filters.page - 1) * filters.limit; const [coupons, total] = await Promise.all([ this.couponModel .find(query) .populate('retailerId') .sort({ priority: -1, createdAt: -1 }) .skip(skip) .limit(filters.limit) .exec(), this.couponModel.countDocuments(query), ]); return { coupons, total, page: filters.page, totalPages: Math.ceil(total / filters.limit), }; } async findOne(id: string): Promise<CouponDocument | null> { return this.couponModel.findById(id).populate('retailerId').exec(); } async create(couponData: Partial<Coupon>): Promise<CouponDocument> { const newCoupon = new this.couponModel({ ...couponData, createdAt: new Date(), updatedAt: new Date(), }); return newCoupon.save(); } async update(id: string, updateData: Partial<Coupon>): Promise<CouponDocument | null> { return this.couponModel.findByIdAndUpdate( id, { ...updateData, updatedAt: new Date() }, { new: true } ).exec(); } async delete(id: string): Promise<boolean> { const result = await this.couponModel.findByIdAndDelete(id).exec(); return !!result; } async findByCode(code: string, retailerId?: string): Promise<CouponDocument | null> { const query: any = { code, status: 'active' }; Iif (retailerId) { query.retailerId = retailerId; } return this.couponModel.findOne(query).populate('retailerId').exec(); } async findActiveCoupons(): Promise<CouponDocument[]> { return this.couponModel.find({ status: 'active', validFrom: { $lte: new Date() }, validUntil: { $gte: new Date() }, }).populate('retailerId').exec(); } async updateCouponPerformance( id: string, performance: { usageCount?: number; savingsGenerated?: number; conversionRate?: number; } ): Promise<void> { await this.couponModel.findByIdAndUpdate(id, { $inc: { 'performance.usageCount': performance.usageCount || 0, 'performance.savingsGenerated': performance.savingsGenerated || 0, }, ...(performance.conversionRate && { 'performance.conversionRate': performance.conversionRate, }), updatedAt: new Date(), }).exec(); } async expireCoupons(): Promise<number> { const result = await this.couponModel.updateMany( { status: 'active', validUntil: { $lt: new Date() }, }, { status: 'expired', updatedAt: new Date(), } ).exec(); return result.modifiedCount; } async getCouponsByRetailer(retailerId: string): Promise<CouponDocument[]> { return this.couponModel.find({ retailerId, status: 'active', }).exec(); } async getCouponsByCategory(category: string): Promise<CouponDocument[]> { return this.couponModel.find({ 'conditions.categories': category, status: 'active', }).populate('retailerId').exec(); } } |