got {messages,phone calls} {fetching,display} working
This commit is contained in:
36
app/queues/fetch-messages.server.ts
Normal file
36
app/queues/fetch-messages.server.ts
Normal file
@ -0,0 +1,36 @@
|
||||
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;
|
||||
const phoneNumber = await db.phoneNumber.findUnique({
|
||||
where: { id: phoneNumberId },
|
||||
include: { organization: true },
|
||||
});
|
||||
if (!phoneNumber) {
|
||||
logger.warn(`No phone number found with id=${phoneNumberId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const organization = phoneNumber.organization;
|
||||
const twilioClient = getTwilioClient(organization);
|
||||
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");
|
||||
const messages = [...messagesSent, ...messagesReceived];
|
||||
|
||||
await insertMessagesQueue.add(`insert messages of id=${phoneNumberId}`, {
|
||||
phoneNumberId,
|
||||
messages,
|
||||
});
|
||||
});
|
34
app/queues/fetch-phone-calls.server.ts
Normal file
34
app/queues/fetch-phone-calls.server.ts
Normal file
@ -0,0 +1,34 @@
|
||||
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;
|
||||
const phoneNumber = await db.phoneNumber.findUnique({
|
||||
where: { id: phoneNumberId },
|
||||
include: { organization: true },
|
||||
});
|
||||
if (!phoneNumber) {
|
||||
logger.warn(`No phone number found with id=${phoneNumberId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const organization = phoneNumber.organization;
|
||||
const twilioClient = getTwilioClient(organization);
|
||||
const [callsSent, callsReceived] = await Promise.all([
|
||||
twilioClient.calls.list({ from: phoneNumber.number }),
|
||||
twilioClient.calls.list({ to: phoneNumber.number }),
|
||||
]);
|
||||
const calls = [...callsSent, ...callsReceived];
|
||||
|
||||
await insertCallsQueue.add(`insert calls of id=${phoneNumberId}`, {
|
||||
phoneNumberId,
|
||||
calls,
|
||||
});
|
||||
});
|
@ -1,3 +1,13 @@
|
||||
import deleteUserDataQueue from "./delete-user-data.server";
|
||||
import fetchPhoneCallsQueue from "./fetch-phone-calls.server";
|
||||
import insertPhoneCallsQueue from "./insert-phone-calls.server";
|
||||
import fetchMessagesQueue from "./fetch-messages.server";
|
||||
import insertMessagesQueue from "./insert-messages.server";
|
||||
|
||||
export default [deleteUserDataQueue];
|
||||
export default [
|
||||
deleteUserDataQueue,
|
||||
fetchPhoneCallsQueue,
|
||||
insertPhoneCallsQueue,
|
||||
fetchMessagesQueue,
|
||||
insertMessagesQueue,
|
||||
];
|
||||
|
48
app/queues/insert-messages.server.ts
Normal file
48
app/queues/insert-messages.server.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import type { MessageInstance } from "twilio/lib/rest/api/v2010/account/message";
|
||||
import type { Message } from "@prisma/client";
|
||||
|
||||
import { Queue } from "~/utils/queue.server";
|
||||
import db from "~/utils/db.server";
|
||||
import { translateMessageDirection, translateMessageStatus } from "~/utils/twilio.server";
|
||||
import logger from "~/utils/logger.server";
|
||||
|
||||
type Payload = {
|
||||
phoneNumberId: string;
|
||||
messages: MessageInstance[];
|
||||
};
|
||||
|
||||
export default Queue<Payload>("insert messages", async ({ data }) => {
|
||||
const { messages, phoneNumberId } = data;
|
||||
const phoneNumber = await db.phoneNumber.findUnique({
|
||||
where: { id: phoneNumberId },
|
||||
include: { organization: true },
|
||||
});
|
||||
if (!phoneNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sms = messages
|
||||
.map<Message>((message) => ({
|
||||
id: message.sid,
|
||||
phoneNumberId: phoneNumber.id,
|
||||
content: message.body,
|
||||
from: message.from,
|
||||
to: message.to,
|
||||
status: translateMessageStatus(message.status),
|
||||
direction: translateMessageDirection(message.direction),
|
||||
sentAt: new Date(message.dateCreated),
|
||||
}))
|
||||
.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
|
||||
|
||||
const { count } = await db.message.createMany({ data: sms, skipDuplicates: true });
|
||||
logger.info(`inserted ${count} new messages for phoneNumberId=${phoneNumberId}`)
|
||||
|
||||
if (!phoneNumber.isFetchingMessages) {
|
||||
return;
|
||||
}
|
||||
|
||||
await db.phoneNumber.update({
|
||||
where: { id: phoneNumberId },
|
||||
data: { isFetchingMessages: null },
|
||||
});
|
||||
});
|
48
app/queues/insert-phone-calls.server.ts
Normal file
48
app/queues/insert-phone-calls.server.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import type { CallInstance } from "twilio/lib/rest/api/v2010/account/call";
|
||||
import type { PhoneCall } from "@prisma/client";
|
||||
|
||||
import { Queue } from "~/utils/queue.server";
|
||||
import db from "~/utils/db.server";
|
||||
import { translateCallDirection, translateCallStatus } from "~/utils/twilio.server";
|
||||
import logger from "~/utils/logger.server";
|
||||
|
||||
type Payload = {
|
||||
phoneNumberId: string;
|
||||
calls: CallInstance[];
|
||||
};
|
||||
|
||||
export default Queue<Payload>("insert phone calls", async ({ data }) => {
|
||||
const { calls, phoneNumberId } = data;
|
||||
const phoneNumber = await db.phoneNumber.findUnique({
|
||||
where: { id: phoneNumberId },
|
||||
include: { organization: true },
|
||||
});
|
||||
if (!phoneNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
const phoneCalls = calls
|
||||
.map<PhoneCall>((call) => ({
|
||||
phoneNumberId,
|
||||
id: call.sid,
|
||||
from: call.from,
|
||||
to: call.to,
|
||||
direction: translateCallDirection(call.direction),
|
||||
status: translateCallStatus(call.status),
|
||||
duration: call.duration,
|
||||
createdAt: new Date(call.dateCreated),
|
||||
}))
|
||||
.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
|
||||
|
||||
const ddd = await db.phoneCall.createMany({ data: phoneCalls, skipDuplicates: true });
|
||||
logger.info(`inserted ${ddd.count || "no"} new phone calls for phoneNumberId=${phoneNumberId}`);
|
||||
|
||||
if (!phoneNumber.isFetchingCalls) {
|
||||
return;
|
||||
}
|
||||
|
||||
await db.phoneNumber.update({
|
||||
where: { id: phoneNumberId },
|
||||
data: { isFetchingCalls: null },
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user