Skip to content

Commit 2157cb1

Browse files
authored
Merge pull request #227 from docusign/feature/workspaces-example-5
Create Upload Request example
2 parents 698e119 + 7e0c3d4 commit 2157cb1

File tree

6 files changed

+190
-2
lines changed

6 files changed

+190
-2
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ const { eg004notary } = require('./lib/notary/controllers');
6262
const { eg001fields } = require('./lib/connectedFields/controllers');
6363
const { eg001Navigator, eg002Navigator } = require('./lib/navigator/controllers');
6464
const { eg001maestro, eg002maestro, eg003maestro, eg004maestro } = require('./lib/maestro/controllers');
65-
const { eg001workspaces, eg002workspaces, eg003workspaces } = require('./lib/workspaces/controllers');
65+
const {
66+
eg001workspaces, eg002workspaces, eg003workspaces,
67+
eg005workspaces,
68+
} = require('./lib/workspaces/controllers');
6669

6770
const PORT = process.env.PORT || 3000;
6871
const HOST = process.env.HOST || 'localhost';
@@ -325,7 +328,9 @@ app.get('/work001', eg001workspaces.getController)
325328
.get('/work002', eg002workspaces.getController)
326329
.post('/work002', eg002workspaces.createController)
327330
.get('/work003', eg003workspaces.getController)
328-
.post('/work003', eg003workspaces.createController);
331+
.post('/work003', eg003workspaces.createController)
332+
.get('/work005', eg005workspaces.getController)
333+
.post('/work005', eg005workspaces.createController);
329334

330335
function dsLoginCB1(req, res, next) { req.dsAuthCodeGrant.oauth_callback1(req, res, next); }
331336
function dsLoginCB2(req, res, next) { req.dsAuthCodeGrant.oauth_callback2(req, res, next); }

