Skip to content

Commit

Permalink
refactor audit_period validation middlewares and entire code structur…
Browse files Browse the repository at this point in the history
…e. refactor entire code quality including testcases
  • Loading branch information
Byunk committed Nov 17, 2023
1 parent 5511fac commit 3ba0bca
Show file tree
Hide file tree
Showing 19 changed files with 1,418 additions and 1,020 deletions.
38 changes: 26 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@aws-sdk/client-s3": "^3.409.0",
"bcrypt": "^5.1.1",
"body-parser": "^1.20.2",
"chai-as-promised": "^7.1.1",
"connect-redis": "^7.1.0",
"dotenv": "^16.3.1",
"express": "^4.18.2",
Expand All @@ -32,7 +33,8 @@
"devDependencies": {
"@types/bcrypt": "^5.0.0",
"@types/chai": "^4.3.8",
"@types/express": "^4.17.17",
"@types/chai-as-promised": "^7.1.8",
"@types/express": "^4.17.21",
"@types/express-session": "^1.17.7",
"@types/mocha": "^10.0.2",
"@types/multer": "^1.4.7",
Expand Down
19 changes: 5 additions & 14 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import express from 'express';
import { redisClient } from './db';
import {
budgets,
documents,
organizations,
transactions,
users,
} from './routes';
import { documents, organizations, transactions, users } from './routes';
import budgetsRouter from './routes/budget';
import bodyParser from 'body-parser';
import session from 'express-session';
import { config } from 'dotenv';
Expand All @@ -16,6 +11,7 @@ import fs from 'fs';
import YAML from 'yaml';
import logger from './config/winston';
import { initDB } from './db/util';
import errorHandler from './middleware/error_handler';

declare module 'express-session' {
export interface SessionData {
Expand Down Expand Up @@ -64,17 +60,12 @@ app.use(function (req, res, next) {
next();
});

app.use('/budgets', budgetsRouter);
app.use('/organizations', organizations);
app.use('/budgets', budgets);
app.use('/transactions', transactions);
app.use('/documents', documents);
app.use('/users', users);

// @ts-ignore: Unreachable code error
app.use(function (err, req, res, next) {
logger.error(err);
res.sendStatus(500);
});
app.use(errorHandler);

app.listen(3000);

Expand Down
14 changes: 14 additions & 0 deletions src/middleware/error_handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Request, Response, NextFunction } from 'express';

export default async function errorHandler(
err: any,
req: Request,
res: Response,
next: NextFunction,
) {
let status = 500;
if ('code' in err) {
status = err.code;
}
res.status(status).send(err.message);
}
3 changes: 2 additions & 1 deletion src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './validate';
export * from './validate_audit_period';
export { wrapAsync } from './utils';
7 changes: 7 additions & 0 deletions src/middleware/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Request, Response, NextFunction } from 'express';

export function wrapAsync(fn: Function) {
return function (req: Request, res: Response, next: NextFunction) {
fn(req, res, next).catch(next);
};
}
176 changes: 0 additions & 176 deletions src/middleware/validate.ts

This file was deleted.

Loading

0 comments on commit 3ba0bca

Please sign in to comment.