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