-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.ts
79 lines (67 loc) · 2.73 KB
/
list.ts
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
import { getPrintFormat, initSmsServiceWithServicePlanId, printFullResponse } from '../../config';
import {
PageResult,
Sms,
} from '@sinch/sdk-core';
const populateDeliveryReportsList = (
deliveryReportsListPage: PageResult<Sms.RecipientDeliveryReport>,
fullDeliveryReportsList: Sms.RecipientDeliveryReport[],
deliveryReportsList: string[],
) => {
fullDeliveryReportsList.push(...deliveryReportsListPage.data);
deliveryReportsListPage.data?.map((deliveryReport) => {
deliveryReportsList.push(`Delivery support status: ${deliveryReport.status} - Type: ${deliveryReport.type} - Batch ID: ${deliveryReport.batch_id}`);
});
};
(async () => {
console.log('**********************');
console.log('* getDeliveryReports *');
console.log('**********************');
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
oneWeekAgo.setHours(0, 0, 0, 0);
const requestData: Sms.ListDeliveryReportsRequestData= {
start_date: oneWeekAgo,
};
const smsService = initSmsServiceWithServicePlanId();
// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response;
try {
response = await smsService.deliveryReports.list(requestData);
} catch (error) {
console.error(`ERROR: Impossible to get the list of delivery reports starting at ${requestData.start_date}`);
throw error;
}
const fullDeliveryReportsList: Sms.RecipientDeliveryReport[] = [];
const deliveryReportsList: string[] = [];
// Loop on all the pages to get all the delivery reports
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateDeliveryReportsList(response, fullDeliveryReportsList, deliveryReportsList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}
const printFormat = getPrintFormat(process.argv);
if (printFormat === 'pretty') {
console.log(deliveryReportsList.length > 0
? 'List of delivery reports: ' + JSON.stringify(deliveryReportsList, null, 2)
: 'Sorry, no delivery reports were found.');
} else {
printFullResponse(fullDeliveryReportsList);
}
// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const deliveryReport of smsService.deliveryReports.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`Delivery support status: ${deliveryReport.status} - Type: ${deliveryReport.type} - Batch ID: ${deliveryReport.batch_id}`);
} else {
console.log(deliveryReport);
}
}
})();