notify of incoming messages and show in app notifications
This commit is contained in:
43
app/queues/notify-incoming-message.server.ts
Normal file
43
app/queues/notify-incoming-message.server.ts
Normal file
@ -0,0 +1,43 @@
|
||||
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);
|
||||
|
||||
await notify(subscriptions, payload);
|
||||
});
|
Reference in New Issue
Block a user