|
|
|
|
@ -31,22 +31,54 @@ export const PATCH: RequestHandler = async (event) => {
|
|
|
|
|
return new Response('Bad Request', { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validar que al menos se envíe algún campo editable
|
|
|
|
|
const hasDueField = Object.prototype.hasOwnProperty.call(payload ?? {}, 'due_date');
|
|
|
|
|
const hasDescField = Object.prototype.hasOwnProperty.call(payload ?? {}, 'description');
|
|
|
|
|
if (!hasDueField && !hasDescField) {
|
|
|
|
|
return new Response('Bad Request', { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// due_date (opcional)
|
|
|
|
|
const due_date_raw = payload?.due_date;
|
|
|
|
|
if (due_date_raw !== null && due_date_raw !== undefined && typeof due_date_raw !== 'string') {
|
|
|
|
|
if (hasDueField && due_date_raw !== null && due_date_raw !== undefined && typeof due_date_raw !== 'string') {
|
|
|
|
|
return new Response('Bad Request', { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
const due_date =
|
|
|
|
|
due_date_raw == null || String(due_date_raw).trim() === ''
|
|
|
|
|
!hasDueField || due_date_raw == null || String(due_date_raw).trim() === ''
|
|
|
|
|
? null
|
|
|
|
|
: String(due_date_raw).trim();
|
|
|
|
|
|
|
|
|
|
if (due_date !== null && !isValidYmd(due_date)) {
|
|
|
|
|
if (hasDueField && due_date !== null && !isValidYmd(due_date)) {
|
|
|
|
|
return new Response(JSON.stringify({ error: 'invalid_due_date' }), {
|
|
|
|
|
status: 400,
|
|
|
|
|
headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' }
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// description (opcional)
|
|
|
|
|
let description: string | undefined = undefined;
|
|
|
|
|
if (hasDescField) {
|
|
|
|
|
const descRaw = payload?.description;
|
|
|
|
|
if (descRaw !== null && descRaw !== undefined && typeof descRaw !== 'string') {
|
|
|
|
|
return new Response('Bad Request', { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
if (descRaw == null) {
|
|
|
|
|
// No permitimos null en description (columna NOT NULL)
|
|
|
|
|
return new Response(JSON.stringify({ error: 'invalid_description' }), {
|
|
|
|
|
status: 400,
|
|
|
|
|
headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' }
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const normalized = String(descRaw).replace(/\s+/g, ' ').trim();
|
|
|
|
|
if (normalized.length < 1 || normalized.length > 1000) {
|
|
|
|
|
return new Response(JSON.stringify({ error: 'invalid_description' }), {
|
|
|
|
|
status: 400,
|
|
|
|
|
headers: { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' }
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
description = normalized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const db = await getDb();
|
|
|
|
|
|
|
|
|
|
// Cargar tarea y validar abierta
|
|
|
|
|
@ -89,7 +121,13 @@ export const PATCH: RequestHandler = async (event) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Aplicar actualización
|
|
|
|
|
db.prepare(`UPDATE tasks SET due_date = ? WHERE id = ?`).run(due_date, taskId);
|
|
|
|
|
if (hasDescField && hasDueField) {
|
|
|
|
|
db.prepare(`UPDATE tasks SET description = ?, due_date = ? WHERE id = ?`).run(description!, due_date, taskId);
|
|
|
|
|
} else if (hasDescField) {
|
|
|
|
|
db.prepare(`UPDATE tasks SET description = ? WHERE id = ?`).run(description!, taskId);
|
|
|
|
|
} else if (hasDueField) {
|
|
|
|
|
db.prepare(`UPDATE tasks SET due_date = ? WHERE id = ?`).run(due_date, taskId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updated = db
|
|
|
|
|
.prepare(`SELECT id, description, due_date, display_code FROM tasks WHERE id = ?`)
|
|
|
|
|
|