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