Skip to content

Commit 84619fc

Browse files
committed
fix for toll free phone number pull issue; also does some user-proofing to make sure toll free numbers do not get deleted from Twilio if the user deletes them from a Spoke instance
1 parent 0015b58 commit 84619fc

File tree

2 files changed

+51
-40
lines changed

2 files changed

+51
-40
lines changed

src/containers/AdminPhoneNumberInventory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ class AdminPhoneNumberInventory extends React.Component {
443443
</Button>
444444
) : null}
445445
</div>
446-
<p>
446+
{/* <p>
447447
{this.state.queriedShortcodes ? (
448448
`This service has ${this.numShortcodes} shortcodes.`
449449
) : null}
@@ -452,7 +452,7 @@ class AdminPhoneNumberInventory extends React.Component {
452452
{this.state.queriedTollfree ? (
453453
`This service has ${this.numTollfreeNumbers} toll free numbers.`
454454
) : null}
455-
</p>
455+
</p> */}
456456

457457

458458
<Dialog

src/extensions/service-vendors/twilio/index.js

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -824,49 +824,52 @@ export async function getTollFreeNumbers(
824824
organization,
825825
opts = {},
826826
) {
827-
828827
const messageServiceSid = await getMessageServiceSid(organization);
829828

830-
// var for count of toll free numbers
831-
let tollfreeNumberCount = 0;
832-
833829
// defensively delete toll free numbers
834-
await r.knex("owned_phone_number").where('area_code', 'Tollfree').del()
835-
836-
console.log("Trying to get toll free numbers");
830+
const currentTollFreeNumbers = await r.knex("owned_phone_number").where('area_code', 'Tollfree').pluck('service_id');
831+
let tollFreeAreaCodes = ['800', '888', '877', '866', '855', '844', '833'];
837832

838833
// getting the shortcode list from twilio
839834
const twilioInstance = await exports.getTwilio(organization);
840-
const response = await twilioInstance.messaging.v1.services(messageServiceSid).phoneNumbers.list({limit: 20});
835+
const response = await twilioInstance.messaging.v1.services(messageServiceSid).phoneNumbers.list({limit: 400});
841836

842-
console.log(response);
843-
844837
// throw error if we get a bad response
845838
if (response.error) {
846839
throw new Error(`Error collecting Toll Free Numbers: ${response.error}`);
847840
}
848841

849-
// add each shortcode to the table
842+
// add each toll free to the table
850843
//TO DO: filter results to just toll free
851844
async function addTollFreeNumberToPhoneNumberTable(tollfree){
852-
return await r.knex("owned_phone_number").insert({
853-
organization_id: organization.id,
854-
phone_number: tollfree.phoneNumber,
855-
service: "twilio",
856-
service_id: tollfree.sid,
857-
area_code: "Tollfree"
858-
//...allocationFields
859-
});
860-
845+
846+
const areaCode = tollfree.phoneNumber.slice(2, 5);
847+
// check that it is a toll free number and not another number type
848+
if (tollFreeAreaCodes.includes(areaCode)){
849+
// check if toll free number is already in the application and only add it
850+
// if it is not in the application
851+
if (!currentTollFreeNumbers.includes(tollfree.sid)){
852+
return await r.knex("owned_phone_number").insert({
853+
organization_id: organization.id,
854+
phone_number: tollfree.phoneNumber,
855+
service: "twilio",
856+
service_id: tollfree.sid,
857+
area_code: "Tollfree"
858+
//...allocationFields
859+
});
860+
}
861+
}
861862
}
862863

863864
// for each response, add it to the table
864-
const tollfreeResponse = response.map(response => {
865+
const tollfreeResponse = response.map(tollfree => {
865866
addTollFreeNumberToPhoneNumberTable(tollfree);
866-
tollfreeNumberCount++;
867867
});
868868

869-
// return the count of short codes
869+
const countResult = await r.knex("owned_phone_number").where('area_code', 'Tollfree').count({count: '*'});
870+
const tollfreeNumberCount = countResult[0].count;
871+
872+
// return the count of toll free numbers
870873
return tollfreeNumberCount;
871874

872875
}
@@ -1020,21 +1023,29 @@ export async function addNumbersToMessagingService(
10201023
* Release a phone number and delete it from the owned_phone_number table
10211024
*/
10221025
async function deleteNumber(twilioInstance, phoneSid, phoneNumber) {
1023-
await twilioInstance
1024-
.incomingPhoneNumbers(phoneSid)
1025-
.remove()
1026-
.catch(err => {
1027-
// Error 20404 means the number does not exist in Twilio. Should be
1028-
// safe to delete from Spoke inventory. Otherwise, throw error and
1029-
// don't delete from Spoke.
1030-
if (err.code === 20404) {
1031-
log.error(
1032-
`Number not found in Twilio, may have already been released: ${phoneNumber} [${phoneSid}]`
1033-
);
1034-
} else {
1035-
throw new Error(`Error deleting twilio number: ${err}`);
1036-
}
1037-
});
1026+
1027+
1028+
let tollFreeAreaCodes = ['800', '888', '877', '866', '855', '844', '833'];
1029+
const areaCode = phoneNumber.slice(2, 5);
1030+
1031+
if (!tollFreeAreaCodes.includes(areaCode)){
1032+
console.log('Removing phone number from Twilio service');
1033+
await twilioInstance
1034+
.incomingPhoneNumbers(phoneSid)
1035+
.remove()
1036+
.catch(err => {
1037+
// Error 20404 means the number does not exist in Twilio. Should be
1038+
// safe to delete from Spoke inventory. Otherwise, throw error and
1039+
// don't delete from Spoke.
1040+
if (err.code === 20404) {
1041+
log.error(
1042+
`Number not found in Twilio, may have already been released: ${phoneNumber} [${phoneSid}]`
1043+
);
1044+
} else {
1045+
throw new Error(`Error deleting twilio number: ${err}`);
1046+
}
1047+
});
1048+
}
10381049
log.debug(`Deleted number ${phoneNumber} [${phoneSid}]`);
10391050
return await r
10401051
.knex("owned_phone_number")

0 commit comments

Comments
 (0)