@@ -15,8 +15,8 @@ const docusign = require('docusign-esign');
1515const config = require ( '../config' ) ;
1616const WorkflowsService = require ( '../services/workflowsService' ) ;
1717const createPrefixedLogger = require ( '../utils/logger' ) ;
18- const { getParameterValueFromUrl } = require ( '../utils/utils' ) ;
1918const { TEMPLATE_TYPE } = require ( '../constants' ) ;
19+ const { getPayloadBySchema } = require ( '../utils/utils' ) ;
2020
2121const oAuth = docusign . ApiClient . OAuth ;
2222const restApi = docusign . ApiClient . RestApi ;
@@ -28,79 +28,6 @@ class WorkflowsController {
2828 static templatesPath = path . join ( path . resolve ( ) , 'assets/templates' ) ;
2929 static logger = createPrefixedLogger ( WorkflowsController . name ) ;
3030
31- /**
32- * Creates workflow instance and sends a response.
33- */
34- static createWorkflow = async ( req , res ) => {
35- try {
36- const args = {
37- basePath : this . basePath ,
38- accessToken : req . user . accessToken ,
39- accountId : req . session . accountId ,
40- templateType : req ?. body ?. templateType ,
41- } ;
42- let templateResponse = await WorkflowsService . getTemplate ( args ) ;
43-
44- if ( ! templateResponse . templateId ) {
45- templateResponse = await WorkflowsService . createTemplate ( args ) ;
46- }
47-
48- const workflow = await WorkflowsService . createWorkflow ( {
49- ...args ,
50- templateId : templateResponse . templateId ,
51- basePath : config . maestroApiUrl ,
52- } ) ;
53-
54- res . json ( { workflowDefinitionId : workflow . workflowDefinitionId } ) ;
55- } catch ( error ) {
56- this . handleErrorResponse ( error , res ) ;
57- }
58- } ;
59-
60- /**
61- * Cancels workflow instance and sends a response.
62- */
63- static cancelWorkflow = async ( req , res ) => {
64- try {
65- const result = await WorkflowsService . cancelWorkflowInstance ( {
66- instanceId : req ?. params ?. instanceId ,
67- accessToken : req ?. user ?. accessToken || req ?. session ?. accessToken ,
68- basePath : config . maestroApiUrl ,
69- accountId : req . session . accountId ,
70- } ) ;
71- res . status ( 200 ) . send ( result ) ;
72- } catch ( error ) {
73- this . handleErrorResponse ( error , res ) ;
74- }
75- } ;
76-
77- /**
78- * Publish workflow by id.
79- */
80- static publishWorkflow = async ( req , res ) => {
81- const workflowId = req ?. body ?. workflowId ;
82-
83- try {
84- let result = await WorkflowsService . publishWorkflow (
85- {
86- basePath : config . maestroApiUrl ,
87- accessToken : req . user . accessToken ,
88- accountId : req . session . accountId ,
89- } ,
90- workflowId
91- ) ;
92-
93- res . status ( 200 ) . send ( result ) ;
94- } catch ( error ) {
95- if ( error ?. errorMessage === 'Consent required' ) {
96- res . status ( 210 ) . send ( error . consentUrl ) ;
97- return ;
98- }
99-
100- this . handleErrorResponse ( error , res ) ;
101- }
102- } ;
103-
10431 /**
10532 * Gets workflow definitions and returns it.
10633 */
@@ -118,35 +45,16 @@ class WorkflowsController {
11845 } ;
11946
12047 /**
121- * Gets workflow instance and returns it .
48+ * Gets the requirements to trigger workflow .
12249 */
123- static getWorkflowInstance = async ( req , res ) => {
50+ static getWorkflowTriggerRequirements = async ( req , res ) => {
12451 try {
125- const result = await WorkflowsService . getWorkflowInstance ( {
126- instanceId : req ?. params ?. instanceId ,
127- definitionId : req ?. params ?. definitionId ,
52+ const results = await WorkflowsService . getWorkflowTriggerRequirements ( {
12853 accessToken : req ?. user ?. accessToken || req ?. session ?. accessToken ,
12954 basePath : config . maestroApiUrl ,
13055 accountId : req . session . accountId ,
56+ workflowId : req . params . definitionId ,
13157 } ) ;
132- res . status ( 200 ) . send ( result ) ;
133- } catch ( error ) {
134- this . handleErrorResponse ( error , res ) ;
135- }
136- } ;
137-
138- /**
139- * Gets workflow instances and returns it.
140- */
141- static getWorkflowInstances = async ( req , res ) => {
142- try {
143- const results = await WorkflowsService . getWorkflowInstances ( {
144- definitionId : req . params . definitionId ,
145- accessToken : req ?. user ?. accessToken || req ?. session ?. accessToken ,
146- basePath : config . maestroApiUrl ,
147- accountId : req . session . accountId ,
148- } ) ;
149-
15058 res . status ( 200 ) . send ( results ) ;
15159 } catch ( error ) {
15260 this . handleErrorResponse ( error , res ) ;
@@ -159,58 +67,25 @@ class WorkflowsController {
15967 static triggerWorkflow = async ( req , res ) => {
16068 const { body } = req ;
16169
162- const mainArgs = {
70+ const args = {
16371 templateType : req . query . type ,
16472 workflowId : req . params . definitionId ,
16573 accessToken : req ?. user ?. accessToken || req ?. session ?. accessToken ,
16674 basePath : config . maestroApiUrl ,
16775 accountId : req . session . accountId ,
168- mtid : undefined ,
169- mtsec : undefined ,
17076 } ;
17177
172- const bodyArgs = { } ;
173- if ( req . query . type === TEMPLATE_TYPE . I9 ) {
174- bodyArgs . preparerName = validator . escape ( body ?. preparerName ) ;
175- bodyArgs . preparerEmail = validator . escape ( body ?. preparerEmail ) ;
176- bodyArgs . employeeName = validator . escape ( body ?. employeeName ) ;
177- bodyArgs . employeeEmail = validator . escape ( body ?. employeeEmail ) ;
178- bodyArgs . hrApproverName = validator . escape ( body ?. hrApproverName ) ;
179- bodyArgs . hrApproverEmail = validator . escape ( body ?. hrApproverEmail ) ;
180- }
181- if ( req . query . type === TEMPLATE_TYPE . OFFER ) {
182- bodyArgs . hrManagerName = validator . escape ( body ?. hrManagerName ) ;
183- bodyArgs . hrManagerEmail = validator . escape ( body ?. hrManagerEmail ) ;
184- bodyArgs . Company = validator . escape ( body ?. Company ) ;
185- }
186- if ( req . query . type === TEMPLATE_TYPE . NDA ) {
187- bodyArgs . hrManagerName = validator . escape ( body ?. hrManagerName ) ;
188- bodyArgs . hrManagerEmail = validator . escape ( body ?. hrManagerEmail ) ;
189- }
190-
191- const args = { ...mainArgs , ...bodyArgs } ;
192-
19378 try {
194- const workflow = await WorkflowsService . getWorkflowDefinition ( args ) ;
195- args . mtid = getParameterValueFromUrl ( workflow . triggerUrl , 'mtid' ) ;
196- args . mtsec = getParameterValueFromUrl ( workflow . triggerUrl , 'mtsec' ) ;
79+ const triggerRequirements = await WorkflowsService . getWorkflowTriggerRequirements ( args ) ;
80+ const payload = getPayloadBySchema ( body , triggerRequirements . trigger_input_schema ) ;
19781
198- const result = await WorkflowsService . triggerWorkflowInstance ( args ) ;
82+ const result = await WorkflowsService . triggerWorkflowInstance ( args , payload , triggerRequirements ) ;
19983 res . status ( 200 ) . send ( result ) ;
20084 } catch ( error ) {
20185 this . handleErrorResponse ( error , res ) ;
20286 }
20387 } ;
20488
205- /**
206- * Download workflow template from assets/[name].json.
207- */
208- static downloadWorkflowTemplate = async ( req , res ) => {
209- const templateName = req . params . templateName ;
210- const templatePath = path . resolve ( this . templatesPath , templateName ) ;
211- res . download ( templatePath ) ;
212- } ;
213-
21489 static handleErrorResponse ( error , res ) {
21590 this . logger . error ( `handleErrorResponse: ${ error } ` ) ;
21691
0 commit comments