2021-08-01 10:54:51 +00:00
|
|
|
import { Queue } from "quirrel/blitz";
|
|
|
|
import type { MessageInstance } from "twilio/lib/rest/api/v2010/account/message";
|
|
|
|
import twilio from "twilio";
|
|
|
|
|
|
|
|
import db, { Direction, MessageStatus } from "../../../../db";
|
|
|
|
import { encrypt } from "../../../../db/_encryption";
|
2021-08-05 17:07:15 +00:00
|
|
|
import notifyIncomingMessageQueue from "./notify-incoming-message";
|
2021-08-01 10:54:51 +00:00
|
|
|
|
|
|
|
type Payload = {
|
2021-08-05 17:07:15 +00:00
|
|
|
organizationId: string;
|
|
|
|
phoneNumberId: string;
|
2021-08-01 10:54:51 +00:00
|
|
|
messageSid: MessageInstance["sid"];
|
|
|
|
};
|
|
|
|
|
|
|
|
const insertIncomingMessageQueue = Queue<Payload>(
|
|
|
|
"api/queue/insert-incoming-message",
|
2021-08-05 17:07:15 +00:00
|
|
|
async ({ messageSid, organizationId, phoneNumberId }) => {
|
|
|
|
const organization = await db.organization.findFirst({
|
|
|
|
where: { id: organizationId },
|
|
|
|
});
|
|
|
|
if (!organization || !organization.twilioAccountSid || !organization.twilioAuthToken) {
|
2021-08-01 10:54:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-05 17:07:15 +00:00
|
|
|
const message = await twilio(organization.twilioAccountSid, organization.twilioAuthToken)
|
|
|
|
.messages.get(messageSid)
|
|
|
|
.fetch();
|
2021-08-01 10:54:51 +00:00
|
|
|
await db.message.create({
|
|
|
|
data: {
|
2021-08-05 17:07:15 +00:00
|
|
|
organizationId,
|
|
|
|
phoneNumberId,
|
|
|
|
id: messageSid,
|
2021-08-01 10:54:51 +00:00
|
|
|
to: message.to,
|
|
|
|
from: message.from,
|
|
|
|
status: translateStatus(message.status),
|
|
|
|
direction: translateDirection(message.direction),
|
|
|
|
sentAt: message.dateCreated,
|
2021-08-05 17:07:15 +00:00
|
|
|
content: encrypt(message.body, organization.encryptionKey),
|
2021-08-01 10:54:51 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-08-05 17:07:15 +00:00
|
|
|
await notifyIncomingMessageQueue.enqueue(
|
|
|
|
{
|
|
|
|
messageSid,
|
|
|
|
organizationId,
|
|
|
|
phoneNumberId,
|
2021-08-01 10:54:51 +00:00
|
|
|
},
|
2021-08-05 17:07:15 +00:00
|
|
|
{ id: `notify-${messageSid}-${organizationId}-${phoneNumberId}` },
|
|
|
|
);
|
2021-08-01 12:04:04 +00:00
|
|
|
},
|
2021-08-01 10:54:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
export default insertIncomingMessageQueue;
|
|
|
|
|
|
|
|
function translateDirection(direction: MessageInstance["direction"]): Direction {
|
|
|
|
switch (direction) {
|
|
|
|
case "inbound":
|
|
|
|
return Direction.Inbound;
|
|
|
|
case "outbound-api":
|
|
|
|
case "outbound-call":
|
|
|
|
case "outbound-reply":
|
|
|
|
default:
|
|
|
|
return Direction.Outbound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function translateStatus(status: MessageInstance["status"]): MessageStatus {
|
|
|
|
switch (status) {
|
|
|
|
case "accepted":
|
|
|
|
return MessageStatus.Accepted;
|
|
|
|
case "canceled":
|
|
|
|
return MessageStatus.Canceled;
|
|
|
|
case "delivered":
|
|
|
|
return MessageStatus.Delivered;
|
|
|
|
case "failed":
|
|
|
|
return MessageStatus.Failed;
|
|
|
|
case "partially_delivered":
|
|
|
|
return MessageStatus.PartiallyDelivered;
|
|
|
|
case "queued":
|
|
|
|
return MessageStatus.Queued;
|
|
|
|
case "read":
|
|
|
|
return MessageStatus.Read;
|
|
|
|
case "received":
|
|
|
|
return MessageStatus.Received;
|
|
|
|
case "receiving":
|
|
|
|
return MessageStatus.Receiving;
|
|
|
|
case "scheduled":
|
|
|
|
return MessageStatus.Scheduled;
|
|
|
|
case "sending":
|
|
|
|
return MessageStatus.Sending;
|
|
|
|
case "sent":
|
|
|
|
return MessageStatus.Sent;
|
|
|
|
case "undelivered":
|
|
|
|
return MessageStatus.Undelivered;
|
|
|
|
}
|
|
|
|
}
|