toggleTodo.ts 663 B

12345678910111213141516171819
  1. import { brivenError, mutation, type Ctx } from '@briven/cli/server';
  2. interface Args {
  3. id: string;
  4. }
  5. export default mutation(async (ctx: Ctx, args: Args) => {
  6. if (!args.id) throw new brivenError('validation_failed', 'id is required', { status: 400 });
  7. const [existing] = await ctx.db('todos').select(['id', 'done']).where({ id: args.id }).limit(1);
  8. if (!existing) throw new brivenError('not_found', `no todo ${args.id}`, { status: 404 });
  9. const next = !existing.done;
  10. await ctx
  11. .db('todos')
  12. .update({ done: next, completedAt: next ? new Date().toISOString() : null })
  13. .where({ id: args.id });
  14. return { id: args.id, done: next };
  15. });