fix: write cook assignments with legacy bridge schema
This commit is contained in:
@@ -225,7 +225,7 @@ router.put('/cook-assignments/:mealId', (req, res) => {
|
||||
try {
|
||||
const mealId = ensureMeal(req.params.mealId);
|
||||
const userId = ensureUser(req.body.user_id ?? req.body.userId);
|
||||
const meal = db.get().prepare('SELECT date, meal_type FROM meals WHERE id = ?').get(mealId);
|
||||
const meal = db.get().prepare('SELECT date, meal_type, title FROM meals WHERE id = ?').get(mealId);
|
||||
const vDate = date(req.body.planned_for_date ?? req.body.plannedForDate ?? meal.date, 'Planned date', true);
|
||||
const mealType = VALID_MEAL_TYPES.includes(req.body.meal_type || req.body.mealType || meal.meal_type) ? (req.body.meal_type || req.body.mealType || meal.meal_type) : meal.meal_type;
|
||||
const errors = collectErrors([vDate]);
|
||||
@@ -236,11 +236,18 @@ router.put('/cook-assignments/:mealId', (req, res) => {
|
||||
WHERE meal_id = ?
|
||||
`).run(userId, vDate.value, mealType, req.body.source_plan_id ?? req.body.sourcePlanId ?? null, currentUserId(req), mealId);
|
||||
if (update.changes === 0) {
|
||||
insertWithOptionalTextId(
|
||||
'planned_meal_cooks',
|
||||
['meal_id', 'user_id', 'planned_for_date', 'meal_type', 'source_plan_id', 'created_by', 'updated_at'],
|
||||
[mealId, userId, vDate.value, mealType, req.body.source_plan_id ?? req.body.sourcePlanId ?? null, currentUserId(req), new Date().toISOString().replace(/\.\d{3}Z$/, 'Z')]
|
||||
);
|
||||
const columns = ['meal_id', 'user_id', 'planned_for_date', 'meal_type', 'source_plan_id', 'created_by', 'updated_at'];
|
||||
const values = [mealId, userId, vDate.value, mealType, req.body.source_plan_id ?? req.body.sourcePlanId ?? null, currentUserId(req), new Date().toISOString().replace(/\.\d{3}Z$/, 'Z')];
|
||||
const available = tableColumns('planned_meal_cooks');
|
||||
if (available.has('meal_date')) {
|
||||
columns.push('meal_date');
|
||||
values.push(vDate.value);
|
||||
}
|
||||
if (available.has('meal_title')) {
|
||||
columns.push('meal_title');
|
||||
values.push(meal.title || null);
|
||||
}
|
||||
insertWithOptionalTextId('planned_meal_cooks', columns, values);
|
||||
}
|
||||
const data = db.get().prepare('SELECT * FROM planned_meal_cooks WHERE meal_id = ?').get(mealId);
|
||||
res.json({ data });
|
||||
|
||||
@@ -91,6 +91,14 @@ function saveCookAssignment(meal, cookUserId, sourcePlanId, createdBy) {
|
||||
const columns = tableColumns('planned_meal_cooks');
|
||||
const insertColumns = ['meal_id', 'user_id', 'planned_for_date', 'meal_type', 'source_plan_id', 'created_by', 'updated_at'];
|
||||
const values = [meal.id, cookUserId, meal.date, meal.meal_type, sourcePlanId || null, createdBy, new Date().toISOString().replace(/\.\d{3}Z$/, 'Z')];
|
||||
if (columns.has('meal_date')) {
|
||||
insertColumns.push('meal_date');
|
||||
values.push(meal.date);
|
||||
}
|
||||
if (columns.has('meal_title')) {
|
||||
insertColumns.push('meal_title');
|
||||
values.push(meal.title || null);
|
||||
}
|
||||
if (columns.has('id')) {
|
||||
insertColumns.unshift('id');
|
||||
values.unshift(crypto.randomUUID());
|
||||
|
||||
Reference in New Issue
Block a user