tango-app-api-audit 1.0.38 → 1.0.40

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.38",
3
+ "version": "1.0.40",
4
4
  "description": "audit & audit metrics apis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -1,4 +1,4 @@
1
- import { download, getUTC, insertOpenSearchData, logger, sendMessageToQueue } from 'tango-app-api-middleware';
1
+ import { chunkArray, download, getUTC, insertOpenSearchData, logger, sendMessageToQueue } from 'tango-app-api-middleware';
2
2
  import { aggregateAuditClientData } from '../service/auditClientData.service.js';
3
3
  import { aggregateUserAudit, updateOneUserAudit } from '../service/userAudit.service.js';
4
4
  import mongoose from 'mongoose';
@@ -21,7 +21,7 @@ export async function userAuditHistory( req, res ) {
21
21
  const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
22
22
  let filter = [
23
23
 
24
- { userId: { $eq: new mongoose.Types.ObjectId( userId ) } },
24
+ { userId: { $eq: userId } },
25
25
 
26
26
  ];
27
27
  if ( inputData.fileType == 'fileDate' ) {
@@ -36,6 +36,24 @@ export async function userAuditHistory( req, res ) {
36
36
  );
37
37
  }
38
38
 
39
+ if ( inputData.auditType?.length > 0 ) {
40
+ filter.push( {
41
+ moduleType: { $in: inputData.auditType },
42
+ } );
43
+ }
44
+
45
+ if ( inputData.filterByStoreId?.length > 0 ) {
46
+ filter.push( {
47
+ storeId: { $in: inputData.filterByStoreId },
48
+ } );
49
+ }
50
+
51
+ if ( inputData.filterByStatus?.length > 0 ) {
52
+ filter.push( {
53
+ auditStatus: { $in: inputData.filterByStatus },
54
+ } );
55
+ }
56
+
39
57
  const query = [
40
58
  {
41
59
  $match: {
@@ -44,17 +62,7 @@ export async function userAuditHistory( req, res ) {
44
62
  },
45
63
  ];
46
64
 
47
- if ( inputData.searchValue && inputData.searchValue!== '' ) {
48
- query.push( {
49
- $match: {
50
- $or: [
51
- { clientId: { $regex: inputData.searchValue, $options: 'i' } },
52
- { clientName: { $regex: inputData.searchValue, $options: 'i' } },
53
- ],
54
65
 
55
- },
56
- } );
57
- }
58
66
  if ( inputData.sortColumnName ) {
59
67
  const sortBy = inputData.sortBy || -1;
60
68
  query.push( {
@@ -63,11 +71,42 @@ export async function userAuditHistory( req, res ) {
63
71
  );
64
72
  }
65
73
 
74
+ query.push( {
75
+ $lookup: {
76
+ from: 'stores',
77
+ let: { storeId: '$storeId' },
78
+ pipeline: [
79
+ {
80
+ $match: {
81
+ $expr: {
82
+ $eq: [ '$storeId', '$$storeId' ],
83
+ },
84
+ },
85
+ },
86
+ {
87
+ $project: {
88
+ storeName: 1,
89
+ },
90
+ },
91
+ ], as: 'store',
92
+ },
93
+
94
+ } );
95
+
96
+ query.push( {
97
+ $unwind: {
98
+ path: '$store', preserveNullAndEmptyArrays: true,
99
+ },
100
+ } );
101
+
66
102
  query.push( {
67
103
  $project: {
68
104
  storeId: 1,
69
- storeName: '$storeId',
105
+ userId: 1,
106
+ storeName: '$store.storeName',
70
107
  auditType: 1,
108
+ zoneName: 1,
109
+ moduleType: 1,
71
110
  beforeCount: 1,
72
111
  afterCount: 1,
73
112
  accuracy: { $round: [
@@ -86,11 +125,57 @@ export async function userAuditHistory( req, res ) {
86
125
  },
87
126
  } );
88
127
 
128
+ if ( inputData.searchValue && inputData.searchValue!== '' ) {
129
+ query.push( {
130
+ $match: {
131
+ $or: [
132
+ { zoneName: { $regex: inputData.searchValue, $options: 'i' } },
133
+ { storeId: { $regex: inputData.storeId, $options: 'i' } },
134
+ { storeName: { $regex: inputData.storeName, $options: 'i' } },
135
+ { moduleType: { $regex: inputData.moduleType, $options: 'i' } },
136
+ { auditStatus: { $regex: inputData.auditStatus, $options: 'i' } },
137
+ ],
138
+
139
+ },
140
+ } );
141
+ }
142
+
89
143
  const count = await aggregateUserAudit( query );
90
144
  if ( count.length == 0 ) {
91
145
  return res.sendError( 'No Data Found', 204 );
92
146
  }
93
147
 
148
+ if ( inputData.isExport ) {
149
+ const chunkedMappingData = await chunkArray( count, 10 );
150
+ const promises = chunkedMappingData.map( async ( chunk ) => {
151
+ const exportData = [];
152
+ chunk.forEach( ( element ) => {
153
+ exportData.push( {
154
+ 'Store Id': element.storeId,
155
+ 'Store Name': element.storeName,
156
+ 'Type': element.auditType,
157
+ 'Before Count': element.beforeCount,
158
+ 'After Count': element.afterCount,
159
+ 'Accuracy': element.accuracy,
160
+ 'Zone Name': element.zoneName,
161
+ 'Audit Type': element.moduleType,
162
+ 'Start Time': element.startTime,
163
+ 'End Time': element.endTime,
164
+ 'Time Spent': element.timeSpent,
165
+ 'Audit Status': element.auditStatus,
166
+ 'CreatedAt': element.createdAt,
167
+
168
+ } );
169
+ } );
170
+ return exportData;
171
+ } );
172
+ const mappedArrays = await Promise.all( promises );
173
+ mappedArrays.flat();
174
+
175
+ await download( mappedArrays, res );
176
+ return;
177
+ }
178
+
94
179
  query.push( {
95
180
  $skip: offset,
96
181
  },
@@ -1,4 +1,4 @@
1
- import { download, fileUpload, getJsonFileData, getUTC, insertOpenSearchData, listFileByPath, sendMessageToQueue, signedUrl, sqsReceive } from 'tango-app-api-middleware';
1
+ import { chunkArray, download, fileUpload, getJsonFileData, getUTC, insertOpenSearchData, listFileByPath, sendMessageToQueue, signedUrl, sqsReceive } from 'tango-app-api-middleware';
2
2
  import { aggregateZoneUserAudit, createZoneUserAudit, findOneZoneUserAudit } from '../service/zoneUserAudit.service.js';
3
3
  import { logger } from 'tango-app-api-middleware';
4
4
  import { aggregateAssignZoneAudit } from '../service/assignZoneAudit.service.js';
@@ -898,3 +898,172 @@ export async function workSpace( req, res ) {
898
898
  return res.sendError( err, 500 );
899
899
  }
900
900
  }
901
+
902
+ export async function userZoneAuditHistory( req, res ) {
903
+ try {
904
+ const inputData = req.body;
905
+ const userId = inputData.userId || req.user._id;
906
+ const limit = inputData.limit || 10;
907
+ const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
908
+ const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
909
+ let filter = [
910
+
911
+ { userId: { $eq: userId } },
912
+ { moduleType: 'zone' },
913
+
914
+ ];
915
+ if ( inputData.fileType == 'fileDate' ) {
916
+ filter.push(
917
+ { fileDateISO: { $gte: dateRange.start } },
918
+ { fileDateISO: { $lte: dateRange.end } },
919
+ );
920
+ } else {
921
+ filter.push(
922
+ { createdAt: { $gte: dateRange.start } },
923
+ { createdAt: { $lte: dateRange.end } },
924
+ );
925
+ }
926
+ if ( inputData.filterByStoreId?.length > 0 ) {
927
+ filter.push( {
928
+ storeId: { $in: inputData.filterByStoreId },
929
+ } );
930
+ }
931
+
932
+ if ( inputData.filterByStatus?.length > 0 ) {
933
+ filter.push( {
934
+ auditStatus: { $in: inputData.filterByStatus },
935
+ } );
936
+ }
937
+
938
+ const query = [
939
+ {
940
+ $match: {
941
+ $and: filter,
942
+ },
943
+ },
944
+ ];
945
+
946
+ if ( inputData.searchValue && inputData.searchValue!== '' ) {
947
+ query.push( {
948
+ $match: {
949
+ $or: [
950
+ { clientId: { $regex: inputData.searchValue, $options: 'i' } },
951
+ { clientName: { $regex: inputData.searchValue, $options: 'i' } },
952
+ ],
953
+
954
+ },
955
+ } );
956
+ }
957
+ if ( inputData.sortColumnName ) {
958
+ const sortBy = inputData.sortBy || -1;
959
+ query.push( {
960
+ $sort: { [inputData.sortColumName]: sortBy },
961
+ },
962
+ );
963
+ }
964
+
965
+ query.push( {
966
+ $lookup: {
967
+ from: 'stores',
968
+ let: { storeId: '$storeId' },
969
+ pipeline: [
970
+ {
971
+ $match: {
972
+ $expr: {
973
+ $eq: [ '$storeId', '$$storeId' ],
974
+ },
975
+ },
976
+ },
977
+ {
978
+ $project: {
979
+ storeName: 1,
980
+ },
981
+ },
982
+ ], as: 'store',
983
+ },
984
+
985
+ } );
986
+
987
+ query.push( {
988
+ $unwind: {
989
+ path: '$store', preserveNullAndEmptyArrays: true,
990
+ },
991
+ } );
992
+
993
+
994
+ query.push( {
995
+
996
+ $project: {
997
+ storeId: 1,
998
+ userId: 1,
999
+ storeName: '$store.storeName',
1000
+ zoneName: 1,
1001
+ auditType: 1,
1002
+ beforeCount: 1,
1003
+ afterCount: 1,
1004
+ accuracy: { $round: [
1005
+ {
1006
+ $divide: [ { $multiply: [ '$beforeCount', { $ifNull: [ '$afterCount', 0 ] } ] }, 100 ],
1007
+ }, 2,
1008
+ ],
1009
+ },
1010
+ startTime: 1,
1011
+ endTime: 1,
1012
+ timeSpent: { $round: [
1013
+ { $divide: [ '$timeSpent', 60 ] }, 2,
1014
+ ] },
1015
+ auditStatus: 1,
1016
+ createdAt: 1,
1017
+ },
1018
+
1019
+ } );
1020
+
1021
+ const count = await aggregateUserAudit( query );
1022
+ if ( count.length == 0 ) {
1023
+ return res.sendError( 'No Data Found', 204 );
1024
+ }
1025
+
1026
+ if ( inputData.isExport ) {
1027
+ const chunkedMappingData = await chunkArray( count, 10 );
1028
+ const promises = chunkedMappingData.map( async ( chunk ) => {
1029
+ const exportData = [];
1030
+ chunk.forEach( ( element ) => {
1031
+ exportData.push( {
1032
+ 'storeId': element.storeId,
1033
+ 'storeName': element.storeName,
1034
+ 'auditType': element.auditType,
1035
+ 'beforeCount': element.beforeCount,
1036
+ 'afterCount': element.afterCount,
1037
+ 'accuracy': element.accuracy,
1038
+ 'startTime': element.startTime,
1039
+ 'endTime': element.endTime,
1040
+ 'timeSpent': element.timeSpent,
1041
+ 'auditStatus': element.auditStatus,
1042
+ 'createdAt': element.createdAt,
1043
+
1044
+ } );
1045
+ } );
1046
+ return exportData;
1047
+ } );
1048
+ const mappedArrays = await Promise.all( promises );
1049
+ mappedArrays.flat();
1050
+
1051
+ await download( mappedArrays, res );
1052
+ return;
1053
+ }
1054
+
1055
+ query.push( {
1056
+ $skip: offset,
1057
+ },
1058
+ {
1059
+ $limit: limit,
1060
+ } );
1061
+ const result = await aggregateUserAudit( query );
1062
+
1063
+ return res.sendSuccess( { result: result } );
1064
+ } catch ( error ) {
1065
+ const err = error.message || 'Internal Server Error';
1066
+ logger.info( { error: error, message: req.body, function: 'userZoneAuditHistory' } );
1067
+ return res.sendError( err, 500 );
1068
+ }
1069
+ }
@@ -44,6 +44,7 @@ export const storeMetricsSchema = joi.object(
44
44
  filterByClientId: joi.array().required(),
45
45
  filterByStoreId: joi.array().optional(),
46
46
  filterByStatus: joi.array().optional(),
47
+ filterByAuditType: joi.array().optional(),
47
48
  sortColumnName: joi.string().optional(),
48
49
  sortBy: joi.number().optional(),
49
50
  limit: joi.number().optional(),
@@ -86,3 +86,4 @@ export const workSpaceSchema = joi.object(
86
86
  export const workSpaceValid = {
87
87
  query: workSpaceSchema,
88
88
  };
89
+
@@ -6,10 +6,9 @@ import { getDraftedData, getZoneAuditFile, save, saveDraft, workSpace } from '..
6
6
 
7
7
  export const zoneAuditRouter=Router();
8
8
 
9
-
9
+ // zone Audit
10
10
  zoneAuditRouter.get( '/get-file', isAllowedSessionHandler, validate( getFileValid ), isExistsQueue, getZoneAuditFile );
11
11
  zoneAuditRouter.post( '/save-draft', isAllowedSessionHandler, validate( saveDraftValid ), saveDraft );
12
12
  zoneAuditRouter.get( '/get-drafted-data', isAllowedSessionHandler, validate( getDraftedDataValid ), getDraftedData );
13
13
  zoneAuditRouter.post( '/save', isAllowedSessionHandler, validate( saveValid ), validateUserAudit, save );
14
14
  zoneAuditRouter.get( '/work-space', isAllowedSessionHandler, validate( workSpaceValid ), workSpace );
15
-