mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
fix: accounts types caching.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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<IAccountType>;
|
||||
}
|
||||
@@ -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();
|
||||
})
|
||||
});
|
||||
};
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
19
server/src/services/Accounts/AccountsTypesServices.ts
Normal file
19
server/src/services/Accounts/AccountsTypesServices.ts
Normal file
@@ -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<IAccountType>}
|
||||
*/
|
||||
getAccountsTypes(tenantId: number): Promise<IAccountType> {
|
||||
const { accountTypeRepository } = this.tenancy.repositories(tenantId);
|
||||
return accountTypeRepository.all();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user