feat: add subscription update and cancel logics
This commit is contained in:
@@ -67,7 +67,7 @@ class ExecutionStep extends Base {
|
||||
if (!execution.testRun && !this.isFailed) {
|
||||
const flow = await execution.$relatedQuery('flow');
|
||||
const user = await flow.$relatedQuery('user');
|
||||
const usageData = await user.$relatedQuery('usageData');
|
||||
const usageData = await user.$relatedQuery('currentUsageData');
|
||||
|
||||
await usageData.increaseConsumedTaskCountByOne();
|
||||
}
|
||||
|
@@ -136,7 +136,7 @@ class Flow extends Base {
|
||||
if (!appConfig.isCloud) return;
|
||||
|
||||
const user = await this.$relatedQuery('user');
|
||||
const usageData = await user.$relatedQuery('usageData');
|
||||
const usageData = await user.$relatedQuery('currentUsageData');
|
||||
|
||||
const hasExceeded = await usageData.checkIfLimitExceeded();
|
||||
|
||||
|
@@ -1,5 +1,7 @@
|
||||
import Base from './base';
|
||||
import User from './user';
|
||||
import UsageData from './usage-data.ee';
|
||||
import { getPlanById } from '../helpers/billing/plans.ee';
|
||||
|
||||
class Subscription extends Base {
|
||||
id!: string;
|
||||
@@ -12,6 +14,8 @@ class Subscription extends Base {
|
||||
nextBillAmount!: string;
|
||||
nextBillDate!: string;
|
||||
lastBillDate?: string;
|
||||
usageData?: UsageData[];
|
||||
currentUsageData?: UsageData;
|
||||
|
||||
static tableName = 'subscriptions';
|
||||
|
||||
@@ -51,7 +55,27 @@ class Subscription extends Base {
|
||||
to: 'users.id',
|
||||
},
|
||||
},
|
||||
usageData: {
|
||||
relation: Base.HasManyRelation,
|
||||
modelClass: UsageData,
|
||||
join: {
|
||||
from: 'subscriptions.id',
|
||||
to: 'usage_data.subscription_id',
|
||||
},
|
||||
},
|
||||
currentUsageData: {
|
||||
relation: Base.HasOneRelation,
|
||||
modelClass: UsageData,
|
||||
join: {
|
||||
from: 'subscriptions.id',
|
||||
to: 'usage_data.subscription_id',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
get plan() {
|
||||
return getPlanById(this.paddlePlanId);
|
||||
}
|
||||
}
|
||||
|
||||
export default Subscription;
|
||||
|
@@ -1,14 +1,16 @@
|
||||
import { raw } from 'objection';
|
||||
import Base from './base';
|
||||
import User from './user';
|
||||
import PaymentPlan from './payment-plan.ee';
|
||||
import Subscription from './subscription.ee';
|
||||
import { getPlanById } from '../helpers/billing/plans.ee';
|
||||
|
||||
class UsageData extends Base {
|
||||
id!: string;
|
||||
userId!: string;
|
||||
subscriptionId?: string;
|
||||
consumedTaskCount!: number;
|
||||
nextResetAt!: string;
|
||||
paymentPlan?: PaymentPlan;
|
||||
subscription?: Subscription;
|
||||
|
||||
static tableName = 'usage_data';
|
||||
|
||||
@@ -19,6 +21,7 @@ class UsageData extends Base {
|
||||
properties: {
|
||||
id: { type: 'string', format: 'uuid' },
|
||||
userId: { type: 'string', format: 'uuid' },
|
||||
subscriptionId: { type: 'string', format: 'uuid' },
|
||||
consumedTaskCount: { type: 'integer' },
|
||||
nextResetAt: { type: 'string' },
|
||||
},
|
||||
@@ -33,20 +36,22 @@ class UsageData extends Base {
|
||||
to: 'users.id',
|
||||
},
|
||||
},
|
||||
paymentPlan: {
|
||||
subscription: {
|
||||
relation: Base.BelongsToOneRelation,
|
||||
modelClass: PaymentPlan,
|
||||
modelClass: Subscription,
|
||||
join: {
|
||||
from: 'usage_data.user_id',
|
||||
to: 'payment_plans.user_id',
|
||||
from: 'usage_data.subscription_id',
|
||||
to: 'subscriptions.id',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
async checkIfLimitExceeded() {
|
||||
const paymentPlan = await this.$relatedQuery('paymentPlan');
|
||||
const subscription = await this.$relatedQuery('subscription');
|
||||
|
||||
return this.consumedTaskCount >= paymentPlan.taskCount;
|
||||
const plan = subscription.plan;
|
||||
|
||||
return this.consumedTaskCount >= plan.quota;
|
||||
}
|
||||
|
||||
async increaseConsumedTaskCountByOne() {
|
||||
|
@@ -1,15 +1,14 @@
|
||||
import { QueryContext, ModelOptions } from 'objection';
|
||||
import bcrypt from 'bcrypt';
|
||||
import crypto from 'crypto';
|
||||
import { DateTime } from 'luxon';
|
||||
import appConfig from '../config/app';
|
||||
import Base from './base';
|
||||
import ExtendedQueryBuilder from './query-builder';
|
||||
import Connection from './connection';
|
||||
import Flow from './flow';
|
||||
import Step from './step';
|
||||
import Execution from './execution';
|
||||
import ExecutionStep from './execution-step';
|
||||
import bcrypt from 'bcrypt';
|
||||
import crypto from 'crypto';
|
||||
import PaymentPlan from './payment-plan.ee';
|
||||
import UsageData from './usage-data.ee';
|
||||
import Subscription from './subscription.ee';
|
||||
|
||||
@@ -26,9 +25,10 @@ class User extends Base {
|
||||
flows?: Flow[];
|
||||
steps?: Step[];
|
||||
executions?: Execution[];
|
||||
paymentPlan?: PaymentPlan;
|
||||
usageData?: UsageData;
|
||||
subscription?: Subscription;
|
||||
usageData?: UsageData[];
|
||||
currentUsageData?: UsageData;
|
||||
subscriptions?: Subscription[];
|
||||
currentSubscription?: Subscription;
|
||||
|
||||
static tableName = 'users';
|
||||
|
||||
@@ -86,29 +86,43 @@ class User extends Base {
|
||||
to: 'executions.flow_id',
|
||||
},
|
||||
},
|
||||
paymentPlan: {
|
||||
relation: Base.HasOneRelation,
|
||||
modelClass: PaymentPlan,
|
||||
join: {
|
||||
from: 'payment_plans.user_id',
|
||||
to: 'users.id',
|
||||
},
|
||||
},
|
||||
usageData: {
|
||||
relation: Base.HasOneRelation,
|
||||
relation: Base.HasManyRelation,
|
||||
modelClass: UsageData,
|
||||
join: {
|
||||
from: 'usage_data.user_id',
|
||||
to: 'users.id',
|
||||
},
|
||||
},
|
||||
subscription: {
|
||||
currentUsageData: {
|
||||
relation: Base.HasOneRelation,
|
||||
modelClass: UsageData,
|
||||
join: {
|
||||
from: 'usage_data.user_id',
|
||||
to: 'users.id',
|
||||
},
|
||||
filter(builder: ExtendedQueryBuilder<UsageData>) {
|
||||
builder.orderBy('created_at', 'desc').first();
|
||||
},
|
||||
},
|
||||
subscriptions: {
|
||||
relation: Base.HasManyRelation,
|
||||
modelClass: Subscription,
|
||||
join: {
|
||||
from: 'subscriptions.user_id',
|
||||
to: 'users.id',
|
||||
},
|
||||
},
|
||||
currentSubscription: {
|
||||
relation: Base.HasOneRelation,
|
||||
modelClass: Subscription,
|
||||
join: {
|
||||
from: 'subscriptions.user_id',
|
||||
to: 'users.id',
|
||||
},
|
||||
filter(builder: ExtendedQueryBuilder<Subscription>) {
|
||||
builder.orderBy('created_at', 'desc').first();
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -167,6 +181,18 @@ class User extends Base {
|
||||
await this.generateHash();
|
||||
}
|
||||
}
|
||||
|
||||
async $afterInsert(queryContext: QueryContext) {
|
||||
await super.$afterInsert(queryContext);
|
||||
|
||||
if (appConfig.isCloud) {
|
||||
await this.$relatedQuery('usageData').insert({
|
||||
userId: this.id,
|
||||
consumedTaskCount: 0,
|
||||
nextResetAt: DateTime.now().plus({ days: 30 }).toISODate(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default User;
|
||||
|
Reference in New Issue
Block a user