|
| 1 | +const numbers = require("../"); |
| 2 | +const config = require("./config"); |
| 3 | +const fs = require("fs"); |
| 4 | + |
| 5 | +numbers.Client.globalOptions.apiEndPoint = config.apiEndPoint; |
| 6 | +numbers.Client.globalOptions.accountId = config.accountId; |
| 7 | +numbers.Client.globalOptions.userName = config.userName; |
| 8 | +numbers.Client.globalOptions.password = config.password; |
| 9 | + |
| 10 | +const selectedSite = config.selectedSiteId; |
| 11 | +const selectedPeer = config.selectedSipPeerId |
| 12 | + |
| 13 | +if(selectedSite === undefined || selectedPeer === undefined){ |
| 14 | + console.log("You must configure a site and sip peer for this demo in your config file"); |
| 15 | + process.exit(1); |
| 16 | +} |
| 17 | + |
| 18 | +if(process.argv.length < 3){ |
| 19 | + console.log("usage: node hostedMessaging-sample.js [number] e.g. node portIn-sample 9195551212"); |
| 20 | + process.exit(1); |
| 21 | +} |
| 22 | + |
| 23 | +const numberToCheck = process.argv[2]; |
| 24 | +// const numberToCheck = "3854293688"; |
| 25 | + |
| 26 | +const checkImportability = async (number) => { |
| 27 | + try { |
| 28 | + const importableResponse = await numbers.ImportTnChecker.checkAsync([number]); |
| 29 | + const isImportable = (importableResponse.importTnCheckerPayload.importTnErrors === 0); |
| 30 | + if (isImportable) { |
| 31 | + return { |
| 32 | + importable: true |
| 33 | + } |
| 34 | + } |
| 35 | + else { |
| 36 | + return { |
| 37 | + importable: false, |
| 38 | + errorMessage: importableResponse.importTnCheckerPayload.importTnErrors.importTnError |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + catch (e) { |
| 43 | + console.log('Error checking importability'); |
| 44 | + console.log(e); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const createImportTnOrder = async (importRequest, numbersToImport) => { |
| 49 | + try { |
| 50 | + const importResponse = await numbers.ImportTnOrder.createAsync(importRequest, numbersToImport); |
| 51 | + const importAccepted = (importResponse.errors === 0); |
| 52 | + if (importAccepted) { |
| 53 | + return { |
| 54 | + accepted: true, |
| 55 | + response: importResponse |
| 56 | + } |
| 57 | + } |
| 58 | + else { |
| 59 | + return { |
| 60 | + accepted: false, |
| 61 | + response: importResponse |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + catch (e) { |
| 66 | + console.log('Error creating import tn order, note that this does not mean the import order was successful, but only that the initial request failed'); |
| 67 | + console.log(e); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +const main = async (number) => { |
| 72 | + const isImportable = await checkImportability(number); |
| 73 | + if (isImportable.importable !== true) { |
| 74 | + const errorCode = isImportable.errorMessage.code; |
| 75 | + const errorDescription = isImportable.errorMessage.description; |
| 76 | + console.log(`Number ${number} is not importable.\n\tCode: ${errorCode}\n\tDescription: ${errorDescription}`); |
| 77 | + return; |
| 78 | + } |
| 79 | + const subscriberInformation = { |
| 80 | + name: "ABC Inc.", |
| 81 | + serviceAddress: { |
| 82 | + houseNumber: "11235", |
| 83 | + streetName: "StreetName", |
| 84 | + stateCode: "NC", |
| 85 | + city: "City", |
| 86 | + county: "county", |
| 87 | + zip: "27606" |
| 88 | + } |
| 89 | + }; |
| 90 | + const importRequest = { |
| 91 | + customerOrderId: "customerOrderId", |
| 92 | + siteId: selectedSite, |
| 93 | + sipPeerId: selectedPeer, |
| 94 | + loaAuthorizingPerson: "LoaAuthorizingPerson", |
| 95 | + subscriber: subscriberInformation |
| 96 | + }; |
| 97 | + const importResponse = await createImportTnOrder(importRequest, [number]); |
| 98 | + console.log(JSON.stringify(importResponse)); |
| 99 | + |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +main(numberToCheck); |
| 104 | + |
| 105 | + |
| 106 | + |
0 commit comments