import type { Prisma } from '@generated/prisma/client'; import type { GroupList, GroupQuery, ProblemDetails, Result, } from '@gruperly/shared'; import { ok } from '@gruperly/shared'; import { getPaginationMetadata, getPaginationOffset } from '@/lib/pagination'; import prisma from '@/lib/prisma'; import { buildGroupWhereForUser, type GroupDb, type GroupRecord, toGroupDto, } from '../../lib'; type ListGroupsDeps = { db?: Pick; }; export class ListGroups { constructor(private readonly deps: ListGroupsDeps = {}) {} async execute( query: GroupQuery, userId: string, ): Promise> { const db = this.deps.db ?? prisma; const where = buildGroupWhereForUser(userId) as Prisma.GroupWhereInput; const [records, total] = await Promise.all([ db.group.findMany({ where, skip: getPaginationOffset(query), take: query.pageSize, orderBy: { createdAt: 'desc' }, }), db.group.count({ where }), ]); return ok({ data: records.map((record: GroupRecord) => toGroupDto(record)), pagination: getPaginationMetadata(query, total), }); } }