Skip to content

Commit

Permalink
redo job workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
engelhartrueben committed Oct 10, 2024
1 parent 1e91298 commit a7f2453
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
37 changes: 37 additions & 0 deletions src/extensions/service-vendors/twilio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,42 @@ export function errorDescription(errorCode) {
};
}

export const addDeactivatedPhoneNumbers = async (
organization,
date
) => {
const { accountSid, authToken } = await getMessageServiceConfig(
"twilio",
organization
);

const client = Twilio(accountSid, authToken);

const addDeactivatedNumberToDataBase = async number => {
return await r.knex("deactivated_numbers").insert({
date_deactivated: date,
phone_number: number
})
}

try {
// gets link to AWS bucket : 2 min expiration
const url = await client.messaging.v1
.deactivations()
.fetch({ date });

// fetchs text file of deactivated numbers
const textList = await fetch(url.redirectTo).split("\n");

// add each number to the db
textList.map(number => {
addDeactivatedNumberToDataBase(number);
});
} catch (err) {
throw Error(err);
}
}

export function addServerEndpoints(addPostRoute) {
addPostRoute(
"/twilio/:orgId?",
Expand Down Expand Up @@ -1208,6 +1244,7 @@ export const fullyConfigured = async (organization, serviceManagerData) => {

export default {
syncMessagePartProcessing: !!process.env.JOBS_SAME_PROCESS,
addDeactivatedPhoneNumbers,
addServerEndpoints,
headerValidator,
convertMessagePartsToMessage,
Expand Down
48 changes: 17 additions & 31 deletions src/workers/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1461,40 +1461,26 @@ export async function deletePhoneNumbers(job) {
* @returns { array } - Array of deactivated numbers
*/
export const addDeactivatedPhoneNumbers = async (job) => {
const regex = new RegExp('\d{4}-\d{2}-\d{2}');
const { date } = JSON.parse(job.payload);

let list;

if (typeof(date) == "string" && !regex.test(date)) {
log.error("fetchDeactivatedPhoneNumbers: BAD FORMAT (date)");
return [];
}

if (!job.organization_id) {
log.error("fetchDeactivatedPhoneNumbers: organization_id is required");
return [];
}

const { authToken, accountSid } = await getMessageServiceConfig(
"twilio",
job.organization_id
);

const client = twilio(accountSid, authToken);
try {
// gets link to AWS bucket : 2 min expiration
const url = await client.messaging.v1
.deactivations()
.fetch({ date: date });
const regex = new RegExp('\d{4}-\d{2}-\d{2}');
const { date, organization_id } = JSON.parse(job.payload);

let list;

// fetchs text file of deactivated numbers
const fetchTextList = fetch(url.redirectTo);
if (typeof(date) == "string" && !regex.test(date)) {
throw Error("fetchDeactivatedPhoneNumbers: BAD FORMAT (date)");
}

if (!organization_id) {
throw Error("fetchDeactivatedPhoneNumbers: organization_id is required");
}

// returns array of deactivated numbers
list = (await (await fetchTextList).text()).split("\n");
log.info(`${job.id} - Gathered deactivated number(s) from ${job.data}`);
// TO DO: add call to twilio service vender addDeactivatedPhoneNumbers
log.info(`${job.id} - Gathered deactivated number(s) for ${date}`);
} catch (err) {
log.error(`fetchDeactivatedPhoneNumbers JOB ${job.id} FAILED: ${err}`);
log.error(`JOB ${job.id} FAILED: ${err.message}`, err);
console.log(`JOB ${job.id} full job error`, err);
} finally {
await defensivelyDeleteJob(job);
}
}

0 comments on commit a7f2453

Please sign in to comment.