shellphone.app/app/api/queue/send-message.ts

35 lines
776 B
TypeScript
Raw Normal View History

import { Queue } from "quirrel/blitz";
import twilio from "twilio";
2021-07-31 14:33:18 +00:00
import db from "../../../db";
2021-07-31 14:33:18 +00:00
type Payload = {
id: string;
customerId: string;
to: string;
content: string;
};
2021-07-31 14:33:18 +00:00
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 } });
2021-07-31 14:33:18 +00:00
const message = await twilio(customer!.accountSid!, customer!.authToken!).messages.create({
body: content,
to,
from: phoneNumber!.phoneNumber,
});
2021-07-31 14:33:18 +00:00
await db.message.update({
where: { id },
data: { twilioSid: message.sid },
});
2021-07-31 14:33:18 +00:00
},
{
retry: ["1min"],
}
);
2021-07-31 14:33:18 +00:00
export default sendMessageQueue;