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: {
|
|
|
|
include: {
|
|
|
|
organization: {
|
|
|
|
select: {
|
|
|
|
memberships: {
|
|
|
|
select: { notificationSubscription: true },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!phoneNumber) {
|
|
|
|
logger.warn(`No phone number found with id=${phoneNumberId}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const subscriptions = phoneNumber.twilioAccount.organization.memberships.flatMap(
|
|
|
|
(membership) => membership.notificationSubscription,
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
});
|