import type { PrismaClient } from '@prisma/client'; import { badRequest, notFound } from '../lib/errors.js'; import { assertActiveTrainee } from './trainer.js'; export interface CommentInput { body: string; sessionId?: string; sessionExerciseId?: string; } /** * The anchor must be a COMPLETED session of the owner; the exercise (when * given) must belong to that session and not be removed. */ async function assertSessionAnchor(prisma: PrismaClient, ownerId: string, input: CommentInput) { if (!input.sessionId) throw badRequest('sessionId is required'); const session = await prisma.trainingSession.findUnique({ where: { id: input.sessionId }, include: { day: { select: { week: { select: { plan: { select: { userId: true } } } } } } }, }); // A session of another user is indistinguishable from a missing one. if (!session || session.day.week.plan.userId !== ownerId) throw notFound('Session not found'); if (session.status !== 'COMPLETED') throw badRequest('Session is not completed'); if (input.sessionExerciseId) { const se = await prisma.sessionExercise.findFirst({ where: { id: input.sessionExerciseId, sessionId: session.id }, }); if (!se || se.removed) throw badRequest('Exercise does not belong to this session'); } } /** Trainer comment on the trainee (target = trainee, author = trainer). */ export async function addTraineeComment( prisma: PrismaClient, trainerId: string, traineeId: string, input: CommentInput, ) { await assertActiveTrainee(prisma, trainerId, traineeId); await assertSessionAnchor(prisma, traineeId, input); const comment = await prisma.comment.create({ data: { authorId: trainerId, targetUserId: traineeId, sessionId: input.sessionId ?? null, sessionExerciseId: input.sessionExerciseId ?? null, body: input.body, }, }); return { comment }; } /** Delete a trainer comment on the trainee (author = trainer only). */ export async function deleteTraineeComment( prisma: PrismaClient, trainerId: string, traineeId: string, commentId: string, ) { await assertActiveTrainee(prisma, trainerId, traineeId); const comment = await prisma.comment.findFirst({ where: { id: commentId, targetUserId: traineeId, authorId: trainerId }, }); if (!comment) throw notFound('Comment not found'); await prisma.comment.delete({ where: { id: comment.id } }); } /** User comment on their own session (target = author = the user). */ export async function addMyComment(prisma: PrismaClient, userId: string, input: CommentInput) { await assertSessionAnchor(prisma, userId, input); const comment = await prisma.comment.create({ data: { authorId: userId, targetUserId: userId, sessionId: input.sessionId ?? null, sessionExerciseId: input.sessionExerciseId ?? null, body: input.body, }, }); return { comment }; } /** Delete a comment the user made on themselves. */ export async function deleteMyComment(prisma: PrismaClient, userId: string, commentId: string) { const comment = await prisma.comment.findFirst({ where: { id: commentId, targetUserId: userId, authorId: userId }, }); if (!comment) throw notFound('Comment not found'); await prisma.comment.delete({ where: { id: comment.id } }); }