|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * Example 001: Set connected fields |
| 4 | + * @author DocuSign |
| 5 | + */ |
| 6 | + |
| 7 | +const path = require('path'); |
| 8 | +const { getTabGroups, sendEnvelope, extractVerificationData } = require('../examples/setConnectedFields'); |
| 9 | +const validator = require('validator'); |
| 10 | +const { getExampleByNumber } = require('../../manifestService'); |
| 11 | +const dsConfig = require('../../../config/index.js').config; |
| 12 | +const { API_TYPES } = require('../../utils.js'); |
| 13 | + |
| 14 | +const eg001SetConnectedFields = exports; |
| 15 | +const exampleNumber = 1; |
| 16 | +const eg = `feg00${exampleNumber}`; // This example reference. |
| 17 | +const api = API_TYPES.CONNECTED_FIELDS; |
| 18 | +const mustAuthenticate = '/ds/mustAuthenticate'; |
| 19 | +const minimumBufferMin = 3; |
| 20 | +const demoDocsPath = path.resolve(__dirname, '../../../demo_documents'); |
| 21 | +const pdfFile = 'World_Wide_Corp_lorem.pdf'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Create the envelope, the embedded signing, and then redirect to the DocuSign signing |
| 25 | + * @param {object} req Request obj |
| 26 | + * @param {object} res Response obj |
| 27 | + */ |
| 28 | +eg001SetConnectedFields.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 selectedAppId = validator.escape(body.appId); |
| 43 | + const verificationData = extractVerificationData(selectedAppId, req.session.apps); |
| 44 | + const envelopeArgs = { |
| 45 | + signerEmail: validator.escape(body.signerEmail), |
| 46 | + signerName: validator.escape(body.signerName), |
| 47 | + docFile: path.resolve(demoDocsPath, pdfFile), |
| 48 | + verificationData |
| 49 | + }; |
| 50 | + const args = { |
| 51 | + accessToken: req.user.accessToken, |
| 52 | + basePath: req.session.basePath, |
| 53 | + accountId: req.session.accountId, |
| 54 | + envelopeArgs: envelopeArgs, |
| 55 | + }; |
| 56 | + |
| 57 | + let results = null; |
| 58 | + try { |
| 59 | + results = await sendEnvelope(args); |
| 60 | + } catch (error) { |
| 61 | + const errorBody = error && error.response && error.response.body; |
| 62 | + // we can pull the DocuSign error code and message from the response body |
| 63 | + const errorCode = errorBody && errorBody.errorCode; |
| 64 | + const errorMessage = errorBody && errorBody.message; |
| 65 | + // In production, may want to provide customized error messages and |
| 66 | + // remediation advice to the user. |
| 67 | + res.render('pages/error', { err: error, errorCode, errorMessage }); |
| 68 | + } |
| 69 | + if (results) { |
| 70 | + req.session.envelopeId = results.envelopeId; // Save for use by other examples |
| 71 | + // which need an envelopeId |
| 72 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 73 | + res.render('pages/example_done', { |
| 74 | + title: example.ExampleName, |
| 75 | + message: formatString(example.ResultsPageText, results.envelopeId), |
| 76 | + }); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | + /** |
| 81 | + * Form page for this application |
| 82 | + */ |
| 83 | + eg001SetConnectedFields.getController = async (req, res) => { |
| 84 | + // Check that the authentication token is ok with a long buffer time. |
| 85 | + // If needed, now is the best time to ask the user to authenticate |
| 86 | + // since they have not yet entered any information into the form. |
| 87 | + const isTokenOK = req.dsAuth.checkToken(); |
| 88 | + if (!isTokenOK) { |
| 89 | + // Save the current operation so it will be resumed after authentication |
| 90 | + req.dsAuth.setEg(req, eg); |
| 91 | + return res.redirect(mustAuthenticate); |
| 92 | + } |
| 93 | + |
| 94 | + const args = { |
| 95 | + accessToken: req.user.accessToken, |
| 96 | + basePath: 'https://api-d.docusign.com', |
| 97 | + accountId: req.session.accountId, |
| 98 | + }; |
| 99 | + const tabGroups = await getTabGroups(args); |
| 100 | + |
| 101 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 102 | + if (tabGroups.length === 0) { |
| 103 | + const additionalPageData = example.AdditionalPage.filter(p => p.Name === 'no_verification_app')[0]; |
| 104 | + |
| 105 | + return res.render('pages/example_done', { |
| 106 | + title: example.ExampleName, |
| 107 | + message: additionalPageData?.ResultsPageText, |
| 108 | + }); |
| 109 | + } |
| 110 | + req.session.apps = tabGroups; |
| 111 | + |
| 112 | + const sourceFile = |
| 113 | + path.basename(__filename)[5].toLowerCase() + |
| 114 | + path.basename(__filename).substr(6); |
| 115 | + res.render('pages/connected-fields/eg001SetConnectedFields', { |
| 116 | + eg: eg, |
| 117 | + csrfToken: req.csrfToken(), |
| 118 | + example: example, |
| 119 | + sourceFile: sourceFile, |
| 120 | + sourceUrl: dsConfig.githubExampleUrl + 'connectedFields/examples/' + sourceFile, |
| 121 | + documentation: dsConfig.documentation + eg, |
| 122 | + showDoc: dsConfig.documentation, |
| 123 | + apps: tabGroups, |
| 124 | + }); |
| 125 | + |
| 126 | +}; |
0 commit comments