2022-05-17 23:54:06 +00:00
|
|
|
import { Queue } from "~/utils/queue.server";
|
|
|
|
import db from "~/utils/db.server";
|
|
|
|
import logger from "~/utils/logger.server";
|
|
|
|
import getTwilioClient from "~/utils/twilio.server";
|
|
|
|
import insertCallsQueue from "./insert-phone-calls.server";
|
|
|
|
|
|
|
|
type Payload = {
|
|
|
|
phoneNumberId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Queue<Payload>("fetch phone calls", async ({ data }) => {
|
|
|
|
const { phoneNumberId } = data;
|
2022-07-01 23:49:34 +00:00
|
|
|
logger.info(`Fetching phone calls for phone number with id=${phoneNumberId}`);
|
2022-05-17 23:54:06 +00:00
|
|
|
const phoneNumber = await db.phoneNumber.findUnique({
|
|
|
|
where: { id: phoneNumberId },
|
2022-06-11 00:09:37 +00:00
|
|
|
include: { twilioAccount: true },
|
2022-05-17 23:54:06 +00:00
|
|
|
});
|
|
|
|
if (!phoneNumber) {
|
|
|
|
logger.warn(`No phone number found with id=${phoneNumberId}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-11 00:09:37 +00:00
|
|
|
const twilioClient = getTwilioClient(phoneNumber.twilioAccount);
|
2022-05-17 23:54:06 +00:00
|
|
|
const [callsSent, callsReceived] = await Promise.all([
|
|
|
|
twilioClient.calls.list({ from: phoneNumber.number }),
|
|
|
|
twilioClient.calls.list({ to: phoneNumber.number }),
|
|
|
|
]);
|
2022-07-01 23:49:34 +00:00
|
|
|
logger.info(
|
|
|
|
`Found ${callsSent.length} outbound calls and ${callsReceived.length} inbound calls for phone number with id=${phoneNumberId}`,
|
|
|
|
);
|
2022-05-17 23:54:06 +00:00
|
|
|
|
2022-07-01 23:49:34 +00:00
|
|
|
const calls = [...callsSent, ...callsReceived];
|
2022-05-17 23:54:06 +00:00
|
|
|
await insertCallsQueue.add(`insert calls of id=${phoneNumberId}`, {
|
|
|
|
phoneNumberId,
|
|
|
|
calls,
|
|
|
|
});
|
|
|
|
});
|