mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 23:00:34 +00:00
feat: organization metadata redesigned.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import BaseModel from 'models/Model';
|
||||
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';
|
||||
|
||||
export default class Tenant extends BaseModel {
|
||||
/**
|
||||
@@ -21,7 +24,7 @@ export default class Tenant extends BaseModel {
|
||||
* Virtual attributes.
|
||||
*/
|
||||
static get virtualAttributes() {
|
||||
return ['isReady'];
|
||||
return ['isReady', 'isBuildRunning'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,6 +34,13 @@ export default class Tenant extends BaseModel {
|
||||
return !!(this.initializedAt && this.seededAt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detarimes the tenant whether is build currently running.
|
||||
*/
|
||||
get isBuildRunning() {
|
||||
return !!this.buildJobId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query modifiers.
|
||||
*/
|
||||
@@ -47,6 +57,7 @@ export default class Tenant extends BaseModel {
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const PlanSubscription = require('./Subscriptions/PlanSubscription');
|
||||
const TenantMetadata = require('./TenantMetadata');
|
||||
|
||||
return {
|
||||
subscriptions: {
|
||||
@@ -55,9 +66,17 @@ export default class Tenant extends BaseModel {
|
||||
join: {
|
||||
from: 'tenants.id',
|
||||
to: 'subscription_plan_subscriptions.tenantId',
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
metadata: {
|
||||
relation: Model.HasOneRelation,
|
||||
modelClass: TenantMetadata.default,
|
||||
join: {
|
||||
from: 'tenants.id',
|
||||
to: 'tenants_metadata.tenantId',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,22 +90,90 @@ export default class Tenant extends BaseModel {
|
||||
|
||||
/**
|
||||
* Records a new subscription for the associated tenant.
|
||||
* @param {string} subscriptionSlug
|
||||
* @param {IPlan} plan
|
||||
*/
|
||||
newSubscription(subscriptionSlug, plan) {
|
||||
const trial = new SubscriptionPeriod(plan.trialInterval, plan.trialPeriod)
|
||||
const period = new SubscriptionPeriod(plan.invoiceInterval, plan.invoicePeriod, trial.getEndDate());
|
||||
const trial = new SubscriptionPeriod(plan.trialInterval, plan.trialPeriod);
|
||||
const period = new SubscriptionPeriod(
|
||||
plan.invoiceInterval,
|
||||
plan.invoicePeriod,
|
||||
trial.getEndDate()
|
||||
);
|
||||
|
||||
return this.$relatedQuery('subscriptions').insert({
|
||||
slug: subscriptionSlug,
|
||||
planId: plan.id,
|
||||
|
||||
trialStartedAt: trial.getStartDate(),
|
||||
trialEndsAt: trial.getEndDate(),
|
||||
|
||||
startsAt: period.getStartDate(),
|
||||
endsAt: period.getEndDate(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new tenant with random organization id.
|
||||
*/
|
||||
static createWithUniqueOrgId(uniqId) {
|
||||
const organizationId = uniqid() || uniqId;
|
||||
return this.query().insert({ organizationId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark as seeded.
|
||||
* @param {number} tenantId
|
||||
*/
|
||||
static markAsSeeded(tenantId) {
|
||||
const seededAt = moment().toMySqlDateTime();
|
||||
return this.query().update({ seededAt }).where({ id: tenantId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the the given organization as initialized.
|
||||
* @param {string} organizationId
|
||||
*/
|
||||
static markAsInitialized(tenantId) {
|
||||
const initializedAt = moment().toMySqlDateTime();
|
||||
return this.query().update({ initializedAt }).where({ id: tenantId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the given tenant as built.
|
||||
*/
|
||||
static markAsBuilt(tenantId) {
|
||||
const builtAt = moment().toMySqlDateTime();
|
||||
return this.query().update({ builtAt }).where({ id: tenantId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the given tenant as built.
|
||||
*/
|
||||
static markAsBuilding(tenantId, buildJobId) {
|
||||
return this.query().update({ buildJobId }).where({ id: tenantId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the given tenant as built.
|
||||
*/
|
||||
static markAsBuildCompleted(tenantId) {
|
||||
return this.query().update({ buildJobId: null }).where({ id: tenantId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the metadata of the given tenant.
|
||||
*/
|
||||
static async saveMetadata(tenantId, metadata) {
|
||||
const foundMetadata = await TenantMetadata.query().findOne({ tenantId });
|
||||
const updateOrInsert = foundMetadata ? 'update' : 'insert';
|
||||
|
||||
return TenantMetadata.query()
|
||||
[updateOrInsert]({
|
||||
tenantId,
|
||||
...metadata,
|
||||
})
|
||||
.where({ tenantId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the metadata of the tenant.
|
||||
*/
|
||||
saveMetadata(metadata) {
|
||||
return Tenant.saveMetadata(this.id, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user