mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
fix: accounts types caching.
This commit is contained in:
@@ -1,10 +1,14 @@
|
|||||||
import { Service } from 'typedi';
|
import { Service, Inject } from 'typedi';
|
||||||
import { Request, Response, Router } from 'express';
|
import { Request, Response, Router, NextFunction } from 'express';
|
||||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||||
import BaseController from 'api/controllers/BaseController';
|
import BaseController from 'api/controllers/BaseController';
|
||||||
|
import AccountsTypesService from 'services/Accounts/AccountsTypesServices';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class AccountsTypesController extends BaseController{
|
export default class AccountsTypesController extends BaseController{
|
||||||
|
@Inject()
|
||||||
|
accountsTypesService: AccountsTypesService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Router constructor.
|
* Router constructor.
|
||||||
*/
|
*/
|
||||||
@@ -12,20 +16,22 @@ export default class AccountsTypesController extends BaseController{
|
|||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.get('/',
|
router.get('/',
|
||||||
asyncMiddleware(this.getAccountTypesList));
|
asyncMiddleware(this.getAccountTypesList.bind(this))
|
||||||
|
);
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve accounts types list.
|
* Retrieve accounts types list.
|
||||||
*/
|
*/
|
||||||
async getAccountTypesList(req: Request, res: Response) {
|
async getAccountTypesList(req: Request, res: Response, next: NextFunction) {
|
||||||
const { AccountType } = req.models;
|
const { tenantId, user } = req;
|
||||||
const accountTypes = await AccountType.query();
|
|
||||||
|
|
||||||
return res.status(200).send({
|
try {
|
||||||
account_types: accountTypes,
|
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 {
|
export interface IAccountsFilter extends IDynamicListFilterDTO {
|
||||||
stringifiedFilterRoles?: string,
|
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
|
// catch 404 and forward to error handler
|
||||||
app.use((req: Request, res: Response, next: NextFunction) => {
|
app.use((req: Request, res: Response, next: NextFunction) => {
|
||||||
return res.boom.notFound();
|
return res.boom.notFound();
|
||||||
})
|
});
|
||||||
};
|
};
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import TenantRepository from 'repositories/TenantRepository';
|
import TenantRepository from 'repositories/TenantRepository';
|
||||||
|
import { IAccount } from 'interfaces';
|
||||||
|
|
||||||
export default class AccountRepository extends TenantRepository {
|
export default class AccountRepository extends TenantRepository {
|
||||||
models: any;
|
models: any;
|
||||||
@@ -33,9 +34,9 @@ export default class AccountRepository extends TenantRepository {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve all accounts on the storage.
|
* Retrieve all accounts on the storage.
|
||||||
* @return {}
|
* @return {IAccount[]}
|
||||||
*/
|
*/
|
||||||
allAccounts() {
|
allAccounts(): IAccount[] {
|
||||||
const { Account } = this.models;
|
const { Account } = this.models;
|
||||||
return this.cache.get('accounts', async () => {
|
return this.cache.get('accounts', async () => {
|
||||||
return Account.query();
|
return Account.query();
|
||||||
@@ -45,8 +46,9 @@ export default class AccountRepository extends TenantRepository {
|
|||||||
/**
|
/**
|
||||||
* Retrieve account of the given account slug.
|
* Retrieve account of the given account slug.
|
||||||
* @param {string} slug
|
* @param {string} slug
|
||||||
|
* @return {IAccount}
|
||||||
*/
|
*/
|
||||||
getBySlug(slug: string) {
|
getBySlug(slug: string): IAccount {
|
||||||
const { Account } = this.models;
|
const { Account } = this.models;
|
||||||
return this.cache.get(`accounts.slug.${slug}`, () => {
|
return this.cache.get(`accounts.slug.${slug}`, () => {
|
||||||
return Account.query().findOne('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.
|
* Retrieve the account by the given id.
|
||||||
* @param {number} id - Account id.
|
* @param {number} id - Account id.
|
||||||
|
* @return {IAccount}
|
||||||
*/
|
*/
|
||||||
getById(id: number) {
|
getById(id: number): IAccount {
|
||||||
const { Account } = this.models;
|
const { Account } = this.models;
|
||||||
return this.cache.get(`accounts.id.${id}`, () => {
|
return this.cache.get(`accounts.id.${id}`, () => {
|
||||||
return Account.query().findById(id);
|
return Account.query().findById(id);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import TenantRepository from 'repositories/TenantRepository';
|
import TenantRepository from 'repositories/TenantRepository';
|
||||||
|
import { IAccountType } from 'interfaces';
|
||||||
|
|
||||||
export default class AccountTypeRepository extends TenantRepository {
|
export default class AccountTypeRepository extends TenantRepository {
|
||||||
cache: any;
|
cache: any;
|
||||||
@@ -17,29 +18,62 @@ export default class AccountTypeRepository extends TenantRepository {
|
|||||||
this.cache = this.tenancy.cache(tenantId);
|
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.
|
* Retrieve account type meta.
|
||||||
* @param {number} accountTypeId
|
* @param {number} accountTypeId
|
||||||
|
* @return {IAccountType}
|
||||||
*/
|
*/
|
||||||
getTypeMeta(accountTypeId: number) {
|
getTypeMeta(accountTypeId: number): IAccountType {
|
||||||
const { AccountType } = this.models;
|
const { AccountType } = this.models;
|
||||||
return this.cache.get(`accountType.${accountTypeId}`, () => {
|
return this.cache.get(`accountType.id.${accountTypeId}`, () => {
|
||||||
return AccountType.query().findById(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;
|
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;
|
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;
|
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