46 lines
1.9 KiB
TypeScript
46 lines
1.9 KiB
TypeScript
// PUT /api/tasks/mirror (desktop) — full-replace of the caller's desktop Idle backlog.
|
|
// Body: [{ id, listId, title, description? }, ...] (camelCase). An empty array is valid and
|
|
// clears the caller's desktop-owned partition. Mirrors PUT /lists. Web-created tasks awaiting
|
|
// pull (consumed=false) and other users' rows are never touched. Any client-supplied ownerId
|
|
// on items is ignored — ownership comes from the verified token.
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody(event);
|
|
if (!Array.isArray(body)) {
|
|
throw createError({ statusCode: 400, statusMessage: "expected an array of tasks" });
|
|
}
|
|
|
|
const items: { id: string; listId: string; title: string; description: string | null }[] = [];
|
|
for (let i = 0; i < body.length; i++) {
|
|
const t = body[i];
|
|
if (typeof t?.id !== "string" || !t.id.trim()) {
|
|
throw createError({ statusCode: 400, statusMessage: `item ${i}: id is required` });
|
|
}
|
|
if (typeof t?.listId !== "string" || !t.listId.trim()) {
|
|
throw createError({ statusCode: 400, statusMessage: `item ${i}: listId is required` });
|
|
}
|
|
if (typeof t?.title !== "string" || !t.title.trim()) {
|
|
throw createError({ statusCode: 400, statusMessage: `item ${i}: title is required` });
|
|
}
|
|
items.push({
|
|
id: t.id,
|
|
listId: t.listId,
|
|
title: t.title,
|
|
description: typeof t.description === "string" ? t.description : null,
|
|
});
|
|
}
|
|
|
|
const ownerId = ownerOf(event);
|
|
const sql = getSql();
|
|
|
|
// Every referenced list must exist for the caller (lists are full-replaced before tasks are mirrored).
|
|
const listIds = [...new Set(items.map((t) => t.listId))];
|
|
for (const id of listIds) {
|
|
if (!(await listExists(sql, ownerId, id))) {
|
|
throw createError({ statusCode: 400, statusMessage: `unknown listId: ${id}` });
|
|
}
|
|
}
|
|
|
|
await mirrorDesktopTasks(sql, ownerId, items);
|
|
return { ok: true, count: items.length };
|
|
});
|