service docusign {
entity user {
key userID : String;
sent: Boolean;
}
}
const express = require('express')
const bodyParser = require('body-parser')
const oauthClient = require('client-oauth2')
const request = require('request-promise')
const app = express()
const port = process.env.PORT || 3000
app.use(bodyParser.json())
app.post('/sendmail', (req, res) => {
const email = req.body.email;
const userid = req.body.userid;
const fname = req.body.fname;
const lname = req.body.lname;
const full_name = fname + " " + lname;
const contractCheck = function(email, userid, full_name, accessToken) {
const userid1 = "'" + userid + "'";
const serviceUrl = "https://backend-service-api.cfapps.eu10.hana.ondemand.com/odatav2/DEFAULT/DOCUSIGN;v=1/user(" + userid1 + ")";
return new Promise(function(resolve, reject) {
const options = {
url: serviceUrl,
resolveWithFullResponse: true,
headers: {
Authorization: 'Bearer ' + accessToken,
Accept: 'application/json'
}
};
request(options)
.then(response => {
if (response && response.statusCode == 200) {
resolve(res.send('contract has been already sent'));
}
})
.catch(error => {
resolve(sendContract(email, full_name, userid, accessToken));
});
});
}
const gettingToken = function() {
return new Promise((resolve, reject) => {
const oautClient = new oauthClient({
accessTokenUri: 'https://<your_sub_account>.authentication.eu10.hana.ondemand.com/oauth/token',
clientId: '<xsuaa client id>',
clientSecret: '<xsuaa client secret>',
scopes: []
});
oautClient.owner.getToken('<your scp email>', '<your scp password>')
.then(result => {
resolve({
accessToken: result.accessToken
});
})
.catch(error => {
reject({
message: 'Error: failed to get access token.',
error: error
});
});
});
}
const sendContract = function(email, full_name, userid, accessToken) {
var url = 'https://api.openconnectors.ext.hanatrial.ondemand.com/elements/api-v2/envelopes';
var headers = {
'authorization': 'User IXnG2ZC2lpg34sd6e2sNWCXTO098272XMAqldM=, Organization 927730555109ef5261b78359b8761, Element gg8vdijY0R0O7y0sBx09yK49mDCbhFzeMWiW/XJM50=',
'accept': 'application/json',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
};
bodyJson = {
"compositeTemplates": [{
"compositeTemplateId": "1",
"inlineTemplates": [{
"recipients": {
"carbonCopies": [{
"email": "<email id of where you want copy>",
"name": "Sudip Ghosh",
"recipientId": "2"
}],
"signers": [{
"email": email,
"name": full_name,
"recipientId": "1",
"roleName": "Employee",
"tabs": {
"textTabs": []
}
}]
},
"sequence": "1"
}],
"serverTemplates": [{
"sequence": "2",
"templateId": "c7d72cca-3391-45d3-aee0-40d6558b037a"
}]
}],
"emailSubject": "Employment Training Bond",
"enableWetSign": "false",
"status": "sent"
}
var options = {
method: 'POST',
url: url,
headers: headers,
formData: {
envelope: JSON.stringify(bodyJson)
}
};
request(options, function(error, response, body) {
if (!error && response.statusCode == 200) {
const backendAPIurl = 'https://backend-service-api.cfapps.eu10.hana.ondemand.com/odatav2/DEFAULT/DOCUSIGN;v=1/user';
bodyData = {
"userID": userid,
"sent": true
}
var options = {
'method': 'POST',
'url': backendAPIurl,
'headers': {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify(bodyData)
};
request(options, function(error, response) {
if (!error && response.statusCode == 201) {
res.send('Contract has been sent');
} else {
res.send('Error Happened');
}
if (error) throw new Error(error);
console.log(response.body);
});
} else {
res.send('Error happened')
}
})
}
gettingToken()
.then(result => {
console.log('Successfully fetched OAuth access token: ' + result.accessToken.substring(0, 16));
return contractCheck(email, userid, full_name, result.accessToken);
})
.catch(error => {
console.log(error.message + ' Reason: ' + error.error);
res.send(error.message + ' Reason: ' + error.error);
});
})
app.listen(port, () => {
console.log('Server is running on port ' + port)
})
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 | |
2 |