All files / src/domains/crm/processors crm.processor.ts

0% Statements 0/23
0% Branches 0/16
0% Functions 0/2
0% Lines 0/21

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                                                                                             
import { Processor, WorkerHost } from '@nestjs/bullmq';
import { Job } from 'bullmq';
import { Logger } from '@nestjs/common';
import { LeadService } from '../services/lead.service';
import { OpportunityService } from '../services/opportunity.service';
import { Types } from 'mongoose';
 
@Processor('crm-processing')
export class CrmProcessor extends WorkerHost {
  private readonly logger = new Logger(CrmProcessor.name);
 
  constructor(
    private readonly leadService: LeadService,
    private readonly opportunityService: OpportunityService,
  ) {
    super();
  }
 
  async process(job: Job<any, any, string>): Promise<any> {
    const { tenantId, leadId, opportunityId, userId } = job.data;
    const tid = new Types.ObjectId(tenantId);
 
    this.logger.log(`Received CRM job: ${job.name} for tenant ${tenantId}`);
 
    if (job.name === 'crm.lead.score') {
      if (leadId) {
        const lead = await this.leadService.calculateLeadScore(
          tid,
          leadId,
          userId ? new Types.ObjectId(userId) : undefined,
        );
        return { status: 'scored', leadScore: lead.leadScore };
      }
    }
 
    if (job.name === 'crm.stale.check') {
      // Mock checking stale logic
      this.logger.log(
        `Checking stale leads and opportunities for tenant ${tenantId}`,
      );
      return { status: 'checked' };
    }
 
    return { status: 'ignored' };
  }
}