return early when data is not available as expected
This commit is contained in:
@ -9,12 +9,17 @@ type Payload = {
|
||||
};
|
||||
|
||||
const fetchMessagesQueue = Queue<Payload>("api/queue/fetch-messages", async ({ customerId }) => {
|
||||
const customer = await db.customer.findFirst({ where: { id: customerId } });
|
||||
const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId } });
|
||||
const [customer, phoneNumber] = await Promise.all([
|
||||
db.customer.findFirst({ where: { id: customerId } }),
|
||||
db.phoneNumber.findFirst({ where: { customerId } }),
|
||||
]);
|
||||
if (!customer || !customer.accountSid || !customer.authToken || !phoneNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
const [sent, received] = await Promise.all([
|
||||
twilio(customer!.accountSid!, customer!.authToken!).messages.list({ from: phoneNumber!.phoneNumber }),
|
||||
twilio(customer!.accountSid!, customer!.authToken!).messages.list({ to: phoneNumber!.phoneNumber }),
|
||||
twilio(customer.accountSid, customer.authToken).messages.list({ from: phoneNumber.phoneNumber }),
|
||||
twilio(customer.accountSid, customer.authToken).messages.list({ to: phoneNumber.phoneNumber }),
|
||||
]);
|
||||
const messagesSent = sent.filter((message) => message.direction.startsWith("outbound"));
|
||||
const messagesReceived = received.filter((message) => message.direction === "inbound");
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Queue } from "quirrel/blitz";
|
||||
import type { MessageInstance } from "twilio/lib/rest/api/v2010/account/message";
|
||||
|
||||
import db, { MessageStatus, Direction, Message } from "../../../../db";
|
||||
import db, { Direction, Message, MessageStatus } from "../../../../db";
|
||||
import { encrypt } from "../../../../db/_encryption";
|
||||
|
||||
type Payload = {
|
||||
@ -11,12 +11,14 @@ type Payload = {
|
||||
|
||||
const insertMessagesQueue = Queue<Payload>("api/queue/insert-messages", async ({ messages, customerId }) => {
|
||||
const customer = await db.customer.findFirst({ where: { id: customerId } });
|
||||
const encryptionKey = customer!.encryptionKey;
|
||||
if (!customer) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sms = messages
|
||||
.map<Omit<Message, "id">>((message) => ({
|
||||
customerId,
|
||||
content: encrypt(message.body, encryptionKey),
|
||||
content: encrypt(message.body, customer.encryptionKey),
|
||||
from: message.from,
|
||||
to: message.to,
|
||||
status: translateStatus(message.status),
|
||||
|
@ -13,14 +13,19 @@ type Payload = {
|
||||
const sendMessageQueue = Queue<Payload>(
|
||||
"api/queue/send-message",
|
||||
async ({ id, customerId, to, content }) => {
|
||||
const customer = await db.customer.findFirst({ where: { id: customerId } });
|
||||
const phoneNumber = await db.phoneNumber.findFirst({ where: { customerId } });
|
||||
const [customer, phoneNumber] = await Promise.all([
|
||||
db.customer.findFirst({ where: { id: customerId } }),
|
||||
db.phoneNumber.findFirst({ where: { customerId } }),
|
||||
]);
|
||||
if (!customer || !customer.accountSid || !customer.authToken || !phoneNumber) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const message = await twilio(customer!.accountSid!, customer!.authToken!).messages.create({
|
||||
const message = await twilio(customer.accountSid, customer.authToken).messages.create({
|
||||
body: content,
|
||||
to,
|
||||
from: phoneNumber!.phoneNumber,
|
||||
from: phoneNumber.phoneNumber,
|
||||
});
|
||||
await db.message.update({
|
||||
where: { id },
|
||||
|
Reference in New Issue
Block a user