mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-30 00:29:46 +08:00
feat: skip update quota if same as latest activated quota (#5631)
This commit is contained in:
@@ -5,6 +5,8 @@ import { FeatureKind } from '../features';
|
|||||||
import { QuotaConfig } from './quota';
|
import { QuotaConfig } from './quota';
|
||||||
import { QuotaType } from './types';
|
import { QuotaType } from './types';
|
||||||
|
|
||||||
|
type Transaction = Parameters<Parameters<PrismaService['$transaction']>[0]>[0];
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class QuotaService {
|
export class QuotaService {
|
||||||
constructor(private readonly prisma: PrismaService) {}
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
@@ -83,6 +85,13 @@ export class QuotaService {
|
|||||||
expiredAt?: Date
|
expiredAt?: Date
|
||||||
) {
|
) {
|
||||||
await this.prisma.$transaction(async tx => {
|
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({
|
const latestPlanVersion = await tx.features.aggregate({
|
||||||
where: {
|
where: {
|
||||||
feature: quota,
|
feature: quota,
|
||||||
@@ -130,8 +139,10 @@ export class QuotaService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async hasQuota(userId: string, quota: QuotaType) {
|
async hasQuota(userId: string, quota: QuotaType, transaction?: Transaction) {
|
||||||
return this.prisma.userFeatures
|
const executor = transaction ?? this.prisma;
|
||||||
|
|
||||||
|
return executor.userFeatures
|
||||||
.count({
|
.count({
|
||||||
where: {
|
where: {
|
||||||
userId,
|
userId,
|
||||||
|
|||||||
Reference in New Issue
Block a user