From ae8401b6f468251a86bf0d23104ba5e00a596a53 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Mon, 22 Jan 2024 06:50:05 +0000 Subject: [PATCH] feat: skip update quota if same as latest activated quota (#5631) --- .../backend/server/src/modules/quota/service.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/backend/server/src/modules/quota/service.ts b/packages/backend/server/src/modules/quota/service.ts index 5f05a59e5..d31637427 100644 --- a/packages/backend/server/src/modules/quota/service.ts +++ b/packages/backend/server/src/modules/quota/service.ts @@ -5,6 +5,8 @@ import { FeatureKind } from '../features'; import { QuotaConfig } from './quota'; import { QuotaType } from './types'; +type Transaction = Parameters[0]>[0]; + @Injectable() export class QuotaService { constructor(private readonly prisma: PrismaService) {} @@ -83,6 +85,13 @@ export class QuotaService { expiredAt?: Date ) { await this.prisma.$transaction(async tx => { + const hasSameActivatedQuota = await this.hasQuota(userId, quota, tx); + + if (hasSameActivatedQuota) { + // don't need to switch + return; + } + const latestPlanVersion = await tx.features.aggregate({ where: { feature: quota, @@ -130,8 +139,10 @@ export class QuotaService { }); } - async hasQuota(userId: string, quota: QuotaType) { - return this.prisma.userFeatures + async hasQuota(userId: string, quota: QuotaType, transaction?: Transaction) { + const executor = transaction ?? this.prisma; + + return executor.userFeatures .count({ where: { userId,