1+ import * as z from 'zod' ;
12import type { Task , GptResponse } from 'wasp/entities' ;
23import type {
34 GenerateGptResponse ,
@@ -10,6 +11,7 @@ import type {
1011import { HttpError } from 'wasp/server' ;
1112import { GeneratedSchedule } from './schedule' ;
1213import OpenAI from 'openai' ;
14+ import { ensureArgsSchemaOrThrowHttpError } from '../server/validation' ;
1315
1416const openai = setupOpenAI ( ) ;
1517function setupOpenAI ( ) {
@@ -20,15 +22,23 @@ function setupOpenAI() {
2022}
2123
2224//#region Actions
23- type GptPayload = {
24- hours : string ;
25- } ;
2625
27- export const generateGptResponse : GenerateGptResponse < GptPayload , GeneratedSchedule > = async ( { hours } , context ) => {
26+ const generateGptResponseInputSchema = z . object ( {
27+ hours : z . string ( ) . regex ( / ^ \d + ( \. \d + ) ? $ / , 'Hours must be a number' ) ,
28+ } ) ;
29+
30+ type GenerateGptResponseInput = z . infer < typeof generateGptResponseInputSchema > ;
31+
32+ export const generateGptResponse : GenerateGptResponse < GenerateGptResponseInput , GeneratedSchedule > = async (
33+ rawArgs ,
34+ context
35+ ) => {
2836 if ( ! context . user ) {
2937 throw new HttpError ( 401 ) ;
3038 }
3139
40+ const { hours } = ensureArgsSchemaOrThrowHttpError ( generateGptResponseInputSchema , rawArgs ) ;
41+
3242 const tasks = await context . entities . Task . findMany ( {
3343 where : {
3444 user : {
@@ -181,11 +191,19 @@ export const generateGptResponse: GenerateGptResponse<GptPayload, GeneratedSched
181191 }
182192} ;
183193
184- export const createTask : CreateTask < Pick < Task , 'description' > , Task > = async ( { description } , context ) => {
194+ const createTaskInputSchema = z . object ( {
195+ description : z . string ( ) . nonempty ( ) ,
196+ } ) ;
197+
198+ type CreateTaskInput = z . infer < typeof createTaskInputSchema > ;
199+
200+ export const createTask : CreateTask < CreateTaskInput , Task > = async ( rawArgs , context ) => {
185201 if ( ! context . user ) {
186202 throw new HttpError ( 401 ) ;
187203 }
188204
205+ const { description } = ensureArgsSchemaOrThrowHttpError ( createTaskInputSchema , rawArgs ) ;
206+
189207 const task = await context . entities . Task . create ( {
190208 data : {
191209 description,
@@ -196,11 +214,21 @@ export const createTask: CreateTask<Pick<Task, 'description'>, Task> = async ({
196214 return task ;
197215} ;
198216
199- export const updateTask : UpdateTask < Partial < Task > , Task > = async ( { id, isDone, time } , context ) => {
217+ const updateTaskInputSchema = z . object ( {
218+ id : z . string ( ) . nonempty ( ) ,
219+ isDone : z . boolean ( ) . optional ( ) ,
220+ time : z . string ( ) . optional ( ) ,
221+ } ) ;
222+
223+ type UpdateTaskInput = z . infer < typeof updateTaskInputSchema > ;
224+
225+ export const updateTask : UpdateTask < UpdateTaskInput , Task > = async ( rawArgs , context ) => {
200226 if ( ! context . user ) {
201227 throw new HttpError ( 401 ) ;
202228 }
203229
230+ const { id, isDone, time } = ensureArgsSchemaOrThrowHttpError ( updateTaskInputSchema , rawArgs ) ;
231+
204232 const task = await context . entities . Task . update ( {
205233 where : {
206234 id,
@@ -214,11 +242,19 @@ export const updateTask: UpdateTask<Partial<Task>, Task> = async ({ id, isDone,
214242 return task ;
215243} ;
216244
217- export const deleteTask : DeleteTask < Pick < Task , 'id' > , Task > = async ( { id } , context ) => {
245+ const deleteTaskInputSchema = z . object ( {
246+ id : z . string ( ) . nonempty ( ) ,
247+ } ) ;
248+
249+ type DeleteTaskInput = z . infer < typeof deleteTaskInputSchema > ;
250+
251+ export const deleteTask : DeleteTask < DeleteTaskInput , Task > = async ( rawArgs , context ) => {
218252 if ( ! context . user ) {
219253 throw new HttpError ( 401 ) ;
220254 }
221255
256+ const { id } = ensureArgsSchemaOrThrowHttpError ( deleteTaskInputSchema , rawArgs ) ;
257+
222258 const task = await context . entities . Task . delete ( {
223259 where : {
224260 id,
0 commit comments