|
| 1 | +import { BaseService } from "./baseService.server"; |
| 2 | +import { tryCatch } from "@trigger.dev/core/utils"; |
| 3 | +import { setSchedulesAddOn } from "~/services/platform.v3.server"; |
| 4 | +import assertNever from "assert-never"; |
| 5 | +import { sendToPlain } from "~/utils/plain.server"; |
| 6 | +import { uiComponent } from "@team-plain/typescript-sdk"; |
| 7 | + |
| 8 | +type Input = { |
| 9 | + userId: string; |
| 10 | + organizationId: string; |
| 11 | + action: "purchase" | "quota-increase"; |
| 12 | + amount: number; |
| 13 | +}; |
| 14 | + |
| 15 | +type Result = |
| 16 | + | { |
| 17 | + success: true; |
| 18 | + } |
| 19 | + | { |
| 20 | + success: false; |
| 21 | + error: string; |
| 22 | + }; |
| 23 | + |
| 24 | +export class SetSchedulesAddOnService extends BaseService { |
| 25 | + async call({ userId, organizationId, action, amount }: Input): Promise<Result> { |
| 26 | + switch (action) { |
| 27 | + case "purchase": { |
| 28 | + const result = await setSchedulesAddOn(organizationId, amount); |
| 29 | + if (!result) { |
| 30 | + return { |
| 31 | + success: false, |
| 32 | + error: "Failed to update schedules", |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + switch (result.result) { |
| 37 | + case "success": { |
| 38 | + return { success: true }; |
| 39 | + } |
| 40 | + case "error": { |
| 41 | + return { success: false, error: result.error }; |
| 42 | + } |
| 43 | + case "max_quota_reached": { |
| 44 | + return { |
| 45 | + success: false, |
| 46 | + error: `You can't purchase more than ${result.maxQuota} schedules without requesting an increase.`, |
| 47 | + }; |
| 48 | + } |
| 49 | + default: { |
| 50 | + return { |
| 51 | + success: false, |
| 52 | + error: "Failed to update schedules, unknown result.", |
| 53 | + }; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + case "quota-increase": { |
| 58 | + const user = await this._replica.user.findFirst({ |
| 59 | + where: { id: userId }, |
| 60 | + }); |
| 61 | + |
| 62 | + if (!user) { |
| 63 | + return { success: false, error: "No matching user found." }; |
| 64 | + } |
| 65 | + |
| 66 | + const organization = await this._replica.organization.findFirst({ |
| 67 | + select: { title: true }, |
| 68 | + where: { id: organizationId }, |
| 69 | + }); |
| 70 | + |
| 71 | + const [error] = await tryCatch( |
| 72 | + sendToPlain({ |
| 73 | + userId, |
| 74 | + email: user.email, |
| 75 | + name: user.name ?? user.displayName ?? user.email, |
| 76 | + title: `Schedules quota request: ${amount}`, |
| 77 | + components: [ |
| 78 | + uiComponent.text({ |
| 79 | + text: `Org: ${organization?.title} (${organizationId})`, |
| 80 | + }), |
| 81 | + uiComponent.divider({ spacingSize: "M" }), |
| 82 | + uiComponent.text({ |
| 83 | + text: `Total schedules requested: ${amount}`, |
| 84 | + }), |
| 85 | + ], |
| 86 | + }) |
| 87 | + ); |
| 88 | + |
| 89 | + if (error) { |
| 90 | + return { success: false, error: error.message }; |
| 91 | + } |
| 92 | + |
| 93 | + return { success: true }; |
| 94 | + } |
| 95 | + default: { |
| 96 | + assertNever(action); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments