notify of incoming messages and show in app notifications
This commit is contained in:
@ -1,13 +1,16 @@
|
||||
import { type ActionFunction } from "@remix-run/node";
|
||||
import { badRequest, html, notFound, serverError } from "remix-utils";
|
||||
import twilio from "twilio";
|
||||
import { z } from "zod";
|
||||
import { Prisma, SubscriptionStatus } from "@prisma/client";
|
||||
|
||||
import insertIncomingMessageQueue from "~/queues/insert-incoming-message.server";
|
||||
import notifyIncomingMessageQueue from "~/queues/notify-incoming-message.server";
|
||||
import logger from "~/utils/logger.server";
|
||||
import db from "~/utils/db.server";
|
||||
import twilio from "twilio";
|
||||
import { smsUrl } from "~/utils/twilio.server";
|
||||
import { decrypt } from "~/utils/encryption";
|
||||
import { validate } from "~/utils/validation.server";
|
||||
|
||||
export const action: ActionFunction = async ({ request }) => {
|
||||
const twilioSignature = request.headers.get("X-Twilio-Signature") || request.headers.get("x-twilio-signature");
|
||||
@ -15,7 +18,14 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
return badRequest("Invalid header X-Twilio-Signature");
|
||||
}
|
||||
|
||||
const body: Body = Object.fromEntries(await request.formData()) as any;
|
||||
const formData = Object.fromEntries(await request.formData());
|
||||
const validation = validate(bodySchema, formData);
|
||||
if (validation.errors) {
|
||||
logger.error(validation.errors);
|
||||
return badRequest("");
|
||||
}
|
||||
|
||||
const body = validation.data;
|
||||
try {
|
||||
const phoneNumbers = await db.phoneNumber.findMany({
|
||||
where: { number: body.To },
|
||||
@ -47,7 +57,7 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
return notFound("Phone number not found");
|
||||
}
|
||||
|
||||
const phoneNumbersWithActiveSub = phoneNumbers.filter(
|
||||
/*const phoneNumbersWithActiveSub = phoneNumbers.filter(
|
||||
(phoneNumber) => phoneNumber.twilioAccount.organization.subscriptions.length > 0,
|
||||
);
|
||||
if (phoneNumbersWithActiveSub.length === 0) {
|
||||
@ -55,7 +65,7 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
// because the organization is on the free plan
|
||||
console.log("no active subscription"); // TODO: uncomment the line below -- beware: refresh phone numbers refetch those missed messages lol
|
||||
// return html("<Response></Response>");
|
||||
}
|
||||
}*/
|
||||
|
||||
const phoneNumber = phoneNumbers.find((phoneNumber) => {
|
||||
// TODO: uncomment the line below
|
||||
@ -65,7 +75,7 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
// maybe we shouldn't let that happen by restricting a phone number to one org?
|
||||
const encryptedAuthToken = phoneNumber.twilioAccount.authToken;
|
||||
const authToken = encryptedAuthToken ? decrypt(encryptedAuthToken) : "";
|
||||
return twilio.validateRequest(authToken, twilioSignature, smsUrl, body);
|
||||
return twilio.validateRequest(authToken, twilioSignature, smsUrl, formData);
|
||||
});
|
||||
if (!phoneNumber) {
|
||||
return badRequest("Invalid webhook");
|
||||
@ -73,10 +83,16 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
|
||||
const messageSid = body.MessageSid;
|
||||
const phoneNumberId = phoneNumber.id;
|
||||
await insertIncomingMessageQueue.add(`insert message ${messageSid} for ${phoneNumberId}`, {
|
||||
messageSid,
|
||||
phoneNumberId,
|
||||
});
|
||||
await Promise.all([
|
||||
insertIncomingMessageQueue.add(`insert message ${messageSid} for ${phoneNumberId}`, {
|
||||
messageSid,
|
||||
phoneNumberId,
|
||||
}),
|
||||
notifyIncomingMessageQueue.add(`notify incoming message ${messageSid} for ${phoneNumberId}`, {
|
||||
messageSid,
|
||||
phoneNumberId,
|
||||
}),
|
||||
]);
|
||||
|
||||
return html("<Response></Response>");
|
||||
} catch (error: any) {
|
||||
@ -86,24 +102,25 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
}
|
||||
};
|
||||
|
||||
type Body = {
|
||||
ToCountry: string;
|
||||
ToState: string;
|
||||
SmsMessageSid: string;
|
||||
NumMedia: string;
|
||||
ToCity: string;
|
||||
FromZip: string;
|
||||
SmsSid: string;
|
||||
FromState: string;
|
||||
SmsStatus: string;
|
||||
FromCity: string;
|
||||
Body: string;
|
||||
FromCountry: string;
|
||||
To: string;
|
||||
ToZip: string;
|
||||
NumSegments: string;
|
||||
MessageSid: string;
|
||||
AccountSid: string;
|
||||
From: string;
|
||||
ApiVersion: string;
|
||||
};
|
||||
const bodySchema = z.object({
|
||||
MessageSid: z.string(),
|
||||
To: z.string(),
|
||||
ToCountry: z.string().optional(),
|
||||
ToState: z.string().optional(),
|
||||
SmsMessageSid: z.string().optional(),
|
||||
NumMedia: z.string().optional(),
|
||||
ToCity: z.string().optional(),
|
||||
FromZip: z.string().optional(),
|
||||
SmsSid: z.string().optional(),
|
||||
FromState: z.string().optional(),
|
||||
SmsStatus: z.string().optional(),
|
||||
FromCity: z.string().optional(),
|
||||
Body: z.string().optional(),
|
||||
FromCountry: z.string().optional(),
|
||||
ToZip: z.string().optional(),
|
||||
NumSegments: z.string().optional(),
|
||||
AccountSid: z.string().optional(),
|
||||
From: z.string().optional(),
|
||||
ApiVersion: z.string().optional(),
|
||||
ReferralNumMedia: z.string().optional(),
|
||||
});
|
||||
|
Reference in New Issue
Block a user