import type { PrismaClient } from '@generated/prisma/client'; import type { AttendeeDto, ProblemDetails, Result, UpdateAttendee } from '@gruperly/shared'; import { err, ok } from '@gruperly/shared'; import { notFoundResourceProblem } from '@/http/problem-builders'; import prisma from '@/lib/prisma'; import { findGroupForUser } from '@/modules/group-waitlist/lib/helpers'; import { toAttendeeDto } from '../../lib/helpers'; type UpdateAttendeeDeps = { db?: Pick; }; type UpdateAttendeeCommand = { groupId: string; attendeeId: string; userId: string; input: UpdateAttendee; }; export class UpdateAttendeeDetails { constructor(private readonly deps: UpdateAttendeeDeps = {}) {} async execute(command: UpdateAttendeeCommand): Promise> { const { groupId, attendeeId, userId, input } = command; const db = this.deps.db ?? prisma; const access = await findGroupForUser(db, groupId, userId); if (!access.ok) return access; const attendee = await db.attendee.findFirst({ where: { id: attendeeId, groupId } }); if (!attendee) return err(notFoundResourceProblem('Attendee', attendeeId)); const updated = await db.attendee.update({ where: { id: attendeeId }, data: normalizeEditableFields(input), }); return ok(toAttendeeDto(updated)); } } function normalizeEditableFields(input: UpdateAttendee): UpdateAttendee { return Object.fromEntries( Object.entries(input).map(([key, value]) => [key, value === '' ? null : value]), ); }