1
1
import { taskStates } from '@nangohq/scheduler' ;
2
2
import type { Task } from '@nangohq/scheduler' ;
3
- import { TaskAction , TaskWebhook , TaskPostConnection } from './types.js' ;
3
+ import type { OrchestratorTask } from './types.js' ;
4
+ import { TaskAction , TaskWebhook , TaskPostConnection , TaskSync } from './types.js' ;
4
5
import { z } from 'zod' ;
5
- import { actionArgsSchema , webhookArgsSchema , postConnectionArgsSchema } from '../routes/v1/postSchedule.js' ;
6
6
import { Err , Ok } from '@nangohq/utils' ;
7
7
import type { Result } from '@nangohq/utils' ;
8
+ import { jsonSchema } from '../utils/validation.js' ;
9
+
10
+ export const commonSchemaArgsFields = {
11
+ connection : z . object ( {
12
+ id : z . number ( ) . positive ( ) ,
13
+ connection_id : z . string ( ) . min ( 1 ) ,
14
+ provider_config_key : z . string ( ) . min ( 1 ) ,
15
+ environment_id : z . number ( ) . positive ( )
16
+ } )
17
+ } ;
18
+
19
+ export const syncArgsSchema = z . object ( {
20
+ type : z . literal ( 'sync' ) ,
21
+ syncId : z . string ( ) . min ( 1 ) ,
22
+ syncName : z . string ( ) . min ( 1 ) ,
23
+ syncJobId : z . number ( ) . int ( ) . positive ( ) ,
24
+ debug : z . boolean ( ) ,
25
+ ...commonSchemaArgsFields
26
+ } ) ;
27
+
28
+ export const actionArgsSchema = z . object ( {
29
+ type : z . literal ( 'action' ) ,
30
+ actionName : z . string ( ) . min ( 1 ) ,
31
+ activityLogId : z . number ( ) . positive ( ) ,
32
+ input : jsonSchema ,
33
+ ...commonSchemaArgsFields
34
+ } ) ;
35
+ export const webhookArgsSchema = z . object ( {
36
+ type : z . literal ( 'webhook' ) ,
37
+ webhookName : z . string ( ) . min ( 1 ) ,
38
+ parentSyncName : z . string ( ) . min ( 1 ) ,
39
+ activityLogId : z . number ( ) . positive ( ) ,
40
+ input : jsonSchema ,
41
+ ...commonSchemaArgsFields
42
+ } ) ;
43
+ export const postConnectionArgsSchema = z . object ( {
44
+ type : z . literal ( 'post-connection-script' ) ,
45
+ postConnectionName : z . string ( ) . min ( 1 ) ,
46
+ fileLocation : z . string ( ) . min ( 1 ) ,
47
+ activityLogId : z . number ( ) . positive ( ) ,
48
+ ...commonSchemaArgsFields
49
+ } ) ;
8
50
9
51
const commonSchemaFields = {
10
52
id : z . string ( ) . uuid ( ) ,
11
53
name : z . string ( ) . min ( 1 ) ,
12
54
groupKey : z . string ( ) . min ( 1 ) ,
13
55
state : z . enum ( taskStates )
14
56
} ;
57
+ const syncSchema = z . object ( {
58
+ ...commonSchemaFields ,
59
+ payload : syncArgsSchema
60
+ } ) ;
15
61
const actionSchema = z . object ( {
16
62
...commonSchemaFields ,
17
63
payload : actionArgsSchema
@@ -25,7 +71,22 @@ const postConnectionSchema = z.object({
25
71
payload : postConnectionArgsSchema
26
72
} ) ;
27
73
28
- export function validateTask ( task : Task ) : Result < TaskAction | TaskWebhook | TaskPostConnection > {
74
+ export function validateTask ( task : Task ) : Result < OrchestratorTask > {
75
+ const sync = syncSchema . safeParse ( task ) ;
76
+ if ( sync . success ) {
77
+ return Ok (
78
+ TaskSync ( {
79
+ id : sync . data . id ,
80
+ state : sync . data . state ,
81
+ name : sync . data . name ,
82
+ syncId : sync . data . payload . syncId ,
83
+ syncName : sync . data . payload . syncName ,
84
+ connection : sync . data . payload . connection ,
85
+ syncJobId : sync . data . payload . syncJobId ,
86
+ debug : sync . data . payload . debug
87
+ } )
88
+ ) ;
89
+ }
29
90
const action = actionSchema . safeParse ( task ) ;
30
91
if ( action . success ) {
31
92
return Ok (
0 commit comments