g1t/apps/sudo/app/lib/billing-actions.server.ts
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 1 | /** |
| 2 | * The changes staff make to how a workspace or an enterprise pays: terms, | |
| 3 | * moving workspaces on and off enterprises, credits, and Stripe billing | |
| 4 | * links. What is acted on comes from the billing service and identity, not | |
| 5 | * from the form; each change shows a confirmation first, and a credit | |
| 6 | * needs the workspace's slug typed out. | |
| 7 | */ | |
| 8 | import type { Terms } from "@g1t/contracts"; | |
| 9 | import { data, redirect } from "react-router"; | |
| 10 | ||
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 11 | import { fields, parseAllowances, parseCredit, parseEmail, parseGoodwill, parseNote, parsePayment, parseSlug, parseTerms, text } from "./forms"; |
| 12 | import { FORGIVE_COST_MICROS, goodwillWarning } from "./pricing"; | |
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 13 | import type { ActionData } from "./review"; |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 14 | import { admin, identity, priceBook } from "./services.server"; |
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 15 | import type { Staff } from "./staff"; |
| 16 | import type { Enterprise } from "./workspaces"; | |
| 17 | ||
| 18 | /** What a page acts on. `accountId` is billing's internal id, never shown. */ | |
| 19 | export type Subject = | |
| 20 | | { kind: "workspace"; slug: string; accountId: string; terms: Terms; billedTo: Enterprise | null } | |
| Stripe webhooks, enterprise invoices, and sudo for both | 21 | | { kind: "enterprise"; accountId: string; name: string; terms: Terms; workspaces: string[]; billingEmail: string | null }; |
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 22 | |
| 23 | function failed(section: string, error: string, values?: Record<string, string>) { | |
| 24 | return data<ActionData>({ error, section, values }, { status: 422 }); | |
| 25 | } | |
| 26 | ||
| 27 | export async function billingAction(request: Request, staff: Staff, subject: Subject, path: string) { | |
| 28 | const form = await request.formData(); | |
| 29 | const intent = text(form, "intent"); | |
| 30 | const confirmed = text(form, "confirm") === "yes"; | |
| 31 | const back = (done: string) => redirect(`${path}?done=${done}#top`); | |
| 32 | const isEnterprise = subject.kind === "enterprise"; | |
| 33 | ||
| 34 | if (intent === "terms") { | |
| 35 | const values = fields(form, "kind", "discount", "ceiling", "note", "until"); | |
| 36 | if (subject.kind === "workspace" && subject.billedTo) { | |
| 37 | return failed("terms", `This workspace is charged on ${subject.billedTo.name}'s terms. Change them on the enterprise.`, values); | |
| 38 | } | |
| 39 | const terms = parseTerms(form, staff.email); | |
| 40 | if (!terms.ok) return failed("terms", terms.error, values); | |
| 41 | if (!confirmed) return { review: { intent, before: subject.terms, after: terms.value, fields: values } } satisfies ActionData; | |
| 42 | const result = await admin.setTerms(subject.accountId, terms.value, staff.email); | |
| 43 | if (!result.ok) return failed("terms", result.error.message, values); | |
| 44 | return back("terms"); | |
| 45 | } | |
| 46 | ||
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 47 | if (intent === "allowances") { |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 48 | // The plan without its price, the account's share of g1t's pools, and staff's overrides. |
| 49 | const values = fields(form, "plan", "oss", "trial", "agents", "runCap", "issueCap", "hold", "note"); | |
| Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put | 50 | if (subject.kind === "workspace" && subject.billedTo) { |
| 51 | return failed("allowances", `This workspace is on ${subject.billedTo.name}. Set its plan and pools on the enterprise.`, values); | |
| 52 | } | |
| 53 | const allowances = parseAllowances(form); | |
| 54 | if (!allowances.ok) return failed("allowances", allowances.error, values); | |
| 55 | const note = parseNote(values.note); | |
| 56 | if (!note.ok) return failed("allowances", note.error, values); | |
| 57 | const result = await admin.setAllowances(subject.accountId, allowances.value, note.value, staff.email); | |
| 58 | if (!result.ok) return failed("allowances", result.error.message, values); | |
| 59 | return back("allowances"); | |
| 60 | } | |
| 61 | ||
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 62 | if (intent === "attach") { |
| 63 | const values = fields(form, "workspace", "target"); | |
| 64 | const section = isEnterprise ? "members" : "billed-to"; | |
| 65 | let slug: string; | |
| 66 | let target: Enterprise; | |
| 67 | if (subject.kind === "enterprise") { | |
| 68 | const parsed = parseSlug(values.workspace); | |
| 69 | if (!parsed.ok) return failed(section, parsed.error, values); | |
| 70 | slug = parsed.value; | |
| 71 | if (subject.workspaces.includes(slug)) return failed(section, `${slug} is already on this enterprise.`, values); | |
| 72 | if (!(await identity.workspace(slug))) return failed(section, `There is no workspace called ${slug}.`, values); | |
| 73 | target = { id: subject.accountId, name: subject.name }; | |
| 74 | } else { | |
| 75 | slug = subject.slug; | |
| 76 | const enterprise = (await admin.accounts()).find((row) => row.account.kind === "enterprise" && row.account.id === values.target); | |
| 77 | if (!enterprise) return failed(section, "Choose an enterprise to move onto.", values); | |
| 78 | target = { id: enterprise.account.id, name: enterprise.account.name }; | |
| 79 | } | |
| 80 | if (!confirmed) return { review: { intent, workspace: slug, targetName: target.name, fields: values } } satisfies ActionData; | |
| 81 | const result = await admin.attach(slug, target.id, staff.email); | |
| 82 | if (!result.ok) return failed(section, result.error.message, values); | |
| 83 | return back("attach"); | |
| 84 | } | |
| 85 | ||
| 86 | if (intent === "detach") { | |
| 87 | const values = fields(form, "workspace"); | |
| 88 | const section = isEnterprise ? "members" : "billed-to"; | |
| 89 | let slug: string; | |
| 90 | let from: string; | |
| 91 | if (subject.kind === "enterprise") { | |
| 92 | slug = values.workspace; | |
| 93 | if (!subject.workspaces.includes(slug)) return failed(section, `${slug} is not on this enterprise.`); | |
| 94 | from = subject.name; | |
| 95 | } else { | |
| 96 | if (!subject.billedTo) return failed(section, `${subject.slug} already pays for itself.`); | |
| 97 | slug = subject.slug; | |
| 98 | from = subject.billedTo.name; | |
| 99 | } | |
| 100 | if (!confirmed) return { review: { intent, workspace: slug, from, fields: values } } satisfies ActionData; | |
| 101 | const result = await admin.attach(slug, null, staff.email); | |
| 102 | if (!result.ok) return failed(section, result.error.message); | |
| 103 | return back("detach"); | |
| 104 | } | |
| 105 | ||
| 106 | if (intent === "credit") { | |
| 107 | const values = fields(form, "workspace", "amount", "note", "confirmation"); | |
| 108 | const workspace = subject.kind === "enterprise" ? values.workspace : subject.slug; | |
| 109 | if (subject.kind === "enterprise" && !subject.workspaces.includes(workspace)) { | |
| 110 | return failed("credit", "Choose one of this enterprise's workspaces.", values); | |
| 111 | } | |
| 112 | const amount = parseCredit(values.amount); | |
| 113 | if (!amount.ok) return failed("credit", amount.error, values); | |
| 114 | const note = parseNote(values.note); | |
| 115 | if (!note.ok) return failed("credit", note.error, values); | |
| 116 | if (values.confirmation !== workspace) { | |
| 117 | return failed("credit", `Type the workspace's slug, ${workspace}, exactly, to issue the credit.`, { ...values, confirmation: "" }); | |
| 118 | } | |
| 119 | const result = await admin.credit(workspace, amount.value, note.value, staff.email); | |
| 120 | if (!result.ok) return failed("credit", result.error.message, values); | |
| 121 | return back("credit"); | |
| 122 | } | |
| 123 | ||
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 124 | if (intent === "payment") { |
| 125 | // A bank transfer that reached g1t outside Stripe's page. | |
| 126 | const values = fields(form, "amount", "reference", "note", "confirmation"); | |
| 127 | if (subject.kind !== "workspace") return failed("top", "Record a payment on the workspace it is for."); | |
| 128 | const payment = parsePayment(form, subject.slug); | |
| 129 | if (!payment.ok) return failed("payment", payment.error, { ...values, confirmation: "" }); | |
| 130 | const { amountMicros, reference, note } = payment.value; | |
| 131 | const result = await admin.recordPayment(subject.slug, amountMicros, reference, note, staff.email); | |
| 132 | if (!result.ok) return failed("payment", result.error.message, values); | |
| 133 | return back("payment"); | |
| 134 | } | |
| 135 | ||
| 136 | if (intent === "goodwill") { | |
| 137 | // A goodwill credit from the workspace's page: the overage, as billing quotes it now. | |
| 138 | const values = fields(form, "amount", "reason", "day"); | |
| 139 | if (subject.kind !== "workspace") return failed("top", "Goodwill is given to one workspace."); | |
| 140 | const quote = await goodwillQuote(subject.slug); | |
| 141 | const parsed = parseGoodwill(form, (amount) => | |
| 142 | // A reason past the one-click credit, a second within 12 months, or past the cap on real cost. | |
| 143 | quote ? ((w) => w.needsReason || w.overCap)(goodwillWarning(amount, quote.goodwill, quote.lastGoodwillAt, quote.cap)) : amount != null, | |
| 144 | ); | |
| 145 | if (!parsed.ok) return failed("goodwill", parsed.error, values); | |
| 146 | const result = await admin.goodwill(subject.slug, parsed.value.amountMicros, parsed.value.reason, staff.email, parsed.value.day); | |
| 147 | if (!result.ok) return failed("goodwill", result.error.message, values); | |
| 148 | return back("goodwill"); | |
| 149 | } | |
| 150 | ||
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 151 | if (intent === "billing-link") { |
| 152 | if (subject.kind !== "workspace") return failed("top", "Billing links are made from a workspace's page."); | |
| 153 | if (!confirmed) return { review: { intent, workspace: subject.slug, fields: {} } } satisfies ActionData; | |
| 154 | const result = await admin.billingLink(subject.slug, staff.email); | |
| 155 | if (!result.ok) return failed("billing-link", result.error.message); | |
| 156 | // Shown once, in this response only: it is never stored or redirected to. | |
| 157 | return { link: result.value, workspace: subject.slug } satisfies ActionData; | |
| 158 | } | |
| 159 | ||
| Stripe webhooks, enterprise invoices, and sudo for both | 160 | if (intent === "billing-email") { |
| 161 | const values = fields(form, "email"); | |
| 162 | if (subject.kind !== "enterprise") return failed("top", "Only an enterprise has an invoice email."); | |
| 163 | const email = parseEmail(values.email); | |
| 164 | if (!email.ok) return failed("invoices", email.error, values); | |
| 165 | if (email.value === subject.billingEmail) return failed("invoices", "That is already where its invoices go.", values); | |
| 166 | if (!confirmed) { | |
| 167 | return { | |
| 168 | review: { intent, name: subject.name, before: subject.billingEmail, after: email.value, fields: { email: email.value } }, | |
| 169 | } satisfies ActionData; | |
| 170 | } | |
| 171 | const result = await admin.enterpriseBilling(subject.accountId, email.value, staff.email); | |
| 172 | if (!result.ok) return failed("invoices", result.error.message, values); | |
| 173 | return back("billing-email"); | |
| 174 | } | |
| 175 | ||
| 176 | if (intent === "invoice") { | |
| 177 | if (subject.kind !== "enterprise") return failed("top", "Only an enterprise is invoiced from sudo."); | |
| 178 | if (!confirmed) { | |
| 179 | return { | |
| 180 | review: { intent, name: subject.name, email: subject.billingEmail, workspaces: subject.workspaces.length, fields: {} }, | |
| 181 | } satisfies ActionData; | |
| 182 | } | |
| 183 | const result = await admin.invoiceEnterprise(subject.accountId, staff.email); | |
| 184 | if (!result.ok) return failed("invoices", result.error.message); | |
| 185 | // Shown in this response; the invoice is also in the list from now on. | |
| 186 | return { invoice: result.value } satisfies ActionData; | |
| 187 | } | |
| 188 | ||
| Billing on Stripe's pages, month-end charges, warnings; sudo by workspace | 189 | return failed("top", "Unknown action."); |
| 190 | } | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 191 | |
| 192 | /** The workspace's goodwill quote from the Overages queue, if it is in it, and the cap on real cost. */ | |
| 193 | export async function goodwillQuote( | |
| 194 | workspace: string, | |
| 195 | ): Promise<{ goodwill: { creditMicros: number; marginMicros: number }; lastGoodwillAt: string | null; cap: number } | null> { | |
| 196 | const [queue, book] = await Promise.all([admin.overages().catch(() => []), priceBook().catch(() => null)]); | |
| 197 | const row = queue.find((overage) => overage.workspace === workspace); | |
| 198 | const cap = book?.free?.overageForgiveCostMicros || FORGIVE_COST_MICROS; | |
| 199 | return row ? { goodwill: row.goodwill, lastGoodwillAt: row.lastGoodwillAt, cap } : null; | |
| 200 | } |