mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 04:10:32 +00:00
87 lines
1.6 KiB
TypeScript
87 lines
1.6 KiB
TypeScript
import { Model, mixin } from 'objection';
|
|
import SystemModel from '@/system/models/SystemModel';
|
|
import { PlanSubscription } from '..';
|
|
|
|
export default class Plan extends mixin(SystemModel) {
|
|
price: number;
|
|
invoiceInternal: number;
|
|
invoicePeriod: string;
|
|
|
|
/**
|
|
* Table name.
|
|
*/
|
|
static get tableName() {
|
|
return 'subscription_plans';
|
|
}
|
|
|
|
/**
|
|
* Timestamps columns.
|
|
*/
|
|
get timestamps() {
|
|
return ['createdAt', 'updatedAt'];
|
|
}
|
|
|
|
/**
|
|
* Defined virtual attributes.
|
|
*/
|
|
static get virtualAttributes() {
|
|
return ['isFree', 'hasTrial'];
|
|
}
|
|
|
|
/**
|
|
* Model modifiers.
|
|
*/
|
|
static get modifiers() {
|
|
return {
|
|
getFeatureBySlug(builder, featureSlug) {
|
|
builder.where('slug', featureSlug);
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Relationship mapping.
|
|
*/
|
|
static get relationMappings() {
|
|
const PlanSubscription = require('system/models/Subscriptions/PlanSubscription');
|
|
|
|
return {
|
|
/**
|
|
* The plan may have many subscriptions.
|
|
*/
|
|
subscriptions: {
|
|
relation: Model.HasManyRelation,
|
|
modelClass: PlanSubscription.default,
|
|
join: {
|
|
from: 'subscription_plans.id',
|
|
to: 'subscription_plan_subscriptions.planId',
|
|
},
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Check if plan is free.
|
|
* @return {boolean}
|
|
*/
|
|
isFree() {
|
|
return this.price <= 0;
|
|
}
|
|
|
|
/**
|
|
* Check if plan is paid.
|
|
* @return {boolean}
|
|
*/
|
|
isPaid() {
|
|
return !this.isFree();
|
|
}
|
|
|
|
/**
|
|
* Check if plan has trial.
|
|
* @return {boolean}
|
|
*/
|
|
hasTrial() {
|
|
return this.trialPeriod && this.trialInterval;
|
|
}
|
|
}
|