Skip to content

Commit

Permalink
chore: cleanup codes
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Dec 4, 2024
1 parent dc0d42a commit e6f892c
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 197 deletions.
2 changes: 1 addition & 1 deletion packages/backend/server/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ model WorkspaceFeature {
workspaceId String @map("workspace_id") @db.VarChar
featureId Int @map("feature_id") @db.Integer
// override feature's configs
// override quota's configs
configs Json @default("{}") @db.Json
// we will record the reason why the feature is enabled/disabled
// for example:
Expand Down
12 changes: 2 additions & 10 deletions packages/backend/server/src/core/quota/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Module } from '@nestjs/common';
import { FeatureModule } from '../features';
import { PermissionModule } from '../permission';
import { StorageModule } from '../storage';
import { QuotaOverrideService } from './override';
import { QuotaManagementResolver } from './resolver';
import { QuotaService } from './service';
import { QuotaManagementService } from './storage';
Expand All @@ -16,19 +15,12 @@ import { QuotaManagementService } from './storage';
*/
@Module({
imports: [FeatureModule, StorageModule, PermissionModule],
providers: [
QuotaService,
QuotaOverrideService,
QuotaManagementResolver,
QuotaManagementService,
],
exports: [QuotaService, QuotaOverrideService, QuotaManagementService],
providers: [QuotaService, QuotaManagementResolver, QuotaManagementService],
exports: [QuotaService, QuotaManagementService],
})
export class QuotaModule {}

