The desktop pushes its full Idle backlog as a JSON array to /tasks/mirror, not per-task. Previously /tasks/mirror matched tasks/[id].put.ts (id=mirror) and rejected the array with 400. New static route validates per-element, accepts empty arrays, upserts each as consumed=true (desktop-owned), deletes consumed=true rows not in the array, and leaves web-created consumed=false rows untouched. Mirrors PUT /lists.
132 lines
4.3 KiB
TypeScript
132 lines
4.3 KiB
TypeScript
import postgres from "postgres";
|
|
import { randomUUID } from "node:crypto";
|
|
|
|
type Sql = ReturnType<typeof postgres>;
|
|
|
|
export interface ListRow {
|
|
id: string;
|
|
name: string;
|
|
}
|
|
|
|
export interface TaskRow {
|
|
id: string;
|
|
list_id: string;
|
|
title: string;
|
|
description: string | null;
|
|
source: string;
|
|
consumed: boolean;
|
|
created_at: Date;
|
|
updated_at: Date;
|
|
}
|
|
|
|
export async function getLists(sql: Sql): Promise<ListRow[]> {
|
|
return sql<ListRow[]>`select id, name from lists order by name`;
|
|
}
|
|
|
|
/** Full-replace catalog: upsert all supplied, delete any list not present (cascades tasks). */
|
|
export async function replaceLists(
|
|
sql: Sql,
|
|
lists: { id: string; name: string }[],
|
|
): Promise<void> {
|
|
await sql.begin(async (tx) => {
|
|
for (const l of lists) {
|
|
await tx`
|
|
insert into lists (id, name, updated_at)
|
|
values (${l.id}, ${l.name}, now())
|
|
on conflict (id) do update set name = excluded.name, updated_at = now()`;
|
|
}
|
|
if (lists.length) {
|
|
const ids = lists.map((l) => l.id);
|
|
await tx`delete from lists where id <> all(${ids})`;
|
|
} else {
|
|
await tx`delete from lists`;
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function listExists(sql: Sql, id: string): Promise<boolean> {
|
|
const rows = await sql`select 1 from lists where id = ${id}`;
|
|
return rows.length > 0;
|
|
}
|
|
|
|
export async function getTasksForList(sql: Sql, listId: string): Promise<TaskRow[]> {
|
|
return sql<TaskRow[]>`select * from tasks where list_id = ${listId} order by created_at`;
|
|
}
|
|
|
|
export async function createWebTask(
|
|
sql: Sql,
|
|
t: { listId: string; title: string; description: string | null },
|
|
): Promise<TaskRow> {
|
|
const id = randomUUID();
|
|
const [row] = await sql<TaskRow[]>`
|
|
insert into tasks (id, list_id, title, description, source, consumed)
|
|
values (${id}, ${t.listId}, ${t.title}, ${t.description}, 'web', false)
|
|
returning *`;
|
|
return row;
|
|
}
|
|
|
|
/** Idempotent upsert of a desktop Idle task by id. Returns whether a row was inserted. */
|
|
export async function upsertDesktopTask(
|
|
sql: Sql,
|
|
id: string,
|
|
t: { listId: string; title: string; description?: string | null },
|
|
): Promise<{ created: boolean }> {
|
|
const [row] = await sql<{ created: boolean }[]>`
|
|
insert into tasks (id, list_id, title, description, source)
|
|
values (${id}, ${t.listId}, ${t.title}, ${t.description ?? null}, 'desktop')
|
|
on conflict (id) do update set
|
|
list_id = excluded.list_id,
|
|
title = excluded.title,
|
|
description = excluded.description,
|
|
updated_at = now()
|
|
returning (xmax = 0) as created`;
|
|
return { created: row.created };
|
|
}
|
|
|
|
/**
|
|
* Full-replace of the desktop-owned partition (the desktop's current Idle backlog).
|
|
* Mirrors `replaceLists`: upsert every supplied task as desktop-owned (consumed=true), and
|
|
* delete any desktop-owned (consumed=true) task not in the payload. Web-created tasks still
|
|
* awaiting pull (consumed=false) are left untouched. An empty array clears the partition.
|
|
*/
|
|
export async function mirrorDesktopTasks(
|
|
sql: Sql,
|
|
items: { id: string; listId: string; title: string; description?: string | null }[],
|
|
): Promise<void> {
|
|
await sql.begin(async (tx) => {
|
|
for (const t of items) {
|
|
await tx`
|
|
insert into tasks (id, list_id, title, description, source, consumed)
|
|
values (${t.id}, ${t.listId}, ${t.title}, ${t.description ?? null}, 'desktop', true)
|
|
on conflict (id) do update set
|
|
list_id = excluded.list_id,
|
|
title = excluded.title,
|
|
description = excluded.description,
|
|
source = 'desktop',
|
|
consumed = true,
|
|
updated_at = now()`;
|
|
}
|
|
const ids = items.map((t) => t.id);
|
|
if (ids.length) {
|
|
await tx`delete from tasks where consumed = true and id <> all(${ids})`;
|
|
} else {
|
|
await tx`delete from tasks where consumed = true`;
|
|
}
|
|
});
|
|
}
|
|
|
|
/** Web-created tasks the desktop has not yet imported. */
|
|
export async function getUnconsumed(sql: Sql): Promise<TaskRow[]> {
|
|
return sql<TaskRow[]>`
|
|
select * from tasks where source = 'web' and consumed = false order by created_at`;
|
|
}
|
|
|
|
export async function consume(sql: Sql, id: string): Promise<boolean> {
|
|
const res = await sql`update tasks set consumed = true, updated_at = now() where id = ${id}`;
|
|
return res.count > 0;
|
|
}
|
|
|
|
export async function deleteTask(sql: Sql, id: string): Promise<void> {
|
|
await sql`delete from tasks where id = ${id}`;
|
|
}
|