-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocusignService.js
More file actions
62 lines (51 loc) · 1.89 KB
/
docusignService.js
File metadata and controls
62 lines (51 loc) · 1.89 KB
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
const DsClient = require("../config/dsClient");
const iam = require('@docusign/iam-sdk');
const getAgreements = async (req) => {
const accessToken = req.headers['authorization'].split(' ')[1];
const userInfo = await DsClient.getUserInfo(accessToken);
const defaultAccount = userInfo.accounts.find(account => account.isDefault === 'true');
const accountId = defaultAccount ? defaultAccount.accountId : null;
try {
if (!accessToken) {
throw new Error("Access token is missing. Please log in again.");
}
const client = new iam.IamClient({accessToken: accessToken });
const agreements = await client.navigator.agreements.getAgreementsList({ accountId: accountId });
if (agreements.data) {
return agreements.data;
} else {
throw new Error("No agreements found");
}
} catch (error) {
console.error("Error fetching agreements from DocuSign:", error.message);
throw error;
}
};
const getAgreementById = async (req, agreementId) => {
const accessToken = req.headers['authorization'].split(' ')[1];
const userInfo = await DsClient.getUserInfo(accessToken);
const defaultAccount = userInfo.accounts.find(account => account.isDefault === 'true');
const accountId = defaultAccount ? defaultAccount.accountId : null;
try {
if (!accessToken) {
throw new Error("Access token is missing. Please log in again.");
}
const client = new iam.IamClient({ accessToken: accessToken });
const agreements = await client.navigator.agreements.getAgreement({ accountId: accountId, agreementId: agreementId });
if (agreements) {
return agreements;
} else {
throw new Error(`Agreement with ID ${agreementId} not found`);
}
} catch (error) {
console.error(
`Error fetching agreement ${agreementId} from DocuSign:`,
error.message
);
throw error;
}
};
module.exports = {
getAgreementById,
getAgreements,
};