Revert "feat(server): deprecated the subscription module."

This reverts commit 3b79ac66ae.
This commit is contained in:
Ahmed Bouhuolia
2024-04-13 11:05:53 +02:00
parent 4d616e9287
commit a39dcd00d5
37 changed files with 1445 additions and 7 deletions

View File

@@ -0,0 +1,129 @@
import { Model, mixin } from 'objection';
import moment from 'moment';
import SystemModel from '@/system/models/SystemModel';
export default class License extends SystemModel {
/**
* Table name.
*/
static get tableName() {
return 'subscription_licenses';
}
/**
* Timestamps columns.
*/
get timestamps() {
return ['createdAt', 'updatedAt'];
}
/**
* Model modifiers.
*/
static get modifiers() {
return {
// Filters active licenses.
filterActiveLicense(query) {
query.where('disabled_at', null);
query.where('used_at', null);
},
// Find license by its code or id.
findByCodeOrId(query, id, code) {
if (id) {
query.where('id', id);
}
if (code) {
query.where('license_code', code);
}
},
// Filters licenses list.
filter(builder, licensesFilter) {
if (licensesFilter.active) {
builder.modify('filterActiveLicense');
}
if (licensesFilter.disabled) {
builder.whereNot('disabled_at', null);
}
if (licensesFilter.used) {
builder.whereNot('used_at', null);
}
if (licensesFilter.sent) {
builder.whereNot('sent_at', null);
}
},
};
}
/**
* Relationship mapping.
*/
static get relationMappings() {
const Plan = require('system/models/Subscriptions/Plan');
return {
plan: {
relation: Model.BelongsToOneRelation,
modelClass: Plan.default,
join: {
from: 'subscription_licenses.planId',
to: 'subscriptions_plans.id',
},
},
};
}
/**
* Deletes the given license code from the storage.
* @param {string} licenseCode
* @return {Promise}
*/
static deleteLicense(licenseCode, viaAttribute = 'license_code') {
return this.query().where(viaAttribute, licenseCode).delete();
}
/**
* Marks the given license code as disabled on the storage.
* @param {string} licenseCode
* @return {Promise}
*/
static markLicenseAsDisabled(licenseCode, viaAttribute = 'license_code') {
return this.query().where(viaAttribute, licenseCode).patch({
disabled_at: moment().toMySqlDateTime(),
});
}
/**
* Marks the given license code as sent on the storage.
* @param {string} licenseCode
*/
static markLicenseAsSent(licenseCode, viaAttribute = 'license_code') {
return this.query().where(viaAttribute, licenseCode).patch({
sent_at: moment().toMySqlDateTime(),
});
}
/**
* Marks the given license code as used on the storage.
* @param {string} licenseCode
* @return {Promise}
*/
static markLicenseAsUsed(licenseCode, viaAttribute = 'license_code') {
return this.query().where(viaAttribute, licenseCode).patch({
used_at: moment().toMySqlDateTime(),
});
}
/**
*
* @param {IIPlan} plan
* @return {boolean}
*/
isEqualPlanPeriod(plan) {
return (
this.invoicePeriod === plan.invoiceInterval &&
license.licensePeriod === license.periodInterval
);
}
}

View File

@@ -0,0 +1,82 @@
import { Model, mixin } from 'objection';
import SystemModel from '@/system/models/SystemModel';
import { PlanSubscription } from '..';
export default class Plan extends mixin(SystemModel) {
/**
* 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;
}
}

View File

@@ -0,0 +1,36 @@
import { Model, mixin } from 'objection';
import SystemModel from '@/system/models/SystemModel';
export default class PlanFeature extends mixin(SystemModel) {
/**
* Table name.
*/
static get tableName() {
return 'subscriptions.plan_features';
}
/**
* Timestamps columns.
*/
static get timestamps() {
return ['createdAt', 'updatedAt'];
}
/**
* Relationship mapping.
*/
static get relationMappings() {
const Plan = require('system/models/Subscriptions/Plan');
return {
plan: {
relation: Model.BelongsToOneRelation,
modelClass: Plan.default,
join: {
from: 'subscriptions.plan_features.planId',
to: 'subscriptions.plans.id',
},
},
};
}
}

View File

