|
| 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 | +}; |
0 commit comments