2022-06-12 21:17:22 +00:00
|
|
|
import { Queue } from "~/utils/queue.server";
|
|
|
|
import db from "~/utils/db.server";
|
|
|
|
import logger from "~/utils/logger.server";
|
|
|
|
import getTwilioClient from "~/utils/twilio.server";
|
2022-06-19 15:57:51 +00:00
|
|
|
import { buildMessageNotificationPayload, notify } from "~/utils/web-push.server";
|
|
|
|
import { notifySSE } from "~/utils/events.server";
|
2022-06-12 21:17:22 +00:00
|
|
|
|
|
|
|
type Payload = {
|
|
|
|
messageSid: string;
|
|
|
|
phoneNumberId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Queue<Payload>("notify incoming message", async ({ data }) => {
|
|
|
|
const { messageSid, phoneNumberId } = data;
|
|
|
|
const phoneNumber = await db.phoneNumber.findUnique({
|
|
|
|
where: { id: phoneNumberId },
|
|
|
|
select: {
|
|
|
|
twilioAccount: {
|
2023-04-29 16:30:07 +00:00
|
|
|
include: { notificationSubscriptions: true },
|
2022-06-12 21:17:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!phoneNumber) {
|
|
|
|
logger.warn(`No phone number found with id=${phoneNumberId}`);
|
|
|
|
return;
|
|
|
|
}
|
2023-04-29 16:30:07 +00:00
|
|
|
const subscriptions = phoneNumber.twilioAccount.notificationSubscriptions;
|
2022-06-12 21:17:22 +00:00
|
|
|
const twilioClient = getTwilioClient(phoneNumber.twilioAccount);
|
|
|
|
const message = await twilioClient.messages.get(messageSid).fetch();
|
|
|
|
const payload = buildMessageNotificationPayload(message);
|
|
|
|
|
|
|
|
await notify(subscriptions, payload);
|
2022-06-19 15:57:51 +00:00
|
|
|
await notifySSE(payload);
|
2022-06-12 21:17:22 +00:00
|
|
|
});
|