@@ -0,0 +1,164 @@
import { Model, mixin } from 'objection';
import SystemModel from '@/system/models/SystemModel';
import moment from 'moment';
import SubscriptionPeriod from '@/services/Subscription/SubscriptionPeriod';
export default class PlanSubscription extends mixin(SystemModel) {
/**
* Table name.
*/
static get tableName() {
return 'subscription_plan_subscriptions';
}
/**
* Timestamps columns.
*/
get timestamps() {
return ['createdAt', 'updatedAt'];
}
/**
* Defined virtual attributes.
*/
static get virtualAttributes() {
return ['active', 'inactive', 'ended', 'onTrial'];
}
/**
* Modifiers queries.
*/
static get modifiers() {
return {
activeSubscriptions(builder) {
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
const now = moment().format(dateFormat);
builder.where('ends_at', '>', now);
builder.where('trial_ends_at', '>', now);
},
inactiveSubscriptions() {
builder.modify('endedTrial');
builder.modify('endedPeriod');
},
subscriptionBySlug(builder, subscriptionSlug) {
builder.where('slug', subscriptionSlug);
},
endedTrial(builder) {
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
const endDate = moment().format(dateFormat);
builder.where('ends_at', '<=', endDate);
},
endedPeriod(builder) {
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
const endDate = moment().format(dateFormat);
builder.where('trial_ends_at', '<=', endDate);
},
};
}
/**
* Relations mappings.
*/
static get relationMappings() {
const Tenant = require('system/models/Tenant');
const Plan = require('system/models/Subscriptions/Plan');
return {
/**
* Plan subscription belongs to tenant.
*/
tenant: {
relation: Model.BelongsToOneRelation,
modelClass: Tenant.default,
join: {
from: 'subscription_plan_subscriptions.tenantId',
to: 'tenants.id',
},
},
/**
* Plan description belongs to plan.
*/
plan: {
relation: Model.BelongsToOneRelation,
modelClass: Plan.default,
join: {
from: 'subscription_plan_subscriptions.planId',
to: 'subscription_plans.id',
},
},
};
}
/**
* Check if subscription is active.
* @return {Boolean}
*/
active() {
return !this.ended() || this.onTrial();
}
/**
* Check if subscription is inactive.
* @return {Boolean}
*/
inactive() {
return !this.active();
}
/**
* Check if subscription period has ended.
* @return {Boolean}
*/
ended() {
return this.endsAt ? moment().isAfter(this.endsAt) : false;
}
/**
* Check if subscription is currently on trial.
* @return {Boolean}
*/
onTrial() {
return this.trailEndsAt ? moment().isAfter(this.trailEndsAt) : false;
}
/**
* Set new period from the given details.
* @param {string} invoiceInterval
* @param {number} invoicePeriod
* @param {string} start
*
* @return {Object}
*/
static setNewPeriod(invoiceInterval, invoicePeriod, start) {
const period = new SubscriptionPeriod(
invoiceInterval,
invoicePeriod,
start,
);
const startsAt = period.getStartDate();
const endsAt = period.getEndDate();
return { startsAt, endsAt };
}
/**
* Renews subscription period.
* @Promise
*/
renew(invoiceInterval, invoicePeriod) {
const { startsAt, endsAt } = PlanSubscription.setNewPeriod(
invoiceInterval,
invoicePeriod,
);
return this.$query().update({ startsAt, endsAt });
}
}

View File

@@ -1,8 +1,10 @@
import moment from 'moment';
import { Model } from 'objection';
import uniqid from 'uniqid';
import SubscriptionPeriod from '@/services/Subscription/SubscriptionPeriod';
import BaseModel from 'models/Model';
import TenantMetadata from './TenantMetadata';
import PlanSubscription from './Subscriptions/PlanSubscription';
export default class Tenant extends BaseModel {
upgradeJobId: string;
@@ -57,13 +59,33 @@ export default class Tenant extends BaseModel {
return !!this.upgradeJobId;
}
/**
* Query modifiers.
*/
static modifiers() {
return {
subscriptions(builder) {
builder.withGraphFetched('subscriptions');
},
};
}
/**
* Relations mappings.
*/
static get relationMappings() {
const PlanSubscription = require('./Subscriptions/PlanSubscription');
const TenantMetadata = require('./TenantMetadata');
return {
subscriptions: {
relation: Model.HasManyRelation,
modelClass: PlanSubscription.default,
join: {
from: 'tenants.id',
to: 'subscription_plan_subscriptions.tenantId',
},
},
metadata: {
relation: Model.HasOneRelation,
modelClass: TenantMetadata.default,
@@ -125,9 +147,9 @@ export default class Tenant extends BaseModel {
/**
* Marks the given tenant as upgrading.
* @param {number} tenantId
* @param {string} upgradeJobId
* @returns
* @param {number} tenantId
* @param {string} upgradeJobId
* @returns
*/
static markAsUpgrading(tenantId, upgradeJobId) {
return this.query().update({ upgradeJobId }).where({ id: tenantId });
@@ -135,8 +157,8 @@ export default class Tenant extends BaseModel {
/**
* Markes the given tenant as upgraded.
* @param {number} tenantId
* @returns
* @param {number} tenantId
* @returns
*/
static markAsUpgraded(tenantId) {
return this.query().update({ upgradeJobId: null }).where({ id: tenantId });

View File

@@ -1,3 +1,7 @@
import Plan from './Subscriptions/Plan';
import PlanFeature from './Subscriptions/PlanFeature';
import PlanSubscription from './Subscriptions/PlanSubscription';
import License from './Subscriptions/License';
import Tenant from './Tenant';
import TenantMetadata from './TenantMetadata';
import SystemUser from './SystemUser';
@@ -7,6 +11,10 @@ import SystemPlaidItem from './SystemPlaidItem';
import { Import } from './Import';
export {
Plan,
PlanFeature,
PlanSubscription,
License,
Tenant,
TenantMetadata,
SystemUser,