always pick the most recent subscription

This commit is contained in:
m5r 2021-10-21 01:03:32 +02:00
parent c0554f321c
commit a4ac0384c2
5 changed files with 51 additions and 10 deletions

View File

@ -2,7 +2,7 @@ import type { BlitzApiRequest, BlitzApiResponse } from "blitz";
import twilio from "twilio"; import twilio from "twilio";
import appLogger from "../../../../integrations/logger"; import appLogger from "../../../../integrations/logger";
import db, { SubscriptionStatus } from "../../../../db"; import db, { Prisma, SubscriptionStatus } from "../../../../db";
import insertIncomingMessageQueue from "../queue/insert-incoming-message"; import insertIncomingMessageQueue from "../queue/insert-incoming-message";
import { smsUrl } from "../../../../integrations/twilio"; import { smsUrl } from "../../../../integrations/twilio";
import type { ApiError } from "../../../core/types"; import type { ApiError } from "../../../core/types";
@ -44,7 +44,16 @@ export default async function incomingMessageHandler(req: BlitzApiRequest, res:
organization: { organization: {
include: { include: {
subscriptions: { subscriptions: {
where: { status: SubscriptionStatus.active }, where: {
OR: [
{ status: { not: SubscriptionStatus.deleted } },
{
status: SubscriptionStatus.deleted,
cancellationEffectiveDate: { gt: new Date() },
},
],
},
orderBy: { lastEventTime: Prisma.SortOrder.desc },
}, },
}, },
}, },

View File

@ -2,7 +2,7 @@ import type { BlitzApiRequest, BlitzApiResponse } from "blitz";
import twilio from "twilio"; import twilio from "twilio";
import type { ApiError } from "app/core/types"; 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 appLogger from "integrations/logger";
import { translateCallStatus, voiceUrl } from "integrations/twilio"; import { translateCallStatus, voiceUrl } from "integrations/twilio";
import updateCallDurationQueue from "../queue/update-call-duration"; import updateCallDurationQueue from "../queue/update-call-duration";
@ -36,7 +36,16 @@ export default async function incomingCallHandler(req: BlitzApiRequest, res: Bli
organization: { organization: {
include: { include: {
subscriptions: { 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: { organization: {
include: { include: {
subscriptions: { subscriptions: {
where: { status: SubscriptionStatus.active }, where: {
OR: [
{ status: { not: SubscriptionStatus.deleted } },
{
status: SubscriptionStatus.deleted,
cancellationEffectiveDate: { gt: new Date() },
},
],
},
orderBy: { lastEventTime: Prisma.SortOrder.desc },
}, },
}, },
}, },

View File

@ -1,7 +1,7 @@
import { NotFoundError, resolver } from "blitz"; import { NotFoundError, resolver } from "blitz";
import { z } from "zod"; import { z } from "zod";
import db from "db"; import db, { Prisma, SubscriptionStatus } from "db";
import { updateSubscriptionPlan } from "integrations/paddle"; import { updateSubscriptionPlan } from "integrations/paddle";
const Body = z.object({ const Body = z.object({
@ -9,7 +9,13 @@ const Body = z.object({
}); });
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ planId }, ctx) => { 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) { if (!subscription) {
throw new NotFoundError(); throw new NotFoundError();
} }

View File

@ -1,6 +1,6 @@
import { resolver } from "blitz"; import { resolver } from "blitz";
import db, { SubscriptionStatus } from "db"; import db, { Prisma, SubscriptionStatus } from "db";
export default resolver.pipe(resolver.authorize(), async (_ = null, { session }) => { export default resolver.pipe(resolver.authorize(), async (_ = null, { session }) => {
if (!session.orgId) return null; if (!session.orgId) return null;
@ -8,7 +8,14 @@ export default resolver.pipe(resolver.authorize(), async (_ = null, { session })
return db.subscription.findFirst({ return db.subscription.findFirst({
where: { where: {
organizationId: session.orgId, 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 },
}); });
}); });

View File

@ -1,6 +1,6 @@
import type { Ctx } from "blitz"; import type { Ctx } from "blitz";
import db, { SubscriptionStatus } from "db"; import db, { Prisma, SubscriptionStatus } from "db";
export default async function getCurrentUser(_ = null, { session }: Ctx) { export default async function getCurrentUser(_ = null, { session }: Ctx) {
if (!session.userId) return null; if (!session.userId) return null;
@ -26,6 +26,7 @@ export default async function getCurrentUser(_ = null, { session }: Ctx) {
}, },
], ],
}, },
orderBy: { lastEventTime: Prisma.SortOrder.desc },
}, },
}, },
}, },