lib/workspaces/controllers/eg001CreateWorkspace.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ eg001CreateWorkspace.createController = async (req, res) => {
5757
}
5858
if (results) {
5959
req.session.workspaceId = results.workspaceId;
60+
req.session.workspaceCreatorId = results.createdByUserId;
6061

6162
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
6263
res.render('pages/example_done', {
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @file
3+
* Example 005: Create a workspace upload request
4+
* @author DocuSign
5+
*/
6+
7+
const path = require('path');
8+
const { createUploadRequest } = require('../../workspaces/examples/createUploadRequest');
9+
const validator = require('validator');
10+
const { getExampleByNumber } = require('../../manifestService');
11+
const dsConfig = require('../../../config/index').config;
12+
const { API_TYPES } = require('../../utils');
13+
14+
const eg005CreateUploadRequest = exports;
15+
const exampleNumber = 5;
16+
const eg = `work00${exampleNumber}`; // This example reference.
17+
const api = API_TYPES.WORKSPACES;
18+
const mustAuthenticate = '/ds/mustAuthenticate';
19+
const minimumBufferMin = 3;
20+
/**
21+
* Create a workspace upload request
22+
* @param {object} req Request obj
23+
* @param {object} res Response obj
24+
*/
25+
eg005CreateUploadRequest.createController = async (req, res) => {
26+
// Step 1. Check the token
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+
// Step 2. Call the worker method
38+
const { body } = req;
39+
const args = {
40+
accessToken: req.user.accessToken,
41+
basePath: req.session.basePath,
42+
accountId: req.session.accountId,
43+
workspaceId: req.session.workspaceId,
44+
workspaceCreatorId: req.session.workspaceCreatorId,
45+
assigneeEmail: validator.escape(body.assigneeEmail),
46+
};
47+
let results = null;
48+
49+
try {
50+
results = await createUploadRequest(args);
51+
} catch (error) {
52+
const errorBody = error?.body || error?.response?.body;
53+
// we can pull the DocuSign error code and message from the response body
54+
const errorCode = errorBody?.errorCode;
55+
const errorMessage = errorBody?.message;
56+
// In production, may want to provide customized error messages and
57+
// remediation advice to the user.
58+
res.render('pages/error', { err: error, errorCode, errorMessage });
59+
}
60+
if (results) {
61+
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
62+
res.render('pages/example_done', {
63+
title: example.ExampleName,
64+
message: formatString(example.ResultsPageText, results.uploadRequestId),
65+
});
66+
}
67+
};
68+
69+
/**
70+
* Form page for this application
71+
*/
72+
eg005CreateUploadRequest.getController = async (req, res) => {
73+
// Check that the authentication token is ok with a long buffer time.
74+
// If needed, now is the best time to ask the user to authenticate
75+
// since they have not yet entered any information into the form.
76+
const isTokenOK = req.dsAuth.checkToken();
77+
if (!isTokenOK) {
78+
// Save the current operation so it will be resumed after authentication
79+
req.dsAuth.setEg(req, eg);
80+
return res.redirect(mustAuthenticate);
81+
}
82+
83+
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
84+
const sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6);
85+
res.render('pages/workspaces-examples/eg005CreateUploadRequest', {
86+
eg: eg,
87+
csrfToken: req.csrfToken(),
88+
workspaceIdOk: !!req.session.workspaceId,
89+
workspaceCreatorIdOk: !!req.session.workspaceCreatorId,
90+
example: example,
91+
sourceFile: sourceFile,
92+
sourceUrl: dsConfig.githubExampleUrl + 'workspaces/examples/' + sourceFile,
93+
documentation: dsConfig.documentation + eg,
94+
showDoc: dsConfig.documentation
95+
});
96+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports.eg001workspaces = require('./eg001CreateWorkspace');
22
module.exports.eg002workspaces = require('./eg002AddDocumentToWorkspace');
33
module.exports.eg003workspaces = require('./eg003SendEnvelopeWithRecipientInfo');
4+
module.exports.eg005workspaces = require('./eg005CreateUploadRequest');
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @file
3+
* Example 005: Create a workspace upload request
4+
* @author DocuSign
5+
*/
6+
7+
const iam = require('@docusign/iam-sdk');
8+
const moment = require('moment');
9+
10+
const createUploadRequest = async (args) => {
11+
//ds-snippet-start:Workspaces5Step2
12+
const client = new iam.IamClient({ accessToken: args.accessToken });
13+
//ds-snippet-end:Workspaces5Step2
14+
15+
const dueDate = moment().add(7, 'days').toDate();
16+
17+
//ds-snippet-start:Workspaces5Step3
18+
// create assignments
19+
const assigneeAssignment = {
20+
uploadRequestResponsibilityTypeId: 'assignee',
21+
firstName: 'Test',
22+
lastName: 'User',
23+
email: args.assigneeEmail,
24+
};
25+
const watcherAssignments = {
26+
assigneeUserId: args.workspaceCreatorId,
27+
uploadRequestResponsibilityTypeId: 'watcher'
28+
};
29+
30+
// create upload request
31+
const createWorkspaceUploadRequestBody = {
32+
name: `Upload Request example ${dueDate}`,
33+
description: 'This is an example upload request created via the workspaces API',
34+
dueDate: dueDate,
35+
status: 'draft',
36+
assignments: [assigneeAssignment, watcherAssignments],
37+
};
38+
//ds-snippet-end:Workspaces5Step3
39+
40+
//ds-snippet-start:Workspaces5Step4
41+
return await client.workspaces1.workspaceUploadRequest.createWorkspaceUploadRequest(
42+
{
43+
accountId: args.accountId,
44+
workspaceId: args.workspaceId,
45+
createWorkspaceUploadRequestBody,
46+
}
47+
);
48+
//ds-snippet-end:Workspaces5Step4
49+
};
50+
51+
module.exports = { createUploadRequest };
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<%- include("../../partials/examplesHead") %>
2+
3+
<%- include("../../partials/exampleInfo") %>
4+
5+
<% if (!workspaceIdOk) { %>
6+
<%- formatString(example.RedirectsToOtherCodeExamples[0].RedirectText, formatString('href="work00{0}"', example.RedirectsToOtherCodeExamples[0].CodeExampleToRedirectTo)) %>
7+
<form class="eg" action="work001" method="get">
8+
<%- include("../../partials/continueButton") %>
9+
</form>
10+
<% } else if (!workspaceCreatorIdOk) { %>
11+
<%- formatString(example.RedirectsToOtherCodeExamples[1].RedirectText, formatString('href="work00{0}"', example.RedirectsToOtherCodeExamples[1].CodeExampleToRedirectTo)) %>
12+
<form class="eg" action="work001" method="get">
13+
<%- include("../../partials/continueButton") %>
14+
</form>
15+
<% } else { %>
16+
<form class="eg" action="" method="post" data-busy="form">
17+
<% if(example.Forms && example.Forms[0].FormName) { %>
18+
<%- example.Forms[0].FormName %>
19+
<% } %>
20+
21+
<div class="form-group">
22+
<label for="assigneeEmail"><%= example.Forms[0].Inputs[0].InputName %></label>
23+
<input type="email" class="form-control" id="assigneeEmail" name="assigneeEmail"
24+
aria-describedby="emailHelp" placeholder="<%= example.Forms[0].Inputs[0].InputPlaceholder %>" required
25+
value="<%= locals.dsConfig.signerEmail %>">
26+
<small id="emailHelp" class="form-text text-muted"><%= locals.manifest.SupportingTexts.HelpingTexts.EmailWontBeShared %></small>
27+
</div>
28+
29+
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
30+
<%- include("../../partials/submitButton") %>
31+
</form>
32+
<% } %>
33+
34+
<%- include("../../partials/examplesFoot") %>

0 commit comments

Comments
 (0)