feat: ensure organization tenant configured.

This commit is contained in:
Ahmed Bouhuolia
2020-09-28 13:30:50 +02:00
parent 9f315ca657
commit d3d772f735
19 changed files with 140 additions and 76 deletions

View File

@@ -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();
};

View 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();
};

View File

@@ -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();
};

View 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();
};