weight edit fixes
This commit is contained in:
@@ -306,15 +306,6 @@ 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,
|
||||
@@ -4103,11 +4094,6 @@ 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,
|
||||
|
||||
@@ -925,7 +925,13 @@ export const updateWeightForBird = async (
|
||||
notes = $5
|
||||
WHERE id = $1
|
||||
AND bird_id = $2
|
||||
AND recorded_on >= CURRENT_DATE - (2 * INTERVAL '1 day')
|
||||
AND id IN (
|
||||
SELECT recent.id
|
||||
FROM weight_records recent
|
||||
WHERE recent.bird_id = $2
|
||||
ORDER BY recent.recorded_on DESC, recent.created_at DESC
|
||||
LIMIT 3
|
||||
)
|
||||
RETURNING id, bird_id, weight_grams, recorded_on::text, notes`,
|
||||
[weightId, birdId, weightGrams, recordedOn, notes],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user