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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | import { Injectable, BadRequestException, NotFoundException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { Ticket, TicketComment, TicketMerge, TicketAttachment } from '../schemas'; import { NumberSeriesService } from '../../mdm/application/services/number-series.service'; import { EventBusService } from '../../../platform/events/event-bus.service'; @Injectable() export class HelpdeskTicketService { constructor( @InjectModel(Ticket.name) private readonly ticketModel: Model<Ticket>, @InjectModel(TicketComment.name) private readonly commentModel: Model<TicketComment>, @InjectModel(TicketMerge.name) private readonly mergeModel: Model<TicketMerge>, @InjectModel(TicketAttachment.name) private readonly attachmentModel: Model<TicketAttachment>, private readonly numberSeries: NumberSeriesService, private readonly eventBus: EventBusService ) {} async createTicket( tenantId: string, data: any, requesterId: string, requesterType: 'Employee' | 'CustomerContact' ): Promise<Ticket> { // Generate next ticket number atomic concurrency-safe let ticketNumber: string; try { ticketNumber = await this.numberSeries.generateNext('TKT', { tenantId }); } catch { // Fallback if series is not pre-seeded const count = await this.ticketModel.countDocuments({ tenantId }).exec(); ticketNumber = `TKT-${String(count + 1).padStart(6, '0')}`; } const ticket = new this.ticketModel({ ...data, tenantId, ticketNumber, requesterId, requesterType, status: 'open' }); await ticket.save(); await this.eventBus.publish('helpdesk.ticket.created.v1', { ticketId: ticket._id.toString(), ticketNumber, tenantId, subject: ticket.subject, priority: ticket.priority, requesterId }, tenantId); return ticket; } async getTicketById(tenantId: string, ticketId: string, clientPortalContext = false): Promise<any> { const ticket = await this.ticketModel.findOne({ _id: ticketId, tenantId }).exec(); if (!ticket) throw new NotFoundException('Ticket not found'); // If client portal, hide internal comments and cost fields const commentsQuery: any = { ticketId: ticket._id, tenantId }; if (clientPortalContext) { commentsQuery.isInternalNote = false; // Hide notes from customers } const comments = await this.commentModel.find(commentsQuery).sort({ createdAt: 1 }).exec(); const attachments = await this.attachmentModel.find({ ticketId: ticket._id, tenantId }).exec(); return { ticket, comments, attachments }; } async addComment( tenantId: string, ticketId: string, authorId: string, authorName: string, authorType: 'Agent' | 'Customer' | 'System', content: string, isInternalNote = false ): Promise<TicketComment> { const ticket = await this.ticketModel.findOne({ _id: ticketId, tenantId }).exec(); if (!ticket) throw new NotFoundException('Ticket not found'); const comment = new this.commentModel({ tenantId, ticketId: ticket._id, authorId, authorName, authorType, content, isInternalNote }); await comment.save(); // Reopen ticket if customer replies if (authorType === 'Customer' && (ticket.status === 'resolved' || ticket.status === 'closed')) { ticket.status = 'open'; await ticket.save(); await this.eventBus.publish('helpdesk.ticket.reopened.v1', { ticketId: ticket._id.toString(), ticketNumber: ticket.ticketNumber, tenantId }, tenantId); } return comment; } async mergeTickets(tenantId: string, sourceId: string, targetId: string, userId: string): Promise<TicketMerge> { const source = await this.ticketModel.findOne({ _id: sourceId, tenantId }).exec(); const target = await this.ticketModel.findOne({ _id: targetId, tenantId }).exec(); if (!source || !target) throw new BadRequestException('Source or target ticket not found'); source.status = 'merged'; await source.save(); const merge = new this.mergeModel({ tenantId, sourceTicketId: source._id, targetTicketId: target._id, mergedById: userId }); await merge.save(); // Copy comments from source to target const comments = await this.commentModel.find({ ticketId: source._id }).exec(); for (const c of comments) { await this.commentModel.create({ tenantId, ticketId: target._id, authorId: c.authorId, authorName: c.authorName, authorType: c.authorType, content: `[Merged from ${source.ticketNumber}]: ${c.content}`, isInternalNote: c.isInternalNote }); } await this.eventBus.publish('helpdesk.ticket.merged.v1', { sourceTicketId: sourceId, targetTicketId: targetId, tenantId }, tenantId); return merge; } async splitTicket( tenantId: string, ticketId: string, subject: string, description: string, userId: string ): Promise<Ticket> { const parent = await this.ticketModel.findOne({ _id: ticketId, tenantId }).exec(); if (!parent) throw new NotFoundException('Parent ticket not found'); // Create child ticket linked to parent const child = await this.createTicket( tenantId, { subject, description, priority: parent.priority, category: parent.category, subcategory: parent.subcategory, parentTicketId: parent._id }, parent.requesterId, parent.requesterType as any ); // Record internal reference note await this.addComment( tenantId, parent._id.toString(), userId, 'System', 'System', `Ticket split completed. Child ticket: ${child.ticketNumber}`, true ); return child; } async resolveTicket(tenantId: string, ticketId: string, resolutionText: string): Promise<Ticket> { const ticket = await this.ticketModel.findOne({ _id: ticketId, tenantId }).exec(); if (!ticket) throw new NotFoundException('Ticket not found'); ticket.status = 'resolved'; ticket.resolvedAt = new Date(); await ticket.save(); await this.addComment(tenantId, ticketId, 'System', 'System', 'System', `Resolution: ${resolutionText}`, false); await this.eventBus.publish('helpdesk.ticket.resolved.v1', { ticketId: ticketId, ticketNumber: ticket.ticketNumber, tenantId }, tenantId); return ticket; } } |