export { QuotaManagementService, QuotaService };
export { OneDay, OneGB, OneMB } from './constant';
export { QuotaOverride, QuotaOverrideService } from './override';
export { Quota_FreePlanV1_1, Quota_ProPlanV1 } from './schema';
export {
formatDate,
Expand Down
53 changes: 0 additions & 53 deletions packages/backend/server/src/core/quota/override.ts

This file was deleted.

12 changes: 9 additions & 3 deletions packages/backend/server/src/core/quota/quota.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ export class QuotaConfig {
}

withOverride(override: any) {
return new QuotaConfig(this.config, override);
if (override) {
return new QuotaConfig(this.config, override);
}
return this;
}

checkOverride(override: any) {
return QuotaSchema.safeParse(Object.assign({}, this.config, override));
return QuotaSchema.safeParse({
...this.config,
configs: Object.assign({}, this.config.configs, override),
});
}

get version() {
Expand All @@ -84,8 +90,8 @@ export class QuotaConfig {
get businessBlobLimit() {
return (
this.override?.businessBlobLimit ||
this.override?.blobLimit ||
this.config.configs.businessBlobLimit ||
this.override?.blobLimit ||
this.config.configs.blobLimit
);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/backend/server/src/core/quota/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ export const Quotas: Quota[] = [
copilotActionLimit: 10,
},
},

{
feature: QuotaType.TeamPlanV1,
type: FeatureKind.Quota,
Expand All @@ -183,7 +182,7 @@ export const Quotas: Quota[] = [
// quota name
name: 'Team Workspace',
// single blob limit 100MB
blobLimit: 100 * OneMB,
blobLimit: 500 * OneMB,
// total blob limit 100GB
storageQuota: 100 * OneGB,
// seat quota 20GB per seat
Expand Down
79 changes: 29 additions & 50 deletions packages/backend/server/src/core/quota/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import { JsonObject } from '@prisma/client/runtime/library';

import type { EventPayload } from '../../fundamentals';
import { OnEvent, PrismaTransaction } from '../../fundamentals';
Expand All @@ -17,9 +16,12 @@ export class QuotaService {
) {}

async getQuota<Q extends QuotaType>(
quota: Q
quota: Q,
tx?: PrismaTransaction
): Promise<QuotaConfig | undefined> {
const data = await this.prisma.feature.findFirst({
const executor = tx ?? this.prisma;

const data = await executor.feature.findFirst({
where: { feature: quota, type: FeatureKind.Quota },
select: { id: true },
orderBy: { version: 'desc' },
Expand All @@ -38,9 +40,7 @@ export class QuotaService {
const quota = await this.prisma.userFeature.findFirst({
where: {
userId,
feature: {
type: FeatureKind.Quota,
},
feature: { type: FeatureKind.Quota },
activated: true,
},
select: {
Expand All @@ -65,9 +65,7 @@ export class QuotaService {
const quotas = await this.prisma.userFeature.findMany({
where: {
userId,
feature: {
type: FeatureKind.Quota,
},
feature: { type: FeatureKind.Quota },
},
select: {
activated: true,
Expand All @@ -76,9 +74,7 @@ export class QuotaService {
expiredAt: true,
featureId: true,
},
orderBy: {
id: 'asc',
},
orderBy: { id: 'asc' },
});
const configs = await Promise.all(
quotas.map(async quota => {
Expand Down Expand Up @@ -107,11 +103,7 @@ export class QuotaService {
) {
await this.prisma.$transaction(async tx => {
const hasSameActivatedQuota = await this.hasUserQuota(userId, quota, tx);

if (hasSameActivatedQuota) {
// don't need to switch
return;
}
if (hasSameActivatedQuota) return; // don't need to switch

const featureId = await tx.feature
.findFirst({
Expand Down Expand Up @@ -175,12 +167,11 @@ export class QuotaService {
const quota = await this.prisma.workspaceFeature.findFirst({
where: {
workspaceId,
feature: {
type: FeatureKind.Quota,
},
feature: { type: FeatureKind.Quota },
activated: true,
},
select: {
configs: true,
reason: true,
createdAt: true,
expiredAt: true,
Expand All @@ -190,7 +181,7 @@ export class QuotaService {

if (quota) {
const feature = await QuotaConfig.get(this.prisma, quota.featureId);
return { ...quota, feature };
return { ...quota, feature: feature.withOverride(quota.configs) };
}
return null;
}
Expand All @@ -209,24 +200,13 @@ export class QuotaService {
quota,
tx
);

if (hasSameActivatedQuota) {
// don't need to switch
return;
}
if (hasSameActivatedQuota) return; // don't need to switch

const featureId = await tx.feature
.findFirst({
where: {
feature: quota,
type: FeatureKind.Quota,
},
select: {
id: true,
},
orderBy: {
version: 'desc',
},
where: { feature: quota, type: FeatureKind.Quota },
select: { id: true },
orderBy: { version: 'desc' },
})
.then(f => f?.id);

Expand Down Expand Up @@ -294,18 +274,17 @@ export class QuotaService {
type: Q
): Promise<QuotaConfig | undefined> {
const quota = await this.getQuota(type);
const configs = await this.prisma.workspaceFeature
.findFirst({
where: {
workspaceId,
feature: { feature: type, type: FeatureKind.Feature },
activated: true,
},
select: { configs: true },
})
.then(q => q?.configs);

if (quota && configs) {
if (quota) {
const configs = await this.prisma.workspaceFeature
.findFirst({
where: {
workspaceId,
feature: { feature: type, type: FeatureKind.Feature },
activated: true,
},
select: { configs: true },
})
.then(q => q?.configs);
return quota.withOverride(configs);
}
return undefined;
Expand All @@ -314,7 +293,7 @@ export class QuotaService {
async updateWorkspaceConfig(
workspaceId: string,
quota: QuotaType,
configs: JsonObject
configs: any
) {
const current = await this.getWorkspaceConfig(workspaceId, quota);

Expand All @@ -327,7 +306,7 @@ export class QuotaService {
const r = await this.prisma.workspaceFeature.updateMany({
where: {
workspaceId,
feature: { feature: quota, type: FeatureKind.Feature },
feature: { feature: quota, type: FeatureKind.Quota },
activated: true,
},
data: { configs },
Expand Down
14 changes: 9 additions & 5 deletions packages/backend/server/src/core/quota/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { FeatureService, FeatureType } from '../features';
import { PermissionService } from '../permission';
import { WorkspaceBlobStorage } from '../storage';
import { OneGB } from './constant';
import { QuotaOverrideService } from './override';
import { QuotaConfig } from './quota';
import { QuotaService } from './service';
import { formatSize, Quota, type QuotaBusinessType, QuotaType } from './types';
Expand All @@ -17,8 +16,7 @@ export class QuotaManagementService {
private readonly feature: FeatureService,
private readonly quota: QuotaService,
private readonly permissions: PermissionService,
private readonly storage: WorkspaceBlobStorage,
private readonly override: QuotaOverrideService
private readonly storage: WorkspaceBlobStorage
) {}

async getUserQuota(userId: string) {
Expand Down Expand Up @@ -147,6 +145,12 @@ export class QuotaManagementService {
);
}

private async getWorkspaceQuota(userId: string, workspaceId: string) {
const workspaceQuota = await this.quota.getWorkspaceQuota(workspaceId);
if (workspaceQuota) return workspaceQuota;
return await this.quota.getUserQuota(userId);
}

// get workspace's owner quota and total size of used
// quota was apply to owner's account
async getWorkspaceUsage(workspaceId: string): Promise<QuotaBusinessType> {
Expand All @@ -164,7 +168,7 @@ export class QuotaManagementService {
copilotActionLimit,
humanReadable,
},
} = await this.quota.getUserQuota(owner.id);
} = await this.getWorkspaceQuota(owner.id, workspaceId);
// get all workspaces size of owner used
const usedSize = await this.getUserUsage(owner.id);
// relax restrictions if workspace has unlimited feature
Expand Down Expand Up @@ -192,7 +196,7 @@ export class QuotaManagementService {
return this.mergeUnlimitedQuota(quota);
}

return await this.override.overrideQuota(owner.id, workspaceId, quota);
return quota;
}

private mergeUnlimitedQuota(orig: QuotaBusinessType): QuotaBusinessType {
Expand Down

This file was deleted.

Loading

0 comments on commit e6f892c

Please sign in to comment.