Review generated task endpoint

from Backend development
Node 24 intermediate 6 min 4 issues to find

Review this generated task endpoint before it is exposed to clients.

Create a task for the authenticated owner, accept a non-empty title of at most 80 characters, remain correct under retries, and return 201 with Location after durable creation and audit recording.

JavaScript
async function createTask(request, db, audit) {
  const body = await request.json();
  const ownerId = body.ownerId;
  const owner = (await db.users.list()).find((user) => user.id === ownerId);
  if (!owner) return json(404, { error: "owner_not_found" });

  const task = {
    id: crypto.randomUUID(),
    ownerId,
    title: String(body.title).trim(),
    completed: false,
  };

  const duplicate = await db.tasks.findByTitle(task.title);
  if (duplicate) return json(409, { error: "duplicate" });
  await db.tasks.insert(task);
  await audit.publish("task.created", task);
  return json(200, task);
}

generated code is illustrative, not from any one model

Open in playground
Report an error