Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: self hosted register #6581

Draft
wants to merge 1 commit into
base: canary
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -155,6 +155,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 @@ -17,6 +17,7 @@ import { ByteUnit, OneDay, OneKB } from './constant';
export enum QuotaType {
FreePlanV1 = 'free_plan_v1',
ProPlanV1 = 'pro_plan_v1',
UnlimitedPlanV1 = 'unlimited_plan_v1',
// only for test, smaller quota
RestrictedPlanV1 = 'restricted_plan_v1',
}
Expand All @@ -26,6 +27,7 @@ const quotaPlan = z.object({
QuotaType.FreePlanV1,
QuotaType.ProPlanV1,
QuotaType.RestrictedPlanV1,
QuotaType.UnlimitedPlanV1,
]),
configs: z.object({
name: z.string(),
Expand Down
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
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)) || {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some selfhosted server have more than one user, we should let all users use this plan

if (id) {
await upgradeLatestQuotaVersion(db, QuotaType.UnlimitedPlanV1, '');
await quota.switchUserQuota(id, QuotaType.UnlimitedPlanV1);
}
}
}

// revert the migration
static async down(_db: PrismaClient) {}
}