tango-app-api-audit 3.4.2 → 3.4.3-alpha.1
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 +2 -3
- package/src/controllers/audit.controllers.js +715 -129
- package/src/controllers/traxAudit.controllers.js +1279 -702
- package/src/docs/audit.docs.js +29 -1
- package/src/docs/traxAudit.docs.js +128 -14
- package/src/dtos/audit.dtos.js +0 -2
- package/src/dtos/traxAudit.dtos.js +20 -6
- package/src/routes/audit.routes.js +5 -1
- package/src/routes/traxAudit.routes.js +8 -7
- package/src/service/auditStoreData.service.js +8 -0
- package/src/service/auditUserWallet.service.js +10 -0
- package/src/service/empDetectionOutput.service.js +5 -0
- package/src/service/storeEmpDetection.service.js +7 -0
- package/src/service/userAudit.service.js +4 -0
- package/src/service/userEmpDetection.service.js +6 -0
|
@@ -2,540 +2,541 @@ import { checkFileExist, chunkArray, download, getDate, getDateWithCustomizeTime
|
|
|
2
2
|
import { aggregateBinaryAudit, createBinaryAudit, updateOneBinaryAuditModel } from '../service/binaryAudit.service.js';
|
|
3
3
|
import { countDocumentsStore, findOneStore } from '../service/store.service.js';
|
|
4
4
|
import { findOneUser } from '../service/user.service.js';
|
|
5
|
-
import { aggregateUserEmpDetection, createUserEmpDetection } from '../service/userEmpDetection.service.js';
|
|
5
|
+
import { aggregateUserEmpDetection, createUserEmpDetection, updateOneUserEmpDetection } from '../service/userEmpDetection.service.js';
|
|
6
6
|
import { aggregateTraxAuditData, findOneTraxAuditData } from '../service/traxAuditData.service.js';
|
|
7
7
|
import { aggregateClient } from '../service/client.service.js';
|
|
8
8
|
import dayjs from 'dayjs';
|
|
9
9
|
import { aggregateAssignAudit } from '../service/assignAudit.service.js';
|
|
10
|
-
import { aggregateStoreEmpDetection } from '../service/storeEmpDetection.service.js';
|
|
10
|
+
import { aggregateStoreEmpDetection, updateManyStoreEmpDetection } from '../service/storeEmpDetection.service.js';
|
|
11
11
|
import { listQueue } from 'tango-app-api-middleware/src/utils/aws/sqs.js';
|
|
12
12
|
import _ from 'lodash';
|
|
13
13
|
import mongoose from 'mongoose';
|
|
14
|
+
import { findOneAuditLog } from '../service/auditLog.service.js';
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
// export async function getDetectionAuditFile( req, res ) {
|
|
18
|
+
// try {
|
|
19
|
+
// const bucket = JSON.parse( process.env.BUCKET );
|
|
20
|
+
// const openSearch = JSON.parse( process.env.OPENSEARCH );
|
|
21
|
+
// const inputData = req.query;
|
|
22
|
+
// const previousDate = new Date( new Date() - 1 );
|
|
23
|
+
// const data = await getDate( previousDate, new Date() );
|
|
24
|
+
// logger.info( { data: data, function: 'data', newDate: new Date() } );
|
|
25
|
+
// logger.info( { userId: req.user._id, queueName: inputData.queueName, moduleType: inputData.moduleType } );
|
|
26
|
+
// const start = data.start;
|
|
27
|
+
// const end = data.end;
|
|
28
|
+
// const fetchData = {};
|
|
29
|
+
// let msg = {};
|
|
30
|
+
// const userQuery = [
|
|
31
|
+
// {
|
|
32
|
+
// $match: {
|
|
33
|
+
// $and: [
|
|
34
|
+
// { userId: req.user._id },
|
|
35
|
+
// { queueName: inputData.queueName },
|
|
36
|
+
// { auditStatus: { $nin: [ 'completed', 'skipped' ] } },
|
|
37
|
+
// { createdAt: { $gte: start } },
|
|
38
|
+
// { createdAt: { $lte: end } },
|
|
39
|
+
// { moduleType: inputData.moduleType },
|
|
40
|
+
// ],
|
|
41
|
+
// },
|
|
42
|
+
// },
|
|
43
|
+
// {
|
|
44
|
+
// $sort: { createdAt: -1 },
|
|
45
|
+
// },
|
|
46
|
+
// {
|
|
47
|
+
// $limit: 1,
|
|
48
|
+
// },
|
|
49
|
+
// ];
|
|
50
|
+
|
|
51
|
+
// const userDetails = await aggregateUserEmpDetection( userQuery );
|
|
52
|
+
// logger.info( { userDetails: userDetails, function: 'userDetails' } );
|
|
53
|
+
// const auditStatus =
|
|
54
|
+
// userDetails && userDetails.length > 0 ? userDetails[0].auditStatus : null;
|
|
55
|
+
// switch ( auditStatus ) {
|
|
56
|
+
// case null:
|
|
57
|
+
// const query = [
|
|
58
|
+
// {
|
|
59
|
+
// $match: {
|
|
60
|
+
// $and: [
|
|
61
|
+
// { userId: { $eq: req.user._id } },
|
|
62
|
+
// { isCompleted: { $eq: false } },
|
|
63
|
+
// { queueName: inputData.queueName },
|
|
64
|
+
// { moduleType: inputData.moduleType },
|
|
65
|
+
// ],
|
|
66
|
+
// },
|
|
67
|
+
// },
|
|
68
|
+
// ];
|
|
69
|
+
// const userAssign = await aggregateAssignAudit( query );
|
|
70
|
+
// if ( userAssign.length > 0 ) {
|
|
71
|
+
// logger.info( 'Hit in user assign', { inputData } );
|
|
72
|
+
// userAssign[0].auditType == 'Audit' ?
|
|
73
|
+
// ( msg = {
|
|
74
|
+
// store_id: userAssign[0].storeId,
|
|
75
|
+
// curr_date: userAssign[0].fileDate,
|
|
76
|
+
// total_count: userAssign[0].fileCount,
|
|
77
|
+
// zone_id: userAssign[0].zoneName,
|
|
78
|
+
// } ) :
|
|
79
|
+
// ( msg = {
|
|
80
|
+
// store_id: userAssign[0].storeId,
|
|
81
|
+
// curr_date: userAssign[0].fileDate,
|
|
82
|
+
// audit_type: userAssign[0].auditType,
|
|
83
|
+
// total_count: userAssign[0].fileCount,
|
|
84
|
+
// zone_id: userAssign[0].zoneName,
|
|
85
|
+
// } );
|
|
86
|
+
// await updateOneAssignAudit(
|
|
87
|
+
// { _id: userAssign[0]._id },
|
|
88
|
+
// { isCompleted: true },
|
|
89
|
+
// );
|
|
90
|
+
// logger.info( 'Hit in auditUserAssignModel updateOne', {
|
|
91
|
+
// _id: userAssign[0]._id,
|
|
92
|
+
// isCompleted: true,
|
|
93
|
+
// } );
|
|
94
|
+
// } else {
|
|
95
|
+
// logger.info( 'Hit in new file', { inputData } );
|
|
96
|
+
// const consumer = await sqsReceive( inputData.queueName );
|
|
97
|
+
// if ( !consumer ) {
|
|
98
|
+
// logger.error( {
|
|
99
|
+
// error: consumer,
|
|
100
|
+
// function: 'getAuditFile',
|
|
101
|
+
// message: 'SQS Receive queue is empty',
|
|
102
|
+
// } );
|
|
103
|
+
// return res.sendError( `${inputData.queueName} is Empty`, 204 );
|
|
104
|
+
// }
|
|
105
|
+
// msg = JSON.parse( consumer );
|
|
106
|
+
// }
|
|
107
|
+
// break;
|
|
108
|
+
|
|
109
|
+
// case 'drafted':
|
|
110
|
+
// logger.info( 'userDetails drafted', { inputData } );
|
|
111
|
+
// const log = await findOneAuditLog(
|
|
112
|
+
// {
|
|
113
|
+
// userId: userDetails[0].userId,
|
|
114
|
+
// fileDate: userDetails[0].fileDate,
|
|
115
|
+
// storeId: userDetails[0].storeId,
|
|
116
|
+
// totalCount: userDetails[0].beforeCount,
|
|
117
|
+
// moduleType: userDetails[0].moduleType,
|
|
118
|
+
// zoneName: userDetails[0].zoneName,
|
|
119
|
+
// },
|
|
120
|
+
// {},
|
|
121
|
+
// { createdAt: -1 },
|
|
122
|
+
// );
|
|
123
|
+
// if ( !log ) {
|
|
124
|
+
// await updateOneUserAudit(
|
|
125
|
+
// { _id: userDetails[0]._id },
|
|
126
|
+
// { $set: { isDraft: false, auditStatus: 'skipped' } },
|
|
127
|
+
// );
|
|
128
|
+
// logger.info( 'audit update in drafted', {
|
|
129
|
+
// _id: userDetails[0]._id,
|
|
130
|
+
// isDraft: false,
|
|
131
|
+
// auditStatus: 'skipped',
|
|
132
|
+
// } );
|
|
133
|
+
// return res.sendError( 'User saved data has been deleted', 204 );
|
|
134
|
+
// }
|
|
135
|
+
// const file = {
|
|
136
|
+
// auditId: userDetails[0]._id,
|
|
137
|
+
// store_id: userDetails[0].storeId,
|
|
138
|
+
// file_date: userDetails[0].fileDate,
|
|
139
|
+
// type: userDetails[0].auditType,
|
|
140
|
+
// queueName: log.queueName,
|
|
141
|
+
// userId: log.userId,
|
|
142
|
+
// moduleType: userDetails[0].moduleType,
|
|
143
|
+
// zoneName: userDetails[0].zoneName,
|
|
144
|
+
// };
|
|
145
|
+
// const result = {
|
|
146
|
+
// junk: log.junk,
|
|
147
|
+
// junk_count: log.junkCount,
|
|
148
|
+
// employee: log.employee,
|
|
149
|
+
// employee_count: log.employeeCount,
|
|
150
|
+
// customer: log.customer,
|
|
151
|
+
// customer_count: log.customerCount,
|
|
152
|
+
// retag_image: log.retagImage,
|
|
153
|
+
// retag_count: log.retagCount,
|
|
154
|
+
// total_count: log.totalCount,
|
|
155
|
+
// };
|
|
156
|
+
|
|
157
|
+
// const userdata = await findOneUser( { _id: log.userId } );
|
|
158
|
+
// if (
|
|
159
|
+
// !inputData.nextId ||
|
|
160
|
+
// inputData.nextId === '' ||
|
|
161
|
+
// inputData.nextId == null
|
|
162
|
+
// ) {
|
|
163
|
+
// const logData = {
|
|
164
|
+
// userId: log.userId,
|
|
165
|
+
// userName: userdata.name,
|
|
166
|
+
// logType: 'audit',
|
|
167
|
+
// logSubType: 'auditStart',
|
|
168
|
+
// logData: {
|
|
169
|
+
// fileDate: userDetails[0].fileDate,
|
|
170
|
+
// auditType: userDetails[0].auditType,
|
|
171
|
+
// storeId: userDetails[0].storeId,
|
|
172
|
+
// clientName: log.queueName,
|
|
173
|
+
// auditId: userDetails[0]._id,
|
|
174
|
+
// moduleType: userDetails[0].moduleType,
|
|
175
|
+
// zoneName: userDetails[0].zoneName,
|
|
176
|
+
// },
|
|
177
|
+
// createdAt: new Date(),
|
|
178
|
+
// };
|
|
179
|
+
// logger.info( { logData: logData } );
|
|
180
|
+
// await insertOpenSearchData( openSearch.auditLog, logData );
|
|
181
|
+
// }
|
|
182
|
+
// const storeQuery = {
|
|
183
|
+
// storeId: userDetails[0].storeId,
|
|
184
|
+
// };
|
|
185
|
+
// const storeFields = {
|
|
186
|
+
// storeId: 1,
|
|
187
|
+
// storeName: 1,
|
|
188
|
+
// address: '$storeProfile.address',
|
|
189
|
+
// };
|
|
190
|
+
// const storeDetails = await findOneStore( storeQuery, storeFields );
|
|
191
|
+
// res.sendSuccess( {
|
|
192
|
+
// result: result,
|
|
193
|
+
// storeId: storeDetails?.storeId,
|
|
194
|
+
// storeName: storeDetails?.storeName,
|
|
195
|
+
// address: storeDetails?.address || '',
|
|
196
|
+
// count: log.totalCount,
|
|
197
|
+
// file: file,
|
|
198
|
+
// isDraft: userDetails[0].isDraft,
|
|
199
|
+
// } );
|
|
200
|
+
// break;
|
|
201
|
+
|
|
202
|
+
// default:
|
|
203
|
+
// msg = {
|
|
204
|
+
// store_id: userDetails[0].storeId,
|
|
205
|
+
// curr_date: userDetails[0].fileDate,
|
|
206
|
+
// audit_type: userDetails[0].auditType,
|
|
207
|
+
// total_count: userDetails[0].beforeCount,
|
|
208
|
+
// zone_id: userDetails[0].zoneName,
|
|
209
|
+
// };
|
|
210
|
+
// break;
|
|
211
|
+
// }
|
|
212
|
+
// if ( msg.audit_type === 'ReAudit' ) {
|
|
213
|
+
// logger.info( 'Hit in user ReAudit', { inputData } );
|
|
214
|
+
// let reauditInsert = {};
|
|
215
|
+
// const [ filterData, auditImage ] = await Promise.all( [
|
|
216
|
+
// getFilterData( msg ),
|
|
217
|
+
// getAuditImage( msg ),
|
|
218
|
+
// ] );
|
|
219
|
+
// if ( filterData.statusCode == 404 || auditImage.statusCode == 404 ) {
|
|
220
|
+
// return res.sendError( 'No Data found', 204 );
|
|
221
|
+
// }
|
|
222
|
+
// const reauditImg = await getReauditImg( filterData, auditImage );
|
|
223
|
+
// if ( userDetails.length === 0 ) {
|
|
224
|
+
// const [ day, month, year ] = msg.curr_date.split( '-' );
|
|
225
|
+
// const temp = `${year}-${month}-${day}`;
|
|
226
|
+
// let start = new Date( temp );
|
|
227
|
+
// const userTimezoneOffset = start.getTimezoneOffset() * 60000;
|
|
228
|
+
// start = new Date( start.getTime() - userTimezoneOffset );
|
|
229
|
+
// start.setUTCHours( 0, 0, 0, 0 );
|
|
230
|
+
// const record = {
|
|
231
|
+
// userId: req.user._id,
|
|
232
|
+
// storeId: msg.store_id,
|
|
233
|
+
// clientId: msg.store_id.split( '-' )[0],
|
|
234
|
+
// auditType: msg.audit_type || 'ReAudit',
|
|
235
|
+
// fileDate: msg.curr_date,
|
|
236
|
+
// queueName: inputData.queueName,
|
|
237
|
+
// beforeCount: reauditImg.length,
|
|
238
|
+
// auditStatus: 'inprogress',
|
|
239
|
+
// fileDateISO: start,
|
|
240
|
+
// timeSpent: 0,
|
|
241
|
+
// startTime: new Date(),
|
|
242
|
+
// zoneName: msg.zone_id,
|
|
243
|
+
// moduleType: inputData.moduleType,
|
|
244
|
+
// };
|
|
245
|
+
// const query ={
|
|
246
|
+
// storeId: msg.store_id,
|
|
247
|
+
// zoneName: msg.zone_id,
|
|
248
|
+
// fileDate: msg.curr_date,
|
|
249
|
+
// moduleType: inputData.moduleType,
|
|
250
|
+
// };
|
|
251
|
+
// let storeRecord = {
|
|
252
|
+
// userId: req.user._id,
|
|
253
|
+
// auditType: msg.audit_type || 'ReAudit',
|
|
254
|
+
// status: 'inprogress',
|
|
255
|
+
// timeSpent: 0,
|
|
256
|
+
// };
|
|
257
|
+
// reauditInsert = await createUserAudit( record );
|
|
258
|
+
|
|
259
|
+
// await updateOneStoreAudit( query, storeRecord );
|
|
260
|
+
// } else {
|
|
261
|
+
// reauditInsert = userDetails[0];
|
|
262
|
+
// }
|
|
263
|
+
// const userdata = await findOneUser( { _id: req.user._id } );
|
|
264
|
+
// if ( inputData.nextId ) {
|
|
265
|
+
// const logData = {
|
|
266
|
+
// userId: req.user._id,
|
|
267
|
+
// userName: userdata.name,
|
|
268
|
+
// logType: 'audit',
|
|
269
|
+
// logSubType: 'auditStart',
|
|
270
|
+
// logData: {
|
|
271
|
+
// fileDate: msg.curr_date,
|
|
272
|
+
// auditType: msg.audit_type,
|
|
273
|
+
// storeId: msg.store_id,
|
|
274
|
+
// clientName: inputData.queueName,
|
|
275
|
+
// auditId: reauditInsert._id,
|
|
276
|
+
// zoneName: msg.zone_id,
|
|
277
|
+
// moduleType: inputData.moduleType,
|
|
278
|
+
// },
|
|
279
|
+
// createdAt: new Date(),
|
|
280
|
+
// };
|
|
281
|
+
// await insertOpenSearchData( openSearch.auditLog, logData );
|
|
282
|
+
// }
|
|
283
|
+
|
|
284
|
+
// const storeQuery = {
|
|
285
|
+
// storeId: msg.store_id,
|
|
286
|
+
// };
|
|
287
|
+
// const storeFields = {
|
|
288
|
+
// storeId: 1,
|
|
289
|
+
// storeName: 1,
|
|
290
|
+
// address: '$storeProfile.address',
|
|
291
|
+
// };
|
|
292
|
+
// const storeInfo = await findOneStore( storeQuery, storeFields );
|
|
293
|
+
// return res.sendSuccess( {
|
|
294
|
+
// result: reauditImg,
|
|
295
|
+
// count: reauditImg.length,
|
|
296
|
+
// storeId: storeInfo?.storeId,
|
|
297
|
+
// storeName: storeInfo?.storeName,
|
|
298
|
+
// address: storeInfo?.address || '',
|
|
299
|
+
// file: {
|
|
300
|
+
// store_id: msg.store_id,
|
|
301
|
+
// file_date: msg.curr_date,
|
|
302
|
+
// type: msg.audit_type || 'ReAudit',
|
|
303
|
+
// queueName: inputData.queueName,
|
|
304
|
+
// auditId: reauditInsert._id,
|
|
305
|
+
// userId: reauditInsert.userId,
|
|
306
|
+
// zoneName: msg.zone_id,
|
|
307
|
+
// moduleType: inputData.moduleType,
|
|
308
|
+
// },
|
|
309
|
+
// isDraft: reauditInsert.isDraft,
|
|
310
|
+
// } );
|
|
311
|
+
// }
|
|
312
|
+
// const storeId = msg.store_id;
|
|
313
|
+
// const fileDate = msg.curr_date;
|
|
314
|
+
|
|
315
|
+
// const files = [];
|
|
316
|
+
// let insertData = {};
|
|
317
|
+
// fetchData.Bucket = bucket.auditInput; // need to change
|
|
318
|
+
// fetchData.file_path = `${fileDate}/${storeId}/${msg.zone_id}/`;
|
|
319
|
+
// if ( inputData.limit ) {
|
|
320
|
+
// fetchData.MaxKeys = inputData.limit;
|
|
321
|
+
// }
|
|
322
|
+
// if ( inputData.nextId ) {
|
|
323
|
+
// fetchData.ContinuationToken = decodeURIComponent( inputData.nextId );
|
|
324
|
+
// }
|
|
325
|
+
// const list = await listFileByPath( fetchData );
|
|
326
|
+
// const folderPath = list.data;
|
|
327
|
+
// const nextQuery = list.pageToken;
|
|
328
|
+
// if ( folderPath?.length > 0 ) {
|
|
329
|
+
// for ( let i = 0; i < folderPath.length; i++ ) {
|
|
330
|
+
// const img = folderPath[i].Key.split( '/' );
|
|
331
|
+
// const image = img[3].split( '.' );
|
|
332
|
+
// const indexes = image[0].split( '_' );
|
|
333
|
+
// fetchData.file_path = folderPath[i].Key;
|
|
334
|
+
// const data = await signedUrl( fetchData );
|
|
335
|
+
// const mapimg = {
|
|
336
|
+
// img_path: data,
|
|
337
|
+
// img_name: image[0],
|
|
338
|
+
// img_id: indexes[1],
|
|
339
|
+
// };
|
|
340
|
+
// files.push( {
|
|
341
|
+
// img_path: data,
|
|
342
|
+
// img_name: image[0],
|
|
343
|
+
// img_id: indexes[1],
|
|
344
|
+
// selected: false,
|
|
345
|
+
// dropped: false,
|
|
346
|
+
// count: 1,
|
|
347
|
+
// mappedid: [ mapimg ],
|
|
348
|
+
// } );
|
|
349
|
+
// }
|
|
350
|
+
// if ( userDetails.length === 0 ) {
|
|
351
|
+
// const [ day, month, year ] = msg.curr_date.split( '-' );
|
|
352
|
+
// const temp = `${year}-${month}-${day}`;
|
|
353
|
+
// let start = new Date( temp );
|
|
354
|
+
// logger.info( { start: start, date: msg.curr_date } );
|
|
355
|
+
// const userTimezoneOffset = start.getTimezoneOffset() * 60000;
|
|
356
|
+
// start = new Date( start.getTime() - userTimezoneOffset );
|
|
357
|
+
// start.setUTCHours( 0, 0, 0, 0 );
|
|
358
|
+
// const record = {
|
|
359
|
+
// userId: req.user._id,
|
|
360
|
+
// storeId: msg.store_id,
|
|
361
|
+
// clientId: msg.store_id.split( '-' )[0],
|
|
362
|
+
// auditType: 'Audit',
|
|
363
|
+
// fileDate: msg.curr_date,
|
|
364
|
+
// queueName: inputData.queueName,
|
|
365
|
+
// beforeCount: msg.total_count || files.length,
|
|
366
|
+
// auditStatus: 'inprogress',
|
|
367
|
+
// fileDateISO: start,
|
|
368
|
+
// timeSpent: 0,
|
|
369
|
+
// startTime: new Date(),
|
|
370
|
+
// zoneName: msg.zone_id,
|
|
371
|
+
// moduleType: inputData.moduleType,
|
|
372
|
+
// };
|
|
373
|
+
// const query ={
|
|
374
|
+
// storeId: msg.store_id,
|
|
375
|
+
// zoneName: msg.zone_id,
|
|
376
|
+
// fileDate: msg.curr_date,
|
|
377
|
+
// moduleType: inputData.moduleType,
|
|
378
|
+
// };
|
|
379
|
+
// const storeRecord = {
|
|
380
|
+
// userId: req.user._id,
|
|
381
|
+
// storeId: msg.store_id,
|
|
382
|
+
// clientId: msg.store_id.split( '-' )[0],
|
|
383
|
+
// auditType: 'Audit',
|
|
384
|
+
// fileDate: msg.curr_date,
|
|
385
|
+
// queueName: inputData.queueName,
|
|
386
|
+
// beforeCount: msg.total_count || files.length,
|
|
387
|
+
// status: 'inprogress',
|
|
388
|
+
// fileDateISO: start,
|
|
389
|
+
// timeSpent: 0,
|
|
390
|
+
// zoneName: msg.zone_id,
|
|
391
|
+
// moduleType: inputData.moduleType,
|
|
392
|
+
// };
|
|
393
|
+
|
|
394
|
+
// insertData = await createUserEmpDetection( record );
|
|
395
|
+
// await updateManyStoreEmpDetection( query, storeRecord );
|
|
396
|
+
// } else {
|
|
397
|
+
// insertData = userDetails[0];
|
|
398
|
+
// }
|
|
399
|
+
// } else {
|
|
400
|
+
// logger.error( {
|
|
401
|
+
// error: { folderPath: folderPath, nextQuery: nextQuery },
|
|
402
|
+
// function: 'getAuditFile',
|
|
403
|
+
// message: 'Bucket image data not availale',
|
|
404
|
+
// } );
|
|
405
|
+
// return res.sendError( 'Bucket is Empty', 204 );
|
|
406
|
+
// }
|
|
407
|
+
// const userdata = await findOneUser( { _id: insertData.userId } );
|
|
408
|
+
// const storeQuery = {
|
|
409
|
+
// storeId: storeId,
|
|
410
|
+
// };
|
|
411
|
+
// const storeFields = {
|
|
412
|
+
// storeName: 1,
|
|
413
|
+
// address: '$storeProfile.address',
|
|
414
|
+
// };
|
|
415
|
+
// const storeInfo = await findOneStore( storeQuery, storeFields );
|
|
416
|
+
// if ( inputData.nextId ) {
|
|
417
|
+
// const logData = {
|
|
418
|
+
// userId: insertData.userId,
|
|
419
|
+
// userName: userdata.name,
|
|
420
|
+
// logType: 'audit',
|
|
421
|
+
// logSubType: 'auditStart',
|
|
422
|
+
// logData: {
|
|
423
|
+
// fileDate: insertData.fileDate,
|
|
424
|
+
// auditType: insertData.auditType,
|
|
425
|
+
// storeId: storeId,
|
|
426
|
+
// clientName: inputData.queueName,
|
|
427
|
+
// auditId: insertData._id,
|
|
428
|
+
// zoneName: msg.zone_id,
|
|
429
|
+
// moduleType: inputData.moduleType,
|
|
430
|
+
// },
|
|
431
|
+
// createdAt: new Date(),
|
|
432
|
+
// };
|
|
433
|
+
// await insertOpenSearchData( openSearch.auditLog, logData );
|
|
434
|
+
// }
|
|
435
|
+
// logger.info( { storeInfo: storeInfo } );
|
|
436
|
+
// return res.sendSuccess( {
|
|
437
|
+
// result: files,
|
|
438
|
+
// count: msg.total_count,
|
|
439
|
+
// storeId: storeId,
|
|
440
|
+
// storeName: storeInfo?.storeName,
|
|
441
|
+
// address: storeInfo?.address,
|
|
442
|
+
// file: {
|
|
443
|
+
// queueName: inputData.queueName,
|
|
444
|
+
// store_id: storeId,
|
|
445
|
+
// file_date: fileDate,
|
|
446
|
+
// type: 'Audit',
|
|
447
|
+
// auditId: insertData._id,
|
|
448
|
+
// userId: insertData.userId,
|
|
449
|
+
// zoneName: msg.zone_id,
|
|
450
|
+
// moduleType: inputData.moduleType,
|
|
451
|
+
// nextToken: nextQuery ? encodeURIComponent( nextQuery ) : null,
|
|
452
|
+
// },
|
|
453
|
+
// isDraft: insertData.isDraft,
|
|
454
|
+
// } );
|
|
455
|
+
// } catch ( error ) {
|
|
456
|
+
// logger.error( {
|
|
457
|
+
// error: error,
|
|
458
|
+
// message: req.query,
|
|
459
|
+
// function: 'getAuditFile',
|
|
460
|
+
// } );
|
|
461
|
+
// return res.sendError( error, 500 );
|
|
462
|
+
// }
|
|
463
|
+
// }
|
|
14
464
|
|
|
15
|
-
|
|
16
|
-
export async function getDetectionAuditFile( req, res ) {
|
|
465
|
+
export async function workSpace( req, res ) {
|
|
17
466
|
try {
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
467
|
+
const inputData = req.body;
|
|
468
|
+
const limit = inputData.limit || 10;
|
|
469
|
+
const offset = inputData.offset ?
|
|
470
|
+
( inputData.offset - 1 ) * limit :
|
|
471
|
+
0;
|
|
472
|
+
const dateRange = await getDate( new Date(), new Date() );
|
|
473
|
+
const filter =[
|
|
474
|
+
{ status: { $in: [ 'active', 'hold' ] } },
|
|
475
|
+
{ 'auditConfigs.audit': { $eq: true } },
|
|
476
|
+
{ clientId: { $in: inputData.clientId } },
|
|
477
|
+
];
|
|
478
|
+
|
|
479
|
+
const temp = [];
|
|
480
|
+
const clientQuery = [
|
|
30
481
|
{
|
|
31
482
|
$match: {
|
|
32
|
-
$and:
|
|
33
|
-
{ userId: req.user._id },
|
|
34
|
-
{ queueName: inputData.queueName },
|
|
35
|
-
{ auditStatus: { $nin: [ 'completed', 'skipped' ] } },
|
|
36
|
-
{ createdAt: { $gte: start } },
|
|
37
|
-
{ createdAt: { $lte: end } },
|
|
38
|
-
{ moduleType: inputData.moduleType },
|
|
39
|
-
],
|
|
483
|
+
$and: filter,
|
|
40
484
|
},
|
|
41
485
|
},
|
|
42
486
|
{
|
|
43
|
-
$
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
{ isCompleted: { $eq: false } },
|
|
62
|
-
{ queueName: inputData.queueName },
|
|
63
|
-
{ moduleType: inputData.moduleType },
|
|
487
|
+
$project: {
|
|
488
|
+
clientName: 1,
|
|
489
|
+
clientId: 1,
|
|
490
|
+
queueName: {
|
|
491
|
+
$cond: [
|
|
492
|
+
{ $eq: [ inputData.moduleType, 'unattended-customer' ] }, '$auditConfigs.traxQueueName.unattendedCustomer',
|
|
493
|
+
{ $cond: [
|
|
494
|
+
{ $eq: [ inputData.moduleType, 'left-in-middle' ] }, '$auditConfigs.traxQueueName.leftInMiddle',
|
|
495
|
+
{
|
|
496
|
+
$cond: [
|
|
497
|
+
{ $eq: [ inputData.moduleType, 'uniform-detection' ] }, '$auditConfigs.traxQueueName.uniformDetection',
|
|
498
|
+
{
|
|
499
|
+
$cond: [
|
|
500
|
+
{ $eq: [ inputData.moduleType, 'mobile-detection' ] }, '$auditConfigs.traxQueueName.mobileDetection', '$auditConfigs.traxQueueName.cameraAngleChange',
|
|
501
|
+
],
|
|
502
|
+
},
|
|
503
|
+
],
|
|
504
|
+
},
|
|
64
505
|
],
|
|
65
|
-
|
|
506
|
+
},
|
|
507
|
+
],
|
|
66
508
|
},
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
logger.info( 'Hit in user assign', { inputData } );
|
|
71
|
-
userAssign[0].auditType == 'Audit' ?
|
|
72
|
-
( msg = {
|
|
73
|
-
store_id: userAssign[0].storeId,
|
|
74
|
-
curr_date: userAssign[0].fileDate,
|
|
75
|
-
total_count: userAssign[0].fileCount,
|
|
76
|
-
zone_id: userAssign[0].zoneName,
|
|
77
|
-
} ) :
|
|
78
|
-
( msg = {
|
|
79
|
-
store_id: userAssign[0].storeId,
|
|
80
|
-
curr_date: userAssign[0].fileDate,
|
|
81
|
-
audit_type: userAssign[0].auditType,
|
|
82
|
-
total_count: userAssign[0].fileCount,
|
|
83
|
-
zone_id: userAssign[0].zoneName,
|
|
84
|
-
} );
|
|
85
|
-
await updateOneAssignAudit(
|
|
86
|
-
{ _id: userAssign[0]._id },
|
|
87
|
-
{ isCompleted: true },
|
|
88
|
-
);
|
|
89
|
-
logger.info( 'Hit in auditUserAssignModel updateOne', {
|
|
90
|
-
_id: userAssign[0]._id,
|
|
91
|
-
isCompleted: true,
|
|
92
|
-
} );
|
|
93
|
-
} else {
|
|
94
|
-
logger.info( 'Hit in new file', { inputData } );
|
|
95
|
-
const consumer = await sqsReceive( inputData.queueName );
|
|
96
|
-
if ( !consumer ) {
|
|
97
|
-
logger.error( {
|
|
98
|
-
error: consumer,
|
|
99
|
-
function: 'getAuditFile',
|
|
100
|
-
message: 'SQS Receive queue is empty',
|
|
101
|
-
} );
|
|
102
|
-
return res.sendError( `${inputData.queueName} is Empty`, 204 );
|
|
103
|
-
}
|
|
104
|
-
msg = JSON.parse( consumer );
|
|
105
|
-
}
|
|
106
|
-
break;
|
|
107
|
-
|
|
108
|
-
case 'drafted':
|
|
109
|
-
logger.info( 'userDetails drafted', { inputData } );
|
|
110
|
-
const log = await findOneAuditLog(
|
|
111
|
-
{
|
|
112
|
-
userId: userDetails[0].userId,
|
|
113
|
-
fileDate: userDetails[0].fileDate,
|
|
114
|
-
storeId: userDetails[0].storeId,
|
|
115
|
-
totalCount: userDetails[0].beforeCount,
|
|
116
|
-
moduleType: userDetails[0].moduleType,
|
|
117
|
-
zoneName: userDetails[0].zoneName,
|
|
118
|
-
},
|
|
119
|
-
{},
|
|
120
|
-
{ createdAt: -1 },
|
|
121
|
-
);
|
|
122
|
-
if ( !log ) {
|
|
123
|
-
await updateOneUserAudit(
|
|
124
|
-
{ _id: userDetails[0]._id },
|
|
125
|
-
{ $set: { isDraft: false, auditStatus: 'skipped' } },
|
|
126
|
-
);
|
|
127
|
-
logger.info( 'audit update in drafted', {
|
|
128
|
-
_id: userDetails[0]._id,
|
|
129
|
-
isDraft: false,
|
|
130
|
-
auditStatus: 'skipped',
|
|
131
|
-
} );
|
|
132
|
-
return res.sendError( 'User saved data has been deleted', 204 );
|
|
133
|
-
}
|
|
134
|
-
const file = {
|
|
135
|
-
auditId: userDetails[0]._id,
|
|
136
|
-
store_id: userDetails[0].storeId,
|
|
137
|
-
file_date: userDetails[0].fileDate,
|
|
138
|
-
type: userDetails[0].auditType,
|
|
139
|
-
queueName: log.queueName,
|
|
140
|
-
userId: log.userId,
|
|
141
|
-
moduleType: userDetails[0].moduleType,
|
|
142
|
-
zoneName: userDetails[0].zoneName,
|
|
143
|
-
};
|
|
144
|
-
const result = {
|
|
145
|
-
junk: log.junk,
|
|
146
|
-
junk_count: log.junkCount,
|
|
147
|
-
employee: log.employee,
|
|
148
|
-
employee_count: log.employeeCount,
|
|
149
|
-
customer: log.customer,
|
|
150
|
-
customer_count: log.customerCount,
|
|
151
|
-
retag_image: log.retagImage,
|
|
152
|
-
retag_count: log.retagCount,
|
|
153
|
-
total_count: log.totalCount,
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
const userdata = await findOneUser( { _id: log.userId } );
|
|
157
|
-
if (
|
|
158
|
-
!inputData.nextId ||
|
|
159
|
-
inputData.nextId === '' ||
|
|
160
|
-
inputData.nextId == null
|
|
161
|
-
) {
|
|
162
|
-
const logData = {
|
|
163
|
-
userId: log.userId,
|
|
164
|
-
userName: userdata.name,
|
|
165
|
-
logType: 'audit',
|
|
166
|
-
logSubType: 'auditStart',
|
|
167
|
-
logData: {
|
|
168
|
-
fileDate: userDetails[0].fileDate,
|
|
169
|
-
auditType: userDetails[0].auditType,
|
|
170
|
-
storeId: userDetails[0].storeId,
|
|
171
|
-
clientName: log.queueName,
|
|
172
|
-
auditId: userDetails[0]._id,
|
|
173
|
-
moduleType: userDetails[0].moduleType,
|
|
174
|
-
zoneName: userDetails[0].zoneName,
|
|
175
|
-
},
|
|
176
|
-
createdAt: new Date(),
|
|
177
|
-
};
|
|
178
|
-
logger.info( { logData: logData } );
|
|
179
|
-
await insertOpenSearchData( openSearch.auditLog, logData );
|
|
180
|
-
}
|
|
181
|
-
const storeQuery = {
|
|
182
|
-
storeId: userDetails[0].storeId,
|
|
183
|
-
};
|
|
184
|
-
const storeFields = {
|
|
185
|
-
storeId: 1,
|
|
186
|
-
storeName: 1,
|
|
187
|
-
address: '$storeProfile.address',
|
|
188
|
-
};
|
|
189
|
-
const storeDetails = await findOneStore( storeQuery, storeFields );
|
|
190
|
-
res.sendSuccess( {
|
|
191
|
-
result: result,
|
|
192
|
-
storeId: storeDetails?.storeId,
|
|
193
|
-
storeName: storeDetails?.storeName,
|
|
194
|
-
address: storeDetails?.address || '',
|
|
195
|
-
count: log.totalCount,
|
|
196
|
-
file: file,
|
|
197
|
-
isDraft: userDetails[0].isDraft,
|
|
198
|
-
} );
|
|
199
|
-
break;
|
|
200
|
-
|
|
201
|
-
default:
|
|
202
|
-
msg = {
|
|
203
|
-
store_id: userDetails[0].storeId,
|
|
204
|
-
curr_date: userDetails[0].fileDate,
|
|
205
|
-
audit_type: userDetails[0].auditType,
|
|
206
|
-
total_count: userDetails[0].beforeCount,
|
|
207
|
-
zone_id: userDetails[0].zoneName,
|
|
208
|
-
};
|
|
209
|
-
break;
|
|
210
|
-
}
|
|
211
|
-
if ( msg.audit_type === 'ReAudit' ) {
|
|
212
|
-
logger.info( 'Hit in user ReAudit', { inputData } );
|
|
213
|
-
let reauditInsert = {};
|
|
214
|
-
const [ filterData, auditImage ] = await Promise.all( [
|
|
215
|
-
getFilterData( msg ),
|
|
216
|
-
getAuditImage( msg ),
|
|
217
|
-
] );
|
|
218
|
-
if ( filterData.statusCode == 404 || auditImage.statusCode == 404 ) {
|
|
219
|
-
return res.sendError( 'No Data found', 204 );
|
|
220
|
-
}
|
|
221
|
-
const reauditImg = await getReauditImg( filterData, auditImage );
|
|
222
|
-
if ( userDetails.length === 0 ) {
|
|
223
|
-
const [ day, month, year ] = msg.curr_date.split( '-' );
|
|
224
|
-
const temp = `${year}-${month}-${day}`;
|
|
225
|
-
let start = new Date( temp );
|
|
226
|
-
const userTimezoneOffset = start.getTimezoneOffset() * 60000;
|
|
227
|
-
start = new Date( start.getTime() - userTimezoneOffset );
|
|
228
|
-
start.setUTCHours( 0, 0, 0, 0 );
|
|
229
|
-
const record = {
|
|
230
|
-
userId: req.user._id,
|
|
231
|
-
storeId: msg.store_id,
|
|
232
|
-
clientId: msg.store_id.split( '-' )[0],
|
|
233
|
-
auditType: msg.audit_type || 'ReAudit',
|
|
234
|
-
fileDate: msg.curr_date,
|
|
235
|
-
queueName: inputData.queueName,
|
|
236
|
-
beforeCount: reauditImg.length,
|
|
237
|
-
auditStatus: 'inprogress',
|
|
238
|
-
fileDateISO: start,
|
|
239
|
-
timeSpent: 0,
|
|
240
|
-
startTime: new Date(),
|
|
241
|
-
zoneName: msg.zone_id,
|
|
242
|
-
moduleType: inputData.moduleType,
|
|
243
|
-
};
|
|
244
|
-
const query ={
|
|
245
|
-
storeId: msg.store_id,
|
|
246
|
-
zoneName: msg.zone_id,
|
|
247
|
-
fileDate: msg.curr_date,
|
|
248
|
-
moduleType: inputData.moduleType,
|
|
249
|
-
};
|
|
250
|
-
let storeRecord = {
|
|
251
|
-
userId: req.user._id,
|
|
252
|
-
auditType: msg.audit_type || 'ReAudit',
|
|
253
|
-
status: 'inprogress',
|
|
254
|
-
timeSpent: 0,
|
|
255
|
-
};
|
|
256
|
-
reauditInsert = await createUserAudit( record );
|
|
509
|
+
},
|
|
510
|
+
},
|
|
511
|
+
];
|
|
257
512
|
|
|
258
|
-
await updateOneStoreAudit( query, storeRecord );
|
|
259
|
-
} else {
|
|
260
|
-
reauditInsert = userDetails[0];
|
|
261
|
-
}
|
|
262
|
-
const userdata = await findOneUser( { _id: req.user._id } );
|
|
263
|
-
if ( inputData.nextId ) {
|
|
264
|
-
const logData = {
|
|
265
|
-
userId: req.user._id,
|
|
266
|
-
userName: userdata.name,
|
|
267
|
-
logType: 'audit',
|
|
268
|
-
logSubType: 'auditStart',
|
|
269
|
-
logData: {
|
|
270
|
-
fileDate: msg.curr_date,
|
|
271
|
-
auditType: msg.audit_type,
|
|
272
|
-
storeId: msg.store_id,
|
|
273
|
-
clientName: inputData.queueName,
|
|
274
|
-
auditId: reauditInsert._id,
|
|
275
|
-
zoneName: msg.zone_id,
|
|
276
|
-
moduleType: inputData.moduleType,
|
|
277
|
-
},
|
|
278
|
-
createdAt: new Date(),
|
|
279
|
-
};
|
|
280
|
-
await insertOpenSearchData( openSearch.auditLog, logData );
|
|
281
|
-
}
|
|
282
513
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
};
|
|
291
|
-
const storeInfo = await findOneStore( storeQuery, storeFields );
|
|
292
|
-
return res.sendSuccess( {
|
|
293
|
-
result: reauditImg,
|
|
294
|
-
count: reauditImg.length,
|
|
295
|
-
storeId: storeInfo?.storeId,
|
|
296
|
-
storeName: storeInfo?.storeName,
|
|
297
|
-
address: storeInfo?.address || '',
|
|
298
|
-
file: {
|
|
299
|
-
store_id: msg.store_id,
|
|
300
|
-
file_date: msg.curr_date,
|
|
301
|
-
type: msg.audit_type || 'ReAudit',
|
|
302
|
-
queueName: inputData.queueName,
|
|
303
|
-
auditId: reauditInsert._id,
|
|
304
|
-
userId: reauditInsert.userId,
|
|
305
|
-
zoneName: msg.zone_id,
|
|
306
|
-
moduleType: inputData.moduleType,
|
|
514
|
+
if ( inputData.searchValue && inputData.searchValue !== '' ) {
|
|
515
|
+
clientQuery.push( {
|
|
516
|
+
$match: {
|
|
517
|
+
$or: [
|
|
518
|
+
{ clientName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
519
|
+
{ clientId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
520
|
+
],
|
|
307
521
|
},
|
|
308
|
-
isDraft: reauditInsert.isDraft,
|
|
309
522
|
} );
|
|
310
523
|
}
|
|
311
|
-
|
|
312
|
-
|
|
524
|
+
if ( inputData.sortColumnName ) {
|
|
525
|
+
const sortBy = inputData.sortBy || -1;
|
|
526
|
+
const sortColumn = inputData.sortColumnName;
|
|
527
|
+
clientQuery.push( {
|
|
528
|
+
$sort: { [sortColumn]: sortBy },
|
|
529
|
+
},
|
|
530
|
+
);
|
|
531
|
+
}
|
|
313
532
|
|
|
314
|
-
const
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
fetchData.file_path = `${fileDate}/${storeId}/${msg.zone_id}/`;
|
|
318
|
-
if ( inputData.limit ) {
|
|
319
|
-
fetchData.MaxKeys = inputData.limit;
|
|
533
|
+
const count = await aggregateClient( clientQuery );
|
|
534
|
+
if ( count?.length == 0 ) {
|
|
535
|
+
return res.sendError( 'No Data Found', 204 );
|
|
320
536
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
const list = await listFileByPath( fetchData );
|
|
325
|
-
const folderPath = list.data;
|
|
326
|
-
const nextQuery = list.pageToken;
|
|
327
|
-
if ( folderPath?.length > 0 ) {
|
|
328
|
-
for ( let i = 0; i < folderPath.length; i++ ) {
|
|
329
|
-
const img = folderPath[i].Key.split( '/' );
|
|
330
|
-
const image = img[3].split( '.' );
|
|
331
|
-
const indexes = image[0].split( '_' );
|
|
332
|
-
fetchData.file_path = folderPath[i].Key;
|
|
333
|
-
const data = await signedUrl( fetchData );
|
|
334
|
-
const mapimg = {
|
|
335
|
-
img_path: data,
|
|
336
|
-
img_name: indexes[1],
|
|
337
|
-
img_id: image[0],
|
|
338
|
-
};
|
|
339
|
-
files.push( {
|
|
340
|
-
img_path: data,
|
|
341
|
-
img_name: indexes[1],
|
|
342
|
-
img_id: image[0],
|
|
343
|
-
selected: false,
|
|
344
|
-
dropped: false,
|
|
345
|
-
count: 1,
|
|
346
|
-
mappedid: [ mapimg ],
|
|
347
|
-
} );
|
|
348
|
-
}
|
|
349
|
-
if ( userDetails.length === 0 ) {
|
|
350
|
-
const [ day, month, year ] = msg.curr_date.split( '-' );
|
|
351
|
-
const temp = `${year}-${month}-${day}`;
|
|
352
|
-
let start = new Date( temp );
|
|
353
|
-
logger.info( { start: start, date: msg.curr_date } );
|
|
354
|
-
const userTimezoneOffset = start.getTimezoneOffset() * 60000;
|
|
355
|
-
start = new Date( start.getTime() - userTimezoneOffset );
|
|
356
|
-
start.setUTCHours( 0, 0, 0, 0 );
|
|
357
|
-
const record = {
|
|
358
|
-
userId: req.user._id,
|
|
359
|
-
storeId: msg.store_id,
|
|
360
|
-
clientId: msg.store_id.split( '-' )[0],
|
|
361
|
-
auditType: 'Audit',
|
|
362
|
-
fileDate: msg.curr_date,
|
|
363
|
-
queueName: inputData.queueName,
|
|
364
|
-
beforeCount: msg.total_count || files.length,
|
|
365
|
-
auditStatus: 'inprogress',
|
|
366
|
-
fileDateISO: start,
|
|
367
|
-
timeSpent: 0,
|
|
368
|
-
startTime: new Date(),
|
|
369
|
-
zoneName: msg.zone_id,
|
|
370
|
-
moduleType: inputData.moduleType,
|
|
371
|
-
};
|
|
372
|
-
const query ={
|
|
373
|
-
storeId: msg.store_id,
|
|
374
|
-
zoneName: msg.zone_id,
|
|
375
|
-
fileDate: msg.curr_date,
|
|
376
|
-
moduleType: inputData.moduleType,
|
|
377
|
-
};
|
|
378
|
-
const storeRecord = {
|
|
379
|
-
userId: req.user._id,
|
|
380
|
-
storeId: msg.store_id,
|
|
381
|
-
clientId: msg.store_id.split( '-' )[0],
|
|
382
|
-
auditType: 'Audit',
|
|
383
|
-
fileDate: msg.curr_date,
|
|
384
|
-
queueName: inputData.queueName,
|
|
385
|
-
beforeCount: msg.total_count || files.length,
|
|
386
|
-
status: 'inprogress',
|
|
387
|
-
fileDateISO: start,
|
|
388
|
-
timeSpent: 0,
|
|
389
|
-
zoneName: msg.zone_id,
|
|
390
|
-
moduleType: inputData.moduleType,
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
insertData = await createUserEmpDetection( record );
|
|
394
|
-
await updateManyStoreAudit( query, storeRecord );
|
|
395
|
-
} else {
|
|
396
|
-
insertData = userDetails[0];
|
|
397
|
-
}
|
|
398
|
-
} else {
|
|
399
|
-
logger.error( {
|
|
400
|
-
error: { folderPath: folderPath, nextQuery: nextQuery },
|
|
401
|
-
function: 'getAuditFile',
|
|
402
|
-
message: 'Bucket image data not availale',
|
|
403
|
-
} );
|
|
404
|
-
return res.sendError( 'Bucket is Empty', 204 );
|
|
405
|
-
}
|
|
406
|
-
const userdata = await findOneUser( { _id: insertData.userId } );
|
|
407
|
-
const storeQuery = {
|
|
408
|
-
storeId: storeId,
|
|
409
|
-
};
|
|
410
|
-
const storeFields = {
|
|
411
|
-
storeName: 1,
|
|
412
|
-
address: '$storeProfile.address',
|
|
413
|
-
};
|
|
414
|
-
const storeInfo = await findOneStore( storeQuery, storeFields );
|
|
415
|
-
if ( inputData.nextId ) {
|
|
416
|
-
const logData = {
|
|
417
|
-
userId: insertData.userId,
|
|
418
|
-
userName: userdata.name,
|
|
419
|
-
logType: 'audit',
|
|
420
|
-
logSubType: 'auditStart',
|
|
421
|
-
logData: {
|
|
422
|
-
fileDate: insertData.fileDate,
|
|
423
|
-
auditType: insertData.auditType,
|
|
424
|
-
storeId: storeId,
|
|
425
|
-
clientName: inputData.queueName,
|
|
426
|
-
auditId: insertData._id,
|
|
427
|
-
zoneName: msg.zone_id,
|
|
428
|
-
moduleType: inputData.moduleType,
|
|
429
|
-
},
|
|
430
|
-
createdAt: new Date(),
|
|
431
|
-
};
|
|
432
|
-
await insertOpenSearchData( openSearch.auditLog, logData );
|
|
433
|
-
}
|
|
434
|
-
logger.info( { storeInfo: storeInfo } );
|
|
435
|
-
return res.sendSuccess( {
|
|
436
|
-
result: files,
|
|
437
|
-
count: msg.total_count,
|
|
438
|
-
storeId: storeId,
|
|
439
|
-
storeName: storeInfo?.storeName,
|
|
440
|
-
address: storeInfo?.address,
|
|
441
|
-
file: {
|
|
442
|
-
queueName: inputData.queueName,
|
|
443
|
-
store_id: storeId,
|
|
444
|
-
file_date: fileDate,
|
|
445
|
-
type: 'Audit',
|
|
446
|
-
auditId: insertData._id,
|
|
447
|
-
userId: insertData.userId,
|
|
448
|
-
zoneName: msg.zone_id,
|
|
449
|
-
moduleType: inputData.moduleType,
|
|
450
|
-
nextToken: nextQuery ? encodeURIComponent( nextQuery ) : null,
|
|
451
|
-
},
|
|
452
|
-
isDraft: insertData.isDraft,
|
|
453
|
-
} );
|
|
454
|
-
} catch ( error ) {
|
|
455
|
-
logger.error( {
|
|
456
|
-
error: error,
|
|
457
|
-
message: req.query,
|
|
458
|
-
function: 'getAuditFile',
|
|
459
|
-
} );
|
|
460
|
-
return res.sendError( error, 500 );
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
export async function workSpace( req, res ) {
|
|
465
|
-
try {
|
|
466
|
-
const inputData = req.body;
|
|
467
|
-
const limit = inputData.limit || 10;
|
|
468
|
-
const offset = inputData.offset ?
|
|
469
|
-
( inputData.offset - 1 ) * limit :
|
|
470
|
-
0;
|
|
471
|
-
const dateRange = await getDate( new Date(), new Date() );
|
|
472
|
-
const filter =[
|
|
473
|
-
{ status: { $in: [ 'active', 'hold' ] } },
|
|
474
|
-
{ 'auditConfigs.audit': { $eq: true } },
|
|
475
|
-
{ clientId: { $in: inputData.clientId } },
|
|
476
|
-
];
|
|
477
|
-
|
|
478
|
-
const temp = [];
|
|
479
|
-
const clientQuery = [
|
|
480
|
-
{
|
|
481
|
-
$match: {
|
|
482
|
-
$and: filter,
|
|
483
|
-
},
|
|
484
|
-
},
|
|
485
|
-
{
|
|
486
|
-
$project: {
|
|
487
|
-
clientName: 1,
|
|
488
|
-
clientId: 1,
|
|
489
|
-
queueName: {
|
|
490
|
-
$cond: [
|
|
491
|
-
{ $eq: [ inputData.moduleType, 'unattended-customer' ] }, '$auditConfigs.traxQueueName.unattendedCustomer',
|
|
492
|
-
{ $cond: [
|
|
493
|
-
{ $eq: [ inputData.moduleType, 'left-in-middle' ] }, '$auditConfigs.traxQueueName.leftInMiddle',
|
|
494
|
-
{
|
|
495
|
-
$cond: [
|
|
496
|
-
{ $eq: [ inputData.moduleType, 'uniform-detection' ] }, '$auditConfigs.traxQueueName.uniformDetection',
|
|
497
|
-
{
|
|
498
|
-
$cond: [
|
|
499
|
-
{ $eq: [ inputData.moduleType, 'mobile-detection' ] }, '$auditConfigs.traxQueueName.mobileDetection', '$auditConfigs.traxQueueName.cameraAngleChange',
|
|
500
|
-
],
|
|
501
|
-
},
|
|
502
|
-
],
|
|
503
|
-
},
|
|
504
|
-
],
|
|
505
|
-
},
|
|
506
|
-
],
|
|
507
|
-
},
|
|
508
|
-
},
|
|
509
|
-
},
|
|
510
|
-
];
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
if ( inputData.searchValue && inputData.searchValue !== '' ) {
|
|
514
|
-
clientQuery.push( {
|
|
515
|
-
$match: {
|
|
516
|
-
$or: [
|
|
517
|
-
{ clientName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
518
|
-
{ clientId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
519
|
-
],
|
|
520
|
-
},
|
|
521
|
-
} );
|
|
522
|
-
}
|
|
523
|
-
if ( inputData.sortColumnName ) {
|
|
524
|
-
const sortBy = inputData.sortBy || -1;
|
|
525
|
-
const sortColumn = inputData.sortColumnName;
|
|
526
|
-
clientQuery.push( {
|
|
527
|
-
$sort: { [sortColumn]: sortBy },
|
|
528
|
-
},
|
|
529
|
-
);
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
const count = await aggregateClient( clientQuery );
|
|
533
|
-
if ( count?.length == 0 ) {
|
|
534
|
-
return res.sendError( 'No Data Found', 204 );
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
if ( !inputData.isExport ) {
|
|
538
|
-
clientQuery.push( { $skip: offset }, { $limit: limit } );
|
|
537
|
+
|
|
538
|
+
if ( !inputData.isExport ) {
|
|
539
|
+
clientQuery.push( { $skip: offset }, { $limit: limit } );
|
|
539
540
|
}
|
|
540
541
|
|
|
541
542
|
const clientDetails = await aggregateClient( clientQuery );
|
|
@@ -708,7 +709,7 @@ export async function workSpace( req, res ) {
|
|
|
708
709
|
{ clientId: { $in: clientList } },
|
|
709
710
|
{ userId: { $eq: req.user._id } },
|
|
710
711
|
{ moduleType: { $eq: inputData.moduleType } },
|
|
711
|
-
{ createdAt: { $gte:
|
|
712
|
+
{ createdAt: { $gte: new Date( dayjs( fileDate ).format( 'YYYY-MM-DD' ) ) } },
|
|
712
713
|
{ createdAt: { $lte: dateRange.end } },
|
|
713
714
|
],
|
|
714
715
|
},
|
|
@@ -741,6 +742,8 @@ export async function workSpace( req, res ) {
|
|
|
741
742
|
},
|
|
742
743
|
];
|
|
743
744
|
|
|
745
|
+
logger.info( { moduleType: inputData.moduleType, key: inputData.moduleType.includes( [ 'uniform-detection', 'mobile-detection' ] ), dateStart: new Date( dayjs( fileDate ).format( 'YYYY-MM-DD' ) ), end: dateRange.end, user: req.user._id, client: clientList } );
|
|
746
|
+
|
|
744
747
|
const userAsignAudit = [
|
|
745
748
|
{
|
|
746
749
|
$match: {
|
|
@@ -795,6 +798,7 @@ export async function workSpace( req, res ) {
|
|
|
795
798
|
|
|
796
799
|
const auditUserCount = inputData.moduleType.includes( [ 'uniform-detection', 'mobile-detection' ] )?await aggregateUserEmpDetection( auditFiles ) : await aggregateBinaryAudit( auditFiles );
|
|
797
800
|
const userIncompleteFilesCount = inputData.moduleType.includes( [ 'uniform-detection', 'mobile-detection' ] )?await aggregateUserEmpDetection( userIncompleteFiles ) : await aggregateBinaryAudit( userIncompleteFiles );
|
|
801
|
+
logger.info( { moduleType: inputData.moduleType, userIncompleteFiles: userIncompleteFilesCount } );
|
|
798
802
|
const userAsignAuditCount = await aggregateAssignAudit( userAsignAudit );
|
|
799
803
|
const completedStoresCount = inputData.moduleType.includes( [ 'uniform-detection', 'mobile-detection' ] )? await aggregateStoreEmpDetection( completedStores ) : await aggregateBinaryAudit( binarCompletedStores );
|
|
800
804
|
const clientAssignedCount = await aggregateAssignAudit( clientAssign );
|
|
@@ -3272,218 +3276,791 @@ export async function summarySplit( req, res ) {
|
|
|
3272
3276
|
total: 1,
|
|
3273
3277
|
},
|
|
3274
3278
|
},
|
|
3275
|
-
];
|
|
3276
|
-
const result = inputData.moduleType.includes( [ 'uniform-detection', 'mobile-detection' ] )? await aggregateUserEmpDetection( query ) : await aggregateBinaryAudit( query );
|
|
3277
|
-
const totalCount = await aggregateTraxAuditData( totalQuery );
|
|
3278
|
-
result[0].totalCount = totalCount[0]?.total;
|
|
3279
|
-
return res.sendSuccess( { result: result.length> 0? result[0]: temp } );
|
|
3279
|
+
];
|
|
3280
|
+
const result = inputData.moduleType.includes( [ 'uniform-detection', 'mobile-detection' ] )? await aggregateUserEmpDetection( query ) : await aggregateBinaryAudit( query );
|
|
3281
|
+
const totalCount = await aggregateTraxAuditData( totalQuery );
|
|
3282
|
+
result[0].totalCount = totalCount[0]?.total;
|
|
3283
|
+
return res.sendSuccess( { result: result.length> 0? result[0]: temp } );
|
|
3284
|
+
} catch ( error ) {
|
|
3285
|
+
const err = error.message || 'Internal Server Error';
|
|
3286
|
+
logger.error( { error: error, message: req.body, function: 'summarySplit' } );
|
|
3287
|
+
return res.sendError( err, 500 );
|
|
3288
|
+
}
|
|
3289
|
+
}
|
|
3290
|
+
|
|
3291
|
+
export async function overviewTable( req, res ) {
|
|
3292
|
+
try {
|
|
3293
|
+
const inputData = req.body;
|
|
3294
|
+
const limit = inputData.limit || 10;
|
|
3295
|
+
const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
|
|
3296
|
+
const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
|
|
3297
|
+
let filters =[
|
|
3298
|
+
{ fileDateISO: { $gte: dateRange.start } },
|
|
3299
|
+
{ fileDateISO: { $lte: dateRange.end } },
|
|
3300
|
+
{ moduleType: { $eq: inputData.moduleType } },
|
|
3301
|
+
{ clientId: { $in: inputData.clientId } },
|
|
3302
|
+
|
|
3303
|
+
];
|
|
3304
|
+
|
|
3305
|
+
if ( inputData.filterByAuditType.length > 0 ) {
|
|
3306
|
+
filters.push( { auditType: { $in: inputData.filterByAuditType } } );
|
|
3307
|
+
}
|
|
3308
|
+
|
|
3309
|
+
if ( inputData.filterByStatus.length > 0 ) {
|
|
3310
|
+
filters.push( { status: { $in: inputData.filterByStatus } } );
|
|
3311
|
+
}
|
|
3312
|
+
|
|
3313
|
+
const query =[
|
|
3314
|
+
{
|
|
3315
|
+
$match: {
|
|
3316
|
+
$and: filters,
|
|
3317
|
+
},
|
|
3318
|
+
|
|
3319
|
+
},
|
|
3320
|
+
{
|
|
3321
|
+
$project: {
|
|
3322
|
+
_id: 0,
|
|
3323
|
+
storeId: 1,
|
|
3324
|
+
clientId: 1,
|
|
3325
|
+
streamName: 1,
|
|
3326
|
+
tempId: 1,
|
|
3327
|
+
question: 1,
|
|
3328
|
+
questionType: 1,
|
|
3329
|
+
answer: 1,
|
|
3330
|
+
userComments: 1,
|
|
3331
|
+
fileDate: 1,
|
|
3332
|
+
moduleType: 1,
|
|
3333
|
+
|
|
3334
|
+
value: {
|
|
3335
|
+
$replaceAll: { input: '$moduleType', find: '-', replacement: ' ' },
|
|
3336
|
+
},
|
|
3337
|
+
beforeCount: 1,
|
|
3338
|
+
afterCount: 1,
|
|
3339
|
+
auditType: 1,
|
|
3340
|
+
status: 1,
|
|
3341
|
+
auditStatus: 1,
|
|
3342
|
+
// userId: 1,
|
|
3343
|
+
},
|
|
3344
|
+
},
|
|
3345
|
+
// {
|
|
3346
|
+
// $lookup: {
|
|
3347
|
+
// from: 'users',
|
|
3348
|
+
// let: { userId: '$userId' },
|
|
3349
|
+
// pipeline: [
|
|
3350
|
+
// {
|
|
3351
|
+
// $match: {
|
|
3352
|
+
// $expr: {
|
|
3353
|
+
// $eq: [ '$_id', '$$userId' ],
|
|
3354
|
+
// },
|
|
3355
|
+
// },
|
|
3356
|
+
// },
|
|
3357
|
+
// {
|
|
3358
|
+
// $project: {
|
|
3359
|
+
// userName: 1,
|
|
3360
|
+
// userEmail: '$email',
|
|
3361
|
+
// },
|
|
3362
|
+
// },
|
|
3363
|
+
// ], as: 'user',
|
|
3364
|
+
// },
|
|
3365
|
+
// },
|
|
3366
|
+
// {
|
|
3367
|
+
// $unwind: {
|
|
3368
|
+
// path: '$user',
|
|
3369
|
+
// preserveNullAndEmptyArrays: true,
|
|
3370
|
+
// },
|
|
3371
|
+
// },
|
|
3372
|
+
{
|
|
3373
|
+
$project: {
|
|
3374
|
+
storeId: 1,
|
|
3375
|
+
clientId: 1,
|
|
3376
|
+
streamName: 1,
|
|
3377
|
+
tempId: 1,
|
|
3378
|
+
question: 1,
|
|
3379
|
+
questionType: 1,
|
|
3380
|
+
answer: 1,
|
|
3381
|
+
userComments: 1,
|
|
3382
|
+
fileDate: 1,
|
|
3383
|
+
moduleType: 1,
|
|
3384
|
+
value: 1,
|
|
3385
|
+
beforeCount: 1,
|
|
3386
|
+
afterCount: 1,
|
|
3387
|
+
auditType: 1,
|
|
3388
|
+
status: 1,
|
|
3389
|
+
auditStatus: 1,
|
|
3390
|
+
// userName: '$user.userName',
|
|
3391
|
+
// userEmail: '$user.userEmail',
|
|
3392
|
+
|
|
3393
|
+
},
|
|
3394
|
+
},
|
|
3395
|
+
];
|
|
3396
|
+
if ( inputData.searchValue && inputData.searchValue!== '' ) {
|
|
3397
|
+
query.push( {
|
|
3398
|
+
$match: {
|
|
3399
|
+
$or: [
|
|
3400
|
+
{ storeId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3401
|
+
{ clientId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3402
|
+
{ fileDate: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3403
|
+
{ auditType: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3404
|
+
{ status: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3405
|
+
{ auditStatus: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3406
|
+
{ value: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3407
|
+
{ streamName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3408
|
+
{ tempId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3409
|
+
{ question: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3410
|
+
{ answer: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3411
|
+
{ userComments: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3412
|
+
// { userName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3413
|
+
// { userEmail: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3414
|
+
],
|
|
3415
|
+
|
|
3416
|
+
},
|
|
3417
|
+
} );
|
|
3418
|
+
}
|
|
3419
|
+
if ( inputData.sortColumnName ) {
|
|
3420
|
+
const sortBy = inputData.sortBy || -1;
|
|
3421
|
+
const sortColumn = inputData.sortColumnName;
|
|
3422
|
+
query.push( {
|
|
3423
|
+
$sort: { [sortColumn]: sortBy },
|
|
3424
|
+
},
|
|
3425
|
+
);
|
|
3426
|
+
}
|
|
3427
|
+
const count = inputData.moduleType.includes( [ 'uniform-detection', 'mobile-detection' ] )? await aggregateStoreEmpDetection( query ) : await aggregateBinaryAudit( query );
|
|
3428
|
+
if ( count.length == 0 ) {
|
|
3429
|
+
return res.sendError( 'No Data Found', 204 );
|
|
3430
|
+
}
|
|
3431
|
+
|
|
3432
|
+
if ( inputData.isExport ) {
|
|
3433
|
+
query.push( { $limit: 10000 } );
|
|
3434
|
+
} else {
|
|
3435
|
+
query.push( {
|
|
3436
|
+
$skip: offset,
|
|
3437
|
+
},
|
|
3438
|
+
{
|
|
3439
|
+
$limit: limit,
|
|
3440
|
+
} );
|
|
3441
|
+
}
|
|
3442
|
+
const result = inputData.moduleType.includes( [ 'uniform-detection', 'mobile-detection' ] )? await aggregateStoreEmpDetection( query ) : await aggregateBinaryAudit( query );
|
|
3443
|
+
if ( inputData.isExport ) {
|
|
3444
|
+
const exportdata = [];
|
|
3445
|
+
result.forEach( ( element ) => {
|
|
3446
|
+
const temp ={
|
|
3447
|
+
'Store ID': element.storeId,
|
|
3448
|
+
'Brand ID': element.clientId,
|
|
3449
|
+
'File Date': element.fileDate,
|
|
3450
|
+
'Result': element.answer,
|
|
3451
|
+
'Product Type': element.value,
|
|
3452
|
+
'Audit Type': element.auditType,
|
|
3453
|
+
// 'User Name': element.userName,
|
|
3454
|
+
// 'User Email': element.userEmail,
|
|
3455
|
+
'Status': element.status,
|
|
3456
|
+
'Audit Status': element.status,
|
|
3457
|
+
};
|
|
3458
|
+
|
|
3459
|
+
switch ( inputData.moduleType ) {
|
|
3460
|
+
case 'camera-angle-change':
|
|
3461
|
+
temp['stream Name'] = element.streamName;
|
|
3462
|
+
temp['User Comments'] = element.userComments;
|
|
3463
|
+
break;
|
|
3464
|
+
case 'unattended-customer':
|
|
3465
|
+
temp['Customer ID'] = element.tempId;
|
|
3466
|
+
temp['Question'] = element.question;
|
|
3467
|
+
temp['User Comments'] = element.userComments;
|
|
3468
|
+
break;
|
|
3469
|
+
case 'left-in-middle':
|
|
3470
|
+
temp['Question'] = element.question;
|
|
3471
|
+
temp['User Comments'] = element.userComments;
|
|
3472
|
+
break;
|
|
3473
|
+
case 'mobile-detection':
|
|
3474
|
+
temp['Before Count'] = element.beforeCount;
|
|
3475
|
+
temp['After Count'] = element.afterCount;
|
|
3476
|
+
break;
|
|
3477
|
+
case 'uniform-detection':
|
|
3478
|
+
temp['Before Count'] = element.beforeCount;
|
|
3479
|
+
temp['After Count'] = element.afterCount;
|
|
3480
|
+
break;
|
|
3481
|
+
}
|
|
3482
|
+
exportdata.push( temp );
|
|
3483
|
+
} );
|
|
3484
|
+
await download( exportdata, res );
|
|
3485
|
+
return;
|
|
3486
|
+
}
|
|
3487
|
+
return res.sendSuccess( { result: result, count: count.length } );
|
|
3488
|
+
} catch ( error ) {
|
|
3489
|
+
const err = error.message || 'Internal Server Error';
|
|
3490
|
+
logger.error( { error: error, message: req.body, function: 'overviewTable' } );
|
|
3491
|
+
return res.sendError( err, 500 );
|
|
3492
|
+
}
|
|
3493
|
+
}
|
|
3494
|
+
|
|
3495
|
+
|
|
3496
|
+
export async function getDetectionAuditFile( req, res ) {
|
|
3497
|
+
try {
|
|
3498
|
+
const openSearch = JSON.parse( process.env.OPENSEARCH );
|
|
3499
|
+
const inputData = req.query;
|
|
3500
|
+
const previousDate = dayjs( new Date() ).subtract( 1, 'days' );
|
|
3501
|
+
const data = await getDate( new Date( dayjs( previousDate ).format( 'YYYY-MM-DD' ) ), new Date() );
|
|
3502
|
+
logger.info( { data: data, previousDate: previousDate, function: 'data', newDate: new Date() } );
|
|
3503
|
+
logger.info( { userId: req.user._id, queueName: inputData.queueName, moduleType: inputData.moduleType } );
|
|
3504
|
+
const start = data.start;
|
|
3505
|
+
const end = data.end;
|
|
3506
|
+
const fetchData = {};
|
|
3507
|
+
let msg = {};
|
|
3508
|
+
const userQuery = [
|
|
3509
|
+
{
|
|
3510
|
+
$match: {
|
|
3511
|
+
$and: [
|
|
3512
|
+
{ userId: req.user._id },
|
|
3513
|
+
{ queueName: inputData.queueName },
|
|
3514
|
+
{ auditStatus: { $nin: [ 'completed', 'skipped' ] } },
|
|
3515
|
+
{ createdAt: { $gte: start } },
|
|
3516
|
+
{ createdAt: { $lte: end } },
|
|
3517
|
+
{ moduleType: inputData.moduleType },
|
|
3518
|
+
],
|
|
3519
|
+
},
|
|
3520
|
+
},
|
|
3521
|
+
{
|
|
3522
|
+
$sort: { createdAt: -1 },
|
|
3523
|
+
},
|
|
3524
|
+
{
|
|
3525
|
+
$limit: 1,
|
|
3526
|
+
},
|
|
3527
|
+
];
|
|
3528
|
+
|
|
3529
|
+
const userDetails = await aggregateUserEmpDetection( userQuery );
|
|
3530
|
+
logger.info( { userDetails: userDetails, function: 'userDetails' } );
|
|
3531
|
+
const auditStatus =
|
|
3532
|
+
userDetails && userDetails.length > 0 ? userDetails[0].auditStatus : null;
|
|
3533
|
+
switch ( auditStatus ) {
|
|
3534
|
+
case null:
|
|
3535
|
+
const query = [
|
|
3536
|
+
{
|
|
3537
|
+
$match: {
|
|
3538
|
+
$and: [
|
|
3539
|
+
{ userId: { $eq: req.user._id } },
|
|
3540
|
+
{ isCompleted: { $eq: false } },
|
|
3541
|
+
{ queueName: inputData.queueName },
|
|
3542
|
+
{ moduleType: inputData.moduleType },
|
|
3543
|
+
],
|
|
3544
|
+
},
|
|
3545
|
+
},
|
|
3546
|
+
];
|
|
3547
|
+
const userAssign = await aggregateAssignAudit( query );
|
|
3548
|
+
if ( userAssign.length > 0 ) {
|
|
3549
|
+
logger.info( 'Hit in user assign', { inputData } );
|
|
3550
|
+
userAssign[0].auditType == 'Audit' ?
|
|
3551
|
+
( msg = {
|
|
3552
|
+
storeId: userAssign[0].storeId,
|
|
3553
|
+
fileDate: userAssign[0].fileDate,
|
|
3554
|
+
fileCount: userAssign[0].fileCount,
|
|
3555
|
+
inputBucketName: userAssign[0].inputBucketName,
|
|
3556
|
+
folderPath: userAssign[0].folderPath,
|
|
3557
|
+
} ) :
|
|
3558
|
+
( msg = {
|
|
3559
|
+
storeId: userAssign[0].storeId,
|
|
3560
|
+
fileDate: userAssign[0].fileDate,
|
|
3561
|
+
auditType: userAssign[0].auditType,
|
|
3562
|
+
fileCount: userAssign[0].fileCount,
|
|
3563
|
+
inputBucketName: userAssign[0].inputBucketName,
|
|
3564
|
+
folderPath: userAssign[0].folderPath,
|
|
3565
|
+
} );
|
|
3566
|
+
await updateOneAssignAudit(
|
|
3567
|
+
{ _id: userAssign[0]._id },
|
|
3568
|
+
{ isCompleted: true },
|
|
3569
|
+
);
|
|
3570
|
+
logger.info( 'Hit in auditUserAssignModel updateOne', {
|
|
3571
|
+
_id: userAssign[0]._id,
|
|
3572
|
+
isCompleted: true,
|
|
3573
|
+
} );
|
|
3574
|
+
} else {
|
|
3575
|
+
logger.info( 'Hit in new file', { inputData } );
|
|
3576
|
+
const consumer = await sqsReceive( inputData.queueName );
|
|
3577
|
+
if ( !consumer ) {
|
|
3578
|
+
logger.error( {
|
|
3579
|
+
error: consumer,
|
|
3580
|
+
function: 'getAuditFile',
|
|
3581
|
+
message: 'SQS Receive queue is empty',
|
|
3582
|
+
} );
|
|
3583
|
+
return res.sendError( `${inputData.queueName} is Empty`, 204 );
|
|
3584
|
+
}
|
|
3585
|
+
msg = JSON.parse( consumer );
|
|
3586
|
+
}
|
|
3587
|
+
break;
|
|
3588
|
+
|
|
3589
|
+
case 'drafted':
|
|
3590
|
+
logger.info( 'userDetails drafted', { inputData } );
|
|
3591
|
+
const log = await findOneAuditLog(
|
|
3592
|
+
{
|
|
3593
|
+
userId: userDetails[0].userId,
|
|
3594
|
+
fileDate: userDetails[0].fileDate,
|
|
3595
|
+
storeId: userDetails[0].storeId,
|
|
3596
|
+
totalCount: userDetails[0].beforeCount,
|
|
3597
|
+
moduleType: userDetails[0].moduleType,
|
|
3598
|
+
},
|
|
3599
|
+
{},
|
|
3600
|
+
{ createdAt: -1 },
|
|
3601
|
+
);
|
|
3602
|
+
if ( !log ) {
|
|
3603
|
+
await updateOneUserEmpDetection(
|
|
3604
|
+
{ _id: userDetails[0]._id },
|
|
3605
|
+
{ $set: { isDraft: false, auditStatus: 'skipped' } },
|
|
3606
|
+
);
|
|
3607
|
+
logger.info( 'audit update in drafted', {
|
|
3608
|
+
_id: userDetails[0]._id,
|
|
3609
|
+
isDraft: false,
|
|
3610
|
+
auditStatus: 'skipped',
|
|
3611
|
+
} );
|
|
3612
|
+
return res.sendError( 'User saved data has been deleted', 204 );
|
|
3613
|
+
}
|
|
3614
|
+
const file = {
|
|
3615
|
+
auditId: userDetails[0]._id,
|
|
3616
|
+
storeId: userDetails[0].storeId,
|
|
3617
|
+
fileDate: userDetails[0].fileDate,
|
|
3618
|
+
auditType: userDetails[0].auditType,
|
|
3619
|
+
queueName: log.queueName,
|
|
3620
|
+
userId: log.userId,
|
|
3621
|
+
moduleType: userDetails[0].moduleType,
|
|
3622
|
+
};
|
|
3623
|
+
const result = {
|
|
3624
|
+
junk: log.junk,
|
|
3625
|
+
junk_count: log.junkCount,
|
|
3626
|
+
employee: log.employee,
|
|
3627
|
+
employee_count: log.employeeCount,
|
|
3628
|
+
retag_image: log.retagImage,
|
|
3629
|
+
retag_count: log.retagCount,
|
|
3630
|
+
total_count: log.totalCount,
|
|
3631
|
+
};
|
|
3632
|
+
|
|
3633
|
+
const userdata = await findOneUser( { _id: log.userId } );
|
|
3634
|
+
if (
|
|
3635
|
+
!inputData.nextId ||
|
|
3636
|
+
inputData.nextId === '' ||
|
|
3637
|
+
inputData.nextId == null
|
|
3638
|
+
) {
|
|
3639
|
+
const logData = {
|
|
3640
|
+
userId: log.userId,
|
|
3641
|
+
userName: userdata.userName,
|
|
3642
|
+
logType: 'traxAudit',
|
|
3643
|
+
logSubType: 'auditStart',
|
|
3644
|
+
logData: {
|
|
3645
|
+
fileDate: userDetails[0].fileDate,
|
|
3646
|
+
auditType: userDetails[0].auditType,
|
|
3647
|
+
storeId: userDetails[0].storeId,
|
|
3648
|
+
clientName: log.queueName,
|
|
3649
|
+
auditId: userDetails[0]._id,
|
|
3650
|
+
moduleType: userDetails[0].moduleType,
|
|
3651
|
+
zoneName: userDetails[0].zoneName,
|
|
3652
|
+
},
|
|
3653
|
+
createdAt: new Date(),
|
|
3654
|
+
};
|
|
3655
|
+
logger.info( { logData: logData } );
|
|
3656
|
+
await insertOpenSearchData( openSearch.auditLog, logData );
|
|
3657
|
+
}
|
|
3658
|
+
const storeQuery = {
|
|
3659
|
+
storeId: userDetails[0].storeId,
|
|
3660
|
+
};
|
|
3661
|
+
const storeFields = {
|
|
3662
|
+
storeId: 1,
|
|
3663
|
+
storeName: 1,
|
|
3664
|
+
address: '$storeProfile.address',
|
|
3665
|
+
};
|
|
3666
|
+
const storeDetails = await findOneStore( storeQuery, storeFields );
|
|
3667
|
+
res.sendSuccess( {
|
|
3668
|
+
result: result,
|
|
3669
|
+
storeId: storeDetails?.storeId,
|
|
3670
|
+
storeName: storeDetails?.storeName,
|
|
3671
|
+
address: storeDetails?.address || '',
|
|
3672
|
+
count: log.totalCount,
|
|
3673
|
+
file: file,
|
|
3674
|
+
isDraft: userDetails[0].isDraft,
|
|
3675
|
+
} );
|
|
3676
|
+
break;
|
|
3677
|
+
|
|
3678
|
+
default:
|
|
3679
|
+
msg = {
|
|
3680
|
+
storeId: userDetails[0].storeId,
|
|
3681
|
+
fileDate: userDetails[0].fileDate,
|
|
3682
|
+
auditType: userDetails[0].auditType,
|
|
3683
|
+
fileCount: userDetails[0].beforeCount,
|
|
3684
|
+
inputBucketName: userDetails[0].inputBucketName,
|
|
3685
|
+
folderPath: userDetails[0].folderPath,
|
|
3686
|
+
};
|
|
3687
|
+
break;
|
|
3688
|
+
}
|
|
3689
|
+
// if ( msg.auditType === 'ReAudit' ) {
|
|
3690
|
+
// logger.info( 'Hit in user ReAudit', { inputData } );
|
|
3691
|
+
// let reauditInsert = {};
|
|
3692
|
+
// const [ filterData, auditImage ] = await Promise.all( [
|
|
3693
|
+
// getFilterData( msg ),
|
|
3694
|
+
// getAuditImage( msg ),
|
|
3695
|
+
// ] );
|
|
3696
|
+
// if ( filterData.statusCode == 404 || auditImage.statusCode == 404 ) {
|
|
3697
|
+
// return res.sendError( 'No Data found', 204 );
|
|
3698
|
+
// }
|
|
3699
|
+
// const reauditImg = await getReauditImg( filterData, auditImage );
|
|
3700
|
+
// if ( userDetails.length === 0 ) {
|
|
3701
|
+
// const [ day, month, year ] = msg.curr_date.split( '-' );
|
|
3702
|
+
// const temp = `${year}-${month}-${day}`;
|
|
3703
|
+
// let start = new Date( temp );
|
|
3704
|
+
// const userTimezoneOffset = start.getTimezoneOffset() * 60000;
|
|
3705
|
+
// start = new Date( start.getTime() - userTimezoneOffset );
|
|
3706
|
+
// start.setUTCHours( 0, 0, 0, 0 );
|
|
3707
|
+
// const record = {
|
|
3708
|
+
// userId: req.user._id,
|
|
3709
|
+
// storeId: msg.store_id,
|
|
3710
|
+
// clientId: msg.store_id.split( '-' )[0],
|
|
3711
|
+
// auditType: msg.audit_type || 'ReAudit',
|
|
3712
|
+
// fileDate: msg.curr_date,
|
|
3713
|
+
// queueName: inputData.queueName,
|
|
3714
|
+
// beforeCount: reauditImg.length,
|
|
3715
|
+
// auditStatus: 'inprogress',
|
|
3716
|
+
// fileDateISO: start,
|
|
3717
|
+
// timeSpent: 0,
|
|
3718
|
+
// startTime: new Date(),
|
|
3719
|
+
// zoneName: msg.zone_id,
|
|
3720
|
+
// moduleType: inputData.moduleType,
|
|
3721
|
+
// };
|
|
3722
|
+
// const query = {
|
|
3723
|
+
// storeId: msg.store_id,
|
|
3724
|
+
// zoneName: msg.zone_id,
|
|
3725
|
+
// fileDate: msg.curr_date,
|
|
3726
|
+
// moduleType: inputData.moduleType,
|
|
3727
|
+
// };
|
|
3728
|
+
// let storeRecord = {
|
|
3729
|
+
// userId: req.user._id,
|
|
3730
|
+
// auditType: msg.audit_type || 'ReAudit',
|
|
3731
|
+
// status: 'inprogress',
|
|
3732
|
+
// timeSpent: 0,
|
|
3733
|
+
// };
|
|
3734
|
+
// reauditInsert = await createUserAudit( record );
|
|
3735
|
+
|
|
3736
|
+
// await updateOneStoreAudit( query, storeRecord );
|
|
3737
|
+
// } else {
|
|
3738
|
+
// reauditInsert = userDetails[0];
|
|
3739
|
+
// }
|
|
3740
|
+
// const userdata = await findOneUser( { _id: req.user._id } );
|
|
3741
|
+
// if ( inputData.nextId ) {
|
|
3742
|
+
// const logData = {
|
|
3743
|
+
// userId: req.user._id,
|
|
3744
|
+
// userName: userdata.name,
|
|
3745
|
+
// logType: 'audit',
|
|
3746
|
+
// logSubType: 'auditStart',
|
|
3747
|
+
// logData: {
|
|
3748
|
+
// fileDate: msg.curr_date,
|
|
3749
|
+
// auditType: msg.audit_type,
|
|
3750
|
+
// storeId: msg.store_id,
|
|
3751
|
+
// clientName: inputData.queueName,
|
|
3752
|
+
// auditId: reauditInsert._id,
|
|
3753
|
+
// zoneName: msg.zone_id,
|
|
3754
|
+
// moduleType: inputData.moduleType,
|
|
3755
|
+
// },
|
|
3756
|
+
// createdAt: new Date(),
|
|
3757
|
+
// };
|
|
3758
|
+
// await insertOpenSearchData( openSearch.auditLog, logData );
|
|
3759
|
+
// }
|
|
3760
|
+
|
|
3761
|
+
// const storeQuery = {
|
|
3762
|
+
// storeId: msg.store_id,
|
|
3763
|
+
// };
|
|
3764
|
+
// const storeFields = {
|
|
3765
|
+
// storeId: 1,
|
|
3766
|
+
// storeName: 1,
|
|
3767
|
+
// address: '$storeProfile.address',
|
|
3768
|
+
// };
|
|
3769
|
+
// const storeInfo = await findOneStore( storeQuery, storeFields );
|
|
3770
|
+
// return res.sendSuccess( {
|
|
3771
|
+
// result: reauditImg,
|
|
3772
|
+
// count: reauditImg.length,
|
|
3773
|
+
// storeId: storeInfo?.storeId,
|
|
3774
|
+
// storeName: storeInfo?.storeName,
|
|
3775
|
+
// address: storeInfo?.address || '',
|
|
3776
|
+
// file: {
|
|
3777
|
+
// store_id: msg.store_id,
|
|
3778
|
+
// file_date: msg.curr_date,
|
|
3779
|
+
// type: msg.audit_type || 'ReAudit',
|
|
3780
|
+
// queueName: inputData.queueName,
|
|
3781
|
+
// auditId: reauditInsert._id,
|
|
3782
|
+
// userId: reauditInsert.userId,
|
|
3783
|
+
// zoneName: msg.zone_id,
|
|
3784
|
+
// moduleType: inputData.moduleType,
|
|
3785
|
+
// },
|
|
3786
|
+
// isDraft: reauditInsert.isDraft,
|
|
3787
|
+
// } );
|
|
3788
|
+
// } to wor for reAudit concept
|
|
3789
|
+
const storeId = msg.storeId;
|
|
3790
|
+
const fileDate = msg.fileDate;
|
|
3791
|
+
|
|
3792
|
+
const files = [];
|
|
3793
|
+
let insertData = {};
|
|
3794
|
+
fetchData.Bucket = msg.inputBucketName; // need to change
|
|
3795
|
+
fetchData.file_path = msg.folderPath;
|
|
3796
|
+
if ( inputData.limit ) {
|
|
3797
|
+
fetchData.MaxKeys = inputData.limit;
|
|
3798
|
+
}
|
|
3799
|
+
if ( inputData.nextId ) {
|
|
3800
|
+
fetchData.ContinuationToken = decodeURIComponent( inputData.nextId );
|
|
3801
|
+
}
|
|
3802
|
+
const list = await listFileByPath( fetchData );
|
|
3803
|
+
const folderPath = list.data;
|
|
3804
|
+
const nextQuery = list.pageToken;
|
|
3805
|
+
|
|
3806
|
+
if ( folderPath?.length > 0 ) {
|
|
3807
|
+
for ( let i = 0; i < folderPath.length; i++ ) {
|
|
3808
|
+
const img = folderPath[i].Key.split( '/' );
|
|
3809
|
+
const image = img[3].split( '.' );
|
|
3810
|
+
const indexes = image[0].split( '_' );
|
|
3811
|
+
fetchData.file_path = folderPath[i].Key;
|
|
3812
|
+
const data = await signedUrl( fetchData );
|
|
3813
|
+
const mapimg = {
|
|
3814
|
+
img_path: data,
|
|
3815
|
+
img_name: image[0],
|
|
3816
|
+
img_id: indexes[1],
|
|
3817
|
+
};
|
|
3818
|
+
files.push( {
|
|
3819
|
+
img_path: data,
|
|
3820
|
+
img_name: image[0],
|
|
3821
|
+
img_id: indexes[1],
|
|
3822
|
+
selected: false,
|
|
3823
|
+
dropped: false,
|
|
3824
|
+
count: 1,
|
|
3825
|
+
mappedid: [ mapimg ],
|
|
3826
|
+
} );
|
|
3827
|
+
}
|
|
3828
|
+
if ( userDetails.length === 0 ) {
|
|
3829
|
+
const [ day, month, year ] = msg.fileDate.split( '-' );
|
|
3830
|
+
const temp = `${year}-${month}-${day}`;
|
|
3831
|
+
let start = new Date( temp );
|
|
3832
|
+
logger.info( { start: start, date: msg.fileDate } );
|
|
3833
|
+
const userTimezoneOffset = start.getTimezoneOffset() * 60000;
|
|
3834
|
+
start = new Date( start.getTime() - userTimezoneOffset );
|
|
3835
|
+
start.setUTCHours( 0, 0, 0, 0 );
|
|
3836
|
+
const record = {
|
|
3837
|
+
userId: req.user._id,
|
|
3838
|
+
storeId: msg.storeId,
|
|
3839
|
+
clientId: msg.storeId.split( '-' )[0],
|
|
3840
|
+
auditType: 'Audit',
|
|
3841
|
+
fileDate: msg.fileDate,
|
|
3842
|
+
inputBucketName: msg.inputBucketName,
|
|
3843
|
+
folderPath: msg.folderPath,
|
|
3844
|
+
queueName: inputData.queueName,
|
|
3845
|
+
beforeCount: msg.fileCount || files.length,
|
|
3846
|
+
auditStatus: 'inprogress',
|
|
3847
|
+
fileDateISO: start,
|
|
3848
|
+
timeSpent: 0,
|
|
3849
|
+
startTime: new Date(),
|
|
3850
|
+
moduleType: inputData.moduleType,
|
|
3851
|
+
};
|
|
3852
|
+
const query = {
|
|
3853
|
+
storeId: msg.storeId,
|
|
3854
|
+
fileDate: msg.fileDate,
|
|
3855
|
+
moduleType: inputData.moduleType,
|
|
3856
|
+
};
|
|
3857
|
+
const storeRecord = {
|
|
3858
|
+
userId: req.user._id,
|
|
3859
|
+
storeId: msg.storeId,
|
|
3860
|
+
clientId: msg.storeId.split( '-' )[0],
|
|
3861
|
+
auditType: 'Audit',
|
|
3862
|
+
fileDate: msg.fileDate,
|
|
3863
|
+
folderPath: msg.folderPath,
|
|
3864
|
+
inputBucketName: msg.inputBucketName,
|
|
3865
|
+
queueName: inputData.queueName,
|
|
3866
|
+
beforeCount: msg.fileCount || files.length,
|
|
3867
|
+
status: 'inprogress',
|
|
3868
|
+
fileDateISO: start,
|
|
3869
|
+
timeSpent: 0,
|
|
3870
|
+
moduleType: inputData.moduleType,
|
|
3871
|
+
};
|
|
3872
|
+
|
|
3873
|
+
insertData = await createUserEmpDetection( record );
|
|
3874
|
+
await updateManyStoreEmpDetection( query, storeRecord );
|
|
3875
|
+
} else {
|
|
3876
|
+
insertData = userDetails[0];
|
|
3877
|
+
}
|
|
3878
|
+
} else {
|
|
3879
|
+
logger.error( {
|
|
3880
|
+
error: { folderPath: folderPath, nextQuery: nextQuery },
|
|
3881
|
+
function: 'getAuditFile',
|
|
3882
|
+
message: 'Bucket image data not availale',
|
|
3883
|
+
} );
|
|
3884
|
+
return res.sendError( 'Bucket is Empty', 204 );
|
|
3885
|
+
}
|
|
3886
|
+
const userdata = await findOneUser( { _id: insertData.userId } );
|
|
3887
|
+
const storeQuery = {
|
|
3888
|
+
storeId: storeId,
|
|
3889
|
+
};
|
|
3890
|
+
const storeFields = {
|
|
3891
|
+
storeName: 1,
|
|
3892
|
+
address: '$storeProfile.address',
|
|
3893
|
+
};
|
|
3894
|
+
const storeInfo = await findOneStore( storeQuery, storeFields );
|
|
3895
|
+
if ( inputData.nextId ) {
|
|
3896
|
+
const logData = {
|
|
3897
|
+
userId: insertData.userId,
|
|
3898
|
+
userName: userdata.userName,
|
|
3899
|
+
logType: 'traxAudit',
|
|
3900
|
+
logSubType: 'auditStart',
|
|
3901
|
+
logData: {
|
|
3902
|
+
fileDate: insertData.fileDate,
|
|
3903
|
+
auditType: insertData.auditType,
|
|
3904
|
+
storeId: storeId,
|
|
3905
|
+
clientName: inputData.queueName,
|
|
3906
|
+
auditId: insertData._id,
|
|
3907
|
+
moduleType: inputData.moduleType,
|
|
3908
|
+
},
|
|
3909
|
+
createdAt: new Date(),
|
|
3910
|
+
};
|
|
3911
|
+
await insertOpenSearchData( openSearch.auditLog, logData );
|
|
3912
|
+
}
|
|
3913
|
+
logger.info( { storeInfo: storeInfo } );
|
|
3914
|
+
return res.sendSuccess( {
|
|
3915
|
+
result: files,
|
|
3916
|
+
count: msg.fileCount,
|
|
3917
|
+
storeId: storeId,
|
|
3918
|
+
storeName: storeInfo?.storeName,
|
|
3919
|
+
address: storeInfo?.address,
|
|
3920
|
+
file: {
|
|
3921
|
+
queueName: inputData.queueName,
|
|
3922
|
+
storeId: storeId,
|
|
3923
|
+
fileDate: fileDate,
|
|
3924
|
+
auditType: 'Audit',
|
|
3925
|
+
auditId: insertData._id,
|
|
3926
|
+
userId: insertData.userId,
|
|
3927
|
+
moduleType: inputData.moduleType,
|
|
3928
|
+
nextToken: nextQuery ? encodeURIComponent( nextQuery ) : null,
|
|
3929
|
+
},
|
|
3930
|
+
isDraft: insertData.isDraft,
|
|
3931
|
+
} );
|
|
3280
3932
|
} catch ( error ) {
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3933
|
+
logger.error( {
|
|
3934
|
+
error: error,
|
|
3935
|
+
message: req.query,
|
|
3936
|
+
function: 'getDetectionAuditFile',
|
|
3937
|
+
} );
|
|
3938
|
+
return res.sendError( error, 500 );
|
|
3284
3939
|
}
|
|
3285
3940
|
}
|
|
3286
3941
|
|
|
3287
|
-
|
|
3942
|
+
|
|
3943
|
+
export async function saveDraft( req, res ) {
|
|
3288
3944
|
try {
|
|
3945
|
+
const openSearch = JSON.parse( process.env.OPENSEARCH );
|
|
3289
3946
|
const inputData = req.body;
|
|
3290
|
-
|
|
3291
|
-
const
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
{ fileDateISO: { $gte: dateRange.start } },
|
|
3295
|
-
{ fileDateISO: { $lte: dateRange.end } },
|
|
3296
|
-
{ moduleType: { $eq: inputData.moduleType } },
|
|
3297
|
-
{ clientId: { $in: inputData.clientId } },
|
|
3298
|
-
|
|
3299
|
-
];
|
|
3300
|
-
|
|
3301
|
-
if ( inputData.filterByAuditType.length > 0 ) {
|
|
3302
|
-
filters.push( { auditType: { $in: inputData.filterByAuditType } } );
|
|
3947
|
+
inputData.userId = req.user._id;
|
|
3948
|
+
const getUserAuditData = await findOneUserAudit( { _id: inputData.auditId } );
|
|
3949
|
+
if ( !getUserAuditData ) {
|
|
3950
|
+
return res.sendError( 'No Data Found', 204 );
|
|
3303
3951
|
}
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
filters.push( { status: { $in: inputData.filterByStatus } } );
|
|
3952
|
+
if ( getUserAuditData.auditStatus == 'skipped' ) {
|
|
3953
|
+
return res.sendError( 'File Assigned to Someone else', 203 );
|
|
3307
3954
|
}
|
|
3308
3955
|
|
|
3309
|
-
const
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3956
|
+
const userQuery = {
|
|
3957
|
+
_id: inputData.auditId,
|
|
3958
|
+
};
|
|
3959
|
+
let userRecord = {
|
|
3960
|
+
isDraft: true,
|
|
3961
|
+
auditStatus: 'drafted',
|
|
3962
|
+
};
|
|
3963
|
+
const storeQuery = {
|
|
3964
|
+
storeId: inputData.storeId,
|
|
3965
|
+
fileDate: inputData.fileDate,
|
|
3966
|
+
zoneName: inputData.zoneName,
|
|
3967
|
+
};
|
|
3314
3968
|
|
|
3315
|
-
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
_id: 0,
|
|
3319
|
-
storeId: 1,
|
|
3320
|
-
clientId: 1,
|
|
3321
|
-
streamName: 1,
|
|
3322
|
-
tempId: 1,
|
|
3323
|
-
question: 1,
|
|
3324
|
-
questionType: 1,
|
|
3325
|
-
answer: 1,
|
|
3326
|
-
userComments: 1,
|
|
3327
|
-
fileDate: 1,
|
|
3328
|
-
moduleType: 1,
|
|
3969
|
+
let storeRecord = {
|
|
3970
|
+
status: 'drafted',
|
|
3971
|
+
};
|
|
3329
3972
|
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
auditType: 1,
|
|
3336
|
-
status: 1,
|
|
3337
|
-
auditStatus: 1,
|
|
3338
|
-
// userId: 1,
|
|
3339
|
-
},
|
|
3340
|
-
},
|
|
3341
|
-
// {
|
|
3342
|
-
// $lookup: {
|
|
3343
|
-
// from: 'users',
|
|
3344
|
-
// let: { userId: '$userId' },
|
|
3345
|
-
// pipeline: [
|
|
3346
|
-
// {
|
|
3347
|
-
// $match: {
|
|
3348
|
-
// $expr: {
|
|
3349
|
-
// $eq: [ '$_id', '$$userId' ],
|
|
3350
|
-
// },
|
|
3351
|
-
// },
|
|
3352
|
-
// },
|
|
3353
|
-
// {
|
|
3354
|
-
// $project: {
|
|
3355
|
-
// userName: 1,
|
|
3356
|
-
// userEmail: '$email',
|
|
3357
|
-
// },
|
|
3358
|
-
// },
|
|
3359
|
-
// ], as: 'user',
|
|
3360
|
-
// },
|
|
3361
|
-
// },
|
|
3362
|
-
// {
|
|
3363
|
-
// $unwind: {
|
|
3364
|
-
// path: '$user',
|
|
3365
|
-
// preserveNullAndEmptyArrays: true,
|
|
3366
|
-
// },
|
|
3367
|
-
// },
|
|
3368
|
-
{
|
|
3369
|
-
$project: {
|
|
3370
|
-
storeId: 1,
|
|
3371
|
-
clientId: 1,
|
|
3372
|
-
streamName: 1,
|
|
3373
|
-
tempId: 1,
|
|
3374
|
-
question: 1,
|
|
3375
|
-
questionType: 1,
|
|
3376
|
-
answer: 1,
|
|
3377
|
-
userComments: 1,
|
|
3378
|
-
fileDate: 1,
|
|
3379
|
-
moduleType: 1,
|
|
3380
|
-
value: 1,
|
|
3381
|
-
beforeCount: 1,
|
|
3382
|
-
afterCount: 1,
|
|
3383
|
-
auditType: 1,
|
|
3384
|
-
status: 1,
|
|
3385
|
-
auditStatus: 1,
|
|
3386
|
-
// userName: '$user.userName',
|
|
3387
|
-
// userEmail: '$user.userEmail',
|
|
3973
|
+
if ( getUserAuditData?.startTime ) {
|
|
3974
|
+
const isoDate = getUserAuditData.startTime;
|
|
3975
|
+
const currentTime = dayjs();
|
|
3976
|
+
const isoDateTime = dayjs( isoDate );
|
|
3977
|
+
const timeDifferenceInMinutes = currentTime.diff( isoDateTime, 'seconds' );
|
|
3388
3978
|
|
|
3389
|
-
|
|
3390
|
-
},
|
|
3391
|
-
];
|
|
3392
|
-
if ( inputData.searchValue && inputData.searchValue!== '' ) {
|
|
3393
|
-
query.push( {
|
|
3394
|
-
$match: {
|
|
3395
|
-
$or: [
|
|
3396
|
-
{ storeId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3397
|
-
{ clientId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3398
|
-
{ fileDate: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3399
|
-
{ auditType: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3400
|
-
{ status: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3401
|
-
{ auditStatus: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3402
|
-
{ value: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3403
|
-
{ streamName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3404
|
-
{ tempId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3405
|
-
{ question: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3406
|
-
{ answer: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3407
|
-
{ userComments: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3408
|
-
// { userName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3409
|
-
// { userEmail: { $regex: inputData.searchValue, $options: 'i' } },
|
|
3410
|
-
],
|
|
3979
|
+
userRecord.timeSpent = timeDifferenceInMinutes;
|
|
3411
3980
|
|
|
3412
|
-
|
|
3413
|
-
} );
|
|
3981
|
+
storeRecord.timeSpent = timeDifferenceInMinutes;
|
|
3414
3982
|
}
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3983
|
+
|
|
3984
|
+
if ( inputData.userCommands ) {
|
|
3985
|
+
userRecord.userCommands = inputData.userCommands;
|
|
3986
|
+
const logData = {
|
|
3987
|
+
userId: req.user._id,
|
|
3988
|
+
userName: req.user.userName,
|
|
3989
|
+
logType: 'trax',
|
|
3990
|
+
logSubType: 'traxDraft',
|
|
3991
|
+
logData: {
|
|
3992
|
+
fileDate: inputData.fileDate,
|
|
3993
|
+
auditType: inputData.auditType,
|
|
3994
|
+
storeId: inputData.storeId,
|
|
3995
|
+
clientName: inputData.queueName,
|
|
3996
|
+
beforeCount: inputData.totalCount,
|
|
3997
|
+
afterCount: inputData.employeeCount,
|
|
3998
|
+
auditId: inputData.auditId,
|
|
3999
|
+
moduleType: inputData.moduleType,
|
|
4000
|
+
},
|
|
4001
|
+
createdAt: new Date(),
|
|
4002
|
+
};
|
|
4003
|
+
await insertOpenSearchData( openSearch.auditLog, logData );
|
|
3422
4004
|
}
|
|
3423
|
-
|
|
3424
|
-
|
|
4005
|
+
await createAuditLog( inputData );
|
|
4006
|
+
await updateOneUserEmpDetection( userQuery, userRecord );
|
|
4007
|
+
await updateOneStoreEmp( storeQuery, storeRecord );
|
|
4008
|
+
return res.sendSuccess( {
|
|
4009
|
+
result: 'The file has been drafted successfully',
|
|
4010
|
+
} );
|
|
4011
|
+
} catch ( error ) {
|
|
4012
|
+
logger.error( {
|
|
4013
|
+
error: error,
|
|
4014
|
+
message: req.query,
|
|
4015
|
+
function: 'saveTraxDraft',
|
|
4016
|
+
} );
|
|
4017
|
+
return res.sendError( error, 500 );
|
|
4018
|
+
}
|
|
4019
|
+
}
|
|
4020
|
+
|
|
4021
|
+
export async function getDraftedData( req, res ) {
|
|
4022
|
+
try {
|
|
4023
|
+
const inputData = req.query;
|
|
4024
|
+
const userId = inputData.userId || req.user._id;
|
|
4025
|
+
const query = {
|
|
4026
|
+
fileDate: inputData.fileDate,
|
|
4027
|
+
storeId: inputData.storeId,
|
|
4028
|
+
userId: userId,
|
|
4029
|
+
moduleType: inputData.moduleType,
|
|
4030
|
+
};
|
|
4031
|
+
const result = await findOneAuditLog( query, {}, { createdAt: -1 }, 1 );
|
|
4032
|
+
|
|
4033
|
+
if ( !result ) {
|
|
3425
4034
|
return res.sendError( 'No Data Found', 204 );
|
|
3426
4035
|
}
|
|
4036
|
+
return res.sendSuccess( { result: result } );
|
|
4037
|
+
} catch ( error ) {
|
|
4038
|
+
const err = error.message || 'Internal Server Error';
|
|
4039
|
+
logger.error( {
|
|
4040
|
+
error: error,
|
|
4041
|
+
message: req.query,
|
|
4042
|
+
function: 'getDraftedTraxData',
|
|
4043
|
+
} );
|
|
4044
|
+
return res.sendError( err, 500 );
|
|
4045
|
+
}
|
|
4046
|
+
}
|
|
3427
4047
|
|
|
3428
|
-
|
|
3429
|
-
|
|
4048
|
+
export async function save( req, res ) {
|
|
4049
|
+
try {
|
|
4050
|
+
const inputData = req.body;
|
|
4051
|
+
const data = await createEmpDetection( inputData );
|
|
4052
|
+
if ( data ) {
|
|
4053
|
+
return res.sendSuccess( { result: 'Updated Successfully' } );
|
|
3430
4054
|
} else {
|
|
3431
|
-
|
|
3432
|
-
$skip: offset,
|
|
3433
|
-
},
|
|
3434
|
-
{
|
|
3435
|
-
$limit: limit,
|
|
3436
|
-
} );
|
|
3437
|
-
}
|
|
3438
|
-
const result = inputData.moduleType.includes( [ 'uniform-detection', 'mobile-detection' ] )? await aggregateStoreEmpDetection( query ) : await aggregateBinaryAudit( query );
|
|
3439
|
-
if ( inputData.isExport ) {
|
|
3440
|
-
const exportdata = [];
|
|
3441
|
-
result.forEach( ( element ) => {
|
|
3442
|
-
const temp ={
|
|
3443
|
-
'Store ID': element.storeId,
|
|
3444
|
-
'Brand ID': element.clientId,
|
|
3445
|
-
'File Date': element.fileDate,
|
|
3446
|
-
'Result': element.answer,
|
|
3447
|
-
'Product Type': element.value,
|
|
3448
|
-
'Audit Type': element.auditType,
|
|
3449
|
-
// 'User Name': element.userName,
|
|
3450
|
-
// 'User Email': element.userEmail,
|
|
3451
|
-
'Status': element.status,
|
|
3452
|
-
'Audit Status': element.status,
|
|
3453
|
-
};
|
|
3454
|
-
|
|
3455
|
-
switch ( inputData.moduleType ) {
|
|
3456
|
-
case 'camera-angle-change':
|
|
3457
|
-
temp['stream Name'] = element.streamName;
|
|
3458
|
-
temp['User Comments'] = element.userComments;
|
|
3459
|
-
break;
|
|
3460
|
-
case 'unattended-customer':
|
|
3461
|
-
temp['Customer ID'] = element.tempId;
|
|
3462
|
-
temp['Question'] = element.question;
|
|
3463
|
-
temp['User Comments'] = element.userComments;
|
|
3464
|
-
break;
|
|
3465
|
-
case 'left-in-middle':
|
|
3466
|
-
temp['Question'] = element.question;
|
|
3467
|
-
temp['User Comments'] = element.userComments;
|
|
3468
|
-
break;
|
|
3469
|
-
case 'mobile-detection':
|
|
3470
|
-
temp['Before Count'] = element.beforeCount;
|
|
3471
|
-
temp['After Count'] = element.afterCount;
|
|
3472
|
-
break;
|
|
3473
|
-
case 'uniform-detection':
|
|
3474
|
-
temp['Before Count'] = element.beforeCount;
|
|
3475
|
-
temp['After Count'] = element.afterCount;
|
|
3476
|
-
break;
|
|
3477
|
-
}
|
|
3478
|
-
exportdata.push( temp );
|
|
3479
|
-
} );
|
|
3480
|
-
await download( exportdata, res );
|
|
3481
|
-
return;
|
|
4055
|
+
return res.sendError( 'something went wrong', 403 );
|
|
3482
4056
|
}
|
|
3483
|
-
return res.sendSuccess( { result: result, count: count.length } );
|
|
3484
4057
|
} catch ( error ) {
|
|
3485
4058
|
const err = error.message || 'Internal Server Error';
|
|
3486
|
-
logger.error( {
|
|
4059
|
+
logger.error( {
|
|
4060
|
+
error: error,
|
|
4061
|
+
message: req.query,
|
|
4062
|
+
function: 'saveTrax',
|
|
4063
|
+
} );
|
|
3487
4064
|
return res.sendError( err, 500 );
|
|
3488
4065
|
}
|
|
3489
4066
|
}
|