mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
feat: ensure organization tenant configured.
This commit is contained in:
@@ -8,6 +8,8 @@ import AttachCurrentTenantUser from 'api/middleware/AttachCurrentTenantUser';
|
||||
import OrganizationService from 'services/Organization';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import BaseController from 'api/controllers/BaseController';
|
||||
import EnsureConfiguredMiddleware from 'api/middleware/EnsureConfiguredMiddleware';
|
||||
import SettingsMiddleware from 'api/middleware/SettingsMiddleware';
|
||||
|
||||
@Service()
|
||||
export default class OrganizationController extends BaseController{
|
||||
@@ -27,6 +29,10 @@ export default class OrganizationController extends BaseController{
|
||||
router.use(TenancyMiddleware);
|
||||
router.use(SubscriptionMiddleware('main'));
|
||||
|
||||
// Should to seed organization tenant be configured.
|
||||
router.use('/seed', SettingsMiddleware);
|
||||
router.use('/seed', EnsureConfiguredMiddleware);
|
||||
|
||||
router.post(
|
||||
'/build',
|
||||
asyncMiddleware(this.build.bind(this))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import express from 'express';
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { check, param, query, matchedData } from 'express-validator';
|
||||
import { difference } from 'lodash';
|
||||
import { raw } from 'objection';
|
||||
@@ -21,7 +21,7 @@ export default class SaleInvoicesController {
|
||||
* Router constructor.
|
||||
*/
|
||||
router() {
|
||||
const router = express.Router();
|
||||
const router = Router();
|
||||
|
||||
router.post(
|
||||
'/',
|
||||
|
||||
@@ -62,9 +62,7 @@ export default class SettingsController extends BaseController{
|
||||
errorReasons.push({
|
||||
type: 'OPTIONS.KEY.NOT.DEFINED',
|
||||
code: 200,
|
||||
keys: notDefinedOptions.map((o) => ({
|
||||
...pick(o, ['key', 'group'])
|
||||
})),
|
||||
keys: notDefinedOptions.map((o) => ({ ...pick(o, ['key', 'group']) })),
|
||||
});
|
||||
}
|
||||
if (errorReasons.length) {
|
||||
@@ -80,7 +78,7 @@ export default class SettingsController extends BaseController{
|
||||
message: 'Options have been saved successfully.',
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve settings.
|
||||
* @param {Request} req
|
||||
|
||||
@@ -9,6 +9,8 @@ import TenancyMiddleware from 'api/middleware/TenancyMiddleware';
|
||||
import EnsureTenantIsInitialized from 'api/middleware/EnsureTenantIsInitialized';
|
||||
import SettingsMiddleware from 'api/middleware/SettingsMiddleware';
|
||||
import I18nMiddleware from 'api/middleware/I18nMiddleware';
|
||||
import EnsureConfiguredMiddleware from 'api/middleware/EnsureConfiguredMiddleware';
|
||||
import EnsureTenantIsSeeded from 'api/middleware/EnsureTenantIsSeeded';
|
||||
|
||||
// Routes
|
||||
import Authentication from 'api/controllers/Authentication';
|
||||
@@ -57,6 +59,8 @@ export default () => {
|
||||
dashboard.use(SubscriptionMiddleware('main'));
|
||||
dashboard.use(EnsureTenantIsInitialized);
|
||||
dashboard.use(SettingsMiddleware);
|
||||
dashboard.use(EnsureConfiguredMiddleware);
|
||||
dashboard.use(EnsureTenantIsSeeded);
|
||||
|
||||
dashboard.use('/users', Container.get(Users).router());
|
||||
dashboard.use('/invite', Container.get(InviteUsers).authRouter());
|
||||
@@ -76,7 +80,7 @@ export default () => {
|
||||
dashboard.use('/purchases', Purchases.router());
|
||||
dashboard.use('/resources', Resources.router());
|
||||
dashboard.use('/exchange_rates', Container.get(ExchangeRates).router());
|
||||
dashboard.use('/media', Media.router())
|
||||
dashboard.use('/media', Media.router());
|
||||
|
||||
app.use('/', dashboard);
|
||||
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import { Container } from 'typedi';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
export default async (req: Request, res: Response, next: NextFunction) => {
|
||||
const { Option } = req.models;
|
||||
const option = await Option.query().where('key', 'app_configured');
|
||||
|
||||
if (option.getMeta('app_configured', false)) {
|
||||
return res.res(400).send({
|
||||
errors: [{ type: 'TENANT.NOT.CONFIGURED', code: 700 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
};
|
||||
12
server/src/api/middleware/EnsureConfiguredMiddleware.ts
Normal file
12
server/src/api/middleware/EnsureConfiguredMiddleware.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
export default (req: Request, res: Response, next: NextFunction) => {
|
||||
const { settings } = req;
|
||||
|
||||
if (!settings.get('app_configured', false)) {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'APP.NOT.CONFIGURED', code: 100 }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
};
|
||||
@@ -16,12 +16,5 @@ export default (req: Request, res: Response, next: Function) => {
|
||||
{ errors: [{ type: 'TENANT.DATABASE.NOT.INITALIZED' }] },
|
||||
);
|
||||
}
|
||||
if (!req.tenant.seededAt) {
|
||||
Logger.info('[ensure_tenant_initialized_middleware] tenant databae not seeded.');
|
||||
return res.boom.badRequest(
|
||||
'Tenant database is not seeded with initial data yet.',
|
||||
{ errors: [{ type: 'TENANT.DATABASE.NOT.SEED' }] },
|
||||
);
|
||||
}
|
||||
next();
|
||||
};
|
||||
19
server/src/api/middleware/EnsureTenantIsSeeded.ts
Normal file
19
server/src/api/middleware/EnsureTenantIsSeeded.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Container } from 'typedi';
|
||||
import { Request, Response } from 'express';
|
||||
|
||||
export default (req: Request, res: Response, next: Function) => {
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
if (!req.tenant) {
|
||||
Logger.info('[ensure_tenant_intialized_middleware] no tenant model.');
|
||||
throw new Error('Should load this middleware after `TenancyMiddleware`.');
|
||||
}
|
||||
if (!req.tenant.seededAt) {
|
||||
Logger.info('[ensure_tenant_initialized_middleware] tenant databae not seeded.');
|
||||
return res.boom.badRequest(
|
||||
'Tenant database is not seeded with initial data yet.',
|
||||
{ errors: [{ type: 'TENANT.DATABASE.NOT.SEED' }] },
|
||||
);
|
||||
}
|
||||
next();
|
||||
};
|
||||
Reference in New Issue
Block a user