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: credits with a kind and expiry, discounts instead of comped, and safer charging | 1 | /** |
| 2 | * An account's terms as sudo names them: a discount from 0 to 100%, and a | |
| 3 | * 100% discount is what used to be "comped" (billing reads a leftover | |
| 4 | * `comped` row as 100%). No Workers or React imports, so it can be tested | |
| 5 | * under Node. | |
| 6 | */ | |
| 7 | import type { Terms } from "@g1t/contracts"; | |
| 8 | ||
| 9 | /** The discount presets on the terms form, in percent. */ | |
| 10 | export const DISCOUNT_PRESETS = [0, 25, 50, 100] as const; | |
| 11 | ||
| 12 | /** The discount in percent, 0 to 100. */ | |
| 13 | export function percentOff(terms: Pick<Terms, "kind" | "discountPercent">): number { | |
| 14 | if (terms.kind === "comped") return 100; | |
| 15 | if (terms.kind === "custom") return Math.min(100, Math.max(0, terms.discountPercent)); | |
| 16 | return 0; | |
| 17 | } | |
| 18 | ||
| 19 | /** A 100% discount: nothing charged, usage shown at its price; g1t's own spend on it held to a monthly budget. */ | |
| 20 | export function fullDiscount(terms: Pick<Terms, "kind" | "discountPercent">): boolean { | |
| 21 | return percentOff(terms) >= 100; | |
| 22 | } | |
| 23 | ||
| 24 | /** `100% discount`, `30% off`, `Custom limit` or `Standard`. */ | |
| 25 | export function termsLabel(terms: Pick<Terms, "kind" | "discountPercent">): string { | |
| 26 | const percent = percentOff(terms); | |
| 27 | if (percent >= 100) return "100% discount"; | |
| 28 | if (percent > 0) return `${percent}% off`; | |
| 29 | return terms.kind === "standard" ? "Standard" : "Custom limit"; | |
| 30 | } |