|
1 | | -/** |
2 | | - * @file |
3 | | - * Example 045: Delete and Undelete an Envelope |
4 | | - * @author DocuSign |
5 | | - */ |
6 | | - |
7 | | -const path = require('path'); |
8 | | -const { deleteEnvelope, moveEnvelopeToFolder, getFolders } = require('../examples/deleteRestoreEnvelope'); |
9 | | -const { getExampleByNumber } = require('../../manifestService'); |
10 | | -const dsConfig = require('../../../config/index.js').config; |
11 | | -const { API_TYPES, formatString } = require('../../utils.js'); |
12 | | -const { getFolderIdByName } = require('../getData.js'); |
13 | | - |
14 | | -const eg045DeleteRestoreEnvelope = exports; |
15 | | -const exampleNumber = 45; |
16 | | -const eg = `eg0${exampleNumber}`; // This example reference. |
17 | | -const api = API_TYPES.ESIGNATURE; |
18 | | -const mustAuthenticate = '/ds/mustAuthenticate'; |
19 | | -const minimumBufferMin = 3; |
20 | | -const restoreEndpoint = `${eg}restore`; |
21 | | -const deleteFolderId = 'recyclebin'; |
22 | | - |
23 | | -/** |
24 | | - * Delete the envelope |
25 | | - * @param {object} req Request obj |
26 | | - * @param {object} res Response obj |
27 | | - */ |
28 | | -eg045DeleteRestoreEnvelope.deleteController = 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 args = { |
42 | | - accessToken: req.user.accessToken, |
43 | | - basePath: req.session.basePath, |
44 | | - accountId: req.session.accountId, |
45 | | - envelopeId: req.body.envelopeId, |
46 | | - deleteFolderId: deleteFolderId, |
47 | | - }; |
48 | | - let results = null; |
49 | | - |
50 | | - try { |
51 | | - results = await deleteEnvelope(args); |
52 | | - } catch (error) { |
53 | | - const errorBody = error?.body || error?.response?.body; |
54 | | - // we can pull the DocuSign error code and message from the response body |
55 | | - const errorCode = errorBody?.errorCode; |
56 | | - const errorMessage = errorBody?.message; |
57 | | - // In production, may want to provide customized error messages and |
58 | | - // remediation advice to the user. |
59 | | - res.render('pages/error', {err: error, errorCode, errorMessage}); |
60 | | - } |
61 | | - if (results) { |
62 | | - req.session.envelopeId = req.body.envelopeId; |
63 | | - |
64 | | - const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
65 | | - const additionalPageData = example.AdditionalPage.find(p => p.Name === 'envelope_is_deleted'); |
66 | | - res.render('pages/example_done', { |
67 | | - title: example.ExampleName, |
68 | | - message: formatString(additionalPageData.ResultsPageText, req.body.envelopeId), |
69 | | - redirectUrl: restoreEndpoint, |
70 | | - }); |
71 | | - } |
72 | | -}; |
73 | | - |
74 | | -/** |
75 | | - * Undelete the envelope |
76 | | - * @param {object} req Request obj |
77 | | - * @param {object} res Response obj |
78 | | - */ |
79 | | -eg045DeleteRestoreEnvelope.restoreController = async (req, res) => { |
80 | | - // Step 1. Check the token |
81 | | - // At this point we should have a good token. But we |
82 | | - // double-check here to enable a better UX to the user. |
83 | | - const isTokenOK = req.dsAuth.checkToken(minimumBufferMin); |
84 | | - if (!isTokenOK) { |
85 | | - req.flash('info', 'Sorry, you need to re-authenticate.'); |
86 | | - // Save the current operation so it will be resumed after authentication |
87 | | - req.dsAuth.setEg(req, eg); |
88 | | - return res.redirect(mustAuthenticate); |
89 | | - } |
90 | | - |
91 | | - const args = { |
92 | | - accessToken: req.user.accessToken, |
93 | | - basePath: req.session.basePath, |
94 | | - accountId: req.session.accountId, |
95 | | - envelopeId: req.session.envelopeId, |
96 | | - fromFolderId: deleteFolderId, |
97 | | - }; |
98 | | - const folderName = req.body.folderName; |
99 | | - let folderId = ''; |
100 | | - |
101 | | - const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
102 | | - |
103 | | - // Step 2. Call the worker method |
104 | | - let results = null; |
105 | | - try { |
106 | | - const folders = await getFolders(args); |
107 | | - folderId = getFolderIdByName(folders.folders, folderName); |
108 | | - |
109 | | - if (!folderId) { |
110 | | - const additionalPageData = example.AdditionalPage.find(page => page.Name === 'folder_does_not_exist'); |
111 | | - return res.render('pages/example_done', { |
112 | | - title: example.ExampleName, |
113 | | - message: formatString(additionalPageData.ResultsPageText, folderName), |
114 | | - redirectUrl: restoreEndpoint, |
115 | | - }); |
116 | | - |
117 | | - } |
118 | | - |
119 | | - results = await moveEnvelopeToFolder({ ...args, folderId }); |
120 | | - } catch (error) { |
121 | | - const errorBody = error?.body || error?.response?.body; |
122 | | - // we can pull the DocuSign error code and message from the response body |
123 | | - const errorCode = errorBody?.errorCode; |
124 | | - const errorMessage = errorBody?.message; |
125 | | - // In production, may want to provide customized error messages and |
126 | | - // remediation advice to the user. |
127 | | - res.render('pages/error', {err: error, errorCode, errorMessage}); |
128 | | - } |
129 | | - if (results) { |
130 | | - res.render('pages/example_done', { |
131 | | - title: example.ExampleName, |
132 | | - message: formatString(example.ResultsPageText, req.session.envelopeId, folderId, folderName), |
133 | | - }); |
134 | | - } |
135 | | -}; |
136 | | - |
137 | | -/** |
138 | | - * Form page for this application |
139 | | - */ |
140 | | -eg045DeleteRestoreEnvelope.getDeleteController = (req, res) => { |
141 | | - // Check that the authentication token is ok with a long buffer time. |
142 | | - // If needed, now is the best time to ask the user to authenticate |
143 | | - // since they have not yet entered any information into the form. |
144 | | - const isTokenOK = req.dsAuth.checkToken(); |
145 | | - if (!isTokenOK) { |
146 | | - // Save the current operation so it will be resumed after authentication |
147 | | - req.dsAuth.setEg(req, eg); |
148 | | - return res.redirect(mustAuthenticate); |
149 | | - } |
150 | | - |
151 | | - const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
152 | | - const sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6); |
153 | | - res.render('pages/examples/eg045DeleteEnvelope', { |
154 | | - eg: eg, csrfToken: req.csrfToken(), |
155 | | - example: example, |
156 | | - envelopeId: req.session.envelopeId, |
157 | | - submitButtonText: res.locals.manifest.SupportingTexts.HelpingTexts.SubmitButtonDeleteText, |
158 | | - sourceFile: sourceFile, |
159 | | - sourceUrl: dsConfig.githubExampleUrl + 'eSignature/examples/' + sourceFile, |
160 | | - documentation: dsConfig.documentation + eg, |
161 | | - showDoc: dsConfig.documentation |
162 | | - }); |
163 | | -}; |
164 | | - |
165 | | - |
166 | | -/** |
167 | | - * Form page for this application |
168 | | - */ |
169 | | -eg045DeleteRestoreEnvelope.getRestoreController = (req, res) => { |
170 | | - // Check that the authentication token is ok with a long buffer time. |
171 | | - // If needed, now is the best time to ask the user to authenticate |
172 | | - // since they have not yet entered any information into the form. |
173 | | - const isTokenOK = req.dsAuth.checkToken(); |
174 | | - if (!isTokenOK) { |
175 | | - // Save the current operation so it will be resumed after authentication |
176 | | - req.dsAuth.setEg(req, eg); |
177 | | - return res.redirect(mustAuthenticate); |
178 | | - } |
179 | | - |
180 | | - if (!req.session.envelopeId) { |
181 | | - return res.redirect(eg); |
182 | | - } |
183 | | - |
184 | | - const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
185 | | - const sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6); |
186 | | - res.render('pages/examples/eg045RestoreEnvelope', { |
187 | | - eg: eg, csrfToken: req.csrfToken(), |
188 | | - example: example, |
189 | | - envelopeId: req.session.envelopeId, |
190 | | - submitButtonText: res.locals.manifest.SupportingTexts.HelpingTexts.SubmitButtonRestoreText, |
191 | | - sourceFile: sourceFile, |
192 | | - sourceUrl: dsConfig.githubExampleUrl + 'eSignature/examples/' + sourceFile, |
193 | | - documentation: dsConfig.documentation + eg, |
194 | | - showDoc: dsConfig.documentation |
195 | | - }); |
196 | | -}; |
| 1 | +/** |
| 2 | + * @file |
| 3 | + * Example 045: Delete and Undelete an Envelope |
| 4 | + * @author DocuSign |
| 5 | + */ |
| 6 | + |
| 7 | +const path = require('path'); |
| 8 | +const { deleteEnvelope, moveEnvelopeToFolder, getFolders } = require('../examples/deleteRestoreEnvelope'); |
| 9 | +const { getExampleByNumber } = require('../../manifestService'); |
| 10 | +const dsConfig = require('../../../config/index.js').config; |
| 11 | +const { API_TYPES, formatString } = require('../../utils.js'); |
| 12 | +const { getFolderIdByName } = require('../getData.js'); |
| 13 | + |
| 14 | +const eg045DeleteRestoreEnvelope = exports; |
| 15 | +const exampleNumber = 45; |
| 16 | +const eg = `eg0${exampleNumber}`; // This example reference. |
| 17 | +const api = API_TYPES.ESIGNATURE; |
| 18 | +const mustAuthenticate = '/ds/mustAuthenticate'; |
| 19 | +const minimumBufferMin = 3; |
| 20 | +const restoreEndpoint = `${eg}restore`; |
| 21 | +const deleteFolderId = 'recyclebin'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Delete the envelope |
| 25 | + * @param {object} req Request obj |
| 26 | + * @param {object} res Response obj |
| 27 | + */ |
| 28 | +eg045DeleteRestoreEnvelope.deleteController = 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 args = { |
| 42 | + accessToken: req.user.accessToken, |
| 43 | + basePath: req.session.basePath, |
| 44 | + accountId: req.session.accountId, |
| 45 | + envelopeId: req.body.envelopeId, |
| 46 | + deleteFolderId: deleteFolderId, |
| 47 | + }; |
| 48 | + let results = null; |
| 49 | + |
| 50 | + try { |
| 51 | + results = await deleteEnvelope(args); |
| 52 | + } catch (error) { |
| 53 | + const errorBody = error?.body || error?.response?.body; |
| 54 | + // we can pull the DocuSign error code and message from the response body |
| 55 | + const errorCode = errorBody?.errorCode; |
| 56 | + const errorMessage = errorBody?.message; |
| 57 | + // In production, may want to provide customized error messages and |
| 58 | + // remediation advice to the user. |
| 59 | + res.render('pages/error', {err: error, errorCode, errorMessage}); |
| 60 | + } |
| 61 | + if (results) { |
| 62 | + req.session.envelopeId = req.body.envelopeId; |
| 63 | + |
| 64 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 65 | + const additionalPageData = example.AdditionalPage.find(p => p.Name === 'envelope_is_deleted'); |
| 66 | + res.render('pages/example_done', { |
| 67 | + title: example.ExampleName, |
| 68 | + message: formatString(additionalPageData.ResultsPageText, req.body.envelopeId), |
| 69 | + redirectUrl: restoreEndpoint, |
| 70 | + }); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +/** |
| 75 | + * Undelete the envelope |
| 76 | + * @param {object} req Request obj |
| 77 | + * @param {object} res Response obj |
| 78 | + */ |
| 79 | +eg045DeleteRestoreEnvelope.restoreController = async (req, res) => { |
| 80 | + // Step 1. Check the token |
| 81 | + // At this point we should have a good token. But we |
| 82 | + // double-check here to enable a better UX to the user. |
| 83 | + const isTokenOK = req.dsAuth.checkToken(minimumBufferMin); |
| 84 | + if (!isTokenOK) { |
| 85 | + req.flash('info', 'Sorry, you need to re-authenticate.'); |
| 86 | + // Save the current operation so it will be resumed after authentication |
| 87 | + req.dsAuth.setEg(req, eg); |
| 88 | + return res.redirect(mustAuthenticate); |
| 89 | + } |
| 90 | + |
| 91 | + const args = { |
| 92 | + accessToken: req.user.accessToken, |
| 93 | + basePath: req.session.basePath, |
| 94 | + accountId: req.session.accountId, |
| 95 | + envelopeId: req.session.envelopeId, |
| 96 | + fromFolderId: deleteFolderId, |
| 97 | + }; |
| 98 | + const folderName = req.body.folderName; |
| 99 | + let folderId = ''; |
| 100 | + |
| 101 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 102 | + |
| 103 | + // Step 2. Call the worker method |
| 104 | + let results = null; |
| 105 | + try { |
| 106 | + const folders = await getFolders(args); |
| 107 | + folderId = getFolderIdByName(folders.folders, folderName); |
| 108 | + |
| 109 | + if (!folderId) { |
| 110 | + const additionalPageData = example.AdditionalPage.find(page => page.Name === 'folder_does_not_exist'); |
| 111 | + return res.render('pages/example_done', { |
| 112 | + title: example.ExampleName, |
| 113 | + message: formatString(additionalPageData.ResultsPageText, folderName), |
| 114 | + redirectUrl: restoreEndpoint, |
| 115 | + }); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + results = await moveEnvelopeToFolder({ ...args, folderId }); |
| 120 | + } catch (error) { |
| 121 | + const errorBody = error?.body || error?.response?.body; |
| 122 | + // we can pull the DocuSign error code and message from the response body |
| 123 | + const errorCode = errorBody?.errorCode; |
| 124 | + const errorMessage = errorBody?.message; |
| 125 | + // In production, may want to provide customized error messages and |
| 126 | + // remediation advice to the user. |
| 127 | + res.render('pages/error', {err: error, errorCode, errorMessage}); |
| 128 | + } |
| 129 | + if (results) { |
| 130 | + res.render('pages/example_done', { |
| 131 | + title: example.ExampleName, |
| 132 | + message: formatString(example.ResultsPageText, req.session.envelopeId, folderId, folderName), |
| 133 | + }); |
| 134 | + } |
| 135 | +}; |
| 136 | + |
| 137 | +/** |
| 138 | + * Form page for this application |
| 139 | + */ |
| 140 | +eg045DeleteRestoreEnvelope.getDeleteController = (req, res) => { |
| 141 | + // Check that the authentication token is ok with a long buffer time. |
| 142 | + // If needed, now is the best time to ask the user to authenticate |
| 143 | + // since they have not yet entered any information into the form. |
| 144 | + const isTokenOK = req.dsAuth.checkToken(); |
| 145 | + if (!isTokenOK) { |
| 146 | + // Save the current operation so it will be resumed after authentication |
| 147 | + req.dsAuth.setEg(req, eg); |
| 148 | + return res.redirect(mustAuthenticate); |
| 149 | + } |
| 150 | + |
| 151 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 152 | + const sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6); |
| 153 | + res.render('pages/examples/eg045DeleteEnvelope', { |
| 154 | + eg: eg, csrfToken: req.csrfToken(), |
| 155 | + example: example, |
| 156 | + envelopeId: req.session.envelopeId, |
| 157 | + submitButtonText: res.locals.manifest.SupportingTexts.HelpingTexts.SubmitButtonDeleteText, |
| 158 | + sourceFile: sourceFile, |
| 159 | + sourceUrl: dsConfig.githubExampleUrl + 'eSignature/examples/' + sourceFile, |
| 160 | + documentation: dsConfig.documentation + eg, |
| 161 | + showDoc: dsConfig.documentation |
| 162 | + }); |
| 163 | +}; |
| 164 | + |
| 165 | + |
| 166 | +/** |
| 167 | + * Form page for this application |
| 168 | + */ |
| 169 | +eg045DeleteRestoreEnvelope.getRestoreController = (req, res) => { |
| 170 | + // Check that the authentication token is ok with a long buffer time. |
| 171 | + // If needed, now is the best time to ask the user to authenticate |
| 172 | + // since they have not yet entered any information into the form. |
| 173 | + const isTokenOK = req.dsAuth.checkToken(); |
| 174 | + if (!isTokenOK) { |
| 175 | + // Save the current operation so it will be resumed after authentication |
| 176 | + req.dsAuth.setEg(req, eg); |
| 177 | + return res.redirect(mustAuthenticate); |
| 178 | + } |
| 179 | + |
| 180 | + if (!req.session.envelopeId) { |
| 181 | + return res.redirect(eg); |
| 182 | + } |
| 183 | + |
| 184 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 185 | + const sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6); |
| 186 | + res.render('pages/examples/eg045RestoreEnvelope', { |
| 187 | + eg: eg, csrfToken: req.csrfToken(), |
| 188 | + example: example, |
| 189 | + envelopeId: req.session.envelopeId, |
| 190 | + submitButtonText: res.locals.manifest.SupportingTexts.HelpingTexts.SubmitButtonRestoreText, |
| 191 | + sourceFile: sourceFile, |
| 192 | + sourceUrl: dsConfig.githubExampleUrl + 'eSignature/examples/' + sourceFile, |
| 193 | + documentation: dsConfig.documentation + eg, |
| 194 | + showDoc: dsConfig.documentation |
| 195 | + }); |
| 196 | +}; |
0 commit comments