Skip to content

Commit 225f4c5

Browse files
added all examples
1 parent a024cc2 commit 225f4c5

File tree

15 files changed

+442
-18
lines changed

15 files changed

+442
-18
lines changed

config/appsettings.example.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
"clickAPIUrl": "https://demo.docusign.net/clickapi",
3030
"adminAPIUrl": "https://api-d.docusign.net/management",
3131
"monitorApiUrl": "https://lens-d.docusign.net",
32+
"maestroApiIrl": "https://demo.services.docusign.net/aow-manage/v1.0",
3233
"codeExamplesManifest": "https://raw.githubusercontent.com/docusign/code-examples-csharp/master/manifest/CodeExamplesManifest.json"
3334
}

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const {
5454
} = require('./lib/admin/controllers');
5555

5656
const { eg001connect } = require('./lib/connect/controllers');
57-
const { eg001maestro } = require('./lib/maestro/controllers');
57+
const { eg001maestro, eg002maestro, eg003maestro } = require('./lib/maestro/controllers');
5858

5959
const PORT = process.env.PORT || 3000;
6060
const HOST = process.env.HOST || 'localhost';
@@ -274,7 +274,11 @@ app.get('/cneg001', eg001connect.getController)
274274
.post('/cneg001', eg001connect.createController);
275275

276276
app.get('/mseg001', eg001maestro.getController)
277-
.post('/mseg001', eg001maestro.createController);
277+
.post('/mseg001', eg001maestro.createController)
278+
.get('/mseg002', eg002maestro.getController)
279+
.post('/mseg002', eg002maestro.createController)
280+
.get('/mseg003', eg003maestro.getController)
281+
.post('/mseg003', eg003maestro.createController);
278282

279283
function dsLoginCB1(req, res, next) { req.dsAuthCodeGrant.oauth_callback1(req, res, next); }
280284
function dsLoginCB2(req, res, next) { req.dsAuthCodeGrant.oauth_callback2(req, res, next); }

lib/maestro/controllers/eg001TriggerWorkflow.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file
3-
* Example 001: Trigger a workflow
3+
* Example 001: How to trigger a Maestro workflow
44
* @author DocuSign
55
*/
66

@@ -21,7 +21,7 @@ const minimumBufferMin = 3;
2121

2222

2323
/**
24-
* Create the envelope
24+
* Trigger workflow
2525
* @param {object} req Request obj
2626
* @param {object} res Response obj
2727
*/
@@ -65,12 +65,13 @@ eg001TriggerWorkflow.createController = async (req, res) => {
6565
res.render('pages/error', { err: error, errorCode, errorMessage });
6666
}
6767
if (results) {
68-
req.session.envelopeId = results.envelopeId; // Save for use by other examples
68+
req.session.instanceId = results.instanceId; // Save for use by other examples
6969
// which need an envelopeId
7070
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
7171
res.render('pages/example_done', {
7272
title: example.ExampleName,
7373
message: formatString(example.ResultsPageText, JSON.stringify(results.envelopeId)),
74+
json: JSON.stringify(results),
7475
});
7576
}
7677
};
@@ -89,18 +90,12 @@ eg001TriggerWorkflow.getController = async (req, res) => {
8990
return res.redirect(mustAuthenticate);
9091
}
9192

92-
req.session.templateId = 'ae232f1f-911f-4115-8fff-6bcf47fa959a';
93-
// req.session.workflowId = '0d4d954c-24d1-49d9-bc7f-4467041141c4';
9493
const args = {
9594
templateId: req.session.templateId,
9695
accessToken: req.user.accessToken,
9796
basePath: dsConfig.maestroApiIrl,
9897
accountId: req.session.accountId,
9998
};
100-
let workflowId; // = '0d4d954c-24d1-49d9-bc7f-4467041141c4';
101-
102-
// req.session.workflowId = 'c71e27eb-9ca7-4e88-8663-9bd2e76e77c7';
103-
10499

