narinder1231 commited on
Commit
b5dcc75
·
1 Parent(s): 4e912ee

seprate logic to build where clause for audit log

Browse files
src/controllers/auditLog.controller.ts CHANGED
@@ -3,6 +3,25 @@ import { Op, FindOptions } from 'sequelize';
3
  import AuditLog from '../models/auditLogs';
4
  import { AuditLogInterface } from '../shared/interfaces/auditLog.interface';
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  const getAuditLogs = async (req: Request, res: Response) => {
7
 
8
  try {
@@ -11,22 +30,7 @@ const getAuditLogs = async (req: Request, res: Response) => {
11
 
12
  const allowedSortColumns = ['id','action_by', 'invoice_id']
13
 
14
- const whereClause: any = {};
15
-
16
- if (filter) {
17
- if (filter.date) {
18
- const date = new Date(filter.date);
19
- if (!isNaN(date.getTime())) {
20
- whereClause.created_at = { [Op.eq]: date };
21
- }
22
- }
23
- if (filter.action_by) {
24
- whereClause.action_by = { [Op.eq]: filter.action_by };
25
- }
26
- if (filter.invoice_id) {
27
- whereClause.invoice_id = { [Op.eq]: filter.invoice_id };
28
- }
29
- }
30
 
31
  const currentPage = parseInt(page as string) || 1;
32
  const pageSize = parseInt(limit as string) || 10;
 
3
  import AuditLog from '../models/auditLogs';
4
  import { AuditLogInterface } from '../shared/interfaces/auditLog.interface';
5
 
6
+ const buildAuditLogWhereClause = (filter: Record<string, any>): 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
+ if (filter.action_by) {
16
+ whereClause.action_by = { [Op.eq]: filter.action_by };
17
+ }
18
+ if (filter.invoice_id) {
19
+ whereClause.invoice_id = { [Op.eq]: filter.invoice_id };
20
+ }
21
+ }
22
+ return whereClause;
23
+ };
24
+
25
  const getAuditLogs = async (req: Request, res: Response) => {
26
 
27
  try {
 
30
 
31
  const allowedSortColumns = ['id','action_by', 'invoice_id']
32
 
33
+ const whereClause = buildAuditLogWhereClause(filter);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  const currentPage = parseInt(page as string) || 1;
36
  const pageSize = parseInt(limit as string) || 10;