-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (80 loc) · 2.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
require('dotenv').config();
const {
BASE_URL,
ACCOUNT_ID,
ACCESS_TOKEN,
TEMPLATE_ID
} = process.env;
const request = require('request-promise-native').defaults({
baseUrl: `${BASE_URL}/accounts/${ACCOUNT_ID}`,
headers: {
Authorization: `Bearer ${ACCESS_TOKEN}`,
},
json: true,
});
(async () => {
if (!BASE_URL || !ACCOUNT_ID || !ACCESS_TOKEN || !TEMPLATE_ID) {
console.error('Need all env vars.');
return;
}
async function createEnvelopeFromTemplate() {
const { envelopeId } = await request({
method: 'POST',
uri: '/envelopes',
headers: {
},
body: {
status: 'created',
emailSubject: 'Please sign',
templateId: TEMPLATE_ID,
},
});
return envelopeId;
}
async function listEnvelopeDocuments(envelopeId) {
return request({
method: 'GET',
uri: `/envelopes/${envelopeId}/documents`
});
}
// https://developers.docusign.com/esign-rest-api/reference/Envelopes/EnvelopeDocumentTabs/get
async function getDocumentTabs(envelopeId, documentId) {
return request({
method: 'GET',
uri: `/envelopes/${envelopeId}/documents/${documentId}/tabs`,
});
}
// https://developers.docusign.com/esign-rest-api/reference/Envelopes/EnvelopeDocumentTabs/update
async function updateDocumentTabs(envelopeId, documentId, tabs) {
return request({
method: 'PUT',
uri: `/envelopes/${envelopeId}/documents/${documentId}/tabs`,
body: tabs,
});
}
async function updateRecipientTabs(envelopeId, recipientId, tabs) {
return request({
method: 'PUT',
uri: `/envelopes/${envelopeId}/recipients/${recipientId}/tabs`,
body: tabs
});
}
const envelopeId = await createEnvelopeFromTemplate();
const { envelopeDocuments } = await listEnvelopeDocuments(envelopeId);
const { documentId } = envelopeDocuments[0];
const tabs = await getDocumentTabs(envelopeId, documentId);
const date = new Date().getTime().toString();
tabs['textTabs'][0].value = date;
// Updating all the tabs at once doesn't work
try {
await updateDocumentTabs(envelopeId, documentId, tabs);
console.log('Success!');
} catch (e) {
console.log(e.message);
}
// Updating the tabs for a single recipient does work
await updateRecipientTabs(envelopeId, tabs['textTabs'][0]['recipientId'], tabs);
const newTabs = await getDocumentTabs(envelopeId, documentId);
console.log(newTabs['textTabs'][0]['value'] === date);
})();