Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usagePriceTable.ts

97 lines3.2 KB

1import {
2 parseUsagePriceForm,
3 usagePriceForm,
4 USAGE_PRICE_FIELDS,
5 type UsagePriceForm,
6} from "./usagePriceForm";
7import type { UsagePriceChange, UsagePriceTarget } from "./usagePriceTargets";
8​
9export type UsagePriceField = (typeof USAGE_PRICE_FIELDS)[number]["key"];
10​
11export interface UsagePriceDraft {
12 readonly id: string;
13 readonly model: string;
14 readonly isNew: boolean;
15 readonly values: Partial<Pick<UsagePriceForm, UsagePriceField>>;
16 readonly removed?: boolean;
18​
19export function isEmptyUsagePriceDraft(draft: UsagePriceDraft) {
20 return (
21 draft.isNew &&
22 draft.model.trim() === "" &&
23 Object.values(draft.values).every((value) => value.trim() === "")
24 );
26​
27function modelPrice(target: UsagePriceTarget, model: string) {
28 return target.prices && Object.hasOwn(target.prices, model) ? target.prices[model] : undefined;
30​
31export function usagePriceCell(
32 targets: readonly UsagePriceTarget[],
33 model: string,
34 field: UsagePriceField,
35) {
36 const optional = USAGE_PRICE_FIELDS.find((entry) => entry.key === field)!.optional;
37 const values = targets.map((target) =>
38 modelPrice(target, model) ? usagePriceForm(model, modelPrice(target, model))[field] : null,
39 );
40 if (targets.some((target) => target.prices === null))
41 return { value: "", placeholder: "Unavailable" };
42 if (values.some((value) => value !== values[0])) return { value: "", placeholder: "Mixed" };
43 return {
44 value: values[0] ?? "",
45 placeholder: values[0] === null ? "Automatic" : optional ? "Input rate" : "0.00",
46 };
48​
49/** Only edited cells replace rates; untouched cells retain each environment's own values. */
50export function usagePriceTableChanges(
51 target: UsagePriceTarget,
52 drafts: readonly UsagePriceDraft[],
53) {
54 const changes: UsagePriceChange[] = [];
55 const errors = new Map<string, string>();
56 for (const draft of drafts) {
57 if (isEmptyUsagePriceDraft(draft)) continue;
58 const model = draft.model.trim();
59 if (draft.removed) {
60 if (modelPrice(target, model)) changes.push({ model, price: null });
61 continue;
62 }
63 const original = usagePriceForm(model, modelPrice(target, model));
64 const form = { ...original, ...draft.values };
65 const parsed = parseUsagePriceForm(form);
66 if (parsed === null) {
67 const missing = USAGE_PRICE_FIELDS.find(
68 (field) => !field.optional && form[field.key].trim() === "",
69 );
70 errors.set(
71 draft.id,
72 model === ""
73 ? "Enter a model ID."
74 : missing
75 ? `${missing.label} is required on ${target.label}.`
76 : "Use non-negative numbers for prices.",
77 );
78 continue;
79 }
80 const next = usagePriceForm(model, parsed.price);
81 if (USAGE_PRICE_FIELDS.some((field) => original[field.key] !== next[field.key]))
82 changes.push(parsed);
83 }
84 return { changes, errors };
86​
87/** Unavailable destinations report a save failure without blocking writable environments. */
88export function usagePriceTableErrors(
89 targets: readonly UsagePriceTarget[],
90 drafts: readonly UsagePriceDraft[],
91) {
92 return new Map(
93 targets
94 .filter((target) => target.unavailable === null && target.prices !== null)
95 .flatMap((target) => [...usagePriceTableChanges(target, drafts).errors]),
96 );