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