import { Account } from 'models'; import TenantRepository from 'repositories/TenantRepository'; import { IAccount } from 'interfaces'; export default class AccountRepository extends TenantRepository { /** * Gets the repository's model. */ get model() { return Account.bindKnex(this.knex); } /** * Retrieve accounts dependency graph. * @returns {} */ 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); }); } /** * Changes account balance. * @param {number} accountId * @param {number} amount * @return {Promise} */ async balanceChange(accountId: number, amount: number): Promise { const method: string = (amount < 0) ? 'decrement' : 'increment'; 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} */ activateById(userId: number): Promise { return super.update({ active: 1 }, { id: userId }); } /** * Inactivate user by the given id. * @param {number} userId - User id. * @return {Promise} */ inactivateById(userId: number): Promise { return super.update({ active: 0 }, { id: userId }); } /** * Activate user by the given id. * @param {number} userId - User id. * @return {Promise} */ async activateByIds(userIds: number[]): Promise { 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} */ async inactivateByIds(userIds: number[]): Promise { const results = await this.model.query() .whereIn('id', userIds) .patch({ active: false }); this.flushCache(); return results; } }