add recipient field to messages and phone calls

This commit is contained in:
m5r
2022-05-22 01:17:43 +02:00
parent 6684dcc0e5
commit 1f37eb45d5
6 changed files with 78 additions and 58 deletions

View File

@ -1,5 +1,5 @@
import type { MessageInstance } from "twilio/lib/rest/api/v2010/account/message";
import type { Message } from "@prisma/client";
import { type Message, Direction } from "@prisma/client";
import { Queue } from "~/utils/queue.server";
import db from "~/utils/db.server";
@ -22,20 +22,25 @@ export default Queue<Payload>("insert messages", async ({ data }) => {
}
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),
}))
.map<Message>((message) => {
const status = translateMessageStatus(message.status);
const direction = translateMessageDirection(message.direction);
return {
id: message.sid,
phoneNumberId: phoneNumber.id,
content: message.body,
recipient: direction === Direction.Outbound ? message.to : message.from,
from: message.from,
to: message.to,
status,
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}`)
logger.info(`inserted ${count} new messages for phoneNumberId=${phoneNumberId}`);
if (!phoneNumber.isFetchingMessages) {
return;

View File

@ -1,5 +1,5 @@
import type { CallInstance } from "twilio/lib/rest/api/v2010/account/call";
import type { PhoneCall } from "@prisma/client";
import { type PhoneCall, Direction } from "@prisma/client";
import { Queue } from "~/utils/queue.server";
import db from "~/utils/db.server";
@ -22,16 +22,21 @@ export default Queue<Payload>("insert phone calls", async ({ data }) => {
}
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),
}))
.map<PhoneCall>((call) => {
const direction = translateCallDirection(call.direction);
const status = translateCallStatus(call.status);
return {
phoneNumberId,
id: call.sid,
recipient: direction === Direction.Outbound ? call.to : call.from,
from: call.from,
to: call.to,
direction,
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 });