Skip to content

Commit

Permalink
fix: self hosted register
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Jul 8, 2024
1 parent de40844 commit 908cd18
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/backend/server/src/core/quota/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,23 @@ export const Quotas: Quota[] = [
copilotActionLimit: 10,
},
},
{
feature: QuotaType.UnlimitedPlanV1,
type: FeatureKind.Quota,
version: 1,
configs: {
// quota name
name: 'Unlimited',
// single blob limit 10MB
blobLimit: 100 * OneMB,
// total blob limit 10GB
storageQuota: 1000 * OneGB,
// history period of validity 7 days
historyPeriod: 7 * OneDay,
// member limit 3
memberLimit: 10,
},
},
];

export function getLatestQuota(type: QuotaType) {
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/server/src/core/quota/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum QuotaType {
FreePlanV1 = 'free_plan_v1',
ProPlanV1 = 'pro_plan_v1',
LifetimeProPlanV1 = 'lifetime_pro_plan_v1',
UnlimitedPlanV1 = 'unlimited_plan_v1',
// only for test, smaller quota
RestrictedPlanV1 = 'restricted_plan_v1',
}
Expand All @@ -28,6 +29,7 @@ const quotaPlan = z.object({
QuotaType.ProPlanV1,
QuotaType.LifetimeProPlanV1,
QuotaType.RestrictedPlanV1,
QuotaType.UnlimitedPlanV1,
]),
configs: z.object({
name: z.string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ModuleRef } from '@nestjs/core';
import { PrismaClient } from '@prisma/client';

import { QuotaService, QuotaType } from '../../core/quota';
import { UserService } from '../../core/user';
import { Config } from '../../fundamentals';
import { upgradeLatestQuotaVersion } from './utils/user-quotas';

export class RefreshNewPlan1713258139569 {
// do the migration
static async up(db: PrismaClient, ref: ModuleRef) {
const config = ref.get(Config, { strict: false });
const { AFFINE_ADMIN_EMAIL } = process.env;
if (config.isSelfhosted && AFFINE_ADMIN_EMAIL) {
const user = ref.get(UserService, { strict: false });
const quota = ref.get(QuotaService, { strict: false });
const { id } = (await user.findUserByEmail(AFFINE_ADMIN_EMAIL)) || {};
if (id) {
await upgradeLatestQuotaVersion(db, QuotaType.UnlimitedPlanV1, '');
await quota.switchUserQuota(id, QuotaType.UnlimitedPlanV1);
}
}
}

// revert the migration
static async down(_db: PrismaClient) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ModuleRef } from '@nestjs/core';
import { PrismaClient } from '@prisma/client';

import { FeatureManagementService } from '../../core/features';
import { QuotaService, QuotaType } from '../../core/quota';
import { UserService } from '../../core/user';
import { Config, CryptoHelper } from '../../fundamentals';

Expand All @@ -13,6 +14,7 @@ export class SelfHostAdmin1 {
const crypto = ref.get(CryptoHelper, { strict: false });
const user = ref.get(UserService, { strict: false });
const feature = ref.get(FeatureManagementService, { strict: false });
const quota = ref.get(QuotaService, { strict: false });
if (
!process.env.AFFINE_ADMIN_EMAIL ||
!process.env.AFFINE_ADMIN_PASSWORD
Expand All @@ -37,6 +39,7 @@ export class SelfHostAdmin1 {
});
if (firstUser) {
await feature.addAdmin(firstUser.id);
await quota.switchUserQuota(firstUser.id, QuotaType.UnlimitedPlanV1);
}
}
}
Expand Down

0 comments on commit 908cd18

Please sign in to comment.