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 | import { Injectable, Logger } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { CrmAutomationRule, CrmAutomationLog, } from '../schemas/automation-import.schema'; @Injectable() export class CrmAutomationService { private readonly logger = new Logger(CrmAutomationService.name); constructor( @InjectModel(CrmAutomationRule.name) private readonly ruleModel: Model<CrmAutomationRule>, @InjectModel(CrmAutomationLog.name) private readonly logModel: Model<CrmAutomationLog>, ) {} async createRule( tenantId: Types.ObjectId, data: Partial<CrmAutomationRule>, ): Promise<CrmAutomationRule> { return this.ruleModel.create({ ...data, tenantId }); } async listRules( tenantId: Types.ObjectId, active?: boolean, ): Promise<CrmAutomationRule[]> { const filter: any = { tenantId }; if (active !== undefined) { filter.active = active; } return this.ruleModel.find(filter).sort({ priority: -1 }).exec(); } async logExecution( tenantId: Types.ObjectId, ruleId: Types.ObjectId, triggerEvent: string, entityType: string, entityId: Types.ObjectId, result: string, actionResults?: any, errorMessage?: string, ): Promise<CrmAutomationLog> { await this.ruleModel .updateOne( { _id: ruleId }, { $inc: { executionCount: 1 }, $set: { lastExecutedAt: new Date() } }, ) .exec(); return this.logModel.create({ tenantId, ruleId, triggerEvent, entityType, entityId, result, actionResults, errorMessage, }); } async getLogs( tenantId: Types.ObjectId, ruleId: string, ): Promise<CrmAutomationLog[]> { return this.logModel .find({ tenantId, ruleId: new Types.ObjectId(ruleId) }) .sort({ createdAt: -1 }) .exec(); } } |