From a4ac0384c21adba0f3be063751212151b7dcee23 Mon Sep 17 00:00:00 2001 From: m5r Date: Thu, 21 Oct 2021 01:03:32 +0200 Subject: [PATCH] always pick the most recent subscription --- app/messages/api/webhook/incoming-message.ts | 13 ++++++++-- app/phone-calls/api/webhook/call.ts | 24 ++++++++++++++++--- app/settings/mutations/update-subscription.ts | 10 ++++++-- app/settings/queries/get-subscription.ts | 11 +++++++-- app/users/queries/get-current-user.ts | 3 ++- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/app/messages/api/webhook/incoming-message.ts b/app/messages/api/webhook/incoming-message.ts index cccdff7..ad2e96b 100644 --- a/app/messages/api/webhook/incoming-message.ts +++ b/app/messages/api/webhook/incoming-message.ts @@ -2,7 +2,7 @@ import type { BlitzApiRequest, BlitzApiResponse } from "blitz"; import twilio from "twilio"; import appLogger from "../../../../integrations/logger"; -import db, { SubscriptionStatus } from "../../../../db"; +import db, { Prisma, SubscriptionStatus } from "../../../../db"; import insertIncomingMessageQueue from "../queue/insert-incoming-message"; import { smsUrl } from "../../../../integrations/twilio"; import type { ApiError } from "../../../core/types"; @@ -44,7 +44,16 @@ export default async function incomingMessageHandler(req: BlitzApiRequest, res: organization: { include: { subscriptions: { - where: { status: SubscriptionStatus.active }, + where: { + OR: [ + { status: { not: SubscriptionStatus.deleted } }, + { + status: SubscriptionStatus.deleted, + cancellationEffectiveDate: { gt: new Date() }, + }, + ], + }, + orderBy: { lastEventTime: Prisma.SortOrder.desc }, }, }, }, diff --git a/app/phone-calls/api/webhook/call.ts b/app/phone-calls/api/webhook/call.ts index 11dc4b8..ee671ea 100644 --- a/app/phone-calls/api/webhook/call.ts +++ b/app/phone-calls/api/webhook/call.ts @@ -2,7 +2,7 @@ import type { BlitzApiRequest, BlitzApiResponse } from "blitz"; import twilio from "twilio"; import type { ApiError } from "app/core/types"; -import db, { Direction, SubscriptionStatus } from "db"; +import db, { Direction, Prisma, SubscriptionStatus } from "db"; import appLogger from "integrations/logger"; import { translateCallStatus, voiceUrl } from "integrations/twilio"; import updateCallDurationQueue from "../queue/update-call-duration"; @@ -36,7 +36,16 @@ export default async function incomingCallHandler(req: BlitzApiRequest, res: Bli organization: { include: { subscriptions: { - where: { status: SubscriptionStatus.active }, + where: { + OR: [ + { status: { not: SubscriptionStatus.deleted } }, + { + status: SubscriptionStatus.deleted, + cancellationEffectiveDate: { gt: new Date() }, + }, + ], + }, + orderBy: { lastEventTime: Prisma.SortOrder.desc }, }, }, }, @@ -101,7 +110,16 @@ export default async function incomingCallHandler(req: BlitzApiRequest, res: Bli organization: { include: { subscriptions: { - where: { status: SubscriptionStatus.active }, + where: { + OR: [ + { status: { not: SubscriptionStatus.deleted } }, + { + status: SubscriptionStatus.deleted, + cancellationEffectiveDate: { gt: new Date() }, + }, + ], + }, + orderBy: { lastEventTime: Prisma.SortOrder.desc }, }, }, }, diff --git a/app/settings/mutations/update-subscription.ts b/app/settings/mutations/update-subscription.ts index aee4a43..651e102 100644 --- a/app/settings/mutations/update-subscription.ts +++ b/app/settings/mutations/update-subscription.ts @@ -1,7 +1,7 @@ import { NotFoundError, resolver } from "blitz"; import { z } from "zod"; -import db from "db"; +import db, { Prisma, SubscriptionStatus } from "db"; import { updateSubscriptionPlan } from "integrations/paddle"; const Body = z.object({ @@ -9,7 +9,13 @@ const Body = z.object({ }); export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ planId }, ctx) => { - const subscription = await db.subscription.findFirst({ where: { organizationId: ctx.session.orgId } }); + const subscription = await db.subscription.findFirst({ + where: { + organizationId: ctx.session.orgId, + status: { not: SubscriptionStatus.deleted }, + }, + orderBy: { lastEventTime: Prisma.SortOrder.desc }, + }); if (!subscription) { throw new NotFoundError(); } diff --git a/app/settings/queries/get-subscription.ts b/app/settings/queries/get-subscription.ts index 693520a..abb46f1 100644 --- a/app/settings/queries/get-subscription.ts +++ b/app/settings/queries/get-subscription.ts @@ -1,6 +1,6 @@ import { resolver } from "blitz"; -import db, { SubscriptionStatus } from "db"; +import db, { Prisma, SubscriptionStatus } from "db"; export default resolver.pipe(resolver.authorize(), async (_ = null, { session }) => { if (!session.orgId) return null; @@ -8,7 +8,14 @@ export default resolver.pipe(resolver.authorize(), async (_ = null, { session }) return db.subscription.findFirst({ where: { organizationId: session.orgId, - OR: [{ status: { not: SubscriptionStatus.deleted } }, { cancellationEffectiveDate: { gt: new Date() } }], + OR: [ + { status: { not: SubscriptionStatus.deleted } }, + { + status: SubscriptionStatus.deleted, + cancellationEffectiveDate: { gt: new Date() }, + }, + ], }, + orderBy: { lastEventTime: Prisma.SortOrder.desc }, }); }); diff --git a/app/users/queries/get-current-user.ts b/app/users/queries/get-current-user.ts index da6ac97..0311b36 100644 --- a/app/users/queries/get-current-user.ts +++ b/app/users/queries/get-current-user.ts @@ -1,6 +1,6 @@ import type { Ctx } from "blitz"; -import db, { SubscriptionStatus } from "db"; +import db, { Prisma, SubscriptionStatus } from "db"; export default async function getCurrentUser(_ = null, { session }: Ctx) { if (!session.userId) return null; @@ -26,6 +26,7 @@ export default async function getCurrentUser(_ = null, { session }: Ctx) { }, ], }, + orderBy: { lastEventTime: Prisma.SortOrder.desc }, }, }, },