mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
feat: link and unlink document to resource model
This commit is contained in:
@@ -3,7 +3,7 @@ import TenantModel from 'models/TenantModel';
|
||||
import ModelSetting from './ModelSetting';
|
||||
import ModelSearchable from './ModelSearchable';
|
||||
|
||||
export default class Attachment extends mixin(TenantModel, [
|
||||
export default class Document extends mixin(TenantModel, [
|
||||
ModelSetting,
|
||||
ModelSearchable,
|
||||
]) {
|
||||
44
packages/server/src/models/DocumentLink.ts
Normal file
44
packages/server/src/models/DocumentLink.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Model, mixin } from 'objection';
|
||||
import TenantModel from 'models/TenantModel';
|
||||
import ModelSetting from './ModelSetting';
|
||||
import ModelSearchable from './ModelSearchable';
|
||||
|
||||
export default class DocumentLink extends mixin(TenantModel, [
|
||||
ModelSetting,
|
||||
ModelSearchable,
|
||||
]) {
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'document_links';
|
||||
}
|
||||
|
||||
/**
|
||||
* Model timestamps.
|
||||
*/
|
||||
get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const Document = require('models/Document');
|
||||
|
||||
return {
|
||||
/**
|
||||
* Sale invoice associated entries.
|
||||
*/
|
||||
document: {
|
||||
relation: Model.HasOneRelation,
|
||||
modelClass: Document.default,
|
||||
join: {
|
||||
from: 'document_links.documentId',
|
||||
to: 'documents.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -410,6 +410,7 @@ export default class SaleInvoice extends mixin(TenantModel, [
|
||||
const Branch = require('models/Branch');
|
||||
const Account = require('models/Account');
|
||||
const TaxRateTransaction = require('models/TaxRateTransaction');
|
||||
const DocumentLink = require('models/DocumentLink');
|
||||
|
||||
return {
|
||||
/**
|
||||
@@ -523,6 +524,21 @@ export default class SaleInvoice extends mixin(TenantModel, [
|
||||
builder.where('reference_type', 'SaleInvoice');
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Invoice may has many attachments.
|
||||
*/
|
||||
attachments: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: DocumentLink.default,
|
||||
join: {
|
||||
from: 'sales_invoices.id',
|
||||
to: 'document_links.modelId',
|
||||
},
|
||||
filter: (builder) => {
|
||||
builder.where('modelRef', 'SaleInvoice');
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user