import { BaseService } from "./baseService.server"; import { tryCatch } from "@trigger.dev/core/utils"; import { setSeatsAddOn } from "~/services/platform.v3.server"; import assertNever from "assert-never"; import { sendToPlain } from "~/utils/plain.server"; import { uiComponent } from "@team-plain/typescript-sdk"; type Input = { userId: string; organizationId: string; action: "purchase" | "quota-increase"; amount: number; }; type Result = | { success: true; } | { success: false; error: string; }; export class SetSeatsAddOnService extends BaseService { async call({ userId, organizationId, action, amount }: Input): Promise { switch (action) { case "purchase": { const result = await setSeatsAddOn(organizationId, amount); if (!result) { return { success: false, error: "Failed to update seats", }; } switch (result.result) { case "success": { return { success: true }; } case "error": { return { success: false, error: result.error }; } case "max_quota_reached": { return { success: false, error: `You can't purchase more than ${result.maxQuota} seats without requesting an increase.`, }; } default: { return { success: false, error: "Failed to update seats, unknown result.", }; } } } case "quota-increase": { const user = await this._replica.user.findFirst({ where: { id: userId }, }); if (!user) { return { success: false, error: "No matching user found." }; } const organization = await this._replica.organization.findFirst({ select: { title: true }, where: { id: organizationId }, }); const [error] = await tryCatch( sendToPlain({ userId, email: user.email, name: user.name ?? user.displayName ?? user.email, title: `Seats quota request: ${amount}`, components: [ uiComponent.text({ text: `Org: ${organization?.title} (${organizationId})`, }), uiComponent.divider({ spacingSize: "M" }), uiComponent.text({ text: `Total seats requested: ${amount}`, }), ], }) ); if (error) { return { success: false, error: error.message }; } return { success: true }; } default: { assertNever(action); } } } }