feat: close specific account.

This commit is contained in:
Ahmed Bouhuolia
2020-10-05 22:05:04 +02:00
parent 63c4567e08
commit 681fa0560e
3 changed files with 118 additions and 3 deletions

View File

@@ -61,7 +61,7 @@ export default class AccountRepository extends TenantRepository {
* @param {number} id - Account id.
* @return {IAccount}
*/
getById(id: number): IAccount {
findById(id: number): IAccount {
const { Account } = this.models;
return this.cache.get(`accounts.id.${id}`, () => {
return Account.query().findById(id);
@@ -93,10 +93,12 @@ export default class AccountRepository extends TenantRepository {
* Inserts a new accounts to the storage.
* @param {IAccount} account
*/
async insert(account: IAccount): Promise<void> {
async insert(accountInput: IAccount): Promise<void> {
const { Account } = this.models;
await Account.query().insertAndFetch({ ...account });
const account = await Account.query().insertAndFetch({ ...accountInput });
this.flushCache();
return account;
}
/**
@@ -121,6 +123,20 @@ export default class AccountRepository extends TenantRepository {
this.flushCache();
}
/**
* Changes account balance.
* @param {number} accountId
* @param {number} amount
* @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);
this.flushCache();
}
/**
* Flush repository cache.
*/