All files / src/domains/helpdesk/services helpdesk-feedback.service.ts

0% Statements 0/24
0% Branches 0/22
0% Functions 0/2
0% Lines 0/20

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                                                                                                                             
import { Injectable, BadRequestException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { CustomerSatisfactionResponse, Ticket } from '../schemas';
import { EventBusService } from '../../../platform/events/event-bus.service';
 
@Injectable()
export class HelpdeskFeedbackService {
  constructor(
    @InjectModel(CustomerSatisfactionResponse.name)
    private readonly responseModel: Model<CustomerSatisfactionResponse>,
    @InjectModel(Ticket.name)
    private readonly ticketModel: Model<Ticket>,
    private readonly eventBus: EventBusService
  ) {}
 
  async submitFeedback(
    tenantId: string,
    ticketId: string,
    score: number,
    surveyType: 'CSAT' | 'NPS',
    comment?: string
  ): Promise<CustomerSatisfactionResponse> {
    const ticket = await this.ticketModel.findOne({ _id: ticketId, tenantId }).exec();
    if (!ticket) throw new BadRequestException('Ticket not found');
 
    // Enforce unique review check per ticket
    let res = await this.responseModel.findOne({ ticketId: ticket._id, tenantId }).exec();
    if (res) throw new BadRequestException('Feedback already submitted for this ticket');
 
    // Evaluate low scores thresholds
    const isLow = (surveyType === 'CSAT' && score <= 2) || (surveyType === 'NPS' && score <= 6);
 
    res = new this.responseModel({
      tenantId,
      ticketId: ticket._id,
      score,
      surveyType,
      feedbackComment: comment,
      isLowRatingEscalated: isLow
    });
    await res.save();
 
    await this.eventBus.publish('helpdesk.feedback.received.v1', {
      feedbackId: res._id.toString(),
      ticketId,
      score,
      tenantId
    }, tenantId);
 
    if (isLow) {
      await this.eventBus.publish('helpdesk.low-rating.detected.v1', {
        feedbackId: res._id.toString(),
        ticketId,
        score,
        tenantId
      }, tenantId);
    }
 
    return res;
  }
}