-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (85 loc) · 3.78 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
const documents = require('./methods/documents.js');
const requestHelpers = require('./helpers/requests.js');
const notifications = require('./methods/notifications.js');
/**
* This part is just the initial method. In other words, it is
* the place where we call the methods we have written and wait
* for certain data to be returned and use this data elsewhere.
*/
async function run() {
try {
// retrieve global values
const countryCode = process.env.COUNTRY_CODE;
const taxId = process.env.SENDER_TAX_ID;
const sourceSystemId = process.env.SENDER_SYSTEM_ID;
/**
* You should have a service where you create your own
* SBD (Standard Business Document), and where you can fetch
* your SBD files from. In this example, we are getting
* our document from the sbds folder in this project.
* You will either add a SBD file in the sbds folder, or change
* the implementation of the below method.
*
* For more information on SBD:
* https://developer-guide.sovos.com/connect-once-api/general-concepts/standard-business-document
*/
const newDocumentResponse = await documents.fetchAndSend();
/**
* You can retrieve all notifications.
* Please pay attention to limits:
* page parameter's range is [1, 10]
* perPage parameter's range is [1, 100] if includeBinaryData is false, and
* perPage parameter's range is [1, 10] if includeBinaryData is true.
*/
const notificationsResponse = await notifications.retrieve(countryCode, {
taxId,
sourceSystemId,
page: 1,
perPage: 20,
includeBinaryData: false
});
// if you have no notifications, the status code will be 404.
if (notificationsResponse.status === 200) {
/**
* You can also get notifications by the document id
* or a single notification by id
*/
const notificationId = notificationsResponse.data.notifications[0].notificationId;
const notifDocId = notificationsResponse.data.notifications[0].metadata.documentId;
const notificationById = await notifications.retrieveById(countryCode, notificationId);
const notificationsByDocument = await notifications.retrieveByDocumentId(countryCode, notifDocId);
/**
* You can acknowledge notificitations
* or you may want to mark these notifications as "unread" if you think
* they are not processed properly
*/
await notifications.mark(countryCode, [notificationId], 'read');
// await notifications.mark(countryCode, notificationIds, 'unread')
}
/**
* Actions on documents: you may cancel, correct or distribute documents.
*
* Here, documentId represents a document that was created and approved
* prior to this call. You will not be able to cancel the document
* before it is approved.
*
* Set an approved document id for the SAMPLE_DOCUMENT_ID if you want to
* use the code below
*/
const documentId = process.env.SAMPLE_DOCUMENT_ID;
if (documentId) {
const documentItem = documents.createDocumentActionItem(documentId, {
reason: 'Reason for cancelling the document',
// add more fields if necessary
// ...
});
await documents.executeAction(countryCode, {
actionCode: 'document.cancellation',
documents: [documentItem]
});
}
} catch (error) {
requestHelpers.handleGenericError(error);
}
}
run();