105100
// if there is no workflow, then create one
106101
if (!req.session.workflowId) {
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @file
3+
* Example 002: How to cancel a Maestro workflow instance
4+
* @author DocuSign
5+
*/
6+
7+
const path = require('path');
8+
const { formatString, API_TYPES } = require('../../utils.js');
9+
const { getExampleByNumber } = require('../../manifestService');
10+
const dsConfig = require('../../../config/index.js').config;
11+
const { cancelWorkflowInstance } = require('../examples/cancelWorkflow');
12+
13+
const eg002CancelWorkflow = exports;
14+
const exampleNumber = 2;
15+
const eg = `mseg00${exampleNumber}`; // This example reference.
16+
const api = API_TYPES.MAESTRO;
17+
const mustAuthenticate = '/ds/mustAuthenticate';
18+
const minimumBufferMin = 3;
19+
20+
21+
/**
22+
* Cancel workflow instance
23+
* @param {object} req Request obj
24+
* @param {object} res Response obj
25+
*/
26+
eg002CancelWorkflow.createController = async (req, res) => {
27+
// Step 1. Check the token
28+
// At this point we should have a good token. But we
29+
// double-check here to enable a better UX to the user.
30+
const isTokenOK = req.dsAuth.checkToken(minimumBufferMin);
31+
if (!isTokenOK) {
32+
req.flash('info', 'Sorry, you need to re-authenticate.');
33+
// Save the current operation so it will be resumed after authentication
34+
req.dsAuth.setEg(req, eg);
35+
return res.redirect(mustAuthenticate);
36+
}
37+
38+
// Step 2. Call the worker method
39+
const args = {
40+
instanceId: req.session.instanceId,
41+
accessToken: req.user.accessToken,
42+
basePath: dsConfig.maestroApiIrl,
43+
accountId: req.session.accountId,
44+
};
45+
let results = null;
46+
47+
try {
48+
results = await cancelWorkflowInstance(args);
49+
} catch (error) {
50+
const errorBody = error && error.response && error.response.body;
51+
// we can pull the DocuSign error code and message from the response body
52+
const errorCode = errorBody && errorBody.errorCode;
53+
const errorMessage = errorBody && errorBody.message;
54+
// In production, may want to provide customized error messages and
55+
// remediation advice to the user.
56+
res.render('pages/error', { err: error, errorCode, errorMessage });
57+
}
58+
if (results) {
59+
// which need an envelopeId
60+
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
61+
res.render('pages/example_done', {
62+
title: example.ExampleName,
63+
message: formatString(example.ResultsPageText, JSON.stringify(req.session.instanceId)),
64+
json: JSON.stringify(results),
65+
});
66+
}
67+
};
68+
69+
/**
70+
* Form page for this application
71+
*/
72+
eg002CancelWorkflow.getController = async (req, res) => {
73+
// Check that the authentication token is ok with a long buffer time.
74+
// If needed, now is the best time to ask the user to authenticate
75+
// since they have not yet entered any information into the form.
76+
const isTokenOK = req.dsAuth.checkToken();
77+
if (!isTokenOK) {
78+
// Save the current operation so it will be resumed after authentication
79+
req.dsAuth.setEg(req, eg);
80+
return res.redirect(mustAuthenticate);
81+
}
82+
83+
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
84+
const sourceFile =
85+
path.basename(__filename)[5].toLowerCase() +
86+
path.basename(__filename).substr(6);
87+
res.render('pages/maestro-examples/eg002CancelWorkflow', {
88+
eg: eg,
89+
csrfToken: req.csrfToken(),
90+
example: example,
91+
workflowId: req.session.workflowId,
92+
instanceId: req.session.instanceId,
93+
sourceFile: sourceFile,
94+
sourceUrl: dsConfig.githubExampleUrl + 'maestro/examples/' + sourceFile,
95+
documentation: dsConfig.documentation + eg,
96+
showDoc: dsConfig.documentation,
97+
});
98+
};
99+
100+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @file
3+
* Example 003: How to get the status of a Maestro workflow instance
4+
* @author DocuSign
5+
*/
6+
7+
const path = require('path');
8+
const { formatString, API_TYPES } = require('../../utils.js');
9+
const { getExampleByNumber } = require('../../manifestService');
10+
const dsConfig = require('../../../config/index.js').config;
11+
const { getWorkflowInstance } = require('../examples/getWorkflowStatus');
12+
13+
const eg002CancelWorkflow = exports;
14+
const exampleNumber = 3;
15+
const eg = `mseg00${exampleNumber}`; // This example reference.
16+
const api = API_TYPES.MAESTRO;
17+
const mustAuthenticate = '/ds/mustAuthenticate';
18+
const minimumBufferMin = 3;
19+
20+
21+
/**
22+
* Get workflow instance status
23+
* @param {object} req Request obj
24+
* @param {object} res Response obj
25+
*/
26+
eg002CancelWorkflow.createController = async (req, res) => {
27+
// Step 1. Check the token
28+
// At this point we should have a good token. But we
29+
// double-check here to enable a better UX to the user.
30+
const isTokenOK = req.dsAuth.checkToken(minimumBufferMin);
31+
if (!isTokenOK) {
32+
req.flash('info', 'Sorry, you need to re-authenticate.');
33+
// Save the current operation so it will be resumed after authentication
34+
req.dsAuth.setEg(req, eg);
35+
return res.redirect(mustAuthenticate);
36+
}
37+
38+
// Step 2. Call the worker method
39+
const args = {
40+
workflowId: req.session.workflowId,
41+
instanceId: req.session.instanceId,
42+
accessToken: req.user.accessToken,
43+
basePath: dsConfig.maestroApiIrl,
44+
accountId: req.session.accountId,
45+
};
46+
let results = null;
47+
48+
try {
49+
results = await getWorkflowInstance(args);
50+
} catch (error) {
51+
const errorBody = error && error.response && error.response.body;
52+
// we can pull the DocuSign error code and message from the response body
53+
const errorCode = errorBody && errorBody.errorCode;
54+
const errorMessage = errorBody && errorBody.message;
55+
// In production, may want to provide customized error messages and
56+
// remediation advice to the user.
57+
res.render('pages/error', { err: error, errorCode, errorMessage });
58+
}
59+
if (results) {
60+
// which need an envelopeId
61+
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
62+
res.render('pages/example_done', {
63+
title: example.ExampleName,
64+
message: formatString(example.ResultsPageText, JSON.stringify(results.instanceState)),
65+
json: JSON.stringify(results),
66+
});
67+
}
68+
};
69+
70+
/**
71+
* Form page for this application
72+
*/
73+
eg002CancelWorkflow.getController = async (req, res) => {
74+
// Check that the authentication token is ok with a long buffer time.
75+
// If needed, now is the best time to ask the user to authenticate
76+
// since they have not yet entered any information into the form.
77+
const isTokenOK = req.dsAuth.checkToken();
78+
if (!isTokenOK) {
79+
// Save the current operation so it will be resumed after authentication
80+
req.dsAuth.setEg(req, eg);
81+
return res.redirect(mustAuthenticate);
82+
}
83+
84+
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
85+
const sourceFile =
86+
path.basename(__filename)[5].toLowerCase() +
87+
path.basename(__filename).substr(6);
88+
res.render('pages/maestro-examples/eg003GetWorkflowStatus', {
89+
eg: eg,
90+
csrfToken: req.csrfToken(),
91+
example: example,
92+
workflowId: req.session.workflowId,
93+
instanceId: req.session.instanceId,
94+
sourceFile: sourceFile,
95+
sourceUrl: dsConfig.githubExampleUrl + 'maestro/examples/' + sourceFile,
96+
documentation: dsConfig.documentation + eg,
97+
showDoc: dsConfig.documentation,
98+
});
99+
};

lib/maestro/controllers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
module.exports.eg001maestro = require('./eg001TriggerWorkflow');
2+
module.exports.eg002maestro = require('./eg002CancelWorkflow');
3+
module.exports.eg003maestro = require('./eg003GetWorkflowStatus');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @file
3+
* Example 002: How to cancel a Maestro workflow instance
4+
* @author DocuSign
5+
*/
6+
7+
const docusign = require('docusign-maestro');
8+
9+
const cancelWorkflowInstance = async (args) => {
10+
const dsApiClient = new docusign.ApiClient();
11+
dsApiClient.setBasePath(args.basePath);
12+
dsApiClient.addDefaultHeader('Authorization', `Bearer ${args.accessToken}`);
13+
14+
const workflowInstanceManagementApi = new docusign.WorkflowInstanceManagementApi(dsApiClient);
15+
const cancelResponse =
16+
await workflowInstanceManagementApi.cancelWorkflowInstance(
17+
args.accountId,
18+
args.instanceId
19+
);
20+
21+
return cancelResponse;
22+
};
23+
24+
module.exports = { cancelWorkflowInstance };
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @file
3+
* Example 003: How to get the status of a Maestro workflow instance
4+
* @author DocuSign
5+
*/
6+
7+
const docusign = require('docusign-maestro');
8+
9+
const getWorkflowInstance = async (args) => {
10+
const dsApiClient = new docusign.ApiClient();
11+
dsApiClient.setBasePath(args.basePath);
12+
dsApiClient.addDefaultHeader('Authorization', `Bearer ${args.accessToken}`);
13+
14+
const workflowInstanceManagementApi = new docusign.WorkflowInstanceManagementApi(dsApiClient);
15+
const workflowInstance =
16+
await workflowInstanceManagementApi.getWorkflowInstance(
17+
args.accountId,
18+
args.workflowId,
19+
args.instanceId
20+
);
21+
22+
return workflowInstance;
23+
};
24+
25+
module.exports = { getWorkflowInstance };

lib/maestro/examples/triggerWorkflow.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file
3-
* Example 001: Trigger workflow
3+
* Example 001: How to trigger a Maestro workflow
44
* @author DocuSign
55
*/
66

@@ -42,7 +42,7 @@ const triggerWorkflow = async (workflow, args) => {
4242
metadata: {}
4343
});
4444

45-
const response = makePostRequest(workflow.triggerUrl, triggerPayload, {
45+
const response = await makePostRequest(workflow.triggerUrl, triggerPayload, {
4646
headers: {
4747
Authorization: `Bearer ${args.accessToken}`,
4848
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)