2022-05-21 23:33:46 +00:00
|
|
|
import { type ActionFunction } from "@remix-run/node";
|
|
|
|
import { badRequest, html, notFound, serverError } from "remix-utils";
|
2022-06-12 21:17:22 +00:00
|
|
|
import twilio from "twilio";
|
|
|
|
import { z } from "zod";
|
2022-05-21 23:33:46 +00:00
|
|
|
|
|
|
|
import insertIncomingMessageQueue from "~/queues/insert-incoming-message.server";
|
2022-06-12 21:17:22 +00:00
|
|
|
import notifyIncomingMessageQueue from "~/queues/notify-incoming-message.server";
|
2022-05-21 23:33:46 +00:00
|
|
|
import logger from "~/utils/logger.server";
|
|
|
|
import db from "~/utils/db.server";
|
|
|
|
import { smsUrl } from "~/utils/twilio.server";
|
|
|
|
import { decrypt } from "~/utils/encryption";
|
2022-06-12 21:17:22 +00:00
|
|
|
import { validate } from "~/utils/validation.server";
|
2022-05-21 23:33:46 +00:00
|
|
|
|
|
|
|
export const action: ActionFunction = async ({ request }) => {
|
|
|
|
const twilioSignature = request.headers.get("X-Twilio-Signature") || request.headers.get("x-twilio-signature");
|
|
|
|
if (!twilioSignature || Array.isArray(twilioSignature)) {
|
|
|
|
return badRequest("Invalid header X-Twilio-Signature");
|
|
|
|
}
|
|
|
|
|
2022-06-12 21:17:22 +00:00
|
|
|
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;
|
2022-05-21 23:33:46 +00:00
|
|
|
try {
|
|
|
|
const phoneNumbers = await db.phoneNumber.findMany({
|
|
|
|
where: { number: body.To },
|
|
|
|
include: {
|
2023-04-29 16:30:07 +00:00
|
|
|
twilioAccount: true,
|
2022-05-21 23:33:46 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
if (phoneNumbers.length === 0) {
|
|
|
|
// phone number is not registered by any organization
|
|
|
|
return notFound("Phone number not found");
|
|
|
|
}
|
|
|
|
|
2022-06-12 21:17:22 +00:00
|
|
|
/*const phoneNumbersWithActiveSub = phoneNumbers.filter(
|
2022-06-11 00:09:37 +00:00
|
|
|
(phoneNumber) => phoneNumber.twilioAccount.organization.subscriptions.length > 0,
|
2022-05-21 23:33:46 +00:00
|
|
|
);
|
|
|
|
if (phoneNumbersWithActiveSub.length === 0) {
|
|
|
|
// accept the webhook but don't store incoming message
|
|
|
|
// because the organization is on the free plan
|
2022-06-11 17:35:09 +00:00
|
|
|
console.log("no active subscription"); // TODO: uncomment the line below -- beware: refresh phone numbers refetch those missed messages lol
|
|
|
|
// return html("<Response></Response>");
|
2022-06-12 21:17:22 +00:00
|
|
|
}*/
|
2022-05-21 23:33:46 +00:00
|
|
|
|
2022-06-11 17:35:09 +00:00
|
|
|
const phoneNumber = phoneNumbers.find((phoneNumber) => {
|
|
|
|
// TODO: uncomment the line below
|
|
|
|
// const phoneNumber = phoneNumbersWithActiveSub.find((phoneNumber) => {
|
2022-05-21 23:33:46 +00:00
|
|
|
// if multiple organizations have the same number
|
|
|
|
// find the organization currently using that phone number
|
|
|
|
// maybe we shouldn't let that happen by restricting a phone number to one org?
|
2022-06-11 00:09:37 +00:00
|
|
|
const encryptedAuthToken = phoneNumber.twilioAccount.authToken;
|
2022-05-21 23:33:46 +00:00
|
|
|
const authToken = encryptedAuthToken ? decrypt(encryptedAuthToken) : "";
|
2022-06-12 21:17:22 +00:00
|
|
|
return twilio.validateRequest(authToken, twilioSignature, smsUrl, formData);
|
2022-05-21 23:33:46 +00:00
|
|
|
});
|
|
|
|
if (!phoneNumber) {
|
|
|
|
return badRequest("Invalid webhook");
|
|
|
|
}
|
|
|
|
|
|
|
|
const messageSid = body.MessageSid;
|
|
|
|
const phoneNumberId = phoneNumber.id;
|
2022-06-12 21:17:22 +00:00
|
|
|
await Promise.all([
|
|
|
|
insertIncomingMessageQueue.add(`insert message ${messageSid} for ${phoneNumberId}`, {
|
|
|
|
messageSid,
|
|
|
|
phoneNumberId,
|
|
|
|
}),
|
|
|
|
notifyIncomingMessageQueue.add(`notify incoming message ${messageSid} for ${phoneNumberId}`, {
|
|
|
|
messageSid,
|
|
|
|
phoneNumberId,
|
|
|
|
}),
|
|
|
|
]);
|
2022-05-21 23:33:46 +00:00
|
|
|
|
|
|
|
return html("<Response></Response>");
|
|
|
|
} catch (error: any) {
|
|
|
|
logger.error(error);
|
|
|
|
|
|
|
|
return serverError(error.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-12 21:17:22 +00:00
|
|
|
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(),
|
|
|
|
});
|