import prisma from '../lib/prisma'; import { HttpError } from '../lib/httpError'; import * as notificationService from './notificationService'; import { logger } from '../lib/logger'; export async function addComment(ticketIdOrDisplay: string, body: string, actorId: string) { const ticket = await prisma.ticket.findFirst({ where: { OR: [{ id: ticketIdOrDisplay }, { displayId: ticketIdOrDisplay }] }, }); if (!ticket) throw new HttpError(404, 'Ticket not found'); const [comment] = await prisma.$transaction([ prisma.comment.create({ data: { body, ticketId: ticket.id, authorId: actorId }, include: { author: { select: { id: true, username: true, displayName: true } } }, }), prisma.auditLog.create({ data: { ticketId: ticket.id, userId: actorId, action: 'COMMENT_ADDED', detail: body }, }), ]); notificationService .notifyCommentCreated(ticket.id, comment.id) .catch((err: Error) => logger.error({ err }, 'notifyCommentCreated failed')); const usernames = notificationService.parseMentions(body); if (usernames.length > 0) { prisma.user .findMany({ where: { username: { in: usernames } }, select: { id: true } }) .then((users) => { const ids = users.map((u) => u.id).filter((id) => id !== actorId); if (ids.length > 0) { return notificationService.notifyMention(ticket.id, comment.id, ids); } }) .catch((err: Error) => logger.error({ err }, 'mention notification failed')); } return comment; } export async function deleteComment( commentId: string, actor: { id: string; role: string }, ) { const comment = await prisma.comment.findUnique({ where: { id: commentId } }); if (!comment) throw new HttpError(404, 'Comment not found'); if (comment.authorId !== actor.id && actor.role !== 'ADMIN') { throw new HttpError(403, 'Not allowed'); } await prisma.$transaction([ prisma.comment.delete({ where: { id: commentId } }), prisma.auditLog.create({ data: { ticketId: comment.ticketId, userId: actor.id, action: 'COMMENT_DELETED', detail: comment.body, }, }), ]); }