shellphone.app/app/messages/api/queue/insert-messages.ts

86 lines
2.3 KiB
TypeScript
Raw Normal View History

import { Queue } from "quirrel/blitz";
import type { MessageInstance } from "twilio/lib/rest/api/v2010/account/message";
2021-07-31 14:33:18 +00:00
import db, { Direction, Message, MessageStatus } from "../../../../db";
2021-08-01 10:46:10 +00:00
import { encrypt } from "../../../../db/_encryption";
2021-07-31 14:33:18 +00:00
type Payload = {
2021-08-05 17:07:15 +00:00
organizationId: string;
phoneNumberId: string;
messages: MessageInstance[];
};
2021-07-31 14:33:18 +00:00
2021-08-05 17:07:15 +00:00
const insertMessagesQueue = Queue<Payload>(
"api/queue/insert-messages",
async ({ messages, organizationId, phoneNumberId }) => {
const phoneNumber = await db.phoneNumber.findFirst({
where: { id: phoneNumberId, organizationId },
include: { organization: true },
});
if (!phoneNumber) {
return;
}
2021-07-31 14:33:18 +00:00
2021-08-05 17:07:15 +00:00
const sms = messages
.map<Message>((message) => ({
id: message.sid,
2021-08-05 17:07:15 +00:00
organizationId,
phoneNumberId: phoneNumber.id,
content: encrypt(message.body, phoneNumber.organization.encryptionKey),
from: message.from,
to: message.to,
status: translateStatus(message.status),
direction: translateDirection(message.direction),
sentAt: new Date(message.dateCreated),
}))
.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
2021-07-31 14:33:18 +00:00
await db.message.createMany({ data: sms, skipDuplicates: true });
2021-08-05 17:07:15 +00:00
},
);
2021-07-31 14:33:18 +00:00
export default insertMessagesQueue;
2021-07-31 14:33:18 +00:00
function translateDirection(direction: MessageInstance["direction"]): Direction {
switch (direction) {
case "inbound":
return Direction.Inbound;
2021-07-31 14:33:18 +00:00
case "outbound-api":
case "outbound-call":
case "outbound-reply":
default:
return Direction.Outbound;
2021-07-31 14:33:18 +00:00
}
}
function translateStatus(status: MessageInstance["status"]): MessageStatus {
switch (status) {
case "accepted":
return MessageStatus.Accepted;
2021-07-31 14:33:18 +00:00
case "canceled":
return MessageStatus.Canceled;
2021-07-31 14:33:18 +00:00
case "delivered":
return MessageStatus.Delivered;
2021-07-31 14:33:18 +00:00
case "failed":
return MessageStatus.Failed;
2021-07-31 14:33:18 +00:00
case "partially_delivered":
return MessageStatus.PartiallyDelivered;
2021-07-31 14:33:18 +00:00
case "queued":
return MessageStatus.Queued;
2021-07-31 14:33:18 +00:00
case "read":
return MessageStatus.Read;
2021-07-31 14:33:18 +00:00
case "received":
return MessageStatus.Received;
2021-07-31 14:33:18 +00:00
case "receiving":
return MessageStatus.Receiving;
2021-07-31 14:33:18 +00:00
case "scheduled":
return MessageStatus.Scheduled;
2021-07-31 14:33:18 +00:00
case "sending":
return MessageStatus.Sending;
2021-07-31 14:33:18 +00:00
case "sent":
return MessageStatus.Sent;
2021-07-31 14:33:18 +00:00
case "undelivered":
return MessageStatus.Undelivered;
2021-07-31 14:33:18 +00:00
}
}