Skip to content

Commit 6723672

Browse files
change api
1 parent 0a41f32 commit 6723672

File tree

7 files changed

+113
-277
lines changed

7 files changed

+113
-277
lines changed

client/src/api/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ export const api = Object.freeze({
124124
const response = await instance.get(`/workflows/definitions`);
125125
return response;
126126
},
127+
getWorkflowTriggerRequirements: async workflowId => {
128+
try {
129+
const response = await instance.put(`/workflows/${workflowId}/requirements`);
130+
return response;
131+
} catch (error) {
132+
return error.response;
133+
}
134+
},
127135
getWorkflowInstance: async workflow => {
128136
const response = await instance.get(`/workflows/${workflow.id}/instances/${workflow.instanceId}`);
129137
return response;

server/api/apiFactory.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const configureInterceptors = (api, accessToken) => {
2+
// Request interceptor for API calls
3+
api.interceptors.request.use(
4+
async config => {
5+
config.headers = {
6+
Accept: 'application/json',
7+
'Content-Type': 'application/json',
8+
};
9+
config.headers.Authorization = `Bearer ${accessToken}`;
10+
return config;
11+
},
12+
error => {
13+
Promise.reject(error);
14+
}
15+
);
16+
17+
api.interceptors.response.use(
18+
response => response,
19+
error => {
20+
// eslint-disable-next-line no-console
21+
console.error(`API call failed. Error: ${error}`);
22+
return Promise.reject(error);
23+
}
24+
);
25+
return api;
26+
};
27+
28+
const createAPI = (axios, accessToken) => {
29+
const api = configureInterceptors(
30+
axios.create({
31+
withCredentials: false,
32+
}),
33+
accessToken
34+
);
35+
return api;
36+
};
37+
38+
const createMaestroApi = (axios, basePath, accountId, accessToken) => {
39+
const api = createAPI(axios, accessToken);
40+
41+
const getWorkflowDefinitions = async params => {
42+
const response = await api.get(`${basePath}/accounts/${accountId}/workflows`, { params });
43+
return response.data;
44+
};
45+
46+
const getTriggerRequirements = async workflowId => {
47+
const response = await api.get(`${basePath}/accounts/${accountId}/workflows/${workflowId}/trigger-requirements`);
48+
return response.data;
49+
};
50+
51+
const triggerWorkflow = async (args, triggerUrl) => {
52+
const response = await api.post(triggerUrl, args);
53+
return response.data;
54+
};
55+
56+
return {
57+
getWorkflowDefinitions,
58+
getTriggerRequirements,
59+
triggerWorkflow,
60+
};
61+
};
62+
63+
module.exports = { createMaestroApi };

server/api/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const axios = require('axios');
2+
const { createMaestroApi } = require('./apiFactory');
3+
4+
const initMaestroApi = (accountId, basePath, accessToken) => createMaestroApi(axios, basePath, accountId, accessToken);
5+
6+
module.exports = { initMaestroApi };

server/controllers/workflowsController.js

Lines changed: 9 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const docusign = require('docusign-esign');
1515
const config = require('../config');
1616
const WorkflowsService = require('../services/workflowsService');
1717
const createPrefixedLogger = require('../utils/logger');
18-
const { getParameterValueFromUrl } = require('../utils/utils');
1918
const { TEMPLATE_TYPE } = require('../constants');
19+
const { getPayloadBySchema } = require('../utils/utils');
2020

2121
const oAuth = docusign.ApiClient.OAuth;
2222
const 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

server/routes/workflowsRouter.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@ const authMiddleware = require('../middlewares/authMiddleware');
44

55
const router = Router();
66

7-
router.get('/:definitionId/instances', authMiddleware, workflowsController.getWorkflowInstances);
8-
router.get('/:definitionId/instances/:instanceId', authMiddleware, workflowsController.getWorkflowInstance);
9-
router.put('/:definitionId/instances/:instanceId/cancel', authMiddleware, workflowsController.cancelWorkflow);
10-
11-
router.post('/create', authMiddleware, workflowsController.createWorkflow);
127
router.put('/:definitionId/trigger', authMiddleware, workflowsController.triggerWorkflow);
13-
router.get('/download/:templateName', workflowsController.downloadWorkflowTemplate);
14-
15-
router.post('/publish', authMiddleware, workflowsController.publishWorkflow);
168
router.get('/definitions', authMiddleware, workflowsController.getWorkflowDefinitions);
9+
router.get('/:definitionId/requirements', authMiddleware, workflowsController.getWorkflowTriggerRequirements);
1710

1811
module.exports = router;

0 commit comments

Comments
 (0)