feat: rewrite repositories with base entity repository class.

feat: sales and purchases status.
feat: sales and purchases auto-increment number.
fix: settings find query with extra columns.
This commit is contained in:
Ahmed Bouhuolia
2020-12-13 19:50:59 +02:00
parent e9e4ddaee0
commit 188e411f02
78 changed files with 1634 additions and 869 deletions

View File

@@ -1,124 +1,28 @@
import { Account } from 'models';
import TenantRepository from 'repositories/TenantRepository';
import { IAccount } from 'interfaces';
export default class AccountRepository extends TenantRepository {
/**
* Constructor method.
*/
constructor(knex, cache) {
super(knex, cache);
this.model = Account;
}
/**
* Retrieve accounts dependency graph.
* @returns {}
*/
async getDependencyGraph() {
const { Account } = this.models;
const accounts = await this.allAccounts();
const cacheKey = this.getCacheKey('accounts.depGraph');
async getDependencyGraph(withRelation) {
const accounts = await this.all(withRelation);
const cacheKey = this.getCacheKey('accounts.depGraph', withRelation);
return this.cache.get(cacheKey, async () => {
return Account.toDependencyGraph(accounts);
return this.model.toDependencyGraph(accounts);
});
}
/**
* Retrieve all accounts on the storage.
* @return {IAccount[]}
*/
allAccounts(withRelations?: string|string[]): IAccount[] {
const { Account } = this.models;
const cacheKey = this.getCacheKey('accounts.depGraph', withRelations);
return this.cache.get(cacheKey, async () => {
return Account.query()
.withGraphFetched(withRelations);
});
}
/**
* Retrieve account of the given account slug.
* @param {string} slug
* @return {IAccount}
*/
getBySlug(slug: string): IAccount {
const { Account } = this.models;
const cacheKey = this.getCacheKey('accounts.slug', slug);
return this.cache.get(cacheKey, () => {
return Account.query().findOne('slug', slug);
});
}
/**
* Retrieve the account by the given id.
* @param {number} id - Account id.
* @return {IAccount}
*/
findById(id: number): IAccount {
const { Account } = this.models;
const cacheKey = this.getCacheKey('accounts.id', id);
return this.cache.get(cacheKey, () => {
return Account.query().findById(id);
});
}
/**
* Retrieve accounts by the given ids.
* @param {number[]} ids -
* @return {IAccount[]}
*/
findByIds(accountsIds: number[]) {
const { Account } = this.models;
const cacheKey = this.getCacheKey('accounts.id', accountsIds);
return this.cache.get(cacheKey, () => {
return Account.query().whereIn('id', accountsIds);
});
}
/**
* Activate the given account.
* @param {number} accountId -
* @return {void}
*/
async activate(accountId: number): Promise<void> {
const { Account } = this.models;
await Account.query().findById(accountId).patch({ active: 1 })
this.flushCache();
}
/**
* Inserts a new accounts to the storage.
* @param {IAccount} account
*/
async insert(accountInput: IAccount): Promise<void> {
const { Account } = this.models;
const account = await Account.query().insertAndFetch({ ...accountInput });
this.flushCache();
return account;
}
/**
* Updates account of the given account.
* @param {number} accountId - Account id.
* @param {IAccount} account
* @return {void}
*/
async edit(accountId: number, accountInput: IAccount): Promise<void> {
const { Account } = this.models;
const account = await Account.query().patchAndFetchById(accountId, { ...accountInput });
this.flushCache();
return account;
}
/**
* Deletes the given account by id.
* @param {number} accountId - Account id.
*/
async deleteById(accountId: number): Promise<void> {
const { Account } = this.models;
await Account.query().deleteById(accountId);
this.flushCache();
}
/**
* Changes account balance.
* @param {number} accountId
@@ -126,17 +30,9 @@ export default class AccountRepository extends TenantRepository {
* @return {Promise<void>}
*/
async balanceChange(accountId: number, amount: number): Promise<void> {
const { Account } = this.models;
const method: string = (amount < 0) ? 'decrement' : 'increment';
await Account.query().where('id', accountId)[method]('amount', amount);
await this.model.query().where('id', accountId)[method]('amount', amount);
this.flushCache();
}
/**
* Flush repository cache.
*/
flushCache(): void {
this.cache.delStartWith(this.repositoryName);
}
}