fix: system repositories.

This commit is contained in:
a.bouhuolia
2020-12-17 17:19:16 +02:00
parent 7a847fc895
commit a67b1fbdd0
54 changed files with 1452 additions and 983 deletions

View File

@@ -1,24 +1,25 @@
import { Account } from 'models';
import TenantRepository from 'repositories/TenantRepository';
import { IAccount } from 'interfaces';
export default class AccountRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = Account;
get model() {
return Account.bindKnex(this.knex);
}
/**
* Retrieve accounts dependency graph.
* @returns {}
*/
async getDependencyGraph(withRelation) {
const accounts = await this.all(withRelation);
async getDependencyGraph(withRelation) {
const cacheKey = this.getCacheKey('accounts.depGraph', withRelation);
return this.cache.get(cacheKey, async () => {
const accounts = await this.all(withRelation);
return this.model.toDependencyGraph(accounts);
});
}
@@ -35,4 +36,50 @@ export default class AccountRepository extends TenantRepository {
await this.model.query().where('id', accountId)[method]('amount', amount);
this.flushCache();
}
/**
* Activate user by the given id.
* @param {number} userId - User id.
* @return {Promise<void>}
*/
activateById(userId: number): Promise<IAccount> {
return super.update({ active: 1 }, { id: userId });
}
/**
* Inactivate user by the given id.
* @param {number} userId - User id.
* @return {Promise<void>}
*/
inactivateById(userId: number): Promise<void> {
return super.update({ active: 0 }, { id: userId });
}
/**
* Activate user by the given id.
* @param {number} userId - User id.
* @return {Promise<void>}
*/
async activateByIds(userIds: number[]): Promise<IAccount> {
const results = await this.model.query()
.whereIn('id', userIds)
.patch({ active: true });
this.flushCache();
return results;
}
/**
* Inactivate user by the given id.
* @param {number} userId - User id.
* @return {Promise<void>}
*/
async inactivateByIds(userIds: number[]): Promise<IAccount> {
const results = await this.model.query()
.whereIn('id', userIds)
.patch({ active: false });
this.flushCache();
return results;
}
}

View File

@@ -15,11 +15,10 @@ interface IJournalTransactionsFilter {
export default class AccountTransactionsRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = AccountTransaction;
get model() {
return AccountTransaction.bindKnex(this.knex);
}
journal(filter: IJournalTransactionsFilter) {

View File

@@ -4,11 +4,10 @@ import { AccountType } from 'models';
export default class AccountTypeRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = AccountType;
get model() {
return AccountType.bindKnex(this.knex);
}
/**

View File

@@ -3,10 +3,9 @@ import TenantRepository from 'repositories/TenantRepository';
export default class BillRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = Bill;
get model() {
return Bill.bindKnex(this.knex);
}
}

View File

@@ -13,6 +13,7 @@ export default class CachableRepository extends EntityRepository{
constructor(knex, cache) {
super(knex);
this.cache = cache;
this.repositoryName = this.constructor.name;
}
/**

View File

@@ -4,10 +4,9 @@ import { Contact } from 'models'
export default class ContactRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = Contact;
get model() {
return Contact.bindKnex(this.knex);
}
}

View File

@@ -1,12 +1,20 @@
import TenantRepository from "./TenantRepository";
import { Customer } from 'models'
import { Customer } from 'models';
export default class CustomerRepository extends TenantRepository {
/**
* Constructor method.
* Contact repository.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = Customer;
this.repositoryName = 'ContactRepository';
}
/**
* Gets the repository's model.
*/
get model() {
return Customer.bindKnex(this.knex);
}
changeBalance(vendorId: number, amount: number) {

View File

@@ -2,7 +2,6 @@ import { cloneDeep, cloneDeepWith, forOwn, isString } from 'lodash';
import ModelEntityNotFound from 'exceptions/ModelEntityNotFound';
export default class EntityRepository {
modelInstance: any;
idColumn: string;
knex: any;
@@ -15,20 +14,11 @@ export default class EntityRepository {
this.idColumn = 'id';
}
/**
* Sets the model to the repository and bind it to knex instance.
*/
set model(model) {
if (!this.modelInstance) {
this.modelInstance = model.bindKnex(this.knex);
}
}
/**
* Retrieve the repository model binded it to knex instance.
*/
get model() {
return this.modelInstance;
throw new Error("The repository's model is not defined.");
}
/**

View File

@@ -3,10 +3,9 @@ import { ExpenseCategory } from 'models';
export default class ExpenseEntyRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = ExpenseCategory;
get model() {
return ExpenseCategory.bindKnex(this.knex);
}
}

View File

@@ -4,19 +4,18 @@ import { Expense } from 'models';
export default class ExpenseRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = Expense;
get model() {
return Expense.bindKnex(this.knex);
}
/**
* Publish the given expense.
* @param {number} expenseId
*/
async publish(expenseId: number): Promise<void> {
super.update({
publish(expenseId: number): Promise<void> {
return super.update({
id: expenseId,
publishedAt: moment().toMySqlDateTime(),
});

View File

@@ -4,10 +4,9 @@ import TenantRepository from "./TenantRepository";
export default class ItemRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = Item;
get model() {
return Item.bindKnex(this.knex);
}
}

View File

@@ -3,10 +3,9 @@ import TenantRepository from 'repositories/TenantRepository';
export default class JournalRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = ManualJournal;
get model() {
return ManualJournal.bindKnex(this.knex);
}
}

View File

@@ -2,11 +2,10 @@ import { PaymentReceiveEntry } from 'models';
import TenantRepository from 'repositories/TenantRepository';
export default class PaymentReceiveEntryRepository extends TenantRepository {
/**
* Constructor method.
/**
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = PaymentReceiveEntry;
get model() {
return PaymentReceiveEntry.bindKnex(this.knex);
}
}

View File

@@ -3,10 +3,9 @@ import TenantRepository from 'repositories/TenantRepository';
export default class PaymentReceiveRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = PaymentReceive;
get model() {
return PaymentReceive.bindKnex(this.knex);
}
}

View File

@@ -3,10 +3,9 @@ import TenantRepository from 'repositories/TenantRepository';
export default class SaleInvoiceRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = SaleInvoice;
}
get model() {
return SaleInvoice.bindKnex(this.knex);
}
}

View File

@@ -3,10 +3,9 @@ import Setting from 'models/Setting';
export default class SettingRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = Setting;
get model() {
return Setting.bindKnex(this.knex);
}
}

View File

@@ -11,6 +11,5 @@ export default class TenantRepository extends CachableRepository {
*/
constructor(knex, cache) {
super(knex, cache);
this.repositoryName = this.constructor.name;
}
}

View File

@@ -3,11 +3,18 @@ import TenantRepository from "./TenantRepository";
export default class VendorRepository extends TenantRepository {
/**
* Constructor method.
* Contact repository.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = Vendor;
this.repositoryName = 'ContactRepository';
}
/**
* Gets the repository's model.
*/
get model() {
return Vendor.bindKnex(this.knex);
}
changeBalance(vendorId: number, amount: number) {

View File

@@ -3,11 +3,10 @@ import TenantRepository from 'repositories/TenantRepository';
export default class ViewRepository extends TenantRepository {
/**
* Constructor method.
* Gets the repository's model.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = View;
get model() {
return View.bindKnex(this.knex);
}
/**