fix: enforce subscription period end
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
} from "@nproxy/domain";
|
||||
import { Prisma, type PrismaClient } from "@prisma/client";
|
||||
import { prisma as defaultPrisma } from "./prisma-client.js";
|
||||
import { reconcileElapsedSubscription } from "./subscription-lifecycle.js";
|
||||
|
||||
export interface GenerationStore
|
||||
extends CreateGenerationRequestDeps,
|
||||
@@ -45,12 +46,28 @@ export function createPrismaGenerationStore(
|
||||
],
|
||||
});
|
||||
|
||||
if (!subscription) {
|
||||
const currentSubscription = await reconcileElapsedSubscription(database, subscription, {
|
||||
reload: async () =>
|
||||
database.subscription.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
status: "active",
|
||||
},
|
||||
include: {
|
||||
plan: true,
|
||||
},
|
||||
orderBy: [{ currentPeriodEnd: "desc" }, { createdAt: "desc" }],
|
||||
}),
|
||||
});
|
||||
|
||||
if (!currentSubscription || currentSubscription.status !== "active") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const cycleStart =
|
||||
subscription.currentPeriodStart ?? subscription.activatedAt ?? subscription.createdAt;
|
||||
currentSubscription.currentPeriodStart ??
|
||||
currentSubscription.activatedAt ??
|
||||
currentSubscription.createdAt;
|
||||
|
||||
const usageAggregation = await database.usageLedgerEntry.aggregate({
|
||||
where: {
|
||||
@@ -64,9 +81,9 @@ export function createPrismaGenerationStore(
|
||||
});
|
||||
|
||||
return {
|
||||
subscriptionId: subscription.id,
|
||||
planId: subscription.planId,
|
||||
monthlyRequestLimit: subscription.plan.monthlyRequestLimit,
|
||||
subscriptionId: currentSubscription.id,
|
||||
planId: currentSubscription.planId,
|
||||
monthlyRequestLimit: currentSubscription.plan.monthlyRequestLimit,
|
||||
usedSuccessfulRequests: usageAggregation._sum.deltaRequests ?? 0,
|
||||
};
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user