Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

feat: add quota for old users (#5318)

DarkSkye0d328676d4e
diff --git a/packages/backend/server/src/data/migrations/1702620653283-old-user-feature.ts b/packages/backend/server/src/data/migrations/1702620653283-old-user-feature.ts
new file mode 100644
index 0000000000..330b33f433
--- /dev/null
+++ b/packages/backend/server/src/data/migrations/1702620653283-old-user-feature.ts
@@ -0,0 +1,37 @@
+import { QuotaType } from '../../modules/quota/types';
+import { PrismaService } from '../../prisma';
+
+export class OldUserFeature1702620653283 {
+ // do the migration
+ static async up(db: PrismaService) {
+ await db.$transaction(async tx => {
+ const latestFreePlan = await tx.features.findFirstOrThrow({
+ where: { feature: QuotaType.FreePlanV1 },
+ orderBy: { version: 'desc' },
+ select: { id: true },
+ });
+
+ // find all users that don't have any features
+ const userIds = await db.user.findMany({
+ where: { NOT: { features: { some: { NOT: { id: { gt: 0 } } } } } },
+ select: { id: true },
+ });
+ console.log(`migrating ${userIds.join('|')} users`);
+
+ await tx.userFeatures.createMany({
+ data: userIds.map(({ id: userId }) => ({
+ userId,
+ featureId: latestFreePlan.id,
+ reason: 'old user feature migration',
+ activated: true,
+ })),
+ });
+ });
+ }
+
+ // revert the migration
+ // WARN: this will drop all user features
+ static async down(db: PrismaService) {
+ await db.userFeatures.deleteMany({});
+ }
+}
diff --git a/packages/backend/server/src/modules/quota/service.ts b/packages/backend/server/src/modules/quota/service.ts
index b20cc3a455..cfafbbc3f0 100644
--- a/packages/backend/server/src/modules/quota/service.ts
+++ b/packages/backend/server/src/modules/quota/service.ts
@@ -83,9 +83,9 @@ export class QuotaService {
expiredAt?: Date
) {
await this.prisma.$transaction(async tx => {
- const latestFreePlan = await tx.features.aggregate({
+ const latestPlanVersion = await tx.features.aggregate({
where: {
- feature: QuotaType.FreePlanV1,
+ feature: quota,
},
_max: {
version: true,
@@ -117,7 +117,7 @@ export class QuotaService {
connect: {
feature_version: {
feature: quota,
- version: latestFreePlan._max.version || 1,
+ version: latestPlanVersion._max.version || 1,
},
type: FeatureKind.Quota,
},