|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * Example 013: How to create an account |
| 4 | + * @author DocuSign |
| 5 | + */ |
| 6 | + |
| 7 | +const path = require('path'); |
| 8 | +const validator = require('validator'); |
| 9 | +const dsConfig = require('../../../config/index.js').config; |
| 10 | +const { createAccount, getOrganizationPlanItem } = require('../examples/createAccount'); |
| 11 | +const { getOrganizationId } = require('../getOrganizationId.js'); |
| 12 | +const { getExampleByNumber } = require('../../manifestService'); |
| 13 | +const { API_TYPES } = require('../../utils.js'); |
| 14 | + |
| 15 | +const eg013CreateAccount = exports; |
| 16 | +const exampleNumber = 13; |
| 17 | +const eg = `aeg0${exampleNumber}`; // This example reference. |
| 18 | +const api = API_TYPES.ADMIN; |
| 19 | +const mustAuthenticate = '/ds/mustAuthenticate'; |
| 20 | +const minimumBufferMin = 3; |
| 21 | + |
| 22 | +/** |
| 23 | + * Clone an account |
| 24 | + * @param {object} req Request obj |
| 25 | + * @param {object} res Response obj |
| 26 | + */ |
| 27 | +eg013CreateAccount.createController = async (req, res) => { |
| 28 | + // Step 1. Check the token |
| 29 | + // At this point we should have a good token. But we |
| 30 | + // double-check here to enable a better UX to the user. |
| 31 | + const isTokenOK = req.dsAuth.checkToken(minimumBufferMin); |
| 32 | + if (!isTokenOK) { |
| 33 | + req.flash('info', 'Sorry, you need to re-authenticate.'); |
| 34 | + // Save the current operation so it will be resumed after authentication |
| 35 | + req.dsAuth.setEg(req, eg); |
| 36 | + return res.redirect(mustAuthenticate); |
| 37 | + } |
| 38 | + |
| 39 | + const { body } = req; |
| 40 | + const args = { |
| 41 | + accessToken: req.user.accessToken, |
| 42 | + basePath: dsConfig.adminAPIUrl, |
| 43 | + organizationId: req.session.organizationId, |
| 44 | + email: validator.escape(body.email), |
| 45 | + firstName: validator.escape(body.firstName), |
| 46 | + lastName: validator.escape(body.lastName), |
| 47 | + subscriptionId: validator.escape(req.session.subscriptionId), |
| 48 | + planId: validator.escape(req.session.planId), |
| 49 | + }; |
| 50 | + |
| 51 | + let results = null; |
| 52 | + |
| 53 | + try { |
| 54 | + results = await createAccount(args); |
| 55 | + } catch (error) { |
| 56 | + // we can pull the DocuSign error code and message from the response body |
| 57 | + const errorBody = error?.response?.body || error?.body; |
| 58 | + const errorCode = errorBody?.errorCode || errorBody?.error; |
| 59 | + const errorMessage = errorBody?.message || errorBody?.error_description; |
| 60 | + |
| 61 | + // In production, may want to provide customized error messages and |
| 62 | + // remediation advice to the user. |
| 63 | + res.render('pages/error', { err: error, errorCode, errorMessage }); |
| 64 | + } |
| 65 | + if (results) { |
| 66 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 67 | + res.render('pages/example_done', { |
| 68 | + title: example.ExampleName, |
| 69 | + message: example.ResultsPageText, |
| 70 | + json: JSON.stringify(results) |
| 71 | + }); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * Form page for this application |
| 77 | + */ |
| 78 | +eg013CreateAccount.getController = async (req, res) => { |
| 79 | + // Check that the authentication token is ok with a long buffer time. |
| 80 | + // If needed, now is the best time to ask the user to authenticate |
| 81 | + // since they have not yet entered any information into the form. |
| 82 | + |
| 83 | + const isTokenOK = req.dsAuth.checkToken(); |
| 84 | + if (!isTokenOK) { |
| 85 | + // Save the current operation so it will be resumed after authentication |
| 86 | + req.dsAuth.setEg(req, eg); |
| 87 | + return res.redirect(mustAuthenticate); |
| 88 | + } |
| 89 | + |
| 90 | + try { |
| 91 | + await getOrganizationId(req); |
| 92 | + const args = { |
| 93 | + accessToken: req.user.accessToken, |
| 94 | + basePath: dsConfig.adminAPIUrl, |
| 95 | + organizationId: req.session.organizationId |
| 96 | + }; |
| 97 | + |
| 98 | + const planItem = await getOrganizationPlanItem(args); |
| 99 | + req.session.subscriptionId = planItem.subscription_id; |
| 100 | + req.session.planId = planItem.plan_id; |
| 101 | + |
| 102 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 103 | + const sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6); |
| 104 | + res.render('pages/admin-examples/eg013CreateAccount', { |
| 105 | + eg: eg, |
| 106 | + csrfToken: req.csrfToken(), |
| 107 | + example: example, |
| 108 | + sourceFile: sourceFile, |
| 109 | + sourceUrl: dsConfig.githubExampleUrl + 'admin/examples/' + sourceFile, |
| 110 | + documentation: dsConfig.documentation + eg, |
| 111 | + showDoc: dsConfig.documentation, |
| 112 | + }); |
| 113 | + } catch (error) { |
| 114 | + const errorCode = error?.response?.body?.errorCode; |
| 115 | + const errorMessage = error?.response?.body?.message; |
| 116 | + |
| 117 | + // In production, may want to provide customized error messages and |
| 118 | + // remediation advice to the user. |
| 119 | + res.render('pages/error', { err: error, errorCode, errorMessage }); |
| 120 | + } |
| 121 | +}; |
0 commit comments