refactor: User permission and limits to run flows

This commit is contained in:
Faruk AYDIN
2023-04-10 17:21:19 +02:00
parent 77e29050c8
commit 9cb4607f69
6 changed files with 55 additions and 72 deletions

View File

@@ -165,18 +165,24 @@ class User extends Base {
this.trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
}
async hasActiveSubscription() {
if (!appConfig.isCloud) {
return false;
async isAllowedToRunFlows() {
if (appConfig.isSelfHosted) {
return true;
}
const subscription = await this.$relatedQuery('currentSubscription');
if (await this.inTrial()) {
return true;
}
return subscription?.isActive;
if ((await this.hasActiveSubscription()) && (await this.withinLimits())) {
return true;
}
return false;
}
async inTrial() {
if (!appConfig.isCloud) {
if (appConfig.isSelfHosted) {
return false;
}
@@ -196,6 +202,24 @@ class User extends Base {
return now < expiryDate;
}
async hasActiveSubscription() {
if (!appConfig.isCloud) {
return false;
}
const subscription = await this.$relatedQuery('currentSubscription');
return subscription?.isActive;
}
async withinLimits() {
const currentSubscription = await this.$relatedQuery('currentSubscription');
const plan = currentSubscription.plan;
const currentUsageData = await this.$relatedQuery('currentUsageData');
return currentUsageData.consumedTaskCount >= plan.quota;
}
async $beforeInsert(queryContext: QueryContext) {
await super.$beforeInsert(queryContext);
await this.generateHash();