| 1 | /** |
| 2 | * Costs & margin and Bill & pricing: one report from billing, read by both |
| 3 | * pages, and the changes staff make on either. |
| 4 | */ |
| 5 | import { data, redirect } from "react-router"; |
| 6 | |
| 7 | import { parseCostSettings, parseMapping, parseRange } from "./costs"; |
| 8 | import { admin } from "./services.server"; |
| 9 | import { settle } from "./settle"; |
| 10 | import { requireStaff } from "./staff"; |
| 11 | |
| 12 | export const DONE: Record<string, string> = { |
| 13 | run: "Ran the analysis: read Cloudflare's bill, reconciled the last 31 days and checked the alerts. The figures below are fresh.", |
| 14 | approved: "Approved. A fall applies now; a rise after the notice period, and owners on the plan are emailed.", |
| 15 | rejected: "Rejected, with the note kept for whoever measures it next.", |
| 16 | settings: "Guardrails saved. They apply from the next run.", |
| 17 | mapping: "Mapping saved. It applies from the next run; read the bill now to see it.", |
| 18 | removed: "Mapping removed.", |
| 19 | lifted: "Breaker lifted for the rest of today (UTC). Hosted-model runs start again; it is recorded in the audit log.", |
| 20 | }; |
| 21 | |
| 22 | export type CostsActionResult = { error: string; section: string; values?: Record<string, string> }; |
| 23 | |
| 24 | export async function costsLoader(request: Request, context: unknown) { |
| 25 | requireStaff(context as Parameters<typeof requireStaff>[0]); |
| 26 | const url = new URL(request.url); |
| 27 | const range = parseRange(url.searchParams.get("days")); |
| 28 | const report = await settle(admin.costs(range)); |
| 29 | const done = url.searchParams.get("done"); |
| 30 | return { |
| 31 | range, |
| 32 | bucket: url.searchParams.get("product"), |
| 33 | report: report.ok ? report.value : null, |
| 34 | error: report.ok ? null : report.error, |
| 35 | done: done ? (DONE[done] ?? null) : null, |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | export async function costsAction(request: Request, context: unknown) { |
| 40 | const staff = requireStaff(context as Parameters<typeof requireStaff>[0]); |
| 41 | const form = await request.formData(); |
| 42 | const intent = String(form.get("intent") ?? ""); |
| 43 | const fail = (section: string, error: string) => |
| 44 | data<CostsActionResult>({ error, section, values: Object.fromEntries([...form.entries()].map(([k, v]) => [k, String(v)])) }, { status: 422 }); |
| 45 | const back = (done: string, hash = "") => { |
| 46 | const url = new URL(request.url); |
| 47 | url.searchParams.set("done", done); |
| 48 | return redirect(`${url.pathname}${url.search}${hash}`); |
| 49 | }; |
| 50 | if (intent === "run") { |
| 51 | const run = await settle(admin.runCosts(staff.email)); |
| 52 | if (!run.ok) return fail("run", `Billing did not answer: ${run.error}`); |
| 53 | if (!run.value.ok) return fail("run", run.value.error.message); |
| 54 | if (run.value.value.problems.length > 0) return fail("run", run.value.value.problems.join(" ")); |
| 55 | throw back("run"); |
| 56 | } |
| 57 | if (intent === "lift") { |
| 58 | const note = String(form.get("note") ?? "").trim().slice(0, 500); |
| 59 | if (note.length < 5) return fail("lift", "Say why it is lifted, for whoever looks next."); |
| 60 | const result = await settle(admin.liftBreaker(note, staff.email)); |
| 61 | if (!result.ok) return fail("lift", `Billing did not answer: ${result.error}`); |
| 62 | if (!result.value.ok) return fail("lift", result.value.error.message); |
| 63 | throw back("lifted", "#spend"); |
| 64 | } |
| 65 | if (intent === "decide") { |
| 66 | const id = String(form.get("id") ?? ""); |
| 67 | const decision = form.get("decision") === "approve" ? "approve" : "reject"; |
| 68 | const note = String(form.get("note") ?? "").trim().slice(0, 500); |
| 69 | if (decision === "reject" && note.length < 5) return fail(`proposal-${id}`, "Say why it is rejected, for whoever measures it next."); |
| 70 | const result = await admin.decideProposal(id, decision, note, staff.email); |
| 71 | if (!result.ok) return fail(`proposal-${id}`, result.error.message); |
| 72 | throw back(decision === "approve" ? "approved" : "rejected", "#proposals"); |
| 73 | } |
| 74 | if (intent === "settings") { |
| 75 | const parsed = parseCostSettings(form); |
| 76 | if (!parsed.ok) return fail("settings", parsed.error); |
| 77 | const result = await admin.setCostSettings(parsed.value, staff.email); |
| 78 | if (!result.ok) return fail("settings", result.error.message); |
| 79 | throw back("settings", "#guardrails"); |
| 80 | } |
| 81 | if (intent === "mapping") { |
| 82 | const parsed = parseMapping(form); |
| 83 | if (!parsed.ok) return fail("mapping", parsed.error); |
| 84 | const result = await admin.setCostMapping(parsed.value, staff.email); |
| 85 | if (!result.ok) return fail("mapping", result.error.message); |
| 86 | throw back(parsed.value.remove ? "removed" : "mapping", "#mappings"); |
| 87 | } |
| 88 | return fail("run", "Unknown change."); |
| 89 | } |