11import { Hono } from 'hono' ;
22import { cronsService } from '../services/crons' ;
33import { workersService } from '../services/workers' ;
4+ import { CronCreateInputSchema , CronUpdateInputSchema , CronSchema } from '../types' ;
5+ import { jsonResponse } from '../utils/validate' ;
46
57const crons = new Hono ( ) ;
68
@@ -10,16 +12,11 @@ crons.put('/:id', async (c) => {
1012 const id = c . req . param ( 'id' ) ;
1113 const body = await c . req . json ( ) ;
1214
13- if ( ! body . value ) {
14- return c . json ( { error : 'Missing required field: value' } , 400 ) ;
15- }
16-
1715 try {
18- const cron = await cronsService . update ( userId , id , {
19- value : body . value ,
20- } ) ;
16+ const payload = CronUpdateInputSchema . parse ( body ) ;
17+ const cron = await cronsService . update ( userId , id , payload ) ;
2118
22- return c . json ( cron ) ;
19+ return jsonResponse ( c , CronSchema , cron ) ;
2320 } catch ( error ) {
2421 console . error ( 'Failed to update cron:' , error ) ;
2522 return c . json ( {
@@ -61,16 +58,10 @@ crons.post('/', async (c) => {
6158 const userId = c . get ( 'userId' ) ;
6259 const body = await c . req . json ( ) ;
6360
64- if ( ! body . workerId || ! body . value ) {
65- return c . json ( { error : 'Missing required fields: workerId, value' } , 400 ) ;
66- }
67-
6861 try {
69- const cron = await cronsService . create ( userId , {
70- workerId : body . workerId ,
71- value : body . value
72- } ) ;
73- return c . json ( cron , 201 ) ;
62+ const payload = CronCreateInputSchema . parse ( body ) ;
63+ const cron = await cronsService . create ( userId , payload ) ;
64+ return jsonResponse ( c , CronSchema , cron , 201 ) ;
7465 } catch ( error ) {
7566 console . error ( 'Failed to create cron:' , error ) ;
7667 return c . json ( {
0 commit comments