fix: upgrade plan correctly (#6543)

This commit is contained in:
darkskygit
2024-04-15 06:20:23 +00:00
parent 1217ef258b
commit 1a3d1a5421
2 changed files with 21 additions and 6 deletions

View File

@@ -1,14 +1,17 @@
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import { Quotas } from '../../core/quota'; import { QuotaType } from '../../core/quota/types';
import { upgradeQuotaVersion } from './utils/user-quotas'; import { upgradeLatestQuotaVersion } from './utils/user-quotas';
export class RefreshFreePlan1712224382221 { export class RefreshFreePlan1712224382221 {
// do the migration // do the migration
static async up(db: PrismaClient) { static async up(db: PrismaClient) {
// free plan 1.0 // free plan 1.1
const quota = Quotas[4]; await upgradeLatestQuotaVersion(
await upgradeQuotaVersion(db, quota, 'free plan 1.1 migration'); db,
QuotaType.FreePlanV1,
'free plan 1.1 migration'
);
} }
// revert the migration // revert the migration

View File

@@ -1,7 +1,8 @@
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import { FeatureKind } from '../../../core/features'; import { FeatureKind } from '../../../core/features';
import { Quota } from '../../../core/quota/types'; import { Quotas } from '../../../core/quota/schema';
import { Quota, QuotaType } from '../../../core/quota/types';
import { upsertFeature } from './user-features'; import { upsertFeature } from './user-features';
export async function upgradeQuotaVersion( export async function upgradeQuotaVersion(
@@ -63,3 +64,14 @@ export async function upgradeQuotaVersion(
}); });
}); });
} }
export async function upgradeLatestQuotaVersion(
db: PrismaClient,
type: QuotaType,
reason: string
) {
const quota = Quotas.filter(f => f.feature === type);
quota.sort((a, b) => b.version - a.version);
const latestQuota = quota[0];
await upgradeQuotaVersion(db, latestQuota, reason);
}