mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
feat(server): align pro plan for free in selfhost (#9973)
close AF-2099
This commit is contained in:
@@ -29,44 +29,6 @@ export class FeatureModel extends BaseModel {
|
||||
};
|
||||
}
|
||||
|
||||
@Transactional()
|
||||
async upsert<T extends FeatureName>(
|
||||
name: T,
|
||||
configs: FeatureConfig<T>,
|
||||
deprecatedType: FeatureType,
|
||||
deprecatedVersion: number
|
||||
) {
|
||||
const parsedConfigs = this.check(name, configs);
|
||||
|
||||
// TODO(@forehalo):
|
||||
// could be a simple upsert operation, but we got useless `version` column in the database
|
||||
// will be fixed when `version` column gets deprecated
|
||||
const latest = await this.try_get_unchecked(name);
|
||||
|
||||
let feature: Feature;
|
||||
if (!latest) {
|
||||
feature = await this.db.feature.create({
|
||||
data: {
|
||||
name,
|
||||
deprecatedType,
|
||||
deprecatedVersion,
|
||||
configs: parsedConfigs,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
feature = await this.db.feature.update({
|
||||
where: { id: latest.id },
|
||||
data: {
|
||||
configs: parsedConfigs,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
this.logger.verbose(`Feature ${name} upserted`);
|
||||
|
||||
return feature as Feature & { configs: FeatureConfig<T> };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest feature from database.
|
||||
*
|
||||
@@ -121,11 +83,66 @@ export class FeatureModel extends BaseModel {
|
||||
return FeatureConfigs[name].type;
|
||||
}
|
||||
|
||||
@Transactional()
|
||||
private async upsert<T extends FeatureName>(
|
||||
name: T,
|
||||
configs: FeatureConfig<T>,
|
||||
deprecatedType: FeatureType,
|
||||
deprecatedVersion: number
|
||||
) {
|
||||
const parsedConfigs = this.check(name, configs);
|
||||
|
||||
// TODO(@forehalo):
|
||||
// could be a simple upsert operation, but we got useless `version` column in the database
|
||||
// will be fixed when `version` column gets deprecated
|
||||
const latest = await this.db.feature.findFirst({
|
||||
where: {
|
||||
name,
|
||||
},
|
||||
orderBy: {
|
||||
deprecatedVersion: 'desc',
|
||||
},
|
||||
});
|
||||
|
||||
let feature: Feature;
|
||||
if (!latest) {
|
||||
feature = await this.db.feature.create({
|
||||
data: {
|
||||
name,
|
||||
deprecatedType,
|
||||
deprecatedVersion,
|
||||
configs: parsedConfigs,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
feature = await this.db.feature.update({
|
||||
where: { id: latest.id },
|
||||
data: {
|
||||
configs: parsedConfigs,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
this.logger.verbose(`Feature ${name} upserted`);
|
||||
|
||||
return feature as Feature & { configs: FeatureConfig<T> };
|
||||
}
|
||||
|
||||
async refreshFeatures() {
|
||||
for (const key in FeatureConfigs) {
|
||||
const name = key as FeatureName;
|
||||
const def = FeatureConfigs[name];
|
||||
await this.upsert(name, def.configs, def.type, def.deprecatedVersion);
|
||||
// self-hosted instance will use pro plan as free plan
|
||||
if (name === 'free_plan_v1' && this.config.isSelfhosted) {
|
||||
await this.upsert(
|
||||
name,
|
||||
FeatureConfigs['pro_plan_v1'].configs,
|
||||
def.type,
|
||||
def.deprecatedVersion
|
||||
);
|
||||
} else {
|
||||
await this.upsert(name, def.configs, def.type, def.deprecatedVersion);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user