|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * Example 001: Trigger a workflow |
| 4 | + * @author DocuSign |
| 5 | + */ |
| 6 | + |
| 7 | +const path = require('path'); |
| 8 | +const validator = require('validator'); |
| 9 | +const { formatString, API_TYPES } = require('../../utils.js'); |
| 10 | +const { getExampleByNumber } = require('../../manifestService'); |
| 11 | +const dsConfig = require('../../../config/index.js').config; |
| 12 | +const { getWorkflowDefinition, triggerWorkflow } = require('../examples/triggerWorkflow'); |
| 13 | +const { createWorkflow, publishWorkflow } = require('../workflowUtils.js'); |
| 14 | + |
| 15 | +const eg001TriggerWorkflow = exports; |
| 16 | +const exampleNumber = 1; |
| 17 | +const eg = `mseg00${exampleNumber}`; // This example reference. |
| 18 | +const api = API_TYPES.MAESTRO; |
| 19 | +const mustAuthenticate = '/ds/mustAuthenticate'; |
| 20 | +const minimumBufferMin = 3; |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * Create the envelope |
| 25 | + * @param {object} req Request obj |
| 26 | + * @param {object} res Response obj |
| 27 | + */ |
| 28 | +eg001TriggerWorkflow.createController = async (req, res) => { |
| 29 | + // Step 1. Check the token |
| 30 | + // At this point we should have a good token. But we |
| 31 | + // double-check here to enable a better UX to the user. |
| 32 | + const isTokenOK = req.dsAuth.checkToken(minimumBufferMin); |
| 33 | + if (!isTokenOK) { |
| 34 | + req.flash('info', 'Sorry, you need to re-authenticate.'); |
| 35 | + // Save the current operation so it will be resumed after authentication |
| 36 | + req.dsAuth.setEg(req, eg); |
| 37 | + return res.redirect(mustAuthenticate); |
| 38 | + } |
| 39 | + |
| 40 | + // Step 2. Call the worker method |
| 41 | + const { body } = req; |
| 42 | + const args = { |
| 43 | + instanceName: validator.escape(body.instanceName), |
| 44 | + signerEmail: validator.escape(body.signerEmail), |
| 45 | + signerName: validator.escape(body.signerName), |
| 46 | + ccEmail: validator.escape(body.ccEmail), |
| 47 | + ccName: validator.escape(body.ccName), |
| 48 | + workflowId: req.session.workflowId, |
| 49 | + accessToken: req.user.accessToken, |
| 50 | + basePath: dsConfig.maestroApiIrl, |
| 51 | + accountId: req.session.accountId, |
| 52 | + }; |
| 53 | + let results = null; |
| 54 | + |
| 55 | + try { |
| 56 | + const workflow = await getWorkflowDefinition(args); |
| 57 | + results = await triggerWorkflow(workflow, args); |
| 58 | + } catch (error) { |
| 59 | + const errorBody = error && error.response && error.response.body; |
| 60 | + // we can pull the DocuSign error code and message from the response body |
| 61 | + const errorCode = errorBody && errorBody.errorCode; |
| 62 | + const errorMessage = errorBody && errorBody.message; |
| 63 | + // In production, may want to provide customized error messages and |
| 64 | + // remediation advice to the user. |
| 65 | + res.render('pages/error', { err: error, errorCode, errorMessage }); |
| 66 | + } |
| 67 | + if (results) { |
| 68 | + req.session.envelopeId = results.envelopeId; // Save for use by other examples |
| 69 | + // which need an envelopeId |
| 70 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 71 | + res.render('pages/example_done', { |
| 72 | + title: example.ExampleName, |
| 73 | + message: formatString(example.ResultsPageText, JSON.stringify(results.envelopeId)), |
| 74 | + }); |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +/** |
| 79 | + * Form page for this application |
| 80 | + */ |
| 81 | +eg001TriggerWorkflow.getController = async (req, res) => { |
| 82 | + // Check that the authentication token is ok with a long buffer time. |
| 83 | + // If needed, now is the best time to ask the user to authenticate |
| 84 | + // since they have not yet entered any information into the form. |
| 85 | + const isTokenOK = req.dsAuth.checkToken(); |
| 86 | + if (!isTokenOK) { |
| 87 | + // Save the current operation so it will be resumed after authentication |
| 88 | + req.dsAuth.setEg(req, eg); |
| 89 | + return res.redirect(mustAuthenticate); |
| 90 | + } |
| 91 | + |
| 92 | + req.session.templateId = 'ae232f1f-911f-4115-8fff-6bcf47fa959a'; |
| 93 | + // req.session.workflowId = '0d4d954c-24d1-49d9-bc7f-4467041141c4'; |
| 94 | + const args = { |
| 95 | + templateId: req.session.templateId, |
| 96 | + accessToken: req.user.accessToken, |
| 97 | + basePath: dsConfig.maestroApiIrl, |
| 98 | + accountId: req.session.accountId, |
| 99 | + }; |
| 100 | + let workflowId; // = '0d4d954c-24d1-49d9-bc7f-4467041141c4'; |
| 101 | + |
| 102 | + // req.session.workflowId = 'c71e27eb-9ca7-4e88-8663-9bd2e76e77c7'; |
| 103 | + |
| 104 | + |
| 105 | + // if there is no workflow, then create one |
| 106 | + if (!req.session.workflowId) { |
| 107 | + if (!req.session.templateId) { |
| 108 | + return res.redirect('/eg008'); |
| 109 | + } |
| 110 | + |
| 111 | + req.session.workflowId = await createWorkflow(args); |
| 112 | + } |
| 113 | + |
| 114 | + if (!req.session.workflowPublished) { |
| 115 | + const consentUrl = await publishWorkflow(args, req.session.workflowId); |
| 116 | + if (consentUrl) { |
| 117 | + const redirectUrl = `${consentUrl}&host=${dsConfig.appUrl}/${eg}`; |
| 118 | + return res.redirect(redirectUrl); |
| 119 | + } |
| 120 | + |
| 121 | + req.session.workflowPublished = true; |
| 122 | + } |
| 123 | + |
| 124 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 125 | + const sourceFile = |
| 126 | + path.basename(__filename)[5].toLowerCase() + |
| 127 | + path.basename(__filename).substr(6); |
| 128 | + res.render('pages/maestro-examples/eg001TriggerWorkflow', { |
| 129 | + eg: eg, |
| 130 | + csrfToken: req.csrfToken(), |
| 131 | + example: example, |
| 132 | + sourceFile: sourceFile, |
| 133 | + sourceUrl: dsConfig.githubExampleUrl + 'maestro/examples/' + sourceFile, |
| 134 | + documentation: dsConfig.documentation + eg, |
| 135 | + showDoc: dsConfig.documentation, |
| 136 | + }); |
| 137 | +}; |
| 138 | + |
| 139 | + |
0 commit comments