All files / apps/work-orders-service/src/triage work-order-triage.consumer.ts

100% Statements 18/18
100% Branches 14/14
100% Functions 2/2
100% Lines 16/16

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 482x 2x 2x   2x 2x   2x                   2x 3x     3x 3x                 2x 2x           2x 1x     1x   1x      
import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq';
import { Injectable, Logger } from '@nestjs/common';
import { EXCHANGES, WORK_ORDER_EVENTS } from '@app/contracts';
import type { WorkOrderEvent } from '@app/contracts';
import { TriageClassifier } from './triage-classifier';
import { TriageService } from './triage.service';
 
export const TRIAGE_QUEUE = 'work-orders.triage';
 
/**
 * Triage rides the same event that fans out to notifications — the service
 * reacts to its own domain event instead of classifying inline, so the write
 * path never waits on (or fails with) the LLM call. No retry/DLQ here on
 * purpose: a failed classification returns null rather than throwing, and
 * re-running a paid, non-deterministic call on redelivery buys little.
 */
@Injectable()
export class WorkOrderTriageConsumer {
  private readonly logger = new Logger(WorkOrderTriageConsumer.name);
 
  constructor(
    private readonly classifier: TriageClassifier,
    private readonly triage: TriageService,
  ) {}
 
  @RabbitSubscribe({
    exchange: EXCHANGES.EVENTS,
    routingKey: WORK_ORDER_EVENTS.CREATED,
    queue: TRIAGE_QUEUE,
    queueOptions: { durable: true },
  })
  async onWorkOrderCreated(event: WorkOrderEvent): Promise<void> {
    const triage = await this.classifier.classify({
      title: event.data.title,
      description: event.data.description,
      priority: event.data.priority,
    });
 
    if (!triage) {
      this.logger.warn(
        `no classification for work order ${event.data.workOrderId}, skipping`,
      );
      return;
    }
    await this.triage.apply(event.data.workOrderId, triage);
  }
}