6
6
StepTypeEnum ,
7
7
UserSessionData ,
8
8
WorkflowTestDataResponseDto ,
9
+ Variables ,
9
10
} from '@novu/shared' ;
10
11
import {
11
12
GetWorkflowByIdsCommand ,
@@ -30,7 +31,16 @@ export class BuildWorkflowTestDataUseCase {
30
31
@InstrumentUsecase ( )
31
32
async execute ( command : WorkflowTestDataCommand ) : Promise < WorkflowTestDataResponseDto > {
32
33
const workflow = await this . fetchWorkflow ( command ) ;
33
- const toSchema = this . buildToFieldSchema ( { user : command . user , steps : workflow . steps } ) ;
34
+ const variables = await this . extractVariables . execute (
35
+ ExtractVariablesCommand . create ( {
36
+ environmentId : command . user . environmentId ,
37
+ organizationId : command . user . organizationId ,
38
+ userId : command . user . _id ,
39
+ workflowId : workflow . _id ,
40
+ } )
41
+ ) ;
42
+ const toSchema = this . buildToFieldSchema ( { user : command . user , steps : workflow . steps , variables } ) ;
43
+
34
44
const payloadSchema = await this . resolvePayloadSchema ( workflow , command ) ;
35
45
const payloadSchemaMock = this . generatePayloadMock ( payloadSchema ) ;
36
46
@@ -84,38 +94,58 @@ export class BuildWorkflowTestDataUseCase {
84
94
private buildToFieldSchema ( {
85
95
user,
86
96
steps,
97
+ variables,
87
98
} : {
88
99
user : UserSessionData ;
89
100
steps : NotificationStepEntity [ ] ;
101
+ variables : Variables ;
90
102
} ) : JSONSchemaDto {
91
- const hasEmailStep = this . hasStepType ( steps , StepTypeEnum . EMAIL ) ;
92
- const hasSmsStep = this . hasStepType ( steps , StepTypeEnum . SMS ) ;
93
-
94
- const properties : { [ key : string ] : JSONSchemaDto } = {
95
- subscriberId : { type : 'string' , default : user . _id } ,
96
- } ;
103
+ // TODO: add subscriber schema
104
+ const toSchema = buildVariablesSchema ( { } ) ;
97
105
98
106
const required : string [ ] = [ 'subscriberId' ] ;
107
+ toSchema . properties ! . subscriberId = { type : 'string' , default : user . _id } ;
99
108
100
- if ( hasEmailStep ) {
101
- properties . email = { type : 'string' , default : user . email ?? '' , format : 'email' } ;
109
+ if ( this . hasStep ( steps , StepTypeEnum . EMAIL ) ) {
110
+ toSchema . properties ! . email = { type : 'string' , default : user . email ?? '' , format : 'email' } ;
102
111
required . push ( 'email' ) ;
103
112
}
104
113
105
- if ( hasSmsStep ) {
106
- properties . phone = { type : 'string' , default : '' } ;
114
+ if ( this . hasStep ( steps , StepTypeEnum . SMS ) ) {
115
+ toSchema . properties ! . phone = { type : 'string' , default : '' } ;
107
116
required . push ( 'phone' ) ;
108
117
}
109
118
110
- return {
111
- type : 'object' ,
112
- properties,
113
- required,
114
- additionalProperties : false ,
115
- } satisfies JSONSchemaDto ;
119
+ if ( variables . subscriber . firstName ) {
120
+ toSchema . properties ! . firstName = { type : 'string' , default : user . firstName || '' } ;
121
+ }
122
+
123
+ if ( variables . subscriber . lastName ) {
124
+ toSchema . properties ! . lastName = { type : 'string' , default : user . lastName || '' } ;
125
+ }
126
+
127
+ if ( variables . subscriber . isOnline ) {
128
+ toSchema . properties ! . avatar = { type : 'boolean' , default : true } ;
129
+ }
130
+
131
+ if ( variables . subscriber . isLastOnline ) {
132
+ toSchema . properties ! . avatar = { type : 'string' , format : 'date-time' , default : new Date ( ) . toISOString ( ) } ;
133
+ }
134
+
135
+ // TODO: add locale as an enum
136
+ if ( variables . subscriber . locale ) {
137
+ toSchema . properties ! . locale = { type : 'string' , default : '' } ;
138
+ }
139
+
140
+ // TODO: add timezone as an enum
141
+ if ( variables . subscriber . timezone ) {
142
+ toSchema . properties ! . timezone = { type : 'string' , default : '' } ;
143
+ }
144
+
145
+ return toSchema ;
116
146
}
117
147
118
- private hasStepType ( steps : NotificationStepEntity [ ] , type : StepTypeEnum ) : boolean {
148
+ private hasStep ( steps : NotificationStepEntity [ ] , type : StepTypeEnum ) : boolean {
119
149
return steps . some ( ( step ) => step . template ?. type === type ) ;
120
150
}
121
151
}
0 commit comments