@@ -65,7 +65,7 @@ export async function awaitEvent(
6565
6666async function getOrCreateEvent (
6767 ctx : MutationCtx ,
68- workflowId : Id < "workflows" > ,
68+ workflowId : Id < "workflows" > | undefined ,
6969 args : { eventId ?: Id < "events" > ; name ?: string } ,
7070 statuses : Doc < "events" > [ "state" ] [ "kind" ] [ ] ,
7171) : Promise < Doc < "events" > > {
@@ -79,6 +79,7 @@ async function getOrCreateEvent(
7979 return event ;
8080 }
8181 assert ( args . name , "Name is required if eventId is not specified" ) ;
82+ assert ( workflowId , "workflowId is required if eventId is not specified" ) ;
8283 for ( const status of statuses ) {
8384 const event = await ctx . db
8485 . query ( "events" )
@@ -101,7 +102,7 @@ async function getOrCreateEvent(
101102
102103export const send = mutation ( {
103104 args : {
104- workflowId : v . id ( "workflows" ) ,
105+ workflowId : v . optional ( v . id ( "workflows" ) ) ,
105106 eventId : v . optional ( v . id ( "events" ) ) ,
106107 name : v . optional ( v . string ( ) ) ,
107108 result : vResultValidator ,
@@ -118,16 +119,17 @@ export const send = mutation({
118119 } ,
119120 [ "waiting" , "created" ] ,
120121 ) ;
122+ const { workflowId } = event ;
121123 const name = args . name ?? event . name ;
122124 switch ( event . state . kind ) {
123125 case "sent" : {
124126 throw new Error (
125- `Event already sent: ${ event . _id } (${ name } ) in workflow ${ args . workflowId } ` ,
127+ `Event already sent: ${ event . _id } (${ name } ) in workflow ${ workflowId } ` ,
126128 ) ;
127129 }
128130 case "consumed" : {
129131 throw new Error (
130- `Event already consumed: ${ event . _id } (${ name } ) in workflow ${ args . workflowId } ` ,
132+ `Event already consumed: ${ event . _id } (${ name } ) in workflow ${ workflowId } ` ,
131133 ) ;
132134 }
133135 case "created" : {
@@ -140,7 +142,7 @@ export const send = mutation({
140142 const step = await ctx . db . get ( event . state . stepId ) ;
141143 assert (
142144 step ,
143- `Entry ${ event . state . stepId } not found when sending event ${ event . _id } (${ name } ) in workflow ${ args . workflowId } ` ,
145+ `Entry ${ event . state . stepId } not found when sending event ${ event . _id } (${ name } ) in workflow ${ workflowId } ` ,
144146 ) ;
145147 assert ( step . step . kind === "event" , "Step is not an event" ) ;
146148 step . step . eventId = event . _id ;
@@ -160,13 +162,13 @@ export const send = mutation({
160162 const anyMoreEvents = await ctx . db
161163 . query ( "events" )
162164 . withIndex ( "workflowId_state" , ( q ) =>
163- q . eq ( "workflowId" , args . workflowId ) . eq ( "state.kind" , "waiting" ) ,
165+ q . eq ( "workflowId" , workflowId ) . eq ( "state.kind" , "waiting" ) ,
164166 )
165167 . order ( "desc" )
166168 . first ( ) ;
167169 if ( ! anyMoreEvents ) {
168- const workflow = await ctx . db . get ( args . workflowId ) ;
169- assert ( workflow , `Workflow ${ args . workflowId } not found` ) ;
170+ const workflow = await ctx . db . get ( workflowId ) ;
171+ assert ( workflow , `Workflow ${ workflowId } not found` ) ;
170172 const workpool = await getWorkpool ( ctx , args . workpoolOptions ) ;
171173 await enqueueWorkflow ( ctx , workflow , workpool ) ;
172174 }
0 commit comments