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 | import { Injectable, Logger } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { SalesTarget, CommissionPlan, CommissionLog, } from '../schemas/automation-import.schema'; @Injectable() export class SalesTargetService { private readonly logger = new Logger(SalesTargetService.name); constructor( @InjectModel(SalesTarget.name) private readonly targetModel: Model<SalesTarget>, @InjectModel(CommissionPlan.name) private readonly planModel: Model<CommissionPlan>, @InjectModel(CommissionLog.name) private readonly logModel: Model<CommissionLog>, ) {} async createTarget( tenantId: Types.ObjectId, data: Partial<SalesTarget>, ): Promise<SalesTarget> { return this.targetModel.create({ ...data, tenantId }); } async getTargets( tenantId: Types.ObjectId, userId?: string, ): Promise<SalesTarget[]> { const filter: any = { tenantId }; if (userId) { filter.userId = new Types.ObjectId(userId); } return this.targetModel.find(filter).exec(); } async calculateTargetAchievement( tenantId: Types.ObjectId, targetId: string, actualValueMinor: number, ): Promise<SalesTarget | null> { const target = await this.targetModel .findOne({ _id: targetId, tenantId }) .exec(); if (!target) return null; const targetVal = target.targetValueMinor || 1; const pct = Math.round((actualValueMinor / targetVal) * 100); return this.targetModel .findOneAndUpdate( { _id: targetId }, { $set: { achievedValueMinor: actualValueMinor, achievementPercentage: pct, }, }, { new: true }, ) .exec(); } async createCommissionPlan( tenantId: Types.ObjectId, data: Partial<CommissionPlan>, ): Promise<CommissionPlan> { return this.planModel.create({ ...data, tenantId }); } async logCommission( tenantId: Types.ObjectId, data: Partial<CommissionLog>, ): Promise<CommissionLog> { return this.logModel.create({ ...data, tenantId }); } async getCommissionLogs( tenantId: Types.ObjectId, userId: string, ): Promise<CommissionLog[]> { return this.logModel .find({ tenantId, userId: new Types.ObjectId(userId) }) .sort({ createdAt: -1 }) .exec(); } } |