fix: enforce subscription period end
This commit is contained in:
@@ -2,6 +2,7 @@ import { getApproximateQuotaBucket, type QuotaBucket } from "@nproxy/domain";
|
||||
import type { PrismaClient, SubscriptionStatus } from "@prisma/client";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma as defaultPrisma } from "./prisma-client.js";
|
||||
import { reconcileElapsedSubscription } from "./subscription-lifecycle.js";
|
||||
|
||||
export interface UserAccountOverview {
|
||||
user: {
|
||||
@@ -58,13 +59,26 @@ export function createPrismaAccountStore(database: PrismaClient = defaultPrisma)
|
||||
],
|
||||
});
|
||||
|
||||
const quota = subscription
|
||||
const currentSubscription = await reconcileElapsedSubscription(database, subscription, {
|
||||
reload: async () =>
|
||||
database.subscription.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
include: {
|
||||
plan: true,
|
||||
},
|
||||
orderBy: [{ currentPeriodEnd: "desc" }, { createdAt: "desc" }],
|
||||
}),
|
||||
});
|
||||
|
||||
const quota = currentSubscription?.status === "active"
|
||||
? await buildQuotaSnapshot(database, userId, {
|
||||
monthlyRequestLimit: subscription.plan.monthlyRequestLimit,
|
||||
monthlyRequestLimit: currentSubscription.plan.monthlyRequestLimit,
|
||||
cycleStart:
|
||||
subscription.currentPeriodStart ??
|
||||
subscription.activatedAt ??
|
||||
subscription.createdAt,
|
||||
currentSubscription.currentPeriodStart ??
|
||||
currentSubscription.activatedAt ??
|
||||
currentSubscription.createdAt,
|
||||
})
|
||||
: null;
|
||||
|
||||
@@ -75,26 +89,30 @@ export function createPrismaAccountStore(database: PrismaClient = defaultPrisma)
|
||||
isAdmin: user.isAdmin,
|
||||
createdAt: user.createdAt,
|
||||
},
|
||||
subscription: subscription
|
||||
subscription: currentSubscription
|
||||
? {
|
||||
id: subscription.id,
|
||||
status: subscription.status,
|
||||
renewsManually: subscription.renewsManually,
|
||||
...(subscription.activatedAt ? { activatedAt: subscription.activatedAt } : {}),
|
||||
...(subscription.currentPeriodStart
|
||||
? { currentPeriodStart: subscription.currentPeriodStart }
|
||||
id: currentSubscription.id,
|
||||
status: currentSubscription.status,
|
||||
renewsManually: currentSubscription.renewsManually,
|
||||
...(currentSubscription.activatedAt
|
||||
? { activatedAt: currentSubscription.activatedAt }
|
||||
: {}),
|
||||
...(subscription.currentPeriodEnd
|
||||
? { currentPeriodEnd: subscription.currentPeriodEnd }
|
||||
...(currentSubscription.currentPeriodStart
|
||||
? { currentPeriodStart: currentSubscription.currentPeriodStart }
|
||||
: {}),
|
||||
...(currentSubscription.currentPeriodEnd
|
||||
? { currentPeriodEnd: currentSubscription.currentPeriodEnd }
|
||||
: {}),
|
||||
...(currentSubscription.canceledAt
|
||||
? { canceledAt: currentSubscription.canceledAt }
|
||||
: {}),
|
||||
...(subscription.canceledAt ? { canceledAt: subscription.canceledAt } : {}),
|
||||
plan: {
|
||||
id: subscription.plan.id,
|
||||
code: subscription.plan.code,
|
||||
displayName: subscription.plan.displayName,
|
||||
monthlyPriceUsd: decimalToNumber(subscription.plan.monthlyPriceUsd),
|
||||
billingCurrency: subscription.plan.billingCurrency,
|
||||
isActive: subscription.plan.isActive,
|
||||
id: currentSubscription.plan.id,
|
||||
code: currentSubscription.plan.code,
|
||||
displayName: currentSubscription.plan.displayName,
|
||||
monthlyPriceUsd: decimalToNumber(currentSubscription.plan.monthlyPriceUsd),
|
||||
billingCurrency: currentSubscription.plan.billingCurrency,
|
||||
isActive: currentSubscription.plan.isActive,
|
||||
},
|
||||
}
|
||||
: null,
|
||||
|
||||
Reference in New Issue
Block a user