import { Failure, Ok, type ProblemDetails, problems, type Result, type ShoppingListDetail, } from '@superlist/shared' import type { Database } from '@/lib/prisma' import { toDetail } from '../serialize' /** Returns a list with its members. Only members can read it. */ export class GetShoppingList { constructor(private readonly db: Database) {} async execute(input: { listId: string userId: string }): Promise> { const membership = await this.db.shoppingListMember.findUnique({ where: { listId_userId: { listId: input.listId, userId: input.userId } }, include: { list: { include: { members: { include: { user: true }, orderBy: [{ role: 'asc' }, { createdAt: 'asc' }], }, invitations: { where: { status: 'pending' }, orderBy: { createdAt: 'desc' }, }, }, }, }, }) if (!membership) { return Failure(problems.notFound('La lista no existe o no tienes acceso.')) } return Ok(toDetail(membership)) } }