mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
feat: refactoring middlewares to typescript.
This commit is contained in:
@@ -139,6 +139,7 @@ export default class AuthenticationController extends BaseController{
|
||||
const registeredUser: ISystemUser = await this.authService.register(registerDTO);
|
||||
|
||||
return res.status(200).send({
|
||||
type: 'success',
|
||||
code: 'REGISTER.SUCCESS',
|
||||
message: 'Register organization has been success.',
|
||||
});
|
||||
@@ -153,7 +154,7 @@ export default class AuthenticationController extends BaseController{
|
||||
errorReasons.push({ type: 'EMAIL.EXISTS', code: 200 });
|
||||
}
|
||||
if (errorReasons.length > 0) {
|
||||
return res.status(200).send({ errors: errorReasons });
|
||||
return res.boom.badRequest(null, { errors: errorReasons });
|
||||
}
|
||||
}
|
||||
next();
|
||||
|
||||
@@ -173,7 +173,6 @@ export default class ItemsController {
|
||||
|
||||
const foundItems: [] = await Item.query().onBuild((builder: any) => {
|
||||
builder.where('name', item.name);
|
||||
|
||||
if (itemId) {
|
||||
builder.whereNot('id', itemId);
|
||||
}
|
||||
|
||||
@@ -33,9 +33,8 @@ import Resources from './controllers/Resources';
|
||||
import ExchangeRates from '@/http/controllers/ExchangeRates';
|
||||
import Media from '@/http/controllers/Media';
|
||||
import Ping from '@/http/controllers/Ping';
|
||||
import Agendash from '@/http/controllers/Agendash';
|
||||
import Subscription from '@/http/controllers/Subscription';
|
||||
import LicensesController from '@/http/controllers/Subscription/Licenses';
|
||||
import Licenses from '@/http/controllers/Subscription/Licenses';
|
||||
|
||||
export default () => {
|
||||
const app = Router();
|
||||
@@ -45,11 +44,11 @@ export default () => {
|
||||
app.use('/auth', Container.get(Authentication).router());
|
||||
app.use('/invite', Container.get(InviteUsers).nonAuthRouter());
|
||||
app.use('/organization', Container.get(Organization).router());
|
||||
app.use('/licenses', Container.get(LicensesController).router());
|
||||
app.use('/licenses', Container.get(Licenses).router());
|
||||
app.use('/subscription', Container.get(Subscription).router());
|
||||
app.use('/ping', Container.get(Ping).router());
|
||||
|
||||
const dashboard = Router();
|
||||
const dashboard = Router();
|
||||
|
||||
dashboard.use(JWTAuth);
|
||||
dashboard.use(AttachCurrentTenantUser)
|
||||
@@ -77,9 +76,8 @@ export default () => {
|
||||
dashboard.use('/purchases', Purchases.router());
|
||||
dashboard.use('/resources', Resources.router());
|
||||
dashboard.use('/exchange_rates', ExchangeRates.router());
|
||||
dashboard.use('/media', Media.router());
|
||||
dashboard.use('/media', Media.router())
|
||||
|
||||
app.use('/agendash', Agendash.router());
|
||||
app.use('/', dashboard);
|
||||
|
||||
return app;
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Container } from 'typedi';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
// eslint-disable-next-line consistent-return
|
||||
export default async (req, res, next) => {
|
||||
export default async (req: Request, res: Response, next: NextFunction) => {
|
||||
const { Option } = req.models;
|
||||
const option = await Option.query().where('key', 'app_configured');
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
|
||||
export default (subscriptionSlug = 'main') => async (req, res, next) => {
|
||||
const { tenant } = req;
|
||||
|
||||
if (!tenant) {
|
||||
throw new Error('Should load `TenancyMiddlware` before this middleware.');
|
||||
}
|
||||
const subscription = await tenant
|
||||
.$relatedQuery('subscriptions')
|
||||
.modify('subscriptionBySlug', subscriptionSlug)
|
||||
.first();
|
||||
|
||||
// Validate in case there is no any already subscription.
|
||||
if (!subscription) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'TENANT.HAS.NO.SUBSCRIPTION' }],
|
||||
});
|
||||
}
|
||||
// Validate in case the subscription is inactive.
|
||||
else if (subscription.inactive()) {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'ORGANIZATION.SUBSCRIPTION.INACTIVE' }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
};
|
||||
33
server/src/http/middleware/SubscriptionMiddleware.ts
Normal file
33
server/src/http/middleware/SubscriptionMiddleware.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Container } from 'typedi';
|
||||
|
||||
export default (subscriptionSlug = 'main') => async (req: Request, res: Response, next: NextFunction) => {
|
||||
const { tenant, tenantId } = req;
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
if (!tenant) {
|
||||
throw new Error('Should load `TenancyMiddlware` before this middleware.');
|
||||
}
|
||||
Logger.info('[subscription_middleware] trying get tenant main subscription.');
|
||||
const subscription = await tenant
|
||||
.$relatedQuery('subscriptions')
|
||||
.modify('subscriptionBySlug', subscriptionSlug)
|
||||
.first();
|
||||
|
||||
// Validate in case there is no any already subscription.
|
||||
if (!subscription) {
|
||||
Logger.info('[subscription_middleware] tenant has no subscription.', { tenantId });
|
||||
return res.boom.badRequest(
|
||||
'Tenant has no subscription.',
|
||||
{ errors: [{ type: 'TENANT.HAS.NO.SUBSCRIPTION' }] }
|
||||
);
|
||||
}
|
||||
// Validate in case the subscription is inactive.
|
||||
else if (subscription.inactive()) {
|
||||
Logger.info('[subscription_middleware] tenant main subscription is expired.', { tenantId });
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'ORGANIZATION.SUBSCRIPTION.INACTIVE' }],
|
||||
});
|
||||
}
|
||||
next();
|
||||
};
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Container } from 'typedi';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import TenantsManager from '@/system/TenantsManager';
|
||||
import tenantModelsLoader from '@/loaders/tenantModels';
|
||||
|
||||
export default async (req, res, next) => {
|
||||
export default async (req: Request, res: Response, next: NextFunction) => {
|
||||
const Logger = Container.get('logger');
|
||||
const organizationId = req.headers['organization-id'] || req.query.organization;
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
const asyncMiddleware = (fn) => (req, res, next) => {
|
||||
Promise.resolve(fn(req, res, next))
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
next(error);
|
||||
});
|
||||
};
|
||||
|
||||
export default asyncMiddleware;
|
||||
17
server/src/http/middleware/asyncMiddleware.ts
Normal file
17
server/src/http/middleware/asyncMiddleware.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import logger from "src/loaders/logger";
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Container } from 'typedi';
|
||||
|
||||
export default (
|
||||
fn: (rq: Request, rs: Response, next?: NextFunction) => {}) =>
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
Promise.resolve(fn(req, res, next))
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
Logger.error('[async_middleware] error.', { error });
|
||||
next(error);
|
||||
});
|
||||
};
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { Container } from 'typedi';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import config from '@/../config/config';
|
||||
|
||||
const authMiddleware = (req, res, next) => {
|
||||
const authMiddleware = (req: Request, res: Response, next: NextFunction) => {
|
||||
const Logger = Container.get('logger');
|
||||
const token = req.headers['x-access-token'] || req.query.token;
|
||||
|
||||
Reference in New Issue
Block a user