-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnot-working.js
141 lines (119 loc) · 3.38 KB
/
not-working.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
const fs = require('fs');
require('dotenv').config();
const {
BASE_URL,
ACCOUNT_ID,
ACCESS_TOKEN,
} = process.env;
const instance = require('axios').create({
baseURL: `${BASE_URL}/accounts/${ACCOUNT_ID}`,
headers: {
Authorization: `Bearer ${ACCESS_TOKEN}`,
},
});
(async () => {
if (!BASE_URL || !ACCOUNT_ID || !ACCESS_TOKEN) {
console.error('Missing env vars!');
return;
}
const clientUserId = Date.now();
const name = 'Yulian Ovdey';
const email = '[email protected]';
async function createEnvelope() {
const result = await instance.post('/envelopes', {
status: 'sent',
emailSubject: 'Please sign',
documents: [
{
documentId: 1,
name: 'Test Document',
documentBase64: fs.readFileSync('Empty Test PDF.pdf').toString('base64'),
fileExtension: 'pdf',
},
],
recipients: {
signers: [
{
name,
recipientId: 1,
clientUserId,
email,
tabs: {
signHereTabs: [
{
documentId: 1,
pageNumber: 1,
anchorXOffset: 0,
anchorYOffset: 0,
name: 'Please sign here',
},
],
},
},
],
},
});
return result.data.envelopeId;
}
async function getEnvelope(envelopeId) {
const envelope = await instance.get(`/envelopes/${envelopeId}?include=recipients`);
return envelope.data;
}
async function createRecipientView(envelopeId) {
const envelope = await getEnvelope(envelopeId);
const signer = envelope.recipients.signers[0];
const result = await instance.post(`/envelopes/${envelopeId}/views/recipient`, {
clientUserId: signer.clientUserId,
userId: signer.userId,
authenticationMethod: 'None',
returnUrl: 'http://localhost:3000',
});
console.log(result.data);
}
const envelopeId = await createEnvelope();
await createRecipientView(envelopeId);
// Wait 30 seconds
//
// During this timeout - visit the recipient URL from the call above and
// update the recipient's name in the "Adopt Your Signature" modal
await new Promise((resolve) => {
setTimeout(resolve, 30000);
});
// Try to create another recipient view - this will fail with "UNKNOWN_ENVELOPE_RECIPIENT"
try {
await createRecipientView(envelopeId);
console.log('Success!');
return;
} catch (e) {
console.log(`Creating another recipient view failed:`, e.response.data.message);
}
// Try to delete the captive recipient using the original recipient name
const result = await instance.delete(`/captive_recipients/signature`, {
data: {
captiveRecipients: [
{
clientUserId,
email,
userName: name,
},
],
},
});
// {
// captiveRecipients: [
// {
// email: '[email protected]',
// userName: 'Yulian Ovdey',
// clientUserId: '1717434736976'
// }
// ]
// }
console.log(result.data);
// Try to create another recipient view - this will fail with "UNKNOWN_ENVELOPE_RECIPIENT"
try {
await createRecipientView(envelopeId);
} catch (e) {
console.log(`Creating another recipient view failed:`, e.response.data.message);
}
})();