|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * Example 001: How to trigger a Maestro 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 { getMaestroWorkflows, triggerWorkflow } = require('../examples/triggerWorkflow'); |
| 13 | +const { createWorkflow, publishWorkflow } = require('../workflowUtils.js'); |
| 14 | + |
| 15 | +const eg001TriggerWorkflow = exports; |
| 16 | +const exampleNumber = 1; |
| 17 | +const eg = `mae00${exampleNumber}`; // This example reference. |
| 18 | +const api = API_TYPES.MAESTRO; |
| 19 | +const mustAuthenticate = '/ds/mustAuthenticate'; |
| 20 | +const minimumBufferMin = 3; |
| 21 | +const workflowName = 'Example workflow - send invite to signer'; |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * Trigger workflow |
| 26 | + * @param {object} req Request obj |
| 27 | + * @param {object} res Response obj |
| 28 | + */ |
| 29 | +eg001TriggerWorkflow.createController = async (req, res) => { |
| 30 | + // Step 1. Check the token |
| 31 | + // At this point we should have a good token. But we |
| 32 | + // double-check here to enable a better UX to the user. |
| 33 | + const isTokenOK = req.dsAuth.checkToken(minimumBufferMin); |
| 34 | + if (!isTokenOK) { |
| 35 | + req.flash('info', 'Sorry, you need to re-authenticate.'); |
| 36 | + // Save the current operation so it will be resumed after authentication |
| 37 | + req.dsAuth.setEg(req, eg); |
| 38 | + return res.redirect(mustAuthenticate); |
| 39 | + } |
| 40 | + |
| 41 | + // Step 2. Call the worker method |
| 42 | + const { body } = req; |
| 43 | + const args = { |
| 44 | + instanceName: validator.escape(body.instanceName), |
| 45 | + signerEmail: validator.escape(body.signerEmail), |
| 46 | + signerName: validator.escape(body.signerName), |
| 47 | + ccEmail: validator.escape(body.ccEmail), |
| 48 | + ccName: validator.escape(body.ccName), |
| 49 | + accessToken: req.user.accessToken, |
| 50 | + basePath: dsConfig.maestroApiUrl, |
| 51 | + accountId: req.session.accountId, |
| 52 | + }; |
| 53 | + |
| 54 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 55 | + try { |
| 56 | + const { instanceUrl, instanceId } = await triggerWorkflow(args, req.session.workflowId); |
| 57 | + req.session.instanceId = instanceId; |
| 58 | + |
| 59 | + return res.render('pages/maestro-examples/eg001EmbedWorkflow', { |
| 60 | + title: example.ExampleName, |
| 61 | + message: formatString(example.ResultsPageText, JSON.stringify(instanceId)), |
| 62 | + instanceUrl, |
| 63 | + }); |
| 64 | + } catch (error) { |
| 65 | + const errorCode = error?.response?.statusCode; |
| 66 | + const errorMessage = error?.response?.body?.message; |
| 67 | + return res.render('pages/error', { err: error, errorCode, errorMessage }); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * Form page for this application |
| 73 | + */ |
| 74 | +eg001TriggerWorkflow.getController = async (req, res) => { |
| 75 | + // Check that the authentication token is ok with a long buffer time. |
| 76 | + // If needed, now is the best time to ask the user to authenticate |
| 77 | + // since they have not yet entered any information into the form. |
| 78 | + const isTokenOK = req.dsAuth.checkToken(); |
| 79 | + if (!isTokenOK) { |
| 80 | + // Save the current operation so it will be resumed after authentication |
| 81 | + req.dsAuth.setEg(req, eg); |
| 82 | + return res.redirect(mustAuthenticate); |
| 83 | + } |
| 84 | + |
| 85 | + const args = { |
| 86 | + accessToken: req.user.accessToken, |
| 87 | + basePath: dsConfig.maestroApiUrl, |
| 88 | + accountId: req.session.accountId, |
| 89 | + }; |
| 90 | + |
| 91 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 92 | + const sourceFile = |
| 93 | + path.basename(__filename)[5].toLowerCase() + |
| 94 | + path.basename(__filename).substr(6); |
| 95 | + |
| 96 | + try { |
| 97 | + const workflows = await getMaestroWorkflows(args); |
| 98 | + if (!workflows.data || workflows.data.length === 0 || !workflows.data.find(wf => wf.name === workflowName)) { |
| 99 | + if (!req.session.templateId) { |
| 100 | + return res.render('pages/maestro-examples/eg001TriggerWorkflow', { |
| 101 | + eg: eg, |
| 102 | + csrfToken: req.csrfToken(), |
| 103 | + example: example, |
| 104 | + templateOk: false, |
| 105 | + sourceFile: sourceFile, |
| 106 | + sourceUrl: dsConfig.githubExampleUrl + 'maestro/examples/' + sourceFile, |
| 107 | + documentation: dsConfig.documentation + eg, |
| 108 | + showDoc: dsConfig.documentation, |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + const newWorkflow = await createWorkflow(args, req.session.templateId); |
| 113 | + const consentUrl = await publishWorkflow(args, newWorkflow.workflowDefinitionId); |
| 114 | + |
| 115 | + req.session.workflowId = newWorkflow.workflowDefinitionId; |
| 116 | + |
| 117 | + if (consentUrl) { |
| 118 | + return res.render('pages/maestro-examples/eg001PublishWorkflow', { |
| 119 | + eg: eg, |
| 120 | + csrfToken: req.csrfToken(), |
| 121 | + example: example, |
| 122 | + message: example.AdditionalPage[0].ResultsPageText, |
| 123 | + consentUrl |
| 124 | + }); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + const workflow = workflows.data.find(wf => wf.name === workflowName); |
| 129 | + req.session.workflowId = workflow.id; |
| 130 | + } catch (error) { |
| 131 | + const errorCode = error?.response?.statusCode; |
| 132 | + const errorMessage = error?.response?.body?.message; |
| 133 | + return res.render('pages/error', { err: error, errorCode, errorMessage }); |
| 134 | + } |
| 135 | + |
| 136 | + res.render('pages/maestro-examples/eg001TriggerWorkflow', { |
| 137 | + eg: eg, |
| 138 | + csrfToken: req.csrfToken(), |
| 139 | + example: example, |
| 140 | + sourceFile: sourceFile, |
| 141 | + templateOk: true, |
| 142 | + sourceUrl: dsConfig.githubExampleUrl + 'maestro/examples/' + sourceFile, |
| 143 | + documentation: dsConfig.documentation + eg, |
| 144 | + showDoc: dsConfig.documentation, |
| 145 | + }); |
| 146 | +}; |
| 147 | + |
| 148 | +/** |
| 149 | + * Publish workflow |
| 150 | + */ |
| 151 | +eg001TriggerWorkflow.publishController = async (req, res) => { |
| 152 | + // Check that the authentication token is ok with a long buffer time. |
| 153 | + // If needed, now is the best time to ask the user to authenticate |
| 154 | + // since they have not yet entered any information into the form. |
| 155 | + const isTokenOK = req.dsAuth.checkToken(); |
| 156 | + if (!isTokenOK) { |
| 157 | + // Save the current operation so it will be resumed after authentication |
| 158 | + req.dsAuth.setEg(req, eg); |
| 159 | + return res.redirect(mustAuthenticate); |
| 160 | + } |
| 161 | + |
| 162 | + const args = { |
| 163 | + accessToken: req.user.accessToken, |
| 164 | + basePath: dsConfig.maestroApiUrl, |
| 165 | + accountId: req.session.accountId, |
| 166 | + }; |
| 167 | + |
| 168 | + try { |
| 169 | + const consentUrl = await publishWorkflow(args, req.session.workflowId); |
| 170 | + |
| 171 | + if (consentUrl) { |
| 172 | + return res.render('pages/maestro-examples/eg001PublishWorkflow', { |
| 173 | + eg: eg, |
| 174 | + csrfToken: req.csrfToken(), |
| 175 | + example: example, |
| 176 | + message: example.AdditionalPage[0].ResultsPageText, |
| 177 | + consentUrl |
| 178 | + }); |
| 179 | + } |
| 180 | + } catch (error) { |
| 181 | + const errorCode = error?.response?.statusCode; |
| 182 | + const errorMessage = error?.response?.body?.message; |
| 183 | + return res.render('pages/error', { err: error, errorCode, errorMessage }); |
| 184 | + } |
| 185 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 186 | + const sourceFile = |
| 187 | + path.basename(__filename)[5].toLowerCase() + |
| 188 | + path.basename(__filename).substr(6); |
| 189 | + |
| 190 | + res.render('pages/maestro-examples/eg001TriggerWorkflow', { |
| 191 | + eg: eg, |
| 192 | + csrfToken: req.csrfToken(), |
| 193 | + example: example, |
| 194 | + sourceFile: sourceFile, |
| 195 | + templateOk: true, |
| 196 | + sourceUrl: dsConfig.githubExampleUrl + 'maestro/examples/' + sourceFile, |
| 197 | + documentation: dsConfig.documentation + eg, |
| 198 | + showDoc: dsConfig.documentation, |
| 199 | + }); |
| 200 | +}; |
0 commit comments