Spaces:
Runtime error
Runtime error
Commit ·
0cffbb2
1
Parent(s): 6c40c26
Expose an api to get all error logs
Browse files- src/app.ts +3 -1
- src/controllers/errorLog.controller.ts +91 -0
- src/routes/errorLog.routes.ts +8 -0
src/app.ts
CHANGED
|
@@ -6,6 +6,7 @@ import pwRouter from './routes/propertyware.routes';
|
|
| 6 |
import userRouter from './routes/users';
|
| 7 |
import auditLogRouter from './routes/auditLogs';
|
| 8 |
import authRouter from './routes/auth';
|
|
|
|
| 9 |
|
| 10 |
const app = express();
|
| 11 |
|
|
@@ -23,7 +24,8 @@ app.use("/api/pw", pwRouter);
|
|
| 23 |
app.use('/api/invoices', invoiceRouter);
|
| 24 |
app.use('/api/', authRouter);
|
| 25 |
app.use('/api/users/', userRouter)
|
| 26 |
-
app.use('/api/
|
|
|
|
| 27 |
|
| 28 |
try {
|
| 29 |
app.listen(port, () => {
|
|
|
|
| 6 |
import userRouter from './routes/users';
|
| 7 |
import auditLogRouter from './routes/auditLogs';
|
| 8 |
import authRouter from './routes/auth';
|
| 9 |
+
import errorLogRouter from './routes/errorLog.routes';
|
| 10 |
|
| 11 |
const app = express();
|
| 12 |
|
|
|
|
| 24 |
app.use('/api/invoices', invoiceRouter);
|
| 25 |
app.use('/api/', authRouter);
|
| 26 |
app.use('/api/users/', userRouter)
|
| 27 |
+
app.use('/api/audit-logs/', auditLogRouter)
|
| 28 |
+
app.use('/api/error-logs/', errorLogRouter)
|
| 29 |
|
| 30 |
try {
|
| 31 |
app.listen(port, () => {
|
src/controllers/errorLog.controller.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Request, Response } from "express";
|
| 2 |
+
|
| 3 |
+
import ErrorLog from "../models/errorLog";
|
| 4 |
+
import { FindOptions, Op } from "sequelize";
|
| 5 |
+
|
| 6 |
+
const buildErrorLogWhereClause = (filter: Record<string, any>) => {
|
| 7 |
+
const whereClause: any = {};
|
| 8 |
+
if (filter) {
|
| 9 |
+
if (filter.date) {
|
| 10 |
+
const date = new Date(filter.date);
|
| 11 |
+
if (!isNaN(date.getTime())) {
|
| 12 |
+
whereClause.created_at = { [Op.eq]: date };
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
if (filter.invoice_id) {
|
| 17 |
+
whereClause.invoice_id = { [Op.eq]: filter.invoice_id };
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
if (filter.error_type) {
|
| 21 |
+
whereClause.error_type = { [Op.eq]: filter.error_type };
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
if (filter.created_before) {
|
| 25 |
+
const beforeDate = new Date(filter.created_before);
|
| 26 |
+
if (!isNaN(beforeDate.getTime())) {
|
| 27 |
+
whereClause.created_at = { ...whereClause.created_at, [Op.lte]: beforeDate };
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
if (filter.created_after) {
|
| 32 |
+
const afterDate = new Date(filter.created_after);
|
| 33 |
+
if (!isNaN(afterDate.getTime())) {
|
| 34 |
+
whereClause.created_at = { ...whereClause.created_at, [Op.gte]: afterDate };
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
return whereClause;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
const getErrorLogs = async (req: Request, res: Response) => {
|
| 43 |
+
|
| 44 |
+
try {
|
| 45 |
+
|
| 46 |
+
const { sort_by, sort_order, page, limit } = req.query;
|
| 47 |
+
const filter = req.query.filter as Record<string, any>;
|
| 48 |
+
|
| 49 |
+
const allowedSortColumns = ['id', 'invoice_id'];
|
| 50 |
+
|
| 51 |
+
const whereClause = buildErrorLogWhereClause(filter);
|
| 52 |
+
|
| 53 |
+
const currentPage = parseInt(page as string) || 1;
|
| 54 |
+
const pageSize = parseInt(limit as string) || 10;
|
| 55 |
+
|
| 56 |
+
const options: FindOptions = {
|
| 57 |
+
where: whereClause,
|
| 58 |
+
limit: pageSize,
|
| 59 |
+
offset: (currentPage - 1) * pageSize,
|
| 60 |
+
order: []
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
if (sort_by && allowedSortColumns.includes(sort_by as string)) {
|
| 64 |
+
options.order = [[sort_by as string, sort_order === 'desc' ? 'DESC' : 'ASC']];
|
| 65 |
+
|
| 66 |
+
} else {
|
| 67 |
+
options.order = [['id', 'ASC']];
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
const [errorLogs, totalErrorLogs] = await Promise.all([
|
| 71 |
+
ErrorLog.findAll(options),
|
| 72 |
+
ErrorLog.count({ where: whereClause }),
|
| 73 |
+
]);
|
| 74 |
+
|
| 75 |
+
const responseData = {
|
| 76 |
+
page: currentPage,
|
| 77 |
+
limit: pageSize,
|
| 78 |
+
total: totalErrorLogs,
|
| 79 |
+
data: errorLogs
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
return res.status(200).json(responseData);
|
| 83 |
+
|
| 84 |
+
}
|
| 85 |
+
catch (error) {
|
| 86 |
+
console.error('Error fetching error log:', error);
|
| 87 |
+
return res.status(500).json({ error: 'Error fetching error log.' });
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
export { getErrorLogs }
|
src/routes/errorLog.routes.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from "express";
|
| 2 |
+
import { getErrorLogs } from "../controllers/errorLog.controller";
|
| 3 |
+
|
| 4 |
+
const errorLogRouter = express.Router();
|
| 5 |
+
|
| 6 |
+
errorLogRouter.get("/", getErrorLogs);
|
| 7 |
+
|
| 8 |
+
export default errorLogRouter
|