shellphone.app/app/queues/fetch-messages.server.ts

40 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

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 insertMessagesQueue from "./insert-messages.server";
type Payload = {
phoneNumberId: string;
};
export default Queue<Payload>("fetch messages", async ({ data }) => {
const { phoneNumberId } = data;
2022-07-01 23:49:34 +00:00
logger.info(`Fetching messages for phone number with id=${phoneNumberId}`);
const phoneNumber = await db.phoneNumber.findUnique({
where: { id: phoneNumberId },
2022-06-11 00:09:37 +00:00
include: { twilioAccount: true },
});
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);
const [sent, received] = await Promise.all([
twilioClient.messages.list({ from: phoneNumber.number }),
twilioClient.messages.list({ to: phoneNumber.number }),
]);
const messagesSent = sent.filter((message) => message.direction.startsWith("outbound"));
const messagesReceived = received.filter((message) => message.direction === "inbound");
2022-07-01 23:49:34 +00:00
logger.info(
`Found ${messagesSent.length} outbound messages and ${messagesReceived.length} inbound messages for phone number with id=${phoneNumberId}`,
);
2022-07-01 23:49:34 +00:00
const messages = [...messagesSent, ...messagesReceived];
await insertMessagesQueue.add(`insert messages of id=${phoneNumberId}`, {
phoneNumberId,
messages,
});
});