-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_server.js
185 lines (170 loc) · 6.23 KB
/
node_server.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const express = require('express')
const _docusign = require('./docusign_module.js')
const docusignDocs = require('./getSignedDocuments.js')
const app = express()
const port = 3000
var config = require('./config/configuration');
var rp = require('request-promise');
var PDFDocument = require('pdfkit');
var fs = require('fs');
var envelopeData = {}, _tempDocId, urls = { bos: null, fs: null};
app.set("view engine", "ejs");
app.get('/sign', (request, response) => {
var f = request.query.f;
var role = (request.query.r == "buyer") ? 0 : 1;
if(role)
var signerData, fileData;
if(f == "bos") {
_tempDocId = 1;
fileData = {
id: "1",
path : "Bill of Sale.pdf",
extension: 'pdf'
};
signerData = [{
id : "1",
clientUserId : '1001',
recipientId: "1",
routingOrder: "1",
name: "Buyer",
email: "shravz4@gmail.com",
tabs: {
signHereTabs: [{
pageNumber: "1",
xPosition: "200",
yPosition: "580",
documentId: "1"
}],
dateSignedTabs: [{
pageNumber: "1",
xPosition: "500",
yPosition: "620",
documentId: "1"
}]
}
},
{
id : "2",
clientUserId : '1002',
recipientId: "2",
routingOrder: "1",
name: "Seller",
email: "shravya.siluveru8@gmail.com",
tabs: {
signHereTabs: [{
pageNumber: "1",
xPosition: "200",
yPosition: "500",
documentId: "1"
}],
dateSignedTabs: [{
pageNumber: "1",
xPosition: "500",
yPosition: "550",
documentId: "1"
}]
}
}]
}
if(role == 0){
if(envelopeData[_tempDocId] != null) {
return response.end("Seller's turn");
}
var d =_docusign.get_embedded_url(fileData, signerData, 0);
d.then((data) => {
envelopeData[_tempDocId] = data;
return generateEmbeddedLink(data.accountId, data.envelopes, data.envelopeDefnition,
data.docusign_credentials, role);
})
.then((embeddedLink) => {
response.redirect(embeddedLink.url);
}).catch((err) =>
console.log(err)
);
}
else if(role == 1){
if(envelopeData[_tempDocId] == null) {
return response.end("Buyer's turn");
}
generateEmbeddedLink(envelopeData[_tempDocId].accountId, envelopeData[_tempDocId].envelopes,
envelopeData[_tempDocId].envelopeDefnition,
envelopeData[_tempDocId].docusign_credentials, role)
.then((embeddedLink) => {
urls.bos = embeddedLink.url;
response.redirect(embeddedLink.url);
}).catch((err) =>
console.log(err)
);
}
});
app.get('/', (request, response) => {
response.render("index");
});
app.get('/bos', (request, response) => {
if(envelopeData['1'] == null)
return response.send("Bill of Sale is not yet generated");
docusignDocs.get_signed_document(envelopeData['1'].accountId,envelopeData['1'].envelopes.envelopeId,
envelopeData['1'].docusign_credentials)
.then((file)=> {
response.contentType("application/pdf");
response.send(file);
});
});
app.get('/buyer', (request, response) => {
response.render("buyer");
});
app.get('/seller', (request, response) => {
response.render("seller");
});
app.get('/success',(request, response) => {
var eventResponse = request.query.event;
if(eventResponse == 'signing_complete'){
// if(flag == 1){
// return response.end("Both users signed");
// }
// flag = 1;
// generateEmbeddedLink(envelopeData[_tempDocId].accountId, envelopeData[_tempDocId].envelopes, envelopeData[_tempDocId].envelopeDefnition, envelopeData[_tempDocId].docusign_credentials, 1)
// .then((embeddedLink) => {
// response.redirect(embeddedLink.url);
// }).catch((err) =>
// console.log(err)
// );
response.end("Signing complete");
}
else if(eventResponse == 'cancel')
response.end("User decides to sign the document later");
else if(eventResponse == 'exception')
response.end("Error in processing the document");
else if(eventResponse == 'fax_pending')
response.end("User decides to fax the document later");
else if(eventResponse == 'session_timeout')
response.end("Session expired");
else if(eventResponse == 'ttl_expired')
response.end("Token used or expired.");
else if(eventResponse == 'decline')
response.end("User decides to decline signing the document");
});
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
});
var generateEmbeddedLink = function(accountId, envelopes, envelopeDefnition, docusign_credentials, i){
//console.log (`Envelope was created! Envelope id: ${envelopes.envelopeId}`);
var signer = envelopeDefnition.recipients.signers[i];
var body = {
"clientUserId": signer.clientUserId,
"recipientId": signer.recipientId,
"userName": signer.name,
"email": signer.email,
"returnUrl": config.oauth_redirect_URI,
"authenticationMethod": config.authentication_method
};
var envelopeId = envelopes.envelopeId;
return rp.post(`https://demo.docusign.net/restapi/v2/accounts/${accountId}/envelopes/${envelopeId}/views/recipient`, {
json: true,
headers: {Authorization: 'Bearer '+docusign_credentials.token},
body: body
});
};