-
Notifications
You must be signed in to change notification settings - Fork 1
/
find.ts
75 lines (63 loc) · 2.24 KB
/
find.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
import { ElasticSipTrunking, PageResult } from '@sinch/sdk-core';
import {
getPrintFormat,
getSipTrunkIdFromConfig,
initElasticSipTrunkingService,
printFullResponse,
} from '../../config';
const populateCallsList = (
callsPage: PageResult<ElasticSipTrunking.Call>,
callsList: ElasticSipTrunking.Call[],
callsDetailsList: string[],
) => {
callsPage.data.map((call: ElasticSipTrunking.Call) => {
callsList.push(call);
callsDetailsList.push(`${call.callId} - From: ${call.from} - To: ${call.to}`);
});
};
(async () => {
console.log('*************');
console.log('* findCalls *');
console.log('*************');
const trunkId = getSipTrunkIdFromConfig();
const requestData: ElasticSipTrunking.FindCallsRequestData = {
trunkId,
callResult: 'COMPLETED',
direction: 'inbound',
};
const elasticSipTrunkingService = initElasticSipTrunkingService();
// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await elasticSipTrunkingService.calls.find(requestData);
const callsList: ElasticSipTrunking.Call[] = [];
const callsDetailsList: string[] = [];
// Loop on all the pages to get all the active numbers
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateCallsList(response, callsList, callsDetailsList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}
const printFormat = getPrintFormat(process.argv);
if (printFormat === 'pretty') {
console.log(callsDetailsList.length > 0
? 'List of calls found:\n' + callsDetailsList.join('\n')
: 'Sorry, no calls were found.');
} else {
printFullResponse(callsList);
}
// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const call of elasticSipTrunkingService.calls.find(requestData)) {
if (printFormat === 'pretty') {
console.log(`${call.callId} - From: ${call.from} - To: ${call.to}`);
} else {
console.log(call);
}
}
})();