attach phone numbers to twilio account
This commit is contained in:
@ -13,16 +13,20 @@ export const action: ActionFunction = async () => {
|
||||
const phoneNumber = await db.phoneNumber.findUnique({
|
||||
where: { id: "PN4f11f0c4155dfb5d5ac8bbab2cc23cbc" },
|
||||
select: {
|
||||
organization: {
|
||||
select: {
|
||||
memberships: {
|
||||
select: { notificationSubscription: true },
|
||||
twilioAccount: {
|
||||
include: {
|
||||
organization: {
|
||||
select: {
|
||||
memberships: {
|
||||
select: { notificationSubscription: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
const subscriptions = phoneNumber!.organization.memberships.flatMap(
|
||||
const subscriptions = phoneNumber!.twilioAccount.organization.memberships.flatMap(
|
||||
(membership) => membership.notificationSubscription,
|
||||
);
|
||||
await notify(subscriptions, {
|
||||
|
@ -22,36 +22,45 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
const organizationId = body.From.slice("client:".length).split("__")[0];
|
||||
|
||||
try {
|
||||
const twilioAccount = await db.twilioAccount.findUnique({ where: { organizationId } });
|
||||
if (!twilioAccount) {
|
||||
// this shouldn't be happening
|
||||
return new Response(null, { status: 402 });
|
||||
}
|
||||
|
||||
const phoneNumber = await db.phoneNumber.findUnique({
|
||||
where: { organizationId_isCurrent: { organizationId, isCurrent: true } },
|
||||
where: { twilioAccountSid_isCurrent: { twilioAccountSid: twilioAccount.accountSid, isCurrent: true } },
|
||||
include: {
|
||||
organization: {
|
||||
twilioAccount: {
|
||||
include: {
|
||||
subscriptions: {
|
||||
where: {
|
||||
OR: [
|
||||
{ status: { not: SubscriptionStatus.deleted } },
|
||||
{
|
||||
status: SubscriptionStatus.deleted,
|
||||
cancellationEffectiveDate: { gt: new Date() },
|
||||
organization: {
|
||||
select: {
|
||||
subscriptions: {
|
||||
where: {
|
||||
OR: [
|
||||
{ status: { not: SubscriptionStatus.deleted } },
|
||||
{
|
||||
status: SubscriptionStatus.deleted,
|
||||
cancellationEffectiveDate: { gt: new Date() },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
orderBy: { lastEventTime: Prisma.SortOrder.desc },
|
||||
},
|
||||
},
|
||||
orderBy: { lastEventTime: Prisma.SortOrder.desc },
|
||||
},
|
||||
twilioAccount: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (phoneNumber?.organization.subscriptions.length === 0) {
|
||||
if (phoneNumber?.twilioAccount.organization.subscriptions.length === 0) {
|
||||
// decline the outgoing call because
|
||||
// the organization is on the free plan
|
||||
return new Response(null, { status: 402 });
|
||||
}
|
||||
|
||||
const encryptedAuthToken = phoneNumber?.organization.twilioAccount?.authToken;
|
||||
const encryptedAuthToken = phoneNumber?.twilioAccount.authToken;
|
||||
const authToken = encryptedAuthToken ? decrypt(encryptedAuthToken) : "";
|
||||
if (
|
||||
!phoneNumber ||
|
||||
|
@ -20,21 +20,24 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
const phoneNumbers = await db.phoneNumber.findMany({
|
||||
where: { number: body.To },
|
||||
include: {
|
||||
organization: {
|
||||
twilioAccount: {
|
||||
include: {
|
||||
subscriptions: {
|
||||
where: {
|
||||
OR: [
|
||||
{ status: { not: SubscriptionStatus.deleted } },
|
||||
{
|
||||
status: SubscriptionStatus.deleted,
|
||||
cancellationEffectiveDate: { gt: new Date() },
|
||||
organization: {
|
||||
select: {
|
||||
subscriptions: {
|
||||
where: {
|
||||
OR: [
|
||||
{ status: { not: SubscriptionStatus.deleted } },
|
||||
{
|
||||
status: SubscriptionStatus.deleted,
|
||||
cancellationEffectiveDate: { gt: new Date() },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
orderBy: { lastEventTime: Prisma.SortOrder.desc },
|
||||
},
|
||||
},
|
||||
orderBy: { lastEventTime: Prisma.SortOrder.desc },
|
||||
},
|
||||
twilioAccount: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -45,7 +48,7 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
}
|
||||
|
||||
const phoneNumbersWithActiveSub = phoneNumbers.filter(
|
||||
(phoneNumber) => phoneNumber.organization.subscriptions.length > 0,
|
||||
(phoneNumber) => phoneNumber.twilioAccount.organization.subscriptions.length > 0,
|
||||
);
|
||||
if (phoneNumbersWithActiveSub.length === 0) {
|
||||
// accept the webhook but don't store incoming message
|
||||
@ -57,7 +60,7 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
// 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?
|
||||
const encryptedAuthToken = phoneNumber.organization.twilioAccount?.authToken;
|
||||
const encryptedAuthToken = phoneNumber.twilioAccount.authToken;
|
||||
const authToken = encryptedAuthToken ? decrypt(encryptedAuthToken) : "";
|
||||
return twilio.validateRequest(authToken, twilioSignature, smsUrl, body);
|
||||
});
|
||||
|
Reference in New Issue
Block a user