import type { PrismaClient } from '@generated/prisma/client'; import type { BillingChargeList, BillingChargesQuery, ProblemDetails, Result } from '@gruperly/shared'; import { ok } from '@gruperly/shared'; import { getPaginationMetadata, getPaginationOffset } from '@/lib/pagination'; import prisma from '@/lib/prisma'; import { toBillingChargeDto } from '../../lib'; type ListBillingChargesDeps = { db?: Pick }; /** The professor's own Pro charges, newest first. */ export class ListBillingCharges { constructor(private readonly deps: ListBillingChargesDeps = {}) {} async execute( query: BillingChargesQuery, userId: string, ): Promise> { const db = this.deps.db ?? prisma; const where = { subscription: { userId } }; const [charges, total] = await Promise.all([ db.billingCharge.findMany({ where, orderBy: { createdAt: 'desc' }, skip: getPaginationOffset(query), take: query.pageSize, }), db.billingCharge.count({ where }), ]); return ok({ data: charges.map(toBillingChargeDto), pagination: getPaginationMetadata(query, total), }); } }