import { Handlers } from '$fresh/server.ts' import { db, kv, Todo, TodoModel } from '@homeman/models.ts' import { ulid } from 'https://deno.land/x/ulid@v0.3.0/mod.ts' import { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts' const TodoPayload = TodoModel.partial({ id: true }).omit({ createdAt: true, }) type TodoPayload = z.infer async function createOrUpdate(todo: TodoPayload) { if (!todo.id || todo.id === '') { const newTodo: Todo = { ...todo, id: ulid(), createdAt: new Date(), } const result = await db.todos.create({ data: newTodo }) await kv.set(['last_todo_updated'], newTodo.id) return result } else { const result = await db.todos.update({ where: { id: todo.id }, data: todo }) await kv.set(['last_todo_updated'], todo.id) return result } } export const handler: Handlers = { async POST(req, _ctx) { if (req.headers.get('content-type')?.includes('json')) { const result = await createOrUpdate( TodoPayload.parse(await req.json()), ) return new Response(JSON.stringify(result)) } else { const form = await req.formData() const id = form.get('id')?.toString() || undefined const doneAt = form.get('doneAt') console.log('todo POST doneAt:', doneAt) const todo = TodoPayload.parse({ id: id, emoji: form.get('emoji')?.toString() || null, doneAt: form.get('doneAt')?.toString() || null, description: form.get('description')?.toString(), assigneeUserId: form.get('assigneeUserId')?.toString() || null, }) if (!id) { delete todo.id } if (!todo.assigneeUserId) { delete todo.id } await createOrUpdate(todo) const url = new URL(req.url) url.pathname = '/' return Response.redirect(url, 303) } }, async DELETE(req, _ctx) { // TODO: form or query params or json let data if (req.headers.get('content-type')?.includes('json')) { data = await req.json() } else { data = { id: new URL(req.url).searchParams.get('id') } } console.log('delete todo data:', data) const todoData = TodoModel.pick({ id: true }).parse(data) const result = await db.todos.delete({ where: todoData }) await kv.set(['last_todo_updated'], todoData.id) return new Response(JSON.stringify(result)) }, async GET(req, ctx) { // TODO: json or query params const accept = req.headers.get('accept') if (accept === 'text/event-stream') { const stream = kv.watch([['last_todo_updated']]).getReader() const body = new ReadableStream({ async start(controller) { console.log( `Streaming todo updates to ${JSON.stringify(ctx.remoteAddr)}...`, ) while (true) { try { const entries = await stream.read() console.log('streaming todo entries:', entries) for (const entry of entries.value || []) { console.log('streaming todo entry:', entry) if (typeof entry.value !== 'string') { console.error('Invalid last_todo_updated entry:', entry) continue } const todo = await db.todos.findFirst({ where: { id: entry.value }, }) const chunk = `data: ${ JSON.stringify({ id: entry.value, versionstamp: entry.versionstamp, value: todo, }) }\n\n` controller.enqueue(new TextEncoder().encode(chunk)) } if (entries.done) { return } } catch (e) { console.error(`Error refreshing todo:`, e) } } }, cancel() { stream.cancel() console.log( `Closed todo updates stream to ${JSON.stringify(ctx.remoteAddr)}`, ) }, }) return new Response(body, { headers: { 'content-type': 'text/event-stream', }, }) } const data = await req.json().catch(() => {}) const todoData = TodoModel.pick({ id: true }).safeParse(data) if (todoData.success) { return new Response( JSON.stringify(await db.todos.findFirst({ where: todoData.data })), ) } else { return new Response(JSON.stringify(await db.todos.findMany({}))) } }, }