@@ -21,6 +21,7 @@ import {
21
21
UpsertControlValuesUseCase ,
22
22
UpsertPreferences ,
23
23
UpsertUserWorkflowPreferencesCommand ,
24
+ slugifyName ,
24
25
} from '@novu/application-generic' ;
25
26
import {
26
27
CreateWorkflowDto ,
@@ -33,7 +34,6 @@ import {
33
34
WorkflowTypeEnum ,
34
35
} from '@novu/shared' ;
35
36
import { UpsertWorkflowCommand } from './upsert-workflow.command' ;
36
- import { WorkflowAlreadyExistException } from '../../exceptions/workflow-already-exist' ;
37
37
import { StepUpsertMechanismFailedMissingIdException } from '../../exceptions/step-upsert-mechanism-failed-missing-id.exception' ;
38
38
import { toResponseWorkflowDto } from '../../mappers/notification-template-mapper' ;
39
39
@@ -64,11 +64,11 @@ export class UpsertWorkflowUseCase {
64
64
private getPreferencesUseCase : GetPreferences
65
65
) { }
66
66
async execute ( command : UpsertWorkflowCommand ) : Promise < WorkflowResponseDto > {
67
- const workflowForUpdate = await this . getWorkflowIfUpdateAndExist ( command ) ;
68
- if ( ! workflowForUpdate && ( await this . workflowExistByExternalId ( command ) ) ) {
69
- throw new WorkflowAlreadyExistException ( command ) ;
70
- }
71
- const workflow = await this . createOrUpdateWorkflow ( workflowForUpdate , command ) ;
67
+ const existingWorkflow = await this . notificationTemplateRepository . findOne ( {
68
+ _id : command . workflowDatabaseIdForUpdate ,
69
+ _environmentId : command . user . environmentId ,
70
+ } ) ;
71
+ const workflow = await this . createOrUpdateWorkflow ( existingWorkflow , command ) ;
72
72
const stepIdToControlValuesMap = await this . upsertControlValues ( workflow , command ) ;
73
73
const preferences = await this . upsertPreference ( command , workflow ) ;
74
74
@@ -188,28 +188,10 @@ export class UpsertWorkflowUseCase {
188
188
description : workflowDto . description || '' ,
189
189
tags : workflowDto . tags || [ ] ,
190
190
critical : false ,
191
+ triggerIdentifier : slugifyName ( workflowDto . name ) ,
191
192
} ;
192
193
}
193
194
194
- private async getWorkflowIfUpdateAndExist ( upsertCommand : UpsertWorkflowCommand ) {
195
- if ( upsertCommand . workflowDatabaseIdForUpdate ) {
196
- return await this . notificationTemplateRepository . findByIdQuery ( {
197
- id : upsertCommand . workflowDatabaseIdForUpdate ,
198
- environmentId : upsertCommand . user . environmentId ,
199
- } ) ;
200
- }
201
- }
202
-
203
- private async workflowExistByExternalId ( upsertCommand : UpsertWorkflowCommand ) {
204
- const { environmentId } = upsertCommand . user ;
205
- const workflowByDbId = await this . notificationTemplateRepository . findByTriggerIdentifier (
206
- environmentId ,
207
- upsertCommand . workflowDto . name
208
- ) ;
209
-
210
- return ! ! workflowByDbId ;
211
- }
212
-
213
195
private convertCreateToUpdateCommand (
214
196
command : UpsertWorkflowCommand ,
215
197
existingWorkflow : NotificationTemplateEntity
@@ -229,6 +211,7 @@ export class UpsertWorkflowUseCase {
229
211
description : workflowDto . description ,
230
212
tags : workflowDto . tags ,
231
213
active : workflowDto . active ?? true ,
214
+ workflowId : workflowDto . workflowId ,
232
215
} ;
233
216
}
234
217
0 commit comments