All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 32s
148 lines
4.1 KiB
JavaScript
148 lines
4.1 KiB
JavaScript
import { z } from "zod";
|
|
|
|
export const metricKeys = [
|
|
"calories",
|
|
"protein",
|
|
"carbs",
|
|
"fat",
|
|
"fiber",
|
|
"sugar",
|
|
"sodium",
|
|
];
|
|
const amount = z.number().finite().min(0).max(100000);
|
|
export const nutrientsSchema = z
|
|
.object(Object.fromEntries(metricKeys.map((key) => [key, amount])))
|
|
.strict();
|
|
const date = z
|
|
.string()
|
|
.regex(/^\d{4}-\d{2}-\d{2}$/)
|
|
.refine((value) => {
|
|
const parsed = new Date(value + "T12:00:00Z");
|
|
return (
|
|
!Number.isNaN(+parsed) && parsed.toISOString().slice(0, 10) === value
|
|
);
|
|
}, "Invalid date");
|
|
const name = z.string().trim().min(1).max(160);
|
|
const food = z.object({
|
|
name,
|
|
serving: z.string().trim().min(1).max(100),
|
|
nutrients: nutrientsSchema,
|
|
});
|
|
const entry = food
|
|
.extend({
|
|
date,
|
|
time: z
|
|
.string()
|
|
.regex(/^([01]\d|2[0-3]):[0-5]\d$/)
|
|
.optional(),
|
|
meal: z.enum(["Breakfast", "Lunch", "Dinner", "Snacks"]),
|
|
quantity: z.number().finite().positive().max(1000),
|
|
})
|
|
.strict();
|
|
const goal = z
|
|
.object({
|
|
enabled: z.boolean(),
|
|
target: z.number().finite().positive().max(100000),
|
|
})
|
|
.strict();
|
|
const settings = z
|
|
.object({
|
|
theme: z.enum(["system", "light", "dark"]),
|
|
accentColor: z
|
|
.string()
|
|
.regex(/^#[a-fA-F0-9]{6}$/)
|
|
.optional(),
|
|
unit: z.enum(["kg", "lb"]),
|
|
goals: z
|
|
.object(Object.fromEntries(metricKeys.map((key) => [key, goal])))
|
|
.strict(),
|
|
exportFormat: z.enum(["markdown", "csv", "json"]),
|
|
exportDays: z.enum(["7", "30", "90", "all"]),
|
|
exportMetrics: z.array(z.enum(metricKeys)).max(7).optional(),
|
|
exportWeight: z.boolean().optional(),
|
|
minimumAverageCalories: amount.optional(),
|
|
})
|
|
.strict();
|
|
export const recordSchema = z
|
|
.object({
|
|
id: z.string().regex(/^[\w-]{1,80}$/),
|
|
type: z.enum(["entry", "food", "plate", "weight", "settings"]),
|
|
updatedAt: z.string().datetime({ offset: false }),
|
|
deleted: z.boolean(),
|
|
data: z.unknown(),
|
|
})
|
|
.strict()
|
|
.superRefine((record, ctx) => {
|
|
if (record.type === "settings" && record.id !== "settings")
|
|
ctx.addIssue({
|
|
code: "custom",
|
|
message: "Settings must use the settings ID",
|
|
});
|
|
if (record.type !== "settings" && record.id === "settings")
|
|
ctx.addIssue({ code: "custom", message: "Reserved settings ID" });
|
|
const schema = {
|
|
entry,
|
|
food: food.strict(),
|
|
plate: z
|
|
.object({
|
|
name,
|
|
items: z
|
|
.array(
|
|
food
|
|
.extend({ quantity: z.number().finite().positive().max(1000) })
|
|
.strict(),
|
|
)
|
|
.min(1)
|
|
.max(100),
|
|
})
|
|
.strict(),
|
|
weight: z
|
|
.object({
|
|
date,
|
|
valueKg: z.number().finite().positive().max(1000),
|
|
notes: z.string().max(500),
|
|
})
|
|
.strict(),
|
|
settings,
|
|
}[record.type];
|
|
const result = schema.safeParse(record.data);
|
|
if (!result.success)
|
|
for (const issue of result.error.issues)
|
|
ctx.addIssue({
|
|
code: "custom",
|
|
message: issue.message,
|
|
path: ["data", ...issue.path],
|
|
});
|
|
});
|
|
|
|
// The full payload breaks same-millisecond ties identically on every device.
|
|
export function compareRecords(a, b) {
|
|
const timestamp = Date.parse(a.updatedAt) - Date.parse(b.updatedAt);
|
|
if (timestamp) return timestamp;
|
|
const left = JSON.stringify([a.deleted, a.data]);
|
|
const right = JSON.stringify([b.deleted, b.data]);
|
|
return left === right ? 0 : left > right ? 1 : -1;
|
|
}
|
|
export function emptyNutrients() {
|
|
return Object.fromEntries(metricKeys.map((k) => [k, 0]));
|
|
}
|
|
export function totalNutrients(entries) {
|
|
const total = emptyNutrients();
|
|
for (const item of entries)
|
|
for (const key of metricKeys)
|
|
total[key] += (item.nutrients[key] || 0) * (item.quantity ?? 1);
|
|
return total;
|
|
}
|
|
export const defaultSettings = {
|
|
theme: "system",
|
|
accentColor: "#8391ed",
|
|
unit: "lb",
|
|
exportFormat: "markdown",
|
|
exportDays: "30",
|
|
goals: Object.fromEntries(
|
|
metricKeys.map((key, i) => [
|
|
key,
|
|
{ enabled: i < 4, target: [2000, 120, 250, 65, 30, 50, 2300][i] },
|
|
]),
|
|
),
|
|
};
|