trimmed weight edit
Deploy / deploy-dev (push) Successful in 2m42s
Deploy / deploy-prod (push) Has been skipped

This commit is contained in:
Corey Blais
2026-06-16 10:00:50 -04:00
parent 454adc6f5e
commit 1e98d55cb5
3 changed files with 56 additions and 20 deletions
+15 -1
View File
@@ -318,6 +318,15 @@ const weightSchema = z.object({
recordedOn: dateStringSchema,
notes: z.string().trim().max(280).optional().or(z.literal('')),
});
const weightEditWindowDays = 3;
const getWeightEditEarliestDate = () => {
const earliestDate = new Date();
earliestDate.setDate(earliestDate.getDate() - (weightEditWindowDays - 1));
return earliestDate.toISOString().slice(0, 10);
};
const isWeightDateEditable = (recordedOn: string) => recordedOn >= getWeightEditEarliestDate();
const vetVisitSchema = z.object({
visitedOn: dateStringSchema,
@@ -4176,6 +4185,11 @@ app.put(
return;
}
if (!isWeightDateEditable(parsed.data.recordedOn)) {
res.status(409).json({ error: 'Weight entries can only be edited for the last 3 days.' });
return;
}
const weight = await updateWeightForBird(
req.params.weightId,
req.params.birdId,
@@ -4185,7 +4199,7 @@ app.put(
);
if (!weight) {
res.status(404).json({ error: 'Weight entry not found.' });
res.status(404).json({ error: 'Weight entry not found or no longer editable.' });
return;
}
@@ -909,6 +909,7 @@ export const updateWeightForBird = async (
notes = $5
WHERE id = $1
AND bird_id = $2
AND recorded_on >= CURRENT_DATE - (2 * INTERVAL '1 day')
RETURNING id, bird_id, weight_grams, recorded_on::text, notes`,
[weightId, birdId, weightGrams, recordedOn, notes],
);