1+ /**
2+ * @file
3+ * Example 012: How to clone 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 { cloneAccount, getAccounts } = require ( '../examples/cloneAccount' ) ;
11+ const { getOrganizationId } = require ( "../getOrganizationId.js" ) ;
12+ const { getExampleByNumber } = require ( "../../manifestService" ) ;
13+ const { API_TYPES } = require ( '../../utils.js' ) ;
14+
15+ const eg012CloneAccount = exports ;
16+ const exampleNumber = 12 ;
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+ eg012CloneAccount . 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+ sourceAccountId : validator . escape ( body . sourceAccountId ) ,
45+ targetAccountName : validator . escape ( body . targetAccountName ) ,
46+ targetAccountFirstName : validator . escape ( body . targetAccountFirstName ) ,
47+ targetAccountLastName : validator . escape ( body . targetAccountLastName ) ,
48+ targetAccountEmail : validator . escape ( body . targetAccountEmail ) ,
49+ } ;
50+
51+ let results = null ;
52+
53+ try {
54+ results = await cloneAccount ( args ) ;
55+ } catch ( error ) {
56+ // we can pull the DocuSign error code and message from the response body
57+ const errorBody = error ?. response ?. body ;
58+ const errorCode = errorBody ?. errorCode ;
59+ const errorMessage = errorBody ?. message ;
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+ eg012CloneAccount . 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 accounts = await getAccounts ( args ) ;
99+
100+ const example = getExampleByNumber ( res . locals . manifest , exampleNumber , api ) ;
101+ const sourceFile = ( path . basename ( __filename ) ) [ 5 ] . toLowerCase ( ) + ( path . basename ( __filename ) ) . substr ( 6 ) ;
102+ res . render ( 'pages/admin-examples/eg012CloneAccount' , {
103+ eg : eg ,
104+ csrfToken : req . csrfToken ( ) ,
105+ example : example ,
106+ sourceFile : sourceFile ,
107+ sourceUrl : dsConfig . githubExampleUrl + "admin/examples/" + sourceFile ,
108+ documentation : dsConfig . documentation + eg ,
109+ showDoc : dsConfig . documentation ,
110+ accounts : accounts . assetGroupAccounts ,
111+ } ) ;
112+ } catch ( error ) {
113+ const errorCode = error ?. response ?. body ?. errorCode ;
114+ const errorMessage = error ?. response ?. body ?. message ;
115+
116+ // In production, may want to provide customized error messages and
117+ // remediation advice to the user.
118+ res . render ( 'pages/error' , { err : error , errorCode, errorMessage } ) ;
119+ }
120+ }
121+
0 commit comments