shellphone.app/app/queues/notify-incoming-message.server.ts
2022-06-15 01:28:32 +02:00

45 lines
1.3 KiB
TypeScript

import { Queue } from "~/utils/queue.server";
import db from "~/utils/db.server";
import logger from "~/utils/logger.server";
import { buildMessageNotificationPayload, notify } from "~/utils/web-push.server";
import getTwilioClient from "~/utils/twilio.server";
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);
// TODO: implement WS/SSE to push new messages for users who haven't enabled push notifications
await notify(subscriptions, payload);
});