diff --git a/server/src/api/controllers/AccountTypes.ts b/server/src/api/controllers/AccountTypes.ts index 34e658c11..0be025ed3 100644 --- a/server/src/api/controllers/AccountTypes.ts +++ b/server/src/api/controllers/AccountTypes.ts @@ -1,10 +1,14 @@ -import { Service } from 'typedi'; -import { Request, Response, Router } from 'express'; +import { Service, Inject } from 'typedi'; +import { Request, Response, Router, NextFunction } from 'express'; import asyncMiddleware from 'api/middleware/asyncMiddleware'; import BaseController from 'api/controllers/BaseController'; +import AccountsTypesService from 'services/Accounts/AccountsTypesServices'; @Service() export default class AccountsTypesController extends BaseController{ + @Inject() + accountsTypesService: AccountsTypesService; + /** * Router constructor. */ @@ -12,20 +16,22 @@ export default class AccountsTypesController extends BaseController{ const router = Router(); router.get('/', - asyncMiddleware(this.getAccountTypesList)); - + asyncMiddleware(this.getAccountTypesList.bind(this)) + ); return router; } /** * Retrieve accounts types list. */ - async getAccountTypesList(req: Request, res: Response) { - const { AccountType } = req.models; - const accountTypes = await AccountType.query(); + async getAccountTypesList(req: Request, res: Response, next: NextFunction) { + const { tenantId, user } = req; - return res.status(200).send({ - account_types: accountTypes, - }); + try { + const accountTypes = await this.accountsTypesService.getAccountsTypes(tenantId); + return res.status(200).send({ account_types: accountTypes }); + } catch (error) { + next(error); + } } }; diff --git a/server/src/interfaces/Account.ts b/server/src/interfaces/Account.ts index e381552f7..398b48275 100644 --- a/server/src/interfaces/Account.ts +++ b/server/src/interfaces/Account.ts @@ -25,3 +25,17 @@ export interface IAccount { export interface IAccountsFilter extends IDynamicListFilterDTO { stringifiedFilterRoles?: string, }; + +export interface IAccountType { + id: number, + key: string, + normal: string, + rootType: string, + childType: string, + balanceSheet: boolean, + incomeSheet: boolean, +} + +export interface IAccountsTypesService { + getAccountsTypes(tenantId: number): Promise; +} \ No newline at end of file diff --git a/server/src/loaders/express.ts b/server/src/loaders/express.ts index 3968e5457..435d6b0e6 100644 --- a/server/src/loaders/express.ts +++ b/server/src/loaders/express.ts @@ -45,5 +45,5 @@ export default ({ app }) => { // catch 404 and forward to error handler app.use((req: Request, res: Response, next: NextFunction) => { return res.boom.notFound(); - }) + }); }; \ No newline at end of file diff --git a/server/src/repositories/AccountRepository.ts b/server/src/repositories/AccountRepository.ts index a8afd678b..2452cffe4 100644 --- a/server/src/repositories/AccountRepository.ts +++ b/server/src/repositories/AccountRepository.ts @@ -1,4 +1,5 @@ import TenantRepository from 'repositories/TenantRepository'; +import { IAccount } from 'interfaces'; export default class AccountRepository extends TenantRepository { models: any; @@ -33,9 +34,9 @@ export default class AccountRepository extends TenantRepository { /** * Retrieve all accounts on the storage. - * @return {} + * @return {IAccount[]} */ - allAccounts() { + allAccounts(): IAccount[] { const { Account } = this.models; return this.cache.get('accounts', async () => { return Account.query(); @@ -45,8 +46,9 @@ export default class AccountRepository extends TenantRepository { /** * Retrieve account of the given account slug. * @param {string} slug + * @return {IAccount} */ - getBySlug(slug: string) { + getBySlug(slug: string): IAccount { const { Account } = this.models; return this.cache.get(`accounts.slug.${slug}`, () => { return Account.query().findOne('slug', slug); @@ -56,8 +58,9 @@ export default class AccountRepository extends TenantRepository { /** * Retrieve the account by the given id. * @param {number} id - Account id. + * @return {IAccount} */ - getById(id: number) { + getById(id: number): IAccount { const { Account } = this.models; return this.cache.get(`accounts.id.${id}`, () => { return Account.query().findById(id); diff --git a/server/src/repositories/AccountTypeRepository.ts b/server/src/repositories/AccountTypeRepository.ts index 0b8f424b5..7b273076e 100644 --- a/server/src/repositories/AccountTypeRepository.ts +++ b/server/src/repositories/AccountTypeRepository.ts @@ -1,4 +1,5 @@ import TenantRepository from 'repositories/TenantRepository'; +import { IAccountType } from 'interfaces'; export default class AccountTypeRepository extends TenantRepository { cache: any; @@ -17,29 +18,62 @@ export default class AccountTypeRepository extends TenantRepository { this.cache = this.tenancy.cache(tenantId); } + /** + * Retrieve all accounts types. + * @return {IAccountType[]} + */ + all() { + const { AccountType } = this.models; + return this.cache.get('accountType.all', () => { + return AccountType.query(); + }); + } + /** * Retrieve account type meta. * @param {number} accountTypeId + * @return {IAccountType} */ - getTypeMeta(accountTypeId: number) { + getTypeMeta(accountTypeId: number): IAccountType { const { AccountType } = this.models; - return this.cache.get(`accountType.${accountTypeId}`, () => { + return this.cache.get(`accountType.id.${accountTypeId}`, () => { return AccountType.query().findById(accountTypeId); }); } - getByKeys(keys: string[]) { + /** + * Retrieve accounts types of the given keys. + * @param {string[]} keys + * @return {IAccountType[]} + */ + getByKeys(keys: string[]): IAccountType[] { const { AccountType } = this.models; - return AccountType.query().whereIn('key', keys); + return this.cache.get(`accountType.keys.${keys.join(',')}`, () => { + return AccountType.query().whereIn('key', keys); + }); } - getByKey(key: string) { + /** + * Retrieve account tpy eof the given key. + * @param {string} key + * @return {IAccountType} + */ + getByKey(key: string): IAccountType { const { AccountType } = this.models; - return AccountType.query().findOne('key', key); + return this.cache.get(`accountType.key.${key}`, () => { + return AccountType.query().findOne('key', key); + }); } - getByRootType(rootType: string) { + /** + * Retrieve accounts types of the given root type. + * @param {string} rootType + * @return {IAccountType[]} + */ + getByRootType(rootType: string): IAccountType[] { const { AccountType } = this.models; - return AccountType.query().where('root_type', rootType); + return this.cache.get(`accountType.rootType.${rootType}`, () => { + return AccountType.query().where('root_type', rootType); + }); } } \ No newline at end of file diff --git a/server/src/services/Accounts/AccountsTypesServices.ts b/server/src/services/Accounts/AccountsTypesServices.ts new file mode 100644 index 000000000..b2b6d02d6 --- /dev/null +++ b/server/src/services/Accounts/AccountsTypesServices.ts @@ -0,0 +1,19 @@ +import { Inject, Service } from 'typedi'; +import TenancyService from 'services/Tenancy/TenancyService'; +import { IAccountsTypesService, IAccountType } from 'interfaces'; + +@Service() +export default class AccountsTypesService implements IAccountsTypesService{ + @Inject() + tenancy: TenancyService; + + /** + * Retrieve all accounts types. + * @param {number} tenantId - + * @return {Promise} + */ + getAccountsTypes(tenantId: number): Promise { + const { accountTypeRepository } = this.tenancy.repositories(tenantId); + return accountTypeRepository.all(); + } +} \ No newline at end of file