narinder1231 commited on
Commit
720932b
·
1 Parent(s): 27423e4

expose api to get all audit logs

Browse files
src/app.ts CHANGED
@@ -4,6 +4,7 @@ import cors from 'cors';
4
  import invoiceRouter from './routes/invoice.routes';
5
  import pwRouter from './routes/propertyware.routes';
6
  import userRouter from './routes/users';
 
7
 
8
  const app = express();
9
 
@@ -20,6 +21,7 @@ app.get('/', (_, res) => {
20
  app.use("/api/pw", pwRouter);
21
  app.use('/api/invoices', invoiceRouter);
22
  app.use('/api/users/', userRouter)
 
23
 
24
  try {
25
  app.listen(port, () => {
 
4
  import invoiceRouter from './routes/invoice.routes';
5
  import pwRouter from './routes/propertyware.routes';
6
  import userRouter from './routes/users';
7
+ import auditLogRouter from './routes/auditLogs';
8
 
9
  const app = express();
10
 
 
21
  app.use("/api/pw", pwRouter);
22
  app.use('/api/invoices', invoiceRouter);
23
  app.use('/api/users/', userRouter)
24
+ app.use('/api/auditLogs/', auditLogRouter)
25
 
26
  try {
27
  app.listen(port, () => {
src/controllers/auditLog.controller.ts ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Request, Response } from 'express';
2
+ import { Op, FindOptions } from 'sequelize';
3
+ import AuditLog from '../models/auditLogs';
4
+
5
+ export const getAuditLogs = async (req: Request, res: Response) => {
6
+ try {
7
+ const { sort_by, sort_order, page, limit } = req.query;
8
+ const filter = req.query.filter as Record<string, any>;
9
+
10
+ const allowedSortColumns = ['id','action_by', 'invoice_id']
11
+
12
+ const whereClause: any = {};
13
+
14
+ if (filter) {
15
+ if (filter.date) {
16
+ const date = new Date(filter.date);
17
+ if (!isNaN(date.getTime())) {
18
+ whereClause.created_at = { [Op.eq]: date };
19
+ }
20
+ }
21
+ if (filter.action_by) {
22
+ whereClause.action_by = { [Op.eq]: filter.action_by };
23
+ }
24
+ if (filter.invoice_id) {
25
+ whereClause.invoice_id = { [Op.eq]: filter.invoice_id };
26
+ }
27
+ }
28
+
29
+ const currentPage = parseInt(page as string) || 1;
30
+ const pageSize = parseInt(limit as string) || 10;
31
+
32
+ const options: FindOptions = {
33
+ where: whereClause,
34
+ limit: pageSize,
35
+ offset: (currentPage - 1) * pageSize,
36
+ order: []
37
+ };
38
+
39
+ if (sort_by && allowedSortColumns.includes(sort_by as string)) {
40
+ options.order = [[sort_by as string, sort_order === 'desc' ? 'DESC' : 'ASC']];
41
+ } else {
42
+ options.order = [['id', 'ASC']];
43
+ }
44
+
45
+ const [auditLogs, totalAuditLogs] = await Promise.all([
46
+ AuditLog.findAll(options),
47
+ AuditLog.count({ where: whereClause }),
48
+ ]);
49
+
50
+ const data = {
51
+ page: currentPage,
52
+ limit: pageSize,
53
+ total: totalAuditLogs,
54
+ auditLogs: auditLogs
55
+ };
56
+ return res.status(200).json(data);
57
+ } catch (error) {
58
+ console.error('Error fetching audit logs:', error);
59
+ return res.status(500).json({ error: 'Error fetching audit logs.' });
60
+ }
61
+ };
src/routes/auditLogs.ts ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import express from "express";
2
+ import {getAuditLogs} from "../controllers/auditLog.controller";
3
+
4
+ const auditLogRouter = express.Router();
5
+
6
+ auditLogRouter.get("/", getAuditLogs);
7
+
8
+ export default auditLogRouter