tango-app-api-audit 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-audit",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "audit & audit metrics apis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -475,7 +475,7 @@ export async function workSpace( req, res ) {
475
475
  },
476
476
  ];
477
477
 
478
- if ( inputData.searchValue ) {
478
+ if ( inputData.searchValue&& inputData.searchValue!== '' ) {
479
479
  clientQuery.push( {
480
480
  $match: {
481
481
  $or: [
@@ -1,5 +1,7 @@
1
1
  import { download, getUTC, logger } from 'tango-app-api-middleware';
2
2
  import { aggregateAuditClientData } from '../service/auditClientData.service.js';
3
+ import { aggregateUserAudit } from '../service/userAudit.service.js';
4
+ import mongoose from 'mongoose';
3
5
  // import { listQueue } from 'tango-app-api-middleware/src/utils/aws/sqs.js';
4
6
 
5
7
 
@@ -15,13 +17,98 @@ export async function getAuditImageList( req, res ) {
15
17
  }
16
18
 
17
19
  export async function userAuditHistory( req, res ) {
18
- // try {
19
- // const inputData = req.body;
20
- // return res.sendSuccess( { result: inputData } );
21
- // } catch ( error ) {
22
- // logger.info( { error: error, message: req.body, function: 'userAuditHistory' } );
23
- // return res.sendError( error );
24
- // }
20
+ try {
21
+ const inputData = req.body;
22
+ const userId = inputData.userId || req.user._id;
23
+ const limit = inputData.limit || 10;
24
+ const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
25
+ const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
26
+ let filter = [
27
+
28
+ { userId: { $eq: new mongoose.Types.ObjectId( userId ) } },
29
+
30
+ ];
31
+ if ( inputData.fileType == 'fileDate' ) {
32
+ filter.push(
33
+ { fileDateISO: { $gte: dateRange.start } },
34
+ { fileDateISO: { $lte: dateRange.end } },
35
+ );
36
+ } else {
37
+ filter.push(
38
+ { createdAt: { $gte: dateRange.start } },
39
+ { createdAt: { $lte: dateRange.end } },
40
+ );
41
+ }
42
+
43
+ const query = [
44
+ {
45
+ $match: {
46
+ $and: filter,
47
+ },
48
+ },
49
+ ];
50
+
51
+ if ( inputData.searchValue && inputData.searchValue!== '' ) {
52
+ query.push( {
53
+ $match: {
54
+ $or: [
55
+ { clientId: { $regex: inputData.searchValue, $options: 'i' } },
56
+ { clientName: { $regex: inputData.searchValue, $options: 'i' } },
57
+ ],
58
+
59
+ },
60
+ } );
61
+ }
62
+ if ( inputData.sortColumnName ) {
63
+ const sortBy = inputData.sortBy || -1;
64
+ query.push( {
65
+ $sort: { [inputData.sortColumName]: sortBy },
66
+ },
67
+ );
68
+ }
69
+
70
+ query.push( {
71
+ $project: {
72
+ storeId: 1,
73
+ storeName: '$storeId',
74
+ auditType: 1,
75
+ beforeCount: 1,
76
+ afterCount: 1,
77
+ accuracy: { $round: [
78
+ {
79
+ $divide: [ { $multiply: [ '$beforeCount', { $ifNull: [ '$afterCount', 0 ] } ] }, 100 ],
80
+ }, 2,
81
+ ],
82
+ },
83
+ startTime: 1,
84
+ endTime: 1,
85
+ timeSpent: { $round: [
86
+ { $divide: [ '$timeSpent', 60 ] }, 2,
87
+ ] },
88
+ auditStatus: 1,
89
+ createdAt: 1,
90
+ },
91
+ } );
92
+
93
+ const count = await aggregateUserAudit( query );
94
+ if ( count.length == 0 ) {
95
+ return res.sendError( 'No Data Found', 204 );
96
+ }
97
+
98
+ query.push( {
99
+ $skip: offset,
100
+ },
101
+ {
102
+ $limit: limit,
103
+ } );
104
+ const result = await aggregateUserAudit( query );
105
+
106
+ return res.sendSuccess( { result: result } );
107
+ } catch ( error ) {
108
+ const err = error.message || 'Internal Server Error';
109
+ logger.info( { error: error, message: req.body, function: 'userAuditHistory' } );
110
+ return res.sendError( err, 500 );
111
+ }
25
112
  }
26
113
 
27
114
  export async function storeMetrics( req, res ) {
@@ -347,7 +434,7 @@ export async function clientMetrics( req, res ) {
347
434
  },
348
435
  },
349
436
  ];
350
- if ( inputData.searchValue ) {
437
+ if ( inputData.searchValue && inputData.searchValue!== '' ) {
351
438
  query.push( {
352
439
  $match: {
353
440
  $or: [
@@ -1,5 +1,5 @@
1
1
  import j2s from 'joi-to-swagger';
2
- import { clientMetricsSchema, getAuditImagesSchema } from '../dtos/auditMetrics.dtos.js';
2
+ import { clientMetricsSchema, getAuditImagesSchema, userAuditHistorySchema } from '../dtos/auditMetrics.dtos.js';
3
3
 
4
4
  export const auditMetricsDocs = {
5
5
 
@@ -47,4 +47,26 @@ export const auditMetricsDocs = {
47
47
  },
48
48
  },
49
49
  },
50
+ '/v3/audit-metrics/user-audit-history': {
51
+ post: {
52
+ tags: [ 'Audit Metrics' ],
53
+ description: `Get list of user wise audit user details with date range`,
54
+ operationId: 'user-audit-history',
55
+ parameters: {},
56
+ requestBody: {
57
+ content: {
58
+ 'application/json': {
59
+ schema: j2s( userAuditHistorySchema ).swagger,
60
+ },
61
+ },
62
+ },
63
+ responses: {
64
+ 200: { description: 'Successful' },
65
+ 401: { description: 'Unauthorized User' },
66
+ 422: { description: 'Field Error' },
67
+ 500: { description: 'Server Error' },
68
+ 204: { description: 'Not Found' },
69
+ },
70
+ },
71
+ },
50
72
  };
@@ -62,7 +62,7 @@ export const saveValid = {
62
62
 
63
63
  export const workSpaceSchema = joi.object(
64
64
  {
65
- searchValue: joi.string().optional(),
65
+ searchValue: joi.string().optional().allow( '' ),
66
66
  offset: joi.string().optional(),
67
67
  limit: joi.number().optional(),
68
68
  isExport: joi.boolean().optional(),
@@ -15,12 +15,20 @@ export const getAuditImagesValid = {
15
15
 
16
16
  export const userAuditHistorySchema = joi.object(
17
17
  {
18
- fromDate: joi.string().required(), // need to clarify about keys --userId
18
+ userId: joi.string().optional(),
19
+ fromDate: joi.string().required(),
19
20
  toDate: joi.string().required(),
20
- storeId: joi.string().required(),
21
- fileDate: joi.string().required(),
22
21
  fileType: joi.string().required(),
23
- storeId: joi.string().required(),
22
+ filterByStoreId: joi.array().required(),
23
+ filterByType: joi.array().optional(),
24
+ filterByStatus: joi.array().optional(),
25
+ searchValue: joi.string().optional().allow( '' ),
26
+ limit: joi.number().optional(),
27
+ offset: joi.number().optional(),
28
+ sortColumnName: joi.string().optional(),
29
+ sortBy: joi.number().optional(),
30
+ isExport: joi.boolean().optional(),
31
+
24
32
  },
25
33
  );
26
34
 
@@ -45,7 +53,7 @@ export const clientMetricsSchema = joi.object(
45
53
  {
46
54
  fromDate: joi.string().required(),
47
55
  toDate: joi.string().required(),
48
- searchValue: joi.string().optional(),
56
+ searchValue: joi.string().optional().allow( '' ),
49
57
  filterByClient: joi.array().required(),
50
58
  filterByStatus: joi.array().optional(),
51
59
  sortColumnName: joi.string().optional(),