2021-07-31 15:57:43 +00:00
|
|
|
import { Queue } from "quirrel/blitz";
|
|
|
|
import twilio from "twilio";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import db from "../../../db";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
type Payload = {
|
2021-07-31 15:57:43 +00:00
|
|
|
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 }) => {
|
2021-07-31 15:57:43 +00:00
|
|
|
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 15:57:43 +00:00
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
await db.message.update({
|
|
|
|
where: { id },
|
|
|
|
data: { twilioSid: message.sid },
|
2021-07-31 15:57:43 +00:00
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
retry: ["1min"],
|
|
|
|
}
|
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 sendMessageQueue;
|