mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 21:30:31 +00:00
120 lines
2.6 KiB
JavaScript
120 lines
2.6 KiB
JavaScript
import { Model, mixin } from 'objection';
|
|
import TenantModel from 'models/TenantModel';
|
|
import { buildFilterQuery } from 'lib/ViewRolesBuilder';
|
|
import ItemSettings from './Item.Settings';
|
|
import ModelSetting from './ModelSetting';
|
|
|
|
export default class Item extends mixin(TenantModel, [ModelSetting]) {
|
|
/**
|
|
* Table name
|
|
*/
|
|
static get tableName() {
|
|
return 'items';
|
|
}
|
|
|
|
/**
|
|
* Model timestamps.
|
|
*/
|
|
get timestamps() {
|
|
return ['createdAt', 'updatedAt'];
|
|
}
|
|
|
|
/**
|
|
* Allows to mark model as resourceable to viewable and filterable.
|
|
*/
|
|
static get resourceable() {
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Model modifiers.
|
|
*/
|
|
static get modifiers() {
|
|
return {
|
|
sortBy(query, columnSort, sortDirection) {
|
|
query.orderBy(columnSort, sortDirection);
|
|
},
|
|
viewRolesBuilder(query, conditions, logicExpression) {
|
|
buildFilterQuery(Item.tableName, conditions, logicExpression)(query);
|
|
},
|
|
|
|
/**
|
|
* Inactive/Active mode.
|
|
*/
|
|
inactiveMode(query, active = false) {
|
|
query.where('items.active', !active);
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Relationship mapping.
|
|
*/
|
|
static get relationMappings() {
|
|
const Media = require('models/Media');
|
|
const Account = require('models/Account');
|
|
const ItemCategory = require('models/ItemCategory');
|
|
|
|
return {
|
|
/**
|
|
* Item may belongs to cateogory model.
|
|
*/
|
|
category: {
|
|
relation: Model.BelongsToOneRelation,
|
|
modelClass: ItemCategory.default,
|
|
join: {
|
|
from: 'items.categoryId',
|
|
to: 'items_categories.id',
|
|
},
|
|
},
|
|
|
|
costAccount: {
|
|
relation: Model.BelongsToOneRelation,
|
|
modelClass: Account.default,
|
|
join: {
|
|
from: 'items.costAccountId',
|
|
to: 'accounts.id',
|
|
},
|
|
},
|
|
|
|
sellAccount: {
|
|
relation: Model.BelongsToOneRelation,
|
|
modelClass: Account.default,
|
|
join: {
|
|
from: 'items.sellAccountId',
|
|
to: 'accounts.id',
|
|
},
|
|
},
|
|
|
|
inventoryAccount: {
|
|
relation: Model.BelongsToOneRelation,
|
|
modelClass: Account.default,
|
|
join: {
|
|
from: 'items.inventoryAccountId',
|
|
to: 'accounts.id',
|
|
},
|
|
},
|
|
|
|
media: {
|
|
relation: Model.ManyToManyRelation,
|
|
modelClass: Media.default,
|
|
join: {
|
|
from: 'items.id',
|
|
through: {
|
|
from: 'media_links.model_id',
|
|
to: 'media_links.media_id',
|
|
},
|
|
to: 'media.id',
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Model settings.
|
|
*/
|
|
static get meta() {
|
|
return ItemSettings;
|
|
}
|
|
}
|