Skip to content

Commit 0b361c5

Browse files
Check workflow instance status before cancelling it (#226)
* check workflow instance status
1 parent b36ad50 commit 0b361c5

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/maestro/controllers/eg004CancelWorkflow.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ const path = require('path');
88
const { formatString, API_TYPES } = require('../../utils.js');
99
const { getExampleByNumber } = require('../../manifestService');
1010
const dsConfig = require('../../../config/index.js').config;
11-
const { cancelWorkflow } = require('../examples/cancelWorkflow');
11+
const { getWorkflowInstance, cancelWorkflow } = require('../examples/cancelWorkflow');
1212

1313
const eg004CancelWorkflow = exports;
1414
const exampleNumber = 4;
1515
const eg = `mae00${exampleNumber}`; // This example reference.
1616
const api = API_TYPES.MAESTRO;
1717
const mustAuthenticate = '/ds/mustAuthenticate';
1818
const minimumBufferMin = 3;
19+
const inProgressWorkflowStatus = 'In Progress';
1920

2021
/**
2122
* Trigger workflow
@@ -83,6 +84,23 @@ eg004CancelWorkflow.getController = async (req, res) => {
8384
return res.render('pages/error', { errorCode, errorMessage, errorInfo });
8485
}
8586

87+
const args = {
88+
workflowId: req.session.workflowId,
89+
instanceId: req.session.instanceId,
90+
accessToken: req.user.accessToken,
91+
basePath: dsConfig.maestroApiUrl,
92+
accountId: req.session.accountId,
93+
};
94+
95+
const instance = await getWorkflowInstance(args);
96+
97+
if (instance.workflowStatus.toLocaleLowerCase() !== inProgressWorkflowStatus.toLocaleLowerCase()) {
98+
const errorCode = '400';
99+
const errorMessage = 'Invalid workflow status';
100+
const errorInfo = example.CustomErrorTexts[2].ErrorMessage;
101+
return res.render('pages/error', { errorCode, errorMessage, errorInfo });
102+
}
103+
86104
const sourceFile =
87105
path.basename(__filename)[5].toLowerCase() +
88106
path.basename(__filename).substr(6);

lib/maestro/examples/cancelWorkflow.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
const iam = require('@docusign/iam-sdk');
22

3+
const getWorkflowInstance = async (args) => {
4+
const client = new iam.IamClient({ accessToken: args.accessToken });
5+
6+
return await client.maestro.workflowInstanceManagement.getWorkflowInstance({
7+
accountId: args.accountId,
8+
workflowId: args.workflowId,
9+
instanceId: args.instanceId
10+
});
11+
};
12+
313
const cancelWorkflow = async (args) => {
414
//ds-snippet-start:Maestro4Step2
515
const client = new iam.IamClient({ accessToken: args.accessToken });
@@ -14,4 +24,4 @@ const cancelWorkflow = async (args) => {
1424
//ds-snippet-end:Maestro4Step3
1525
};
1626

17-
module.exports = { cancelWorkflow };
27+
module.exports = { getWorkflowInstance, cancelWorkflow };

0 commit comments

Comments
 (0)