import type { PrismaClient } from '@generated/prisma/client'; import type { Plan, PlanLimits, ProblemDetails, Result, UpdatePlanLimitsInput } from '@gruperly/shared'; import { ok } from '@gruperly/shared'; import prisma from '@/lib/prisma'; import { logger } from '@/logger'; import { type PlanCatalog, planCatalog } from '@/modules/billing/lib'; type UpdatePlanLimitsDeps = { db?: Pick; catalog?: Pick; }; /** New limits for a plan. They apply at once to every account on it. */ export class UpdatePlanLimits { constructor(private readonly deps: UpdatePlanLimitsDeps = {}) {} async execute( plan: Plan, input: UpdatePlanLimitsInput, adminId: string, ): Promise> { const db = this.deps.db ?? prisma; const data = { ...input, updatedById: adminId }; await db.planConfig.upsert({ where: { plan }, create: { plan, ...data }, update: data }); (this.deps.catalog ?? planCatalog).invalidate(); logger.info({ plan, adminId, limits: input }, 'Plan limits updated'); return ok(input); } }