mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
fix: lock mutate base currency once organization has transactions.
This commit is contained in:
@@ -19,6 +19,8 @@ import { DATE_FORMATS } from 'services/Miscellaneous/DateFormats/constants';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import BaseController from 'api/controllers/BaseController';
|
||||
|
||||
const ACCEPTED_LOCATIONS = ['libya'];
|
||||
|
||||
@Service()
|
||||
export default class OrganizationController extends BaseController {
|
||||
@Inject()
|
||||
@@ -39,14 +41,14 @@ export default class OrganizationController extends BaseController {
|
||||
router.use('/build', SubscriptionMiddleware('main'));
|
||||
router.post(
|
||||
'/build',
|
||||
this.buildValidationSchema,
|
||||
this.organizationValidationSchema,
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.build.bind(this)),
|
||||
this.handleServiceErrors.bind(this)
|
||||
);
|
||||
router.put(
|
||||
'/',
|
||||
this.buildValidationSchema,
|
||||
this.organizationValidationSchema,
|
||||
this.validationResult,
|
||||
this.asyncMiddleware(this.updateOrganization.bind(this)),
|
||||
this.handleServiceErrors.bind(this)
|
||||
@@ -61,15 +63,17 @@ export default class OrganizationController extends BaseController {
|
||||
|
||||
/**
|
||||
* Organization setup schema.
|
||||
* @return {ValidationChain[]}
|
||||
*/
|
||||
private get buildValidationSchema(): ValidationChain[] {
|
||||
private get organizationValidationSchema(): ValidationChain[] {
|
||||
return [
|
||||
check('name').exists().trim(),
|
||||
check('industry').optional().isString(),
|
||||
check('location').exists().isString().isIn(ACCEPTED_LOCATIONS),
|
||||
check('base_currency').exists().isIn(ACCEPTED_CURRENCIES),
|
||||
check('timezone').exists().isIn(moment.tz.names()),
|
||||
check('fiscal_year').exists().isIn(MONTHS),
|
||||
check('industry').optional().isString(),
|
||||
check('language').optional().isString().isIn(ACCEPTED_LOCALES),
|
||||
check('language').exists().isString().isIn(ACCEPTED_LOCALES),
|
||||
check('date_format').optional().isIn(DATE_FORMATS),
|
||||
];
|
||||
}
|
||||
@@ -117,7 +121,9 @@ export default class OrganizationController extends BaseController {
|
||||
const organization = await this.organizationService.currentOrganization(
|
||||
tenantId
|
||||
);
|
||||
return res.status(200).send({ organization });
|
||||
return res.status(200).send({
|
||||
organization: this.transfromToResponse(organization),
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
@@ -139,7 +145,7 @@ export default class OrganizationController extends BaseController {
|
||||
const tenantDTO = this.matchedBodyData(req);
|
||||
|
||||
try {
|
||||
const organization = await this.organizationService.updateOrganization(
|
||||
await this.organizationService.updateOrganization(
|
||||
tenantId,
|
||||
tenantDTO
|
||||
);
|
||||
@@ -183,6 +189,11 @@ export default class OrganizationController extends BaseController {
|
||||
errors: [{ type: 'TENANT_IS_BUILDING', code: 300 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'BASE_CURRENCY_MUTATE_LOCKED') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'BASE_CURRENCY_MUTATE_LOCKED', code: 400 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
|
||||
40
server/src/api/controllers/OrganizationDashboard.ts
Normal file
40
server/src/api/controllers/OrganizationDashboard.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { Request, Response, Router } from 'express';
|
||||
import BaseController from 'api/controllers/BaseController';
|
||||
import OrganizationService from 'services/Organization';
|
||||
|
||||
@Service()
|
||||
export default class OrganizationDashboardController extends BaseController {
|
||||
@Inject()
|
||||
organizationService: OrganizationService;
|
||||
|
||||
/**
|
||||
* Router constructor.
|
||||
*/
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
router.get(
|
||||
'/base_currency_mutate',
|
||||
this.baseCurrencyMutateAbility.bind(this)
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
private async baseCurrencyMutateAbility(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: Function
|
||||
) {
|
||||
const { tenantId } = req;
|
||||
|
||||
try {
|
||||
const abilities =
|
||||
await this.organizationService.mutateBaseCurrencyAbility(tenantId);
|
||||
|
||||
return res.status(200).send({ abilities });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,7 @@ import InventoryAdjustments from 'api/controllers/Inventory/InventoryAdjustments
|
||||
import asyncRenderMiddleware from './middleware/AsyncRenderMiddleware';
|
||||
import Jobs from './controllers/Jobs';
|
||||
import Miscellaneous from 'api/controllers/Miscellaneous';
|
||||
import OrganizationDashboard from 'api/controllers/OrganizationDashboard';
|
||||
|
||||
export default () => {
|
||||
const app = Router();
|
||||
@@ -75,6 +76,7 @@ export default () => {
|
||||
dashboard.use(I18nAuthenticatedMiddlware);
|
||||
dashboard.use(EnsureTenantIsSeeded);
|
||||
|
||||
dashboard.use('/organization', Container.get(OrganizationDashboard).router());
|
||||
dashboard.use('/invite', Container.get(InviteUsers).authRouter());
|
||||
dashboard.use('/currencies', Container.get(Currencies).router());
|
||||
dashboard.use('/settings', Container.get(Settings).router());
|
||||
|
||||
Reference in New Issue
Block a user