tango-app-api-audit 3.3.0-alpha.2 → 3.3.0-alpha.21
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 +3 -3
- package/src/controllers/audit.controllers.js +487 -88
- package/src/docs/audit.docs.js +64 -46
- package/src/dtos/audit.dtos.js +28 -4
- package/src/routes/audit.routes.js +8 -8
- package/src/service/auditStoreData.service.js +1 -1
- package/src/validation/audit.validation.js +9 -8
- package/src/controllers/auditMetrics.controllers.js +0 -71
- package/src/controllers/zoneAudit.controller.js +0 -1827
- package/src/docs/auditMetrics.docs.js +0 -70
- package/src/docs/zoneAudit.docs.js +0 -268
- package/src/dtos/auditMetrics.dtos.js +0 -39
- package/src/dtos/zoneAudit.dtos.js +0 -171
- package/src/routes/auditMetrics.routes.js +0 -14
- package/src/routes/zone.routes.js +0 -24
- package/src/validation/zoneAudit.validation.js +0 -167
|
@@ -1,1827 +0,0 @@
|
|
|
1
|
-
import { download, fileUpload, getJsonFileData, getUTC, insertOpenSearchData, listFileByPath, listFileWithoutLimit, sendMessageToQueue, signedUrl, sqsReceive } from 'tango-app-api-middleware';
|
|
2
|
-
import { aggregateZoneUserAudit, createZoneUserAudit, findOneZoneUserAudit, updateOneZoneUserAudit } from '../service/zoneUserAudit.service.js';
|
|
3
|
-
import { logger } from 'tango-app-api-middleware';
|
|
4
|
-
import { findOneUser } from '../service/user.service.js';
|
|
5
|
-
import { findOneStore } from '../service/store.service.js';
|
|
6
|
-
import dayjs from 'dayjs';
|
|
7
|
-
import { aggregateUserAudit, findOneUserAudit, updateOneUserAudit } from '../service/userAudit.service.js';
|
|
8
|
-
import { aggregateAssignAudit, createAssignAudit } from '../service/assignAudit.service.js';
|
|
9
|
-
import { createAuditLog, findOneAuditLog } from '../service/auditLog.service.js';
|
|
10
|
-
import { mapCustomer, mapEmployee, mapJunk, splitB20000, splitE0, splitG20000, zipDownloadImage } from '../validation/audit.validation.js';
|
|
11
|
-
import { aggregateClient, findClient, findOneClient } from '../service/client.service.js';
|
|
12
|
-
import _ from 'lodash';
|
|
13
|
-
import mongoose from 'mongoose';
|
|
14
|
-
import { listQueue } from 'tango-app-api-middleware/src/utils/aws/sqs.js';
|
|
15
|
-
import { getReauditImg } from './audit.controllers.js';
|
|
16
|
-
import { getZoneAuditFilterData, getZoneAuditImageData } from '../validation/zoneAudit.validation.js';
|
|
17
|
-
|
|
18
|
-
export async function getZoneAuditFile( req, res ) {
|
|
19
|
-
try {
|
|
20
|
-
const bucket = JSON.parse( process.env.BUCKET );
|
|
21
|
-
const openSearch = JSON.parse( process.env.OPENSEARCH );
|
|
22
|
-
const inputData = req.query;
|
|
23
|
-
const data = await getUTC( new Date(), new Date() );
|
|
24
|
-
const start = data.start;
|
|
25
|
-
const end = data.end;
|
|
26
|
-
const fetchData = {};
|
|
27
|
-
let msg = {};
|
|
28
|
-
|
|
29
|
-
const userQuery = [
|
|
30
|
-
{
|
|
31
|
-
$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: 'zone' },
|
|
39
|
-
],
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
$sort: { createdAt: -1 },
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
$limit: 1,
|
|
47
|
-
},
|
|
48
|
-
];
|
|
49
|
-
const userDetails = await aggregateZoneUserAudit( userQuery );
|
|
50
|
-
const zoneAuditStatus =
|
|
51
|
-
userDetails && userDetails.length > 0 ? userDetails[0].auditStatus : null;
|
|
52
|
-
|
|
53
|
-
switch ( zoneAuditStatus ) {
|
|
54
|
-
case null:
|
|
55
|
-
const query = [
|
|
56
|
-
{
|
|
57
|
-
$match: {
|
|
58
|
-
$and: [
|
|
59
|
-
{ userId: { $eq: req.user._id } },
|
|
60
|
-
{ isCompleted: { $eq: false } },
|
|
61
|
-
{ queueName: inputData.queueName },
|
|
62
|
-
],
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
{
|
|
66
|
-
$sort: { createdAt: -1 },
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
$limit: 1,
|
|
70
|
-
},
|
|
71
|
-
];
|
|
72
|
-
|
|
73
|
-
const userAssign = await aggregateAssignAudit( query );
|
|
74
|
-
if ( userAssign.length > 0 ) {
|
|
75
|
-
logger.info( 'Hit in user assign', { inputData } );
|
|
76
|
-
} else {
|
|
77
|
-
logger.info( 'Hit in new file', { inputData } );
|
|
78
|
-
const consumer = await sqsReceive( inputData.queueName );
|
|
79
|
-
if ( !consumer ) {
|
|
80
|
-
logger.error( {
|
|
81
|
-
error: consumer,
|
|
82
|
-
function: 'getAuditFile',
|
|
83
|
-
message: 'SQS Receive queue is empty',
|
|
84
|
-
} );
|
|
85
|
-
return res.sendError( `${inputData.queueName} is Empty`, 204 );
|
|
86
|
-
}
|
|
87
|
-
msg = JSON.parse( consumer );
|
|
88
|
-
}
|
|
89
|
-
break;
|
|
90
|
-
|
|
91
|
-
case 'drafted':
|
|
92
|
-
logger.info( 'userDetails drafted', { inputData } );
|
|
93
|
-
|
|
94
|
-
const log = await findOneZoneUserAudit(
|
|
95
|
-
{
|
|
96
|
-
userId: userDetails[0].userId,
|
|
97
|
-
fileDate: userDetails[0].fileDate,
|
|
98
|
-
storeId: userDetails[0].storeId,
|
|
99
|
-
totalCount: userDetails[0].beforeCount,
|
|
100
|
-
moduleType: 'zone',
|
|
101
|
-
zoneName: userDetails[0].zoneName,
|
|
102
|
-
},
|
|
103
|
-
{},
|
|
104
|
-
{ createdAt: -1 },
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
if ( !log ) {
|
|
108
|
-
await updateOneZoneUserAudit(
|
|
109
|
-
{ _id: userDetails[0]._id },
|
|
110
|
-
{ $set: { isDraft: false, auditStatus: 'skipped' } },
|
|
111
|
-
);
|
|
112
|
-
logger.info( 'zone audit update in drafted', {
|
|
113
|
-
_id: userDetails[0]._id,
|
|
114
|
-
isDraft: false,
|
|
115
|
-
auditStatus: 'skipped',
|
|
116
|
-
} );
|
|
117
|
-
return res.sendError( 'User saved data has been deleted', 204 );
|
|
118
|
-
}
|
|
119
|
-
const file = {
|
|
120
|
-
auditId: userDetails[0]._id,
|
|
121
|
-
store_id: userDetails[0].storeId,
|
|
122
|
-
file_date: userDetails[0].fileDate,
|
|
123
|
-
type: userDetails[0].auditType,
|
|
124
|
-
queueName: log.queueName,
|
|
125
|
-
userId: log.userId,
|
|
126
|
-
zoneName: userDetails[0].zoneName,
|
|
127
|
-
};
|
|
128
|
-
const result = {
|
|
129
|
-
junk: log.junk,
|
|
130
|
-
junk_count: log.junkCount,
|
|
131
|
-
employee: log.employee,
|
|
132
|
-
employee_count: log.employeeCount,
|
|
133
|
-
customer: log.customer,
|
|
134
|
-
customer_count: log.customerCount,
|
|
135
|
-
retag_image: log.retagImage,
|
|
136
|
-
retag_count: log.retagCount,
|
|
137
|
-
total_count: log.totalCount,
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const userdata = await findOneUser( { _id: log.userId } );
|
|
141
|
-
|
|
142
|
-
if (
|
|
143
|
-
!inputData.nextId ||
|
|
144
|
-
inputData.nextId === '' ||
|
|
145
|
-
inputData.nextId == null
|
|
146
|
-
) {
|
|
147
|
-
const logData = {
|
|
148
|
-
userId: log.userId,
|
|
149
|
-
userName: userdata.name,
|
|
150
|
-
logType: 'auditZone',
|
|
151
|
-
logSubType: 'auditZoneStart',
|
|
152
|
-
logData: {
|
|
153
|
-
file_date: userDetails[0].fileDate,
|
|
154
|
-
auditType: userDetails[0].auditType,
|
|
155
|
-
storeId: userDetails[0].storeId,
|
|
156
|
-
clientName: log.queueName,
|
|
157
|
-
auditId: userDetails[0]._id,
|
|
158
|
-
zoneName: userDetails[0].zoneName,
|
|
159
|
-
},
|
|
160
|
-
};
|
|
161
|
-
logger.info( { logData: logData } );
|
|
162
|
-
await insertOpenSearchData( openSearch.activityLog, logData );
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const storeQuery = {
|
|
166
|
-
storeId: userDetails[0].storeId,
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
const storeFields = {
|
|
170
|
-
storeId: 1,
|
|
171
|
-
storeName: 1,
|
|
172
|
-
address: '$storeProfile.address',
|
|
173
|
-
};
|
|
174
|
-
const storeDetails = await findOneStore( storeQuery, storeFields );
|
|
175
|
-
res.sendSuccess( {
|
|
176
|
-
result: result,
|
|
177
|
-
storeId: storeDetails?.storeId,
|
|
178
|
-
storeName: storeDetails?.storeName,
|
|
179
|
-
address: storeDetails?.address || '',
|
|
180
|
-
count: log.totalCount,
|
|
181
|
-
file: file,
|
|
182
|
-
isDraft: userDetails[0].isDraft,
|
|
183
|
-
} );
|
|
184
|
-
break;
|
|
185
|
-
|
|
186
|
-
default:
|
|
187
|
-
msg = {
|
|
188
|
-
store_id: userDetails[0].storeId,
|
|
189
|
-
curr_date: userDetails[0].fileDate,
|
|
190
|
-
audit_type: userDetails[0].auditType,
|
|
191
|
-
total_count: userDetails[0].beforeCount,
|
|
192
|
-
zone_id: userDetails[0].zoneName,
|
|
193
|
-
};
|
|
194
|
-
break;
|
|
195
|
-
}
|
|
196
|
-
/** *
|
|
197
|
-
*
|
|
198
|
-
* Reaudit pending
|
|
199
|
-
*/
|
|
200
|
-
|
|
201
|
-
if ( msg.audit_type === 'ReAudit' ) {
|
|
202
|
-
logger.info( 'Hit in zone user ReAudit', { inputData } );
|
|
203
|
-
let reauditInsert = {};
|
|
204
|
-
const [ filterData, auditImage ] = await Promise.all( [
|
|
205
|
-
getZoneFilterData( msg ),
|
|
206
|
-
getZoneAuditImage( msg ),
|
|
207
|
-
] );
|
|
208
|
-
if ( filterData.statusCode == 404 || auditImage.statusCode == 404 ) {
|
|
209
|
-
return res.sendError( 'No Data found', 204 );
|
|
210
|
-
}
|
|
211
|
-
const reauditImg = await getReauditImg( filterData, auditImage );
|
|
212
|
-
if ( userDetails.length === 0 ) {
|
|
213
|
-
const [ day, month, year ] = msg.curr_date.split( '-' );
|
|
214
|
-
const temp = `${year}-${month}-${day}`;
|
|
215
|
-
let start = new Date( temp );
|
|
216
|
-
const userTimezoneOffset = start.getTimezoneOffset() * 60000;
|
|
217
|
-
start = new Date( start.getTime() - userTimezoneOffset );
|
|
218
|
-
start.setUTCHours( 0, 0, 0, 0 );
|
|
219
|
-
const record = {
|
|
220
|
-
userId: req.user._id,
|
|
221
|
-
storeId: msg.store_id,
|
|
222
|
-
clientId: msg.store_id.split( '-' )[0],
|
|
223
|
-
auditType: msg.audit_type || 'ReAudit',
|
|
224
|
-
fileDate: msg.curr_date,
|
|
225
|
-
queueName: inputData.queueName,
|
|
226
|
-
beforeCount: reauditImg.length,
|
|
227
|
-
zoneAuditStatus: 'inprogress',
|
|
228
|
-
zoneName: msg.zone_id,
|
|
229
|
-
fileDateISO: start,
|
|
230
|
-
timeSpent: 0,
|
|
231
|
-
startTime: new Date(),
|
|
232
|
-
moduleType: 'zone',
|
|
233
|
-
};
|
|
234
|
-
const storeRecord = {
|
|
235
|
-
userId: req.user._id,
|
|
236
|
-
storeId: msg.store_id,
|
|
237
|
-
clientId: msg.store_id.split( '-' )[0],
|
|
238
|
-
auditType: msg.audit_type || 'ReAudit',
|
|
239
|
-
fileDate: msg.curr_date,
|
|
240
|
-
queueName: inputData.queueName,
|
|
241
|
-
beforeCount: reauditImg.length,
|
|
242
|
-
status: 'inprogress',
|
|
243
|
-
fileDateISO: start,
|
|
244
|
-
timeSpent: 0,
|
|
245
|
-
zoneName: msg.zone_id,
|
|
246
|
-
};
|
|
247
|
-
reauditInsert = await createZoneUserAudit( record );
|
|
248
|
-
await createStoreZoneAudit( storeRecord );
|
|
249
|
-
} else {
|
|
250
|
-
reauditInsert = userDetails[0];
|
|
251
|
-
}
|
|
252
|
-
const userdata = await findOneUser( { _id: req.user._id } );
|
|
253
|
-
if ( inputData.nextId ) {
|
|
254
|
-
const logData = {
|
|
255
|
-
userId: req.user._id,
|
|
256
|
-
userName: userdata.name,
|
|
257
|
-
logType: 'auditZone',
|
|
258
|
-
logSubType: 'auditZoneStart',
|
|
259
|
-
logData: {
|
|
260
|
-
fileDate: msg.curr_date,
|
|
261
|
-
auditType: msg.audit_type,
|
|
262
|
-
storeId: msg.store_id,
|
|
263
|
-
clientName: inputData.queueName,
|
|
264
|
-
auditId: reauditInsert._id,
|
|
265
|
-
zoneName: msg.zone_id,
|
|
266
|
-
},
|
|
267
|
-
};
|
|
268
|
-
await insertOpenSearchData( openSearch.activityLog, logData );
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const storeQuery = {
|
|
272
|
-
storeId: msg.store_id,
|
|
273
|
-
};
|
|
274
|
-
const storeFields = {
|
|
275
|
-
storeId: 1,
|
|
276
|
-
storeName: 1,
|
|
277
|
-
address: '$storeProfile.address',
|
|
278
|
-
};
|
|
279
|
-
const storeInfo = await findOneStore( storeQuery, storeFields );
|
|
280
|
-
return res.sendSuccess( {
|
|
281
|
-
result: reauditImg,
|
|
282
|
-
count: reauditImg.length,
|
|
283
|
-
storeId: storeInfo?.storeId,
|
|
284
|
-
storeName: storeInfo?.storeName,
|
|
285
|
-
address: storeInfo?.address || '',
|
|
286
|
-
file: {
|
|
287
|
-
store_id: msg.store_id,
|
|
288
|
-
file_date: msg.curr_date,
|
|
289
|
-
type: msg.audit_type || 'ReAudit',
|
|
290
|
-
queueName: inputData.queueName,
|
|
291
|
-
auditId: reauditInsert._id,
|
|
292
|
-
userId: reauditInsert.userId,
|
|
293
|
-
zoneName: msg.zone_id,
|
|
294
|
-
},
|
|
295
|
-
isDraft: reauditInsert.isDraft,
|
|
296
|
-
} );
|
|
297
|
-
}
|
|
298
|
-
// ////////////
|
|
299
|
-
|
|
300
|
-
const storeId = msg.store_id;
|
|
301
|
-
const fileDate = msg.curr_date;
|
|
302
|
-
const files = [];
|
|
303
|
-
let insertData = {};
|
|
304
|
-
fetchData.Bucket = bucket.auditInput; // need to change
|
|
305
|
-
fetchData.file_path = `${fileDate}/${storeId}/${msg.zone_id}/`;
|
|
306
|
-
if ( inputData.limit ) {
|
|
307
|
-
fetchData.MaxKeys = inputData.limit;
|
|
308
|
-
}
|
|
309
|
-
if ( inputData.nextId ) {
|
|
310
|
-
fetchData.ContinuationToken = decodeURIComponent( inputData.nextId );
|
|
311
|
-
}
|
|
312
|
-
const list = await listFileByPath( fetchData );
|
|
313
|
-
|
|
314
|
-
const folderPath = list.data;
|
|
315
|
-
const nextQuery = list.pageToken;
|
|
316
|
-
logger.info( { list: list } );
|
|
317
|
-
if ( folderPath?.length > 0 ) {
|
|
318
|
-
for ( let i = 0; i < folderPath.length; i++ ) {
|
|
319
|
-
const img = folderPath[i].Key.split( '/' );
|
|
320
|
-
const image = img[3].split( '.' );
|
|
321
|
-
const indexes = image[0].split( '_' );
|
|
322
|
-
fetchData.file_path = folderPath[i].Key;
|
|
323
|
-
|
|
324
|
-
const data = await signedUrl( fetchData );
|
|
325
|
-
const mapimg = {
|
|
326
|
-
img_path: data,
|
|
327
|
-
img_name: indexes[1],
|
|
328
|
-
img_id: image[0],
|
|
329
|
-
};
|
|
330
|
-
files.push( {
|
|
331
|
-
img_path: data,
|
|
332
|
-
img_name: indexes[1],
|
|
333
|
-
img_id: image[0],
|
|
334
|
-
selected: false,
|
|
335
|
-
dropped: false,
|
|
336
|
-
count: 1,
|
|
337
|
-
mappedid: [ mapimg ],
|
|
338
|
-
} );
|
|
339
|
-
}
|
|
340
|
-
if ( userDetails.length === 0 ) {
|
|
341
|
-
const [ day, month, year ] = msg.curr_date.split( '-' );
|
|
342
|
-
const temp = `${year}-${month}-${day}`;
|
|
343
|
-
let start = new Date( temp );
|
|
344
|
-
logger.info( { start: start, date: msg.curr_date } );
|
|
345
|
-
const userTimezoneOffset = start.getTimezoneOffset() * 60000;
|
|
346
|
-
start = new Date( start.getTime() - userTimezoneOffset );
|
|
347
|
-
start.setUTCHours( 0, 0, 0, 0 );
|
|
348
|
-
|
|
349
|
-
const record = {
|
|
350
|
-
userId: req.user._id,
|
|
351
|
-
storeId: msg.store_id,
|
|
352
|
-
clientId: msg.store_id.split( '-' )[0],
|
|
353
|
-
auditType: 'Audit',
|
|
354
|
-
fileDate: msg.curr_date,
|
|
355
|
-
queueName: inputData.queueName,
|
|
356
|
-
beforeCount: msg.total_count || files.length,
|
|
357
|
-
zoneAuditStatus: 'inprogress',
|
|
358
|
-
zoneName: msg.zone_id,
|
|
359
|
-
fileDateISO: start,
|
|
360
|
-
timeSpent: 0,
|
|
361
|
-
startTime: new Date(),
|
|
362
|
-
moduleType: 'zone',
|
|
363
|
-
};
|
|
364
|
-
|
|
365
|
-
const storeRecord = {
|
|
366
|
-
userId: req.user._id,
|
|
367
|
-
storeId: msg.store_id,
|
|
368
|
-
clientId: msg.store_id.split( '-' )[0],
|
|
369
|
-
auditType: 'Audit',
|
|
370
|
-
fileDate: msg.curr_date,
|
|
371
|
-
queueName: inputData.queueName,
|
|
372
|
-
beforeCount: msg.total_count || files.length,
|
|
373
|
-
status: 'inprogress',
|
|
374
|
-
fileDateISO: start,
|
|
375
|
-
timeSpent: 0,
|
|
376
|
-
zoneName: msg.zone_id,
|
|
377
|
-
|
|
378
|
-
};
|
|
379
|
-
insertData = await createZoneUserAudit( record );
|
|
380
|
-
await createStoreZoneAudit( storeRecord );
|
|
381
|
-
} else {
|
|
382
|
-
insertData = userDetails[0];
|
|
383
|
-
}
|
|
384
|
-
} else {
|
|
385
|
-
logger.error( {
|
|
386
|
-
error: { folderPath: folderPath, nextQuery: nextQuery },
|
|
387
|
-
function: 'getAuditFile',
|
|
388
|
-
message: 'Bucket image data not availale',
|
|
389
|
-
} );
|
|
390
|
-
return res.sendError( 'Bucket is Empty', 204 );
|
|
391
|
-
}
|
|
392
|
-
const userdata = await findOneUser( { _id: insertData.userId } );
|
|
393
|
-
const storeQuery = {
|
|
394
|
-
storeId: storeId,
|
|
395
|
-
};
|
|
396
|
-
const storeFields = {
|
|
397
|
-
storeName: 1,
|
|
398
|
-
address: '$storeProfile.address',
|
|
399
|
-
};
|
|
400
|
-
const storeInfo = await findOneStore( storeQuery, storeFields );
|
|
401
|
-
if ( inputData.nextId ) {
|
|
402
|
-
const logData = {
|
|
403
|
-
userId: insertData.userId,
|
|
404
|
-
userName: userdata.name,
|
|
405
|
-
logType: 'auditZone',
|
|
406
|
-
logSubType: 'auditZoneStart',
|
|
407
|
-
logData: {
|
|
408
|
-
fileDate: insertData.fileDate,
|
|
409
|
-
auditType: insertData.auditType,
|
|
410
|
-
storeId: storeId,
|
|
411
|
-
clientName: inputData.queueName,
|
|
412
|
-
auditId: insertData._id,
|
|
413
|
-
zoneName: msg.zone_id,
|
|
414
|
-
},
|
|
415
|
-
};
|
|
416
|
-
await insertOpenSearchData( openSearch.activityLog, logData );
|
|
417
|
-
}
|
|
418
|
-
logger.info( { storeInfo: storeInfo } );
|
|
419
|
-
|
|
420
|
-
return res.sendSuccess( {
|
|
421
|
-
result: files,
|
|
422
|
-
count: msg.total_count,
|
|
423
|
-
storeId: storeId,
|
|
424
|
-
storeName: storeInfo?.storeName,
|
|
425
|
-
address: storeInfo?.address,
|
|
426
|
-
zoneName: msg.zone_id,
|
|
427
|
-
file: {
|
|
428
|
-
queueName: inputData.queueName,
|
|
429
|
-
store_id: storeId,
|
|
430
|
-
file_date: fileDate,
|
|
431
|
-
type: 'Audit',
|
|
432
|
-
auditId: insertData._id,
|
|
433
|
-
userId: insertData.userId,
|
|
434
|
-
nextToken: nextQuery ? encodeURIComponent( nextQuery ) : null,
|
|
435
|
-
},
|
|
436
|
-
isDraft: insertData.isDraft,
|
|
437
|
-
} );
|
|
438
|
-
} catch ( error ) {
|
|
439
|
-
// {"store_id": "430-5", "curr_date": '08-08-2024', "total_count": 115, "zone_id": "HR"}
|
|
440
|
-
// {"store_id":"11-2043","audit_type":"ReAudit","curr_date":"07-08-2024","total_count":41,"before_count":88}
|
|
441
|
-
// {store_id: '430-5', curr_date: '08-08-2024', total_count: 115,zone_id:'HR'}
|
|
442
|
-
// {'store_id': "430-5", 'curr_date': "08-08-2024", 'total_count': 115,'zone_id':"HR"}
|
|
443
|
-
// {'store_id': '430-5', 'curr_date': '08-08-2024', 'total_count': 115,'zone_id':"HR"}
|
|
444
|
-
// {"store_id":"430-5","curr_date":"08-08-2024","total_count":115,"zone_id":"HR"}
|
|
445
|
-
logger.error( {
|
|
446
|
-
error: error,
|
|
447
|
-
message: req.query,
|
|
448
|
-
function: 'getZoneAuditFile',
|
|
449
|
-
} );
|
|
450
|
-
return res.sendError( error, 500 );
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
export async function saveDraft( req, res ) {
|
|
455
|
-
try {
|
|
456
|
-
const openSearch = JSON.parse( process.env.OPENSEARCH );
|
|
457
|
-
const inputData = req.body;
|
|
458
|
-
inputData.userId = req.user._id;
|
|
459
|
-
const getUserAuditData = await findOneZoneUserAudit( { _id: inputData.auditId } );
|
|
460
|
-
if ( !getUserAuditData ) {
|
|
461
|
-
return res.sendError( 'No Data Found', 204 );
|
|
462
|
-
}
|
|
463
|
-
if ( getUserAuditData.auditStatus == 'skipped' ) {
|
|
464
|
-
return res.sendError( 'File Assigned to Someone else', 203 );
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
const userQuery = {
|
|
468
|
-
_id: inputData.auditId,
|
|
469
|
-
};
|
|
470
|
-
let userRecord = {
|
|
471
|
-
isDraft: true,
|
|
472
|
-
auditStatus: 'drafted',
|
|
473
|
-
};
|
|
474
|
-
const storeQuery = {
|
|
475
|
-
storeId: inputData.storeId,
|
|
476
|
-
fileDate: inputData.fileDate,
|
|
477
|
-
};
|
|
478
|
-
|
|
479
|
-
let storeRecord = {
|
|
480
|
-
status: 'drafted',
|
|
481
|
-
};
|
|
482
|
-
|
|
483
|
-
if ( getUserAuditData?.startTime ) {
|
|
484
|
-
const isoDate = getUserAuditData.startTime;
|
|
485
|
-
const currentTime = dayjs();
|
|
486
|
-
const isoDateTime = dayjs( isoDate );
|
|
487
|
-
const timeDifferenceInMinutes = currentTime.diff( isoDateTime, 'seconds' );
|
|
488
|
-
|
|
489
|
-
userRecord.timeSpent = timeDifferenceInMinutes;
|
|
490
|
-
|
|
491
|
-
storeRecord.timeSpent = timeDifferenceInMinutes;
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
if ( inputData.userCommands ) {
|
|
495
|
-
userRecord.userCommands = inputData.userCommands;
|
|
496
|
-
const logData = {
|
|
497
|
-
userId: req.user._id,
|
|
498
|
-
userName: req.user.userName,
|
|
499
|
-
logType: 'zoneAudit',
|
|
500
|
-
logSubType: 'auditDraft',
|
|
501
|
-
logData: {
|
|
502
|
-
auditType: inputData.auditType,
|
|
503
|
-
storeId: inputData.storeId,
|
|
504
|
-
clientName: inputData.queueName,
|
|
505
|
-
beforeCount: inputData.totalCount,
|
|
506
|
-
afterCount: inputData.customerCount,
|
|
507
|
-
auditId: inputData.auditId,
|
|
508
|
-
},
|
|
509
|
-
};
|
|
510
|
-
await insertOpenSearchData( openSearch.activityLog, logData );
|
|
511
|
-
}
|
|
512
|
-
await createAuditLog( inputData );
|
|
513
|
-
await updateOneUserAudit( userQuery, userRecord );
|
|
514
|
-
await updateOneStoreZoneAudit( storeQuery, storeRecord );
|
|
515
|
-
return res.sendSuccess( {
|
|
516
|
-
result: 'The file has been drafted successfully',
|
|
517
|
-
} );
|
|
518
|
-
} catch ( error ) {
|
|
519
|
-
const err = error.message || 'Internal Server Error';
|
|
520
|
-
logger.error( { error: error, message: req.body, function: 'saveDraft' } );
|
|
521
|
-
return res.sendError( err, 500 );
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
export async function getDraftedData( req, res ) {
|
|
526
|
-
try {
|
|
527
|
-
const inputData = req.query;
|
|
528
|
-
const userId = inputData.userId || req.user._id;
|
|
529
|
-
const query = {
|
|
530
|
-
fileDate: inputData.fileDate,
|
|
531
|
-
storeId: inputData.storeId,
|
|
532
|
-
zoneName: inputData.zoneName,
|
|
533
|
-
moduleType: 'zone',
|
|
534
|
-
userId: userId,
|
|
535
|
-
};
|
|
536
|
-
const result = await findOneAuditLog( query, {}, { createdAt: -1 }, 1 );
|
|
537
|
-
|
|
538
|
-
if ( !result ) {
|
|
539
|
-
return res.sendError( 'No Data Found', 204 );
|
|
540
|
-
}
|
|
541
|
-
return res.sendSuccess( { result: result } );
|
|
542
|
-
} catch ( error ) {
|
|
543
|
-
const err = error.message || 'Internal Server Error';
|
|
544
|
-
logger.error( {
|
|
545
|
-
error: error,
|
|
546
|
-
message: req.query,
|
|
547
|
-
function: 'getDraftedData',
|
|
548
|
-
} );
|
|
549
|
-
return res.sendError( err, 500 );
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
export async function save( req, res ) {
|
|
554
|
-
try {
|
|
555
|
-
const bucket = JSON.parse( process.env.BUCKET );
|
|
556
|
-
const sqs = JSON.parse( process.env.SQS );
|
|
557
|
-
|
|
558
|
-
const inputData = req.body;
|
|
559
|
-
const params = {
|
|
560
|
-
Key: `${inputData.storeId}/${inputData.zoneName}/${inputData.fileDate}/${bucket.masterJsonFile}`,
|
|
561
|
-
};
|
|
562
|
-
|
|
563
|
-
if ( inputData.auditType === 'ReAudit' ) {
|
|
564
|
-
params.Bucket = bucket.auditUploadBucket;
|
|
565
|
-
} else {
|
|
566
|
-
params.Bucket = bucket.masterJson;
|
|
567
|
-
}
|
|
568
|
-
const fileData = await getJsonFileData( params );
|
|
569
|
-
|
|
570
|
-
if ( fileData?.statusCode ) {
|
|
571
|
-
logger.error( {
|
|
572
|
-
error: `ERROR IN UPLOAD PATH: ${inputData.storeId}/${inputData.zoneName}/${inputData.fileDate}/${bucket.masterJsonFile}`,
|
|
573
|
-
type: 'UPLOAD_ERROR',
|
|
574
|
-
} );
|
|
575
|
-
return res.sendError( fileData, 500 );
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
const masterJsonData = JSON.parse( fileData.toString( 'utf-8' ) );
|
|
579
|
-
|
|
580
|
-
const [ G20000, B20000, E0 ] = await Promise.all( [
|
|
581
|
-
splitG20000( masterJsonData.person_data ),
|
|
582
|
-
splitB20000( masterJsonData.person_data ),
|
|
583
|
-
splitE0( masterJsonData.person_data ),
|
|
584
|
-
] );
|
|
585
|
-
|
|
586
|
-
const [ mappingData ] = await Promise.all( [
|
|
587
|
-
mapEmployee( B20000, inputData.employee ),
|
|
588
|
-
mapJunk( B20000, inputData.junk ),
|
|
589
|
-
mapCustomer( B20000, inputData.customer ),
|
|
590
|
-
] );
|
|
591
|
-
|
|
592
|
-
const tempData = _.sortBy( [ ...mappingData, ...G20000, ...E0 ], 'index' );
|
|
593
|
-
|
|
594
|
-
const tempObject = { person_data: tempData };
|
|
595
|
-
|
|
596
|
-
const uploadDataParams = {
|
|
597
|
-
Bucket: bucket.auditUploadBucket,
|
|
598
|
-
Key: `${inputData.storeId}/${inputData.zoneName}/${inputData.fileDate}/`,
|
|
599
|
-
ContentType: 'application/json',
|
|
600
|
-
fileName: `${bucket.masterJsonFile}`,
|
|
601
|
-
body: JSON.stringify( tempObject ),
|
|
602
|
-
};
|
|
603
|
-
const mappingUpload = await fileUpload( uploadDataParams );
|
|
604
|
-
|
|
605
|
-
if ( mappingUpload?.statusCode ) {
|
|
606
|
-
logger.error( {
|
|
607
|
-
error: `ERROR IN UPLOAD PATH: ${inputData.storeId}/${inputData.zoneName}/${inputData.fileDate}/${bucket.masterJsonFile}`,
|
|
608
|
-
type: 'UPLOAD_ERROR',
|
|
609
|
-
} );
|
|
610
|
-
return res.sendError( mappingUpload, 500 );
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
let getUserAuditData = await findOneUserAudit( {
|
|
614
|
-
_id: inputData.auditId,
|
|
615
|
-
} );
|
|
616
|
-
const isoDate = getUserAuditData.startTime;
|
|
617
|
-
const currentTime = dayjs();
|
|
618
|
-
const isoDateTime = dayjs( isoDate );
|
|
619
|
-
const timeDifferenceInMinutes = currentTime.diff( isoDateTime, 'seconds' );
|
|
620
|
-
|
|
621
|
-
await updateOneUserAudit(
|
|
622
|
-
{ _id: inputData.auditId },
|
|
623
|
-
{
|
|
624
|
-
auditStatus: 'completed',
|
|
625
|
-
endTime: Date.now(),
|
|
626
|
-
isDraft: false,
|
|
627
|
-
afterCount: inputData.customerCount,
|
|
628
|
-
timeSpent: timeDifferenceInMinutes,
|
|
629
|
-
},
|
|
630
|
-
);
|
|
631
|
-
|
|
632
|
-
/** *
|
|
633
|
-
*
|
|
634
|
-
* logs need to be added
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
*/
|
|
638
|
-
|
|
639
|
-
let storeConfig = await findOneStore(
|
|
640
|
-
{
|
|
641
|
-
storeId: getUserAuditData.storeId,
|
|
642
|
-
},
|
|
643
|
-
{
|
|
644
|
-
auditConfigs: 1,
|
|
645
|
-
clientId: 1,
|
|
646
|
-
},
|
|
647
|
-
);
|
|
648
|
-
let storeAuditData = await findOneStoreZoneAudit( {
|
|
649
|
-
storeId: inputData.storeId,
|
|
650
|
-
zoneName: inputData.zoneName,
|
|
651
|
-
fileDate: inputData.fileDate,
|
|
652
|
-
} );
|
|
653
|
-
|
|
654
|
-
let clientData = await findClient( { clientId: storeConfig.clientId } );
|
|
655
|
-
|
|
656
|
-
if ( (
|
|
657
|
-
( storeAuditData.beforeCount - inputData.customerCount ) /
|
|
658
|
-
storeAuditData.beforeCount <
|
|
659
|
-
storeConfig.auditConfigs.ratio
|
|
660
|
-
) && inputData.auditType == 'Audit' ) {
|
|
661
|
-
logger.info(
|
|
662
|
-
`Hit in Reaudit pushing queue Store Id=${inputData.storeId},zone Name = ${inputData.zoneName}File Date=${inputData.fileDate}, /n \nQueue Name = ${clientData[0].auditConfigs.queueName}`,
|
|
663
|
-
'Reaudit',
|
|
664
|
-
);
|
|
665
|
-
|
|
666
|
-
const sqsProduceQueue = {
|
|
667
|
-
QueueUrl: `${sqs.url}${clientData[0].auditConfigs.queueName}`,
|
|
668
|
-
MessageBody: JSON.stringify( {
|
|
669
|
-
store_id: inputData.storeId,
|
|
670
|
-
zone_id: inputData.zoneName,
|
|
671
|
-
audit_type: 'ReAudit',
|
|
672
|
-
curr_date: inputData.fileDate,
|
|
673
|
-
total_count: inputData.customerCount,
|
|
674
|
-
before_count: storeAuditData.beforeCount,
|
|
675
|
-
} ),
|
|
676
|
-
};
|
|
677
|
-
const sqsQueue = await sendMessageToQueue( sqsProduceQueue.QueueUrl, sqsProduceQueue.MessageBody );
|
|
678
|
-
logger.info( { sqsQueue: sqsQueue } );
|
|
679
|
-
|
|
680
|
-
if ( sqsQueue.statusCode ) {
|
|
681
|
-
logger.error( {
|
|
682
|
-
error: `ERROR IN UPLOAD PATH: ${inputData.storeId}/${inputData.zoneName}/${inputData.fileDate}/${bucket.masterJsonFile}`,
|
|
683
|
-
type: 'UPLOAD_ERROR',
|
|
684
|
-
} );
|
|
685
|
-
return res.sendError( mappingUpload, 500 );
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
await updateOneStoreZoneAudit( { storeId: inputData.storeId, zoneName: inputData.zoneName },
|
|
689
|
-
{
|
|
690
|
-
status: 'not_assign',
|
|
691
|
-
afterCount: inputData.customerCount,
|
|
692
|
-
auditType: 'ReAudit',
|
|
693
|
-
timeSpent: timeDifferenceInMinutes,
|
|
694
|
-
} );
|
|
695
|
-
} else {
|
|
696
|
-
logger.info( 'Hit in Features', { data: ( _.omit( inputData, [ 'junk', 'employee', 'customer' ] ) ) } );
|
|
697
|
-
await updateOneStoreZoneAudit( { storeId: inputData.storeId, zoneName: inputData.zoneName },
|
|
698
|
-
{
|
|
699
|
-
status: 'completed',
|
|
700
|
-
afterCount: inputData.customerCount,
|
|
701
|
-
timeSpent: timeDifferenceInMinutes,
|
|
702
|
-
} );
|
|
703
|
-
|
|
704
|
-
const sqsProduceQueue = {
|
|
705
|
-
QueueUrl: `${sqs.url}${sqs.feature}`,
|
|
706
|
-
|
|
707
|
-
MessageBody: JSON.stringify( {
|
|
708
|
-
store_id: inputData.storeId,
|
|
709
|
-
zone_id: inputData.zoneName,
|
|
710
|
-
store_date: inputData.fileDate,
|
|
711
|
-
bucket_name: `${bucket.auditUploadBucket}`,
|
|
712
|
-
process_type: 'audit',
|
|
713
|
-
} ),
|
|
714
|
-
};
|
|
715
|
-
logger.info( { sqsProduceQueue: sqsProduceQueue } );
|
|
716
|
-
const sqsQueue = await sendMessageToQueue( sqsProduceQueue.QueueUrl, sqsProduceQueue.MessageBody );
|
|
717
|
-
logger.info( { sqsQueue: sqsQueue } );
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
return res.sendSuccess( { result: 'Updated Successfully' } );
|
|
721
|
-
} catch ( error ) {
|
|
722
|
-
const err = error.message || 'Internal Server Error';
|
|
723
|
-
logger.error( { error: error, message: req.body, function: 'save' } );
|
|
724
|
-
return res.sendError( err, 500 );
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
export async function workSpace( req, res ) {
|
|
729
|
-
try {
|
|
730
|
-
const inputData = req.query;
|
|
731
|
-
const limit = inputData.limit || 10;
|
|
732
|
-
const offset = inputData.offset ?
|
|
733
|
-
( inputData.offset - 1 ) * inputData.limit : 0;
|
|
734
|
-
const dateRange = await getUTC( new Date(), new Date() );
|
|
735
|
-
|
|
736
|
-
const temp = [];
|
|
737
|
-
const clientQuery = [
|
|
738
|
-
{
|
|
739
|
-
$match: {
|
|
740
|
-
$and: [
|
|
741
|
-
{ status: { $in: [ 'active', 'hold' ] } },
|
|
742
|
-
{ 'auditConfigs.audit': { $eq: true } }, // NOTE: modified the code need to check for enable zone
|
|
743
|
-
],
|
|
744
|
-
},
|
|
745
|
-
},
|
|
746
|
-
{
|
|
747
|
-
$project: {
|
|
748
|
-
clientName: '$clientName',
|
|
749
|
-
clientId: '$clientId',
|
|
750
|
-
},
|
|
751
|
-
},
|
|
752
|
-
];
|
|
753
|
-
|
|
754
|
-
if ( inputData.searchValue && inputData.searchValue !== '' ) {
|
|
755
|
-
clientQuery.push( {
|
|
756
|
-
$match: {
|
|
757
|
-
$or: [
|
|
758
|
-
{ clientName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
759
|
-
{ clientId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
760
|
-
],
|
|
761
|
-
},
|
|
762
|
-
} );
|
|
763
|
-
}
|
|
764
|
-
const count = await aggregateClient( clientQuery );
|
|
765
|
-
logger.info( { count: count } );
|
|
766
|
-
if ( count?.length == 0 ) {
|
|
767
|
-
return res.sendError( 'No Data Found', 204 );
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
if ( inputData.limit && !inputData.isExport ) {
|
|
771
|
-
clientQuery.push( { $skip: offset }, { $limit: limit } );
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
const clientDetails = await aggregateClient( clientQuery );
|
|
775
|
-
const clientList = clientDetails.map( ( i ) => i.clientId );
|
|
776
|
-
// const query = [
|
|
777
|
-
// {
|
|
778
|
-
// $match: {
|
|
779
|
-
// $and: [
|
|
780
|
-
// {
|
|
781
|
-
// clientId: { $in: clientList },
|
|
782
|
-
// },
|
|
783
|
-
// {
|
|
784
|
-
// moduleType: 'zone',
|
|
785
|
-
// },
|
|
786
|
-
// ],
|
|
787
|
-
|
|
788
|
-
// },
|
|
789
|
-
// },
|
|
790
|
-
// {
|
|
791
|
-
// $group: {
|
|
792
|
-
// _id: '$clientId',
|
|
793
|
-
// clientId: { $last: '$clientId' },
|
|
794
|
-
// totalFilesCount: { $last: '$totalFilesCount' },
|
|
795
|
-
// clientName: { $last: '$clientName' },
|
|
796
|
-
// queueName: { $last: '$queueName' },
|
|
797
|
-
// },
|
|
798
|
-
// },
|
|
799
|
-
// {
|
|
800
|
-
// $project: {
|
|
801
|
-
// clientId: 1,
|
|
802
|
-
// totalFilesCount: 1,
|
|
803
|
-
// clientName: 1,
|
|
804
|
-
// queueName: 1,
|
|
805
|
-
// },
|
|
806
|
-
// },
|
|
807
|
-
// ];
|
|
808
|
-
|
|
809
|
-
const fileDate = dayjs( dateRange.start ).subtract( 1, 'days' );
|
|
810
|
-
|
|
811
|
-
const completedFiles = [
|
|
812
|
-
{
|
|
813
|
-
$match: {
|
|
814
|
-
$and: [
|
|
815
|
-
{ clientId: { $in: clientList } },
|
|
816
|
-
{ fileDateISO: { $eq: new Date( fileDate ) } },
|
|
817
|
-
],
|
|
818
|
-
},
|
|
819
|
-
},
|
|
820
|
-
{
|
|
821
|
-
$project: {
|
|
822
|
-
clientId: 1,
|
|
823
|
-
completed: {
|
|
824
|
-
$cond: [ { $eq: [ '$status', 'completed' ] }, 1, 0 ],
|
|
825
|
-
},
|
|
826
|
-
},
|
|
827
|
-
},
|
|
828
|
-
{
|
|
829
|
-
$group: {
|
|
830
|
-
_id: { clientId: '$clientId', storeId: '$storeId' },
|
|
831
|
-
clientId: { $first: '$clientId' },
|
|
832
|
-
completedCount: { $sum: '$completed' },
|
|
833
|
-
},
|
|
834
|
-
},
|
|
835
|
-
{
|
|
836
|
-
$group: {
|
|
837
|
-
_id: '$clientId',
|
|
838
|
-
clientId: { $first: '$clientId' },
|
|
839
|
-
completedCount: { $first: '$completedCount' },
|
|
840
|
-
},
|
|
841
|
-
},
|
|
842
|
-
];
|
|
843
|
-
|
|
844
|
-
const auditFiles = [
|
|
845
|
-
{
|
|
846
|
-
$match: {
|
|
847
|
-
$and: [
|
|
848
|
-
{ clientId: { $in: clientList } },
|
|
849
|
-
{ moduleType: { $eq: 'zone' } },
|
|
850
|
-
{ createdAt: { $gte: dateRange.start } },
|
|
851
|
-
{ createdAt: { $lte: dateRange.end } },
|
|
852
|
-
],
|
|
853
|
-
},
|
|
854
|
-
},
|
|
855
|
-
{
|
|
856
|
-
$project: {
|
|
857
|
-
clientId: 1,
|
|
858
|
-
pending: {
|
|
859
|
-
$cond: [
|
|
860
|
-
{ $or: [ { $in: [ '$auditStatus', [ 'inprogress', 'drafted' ] ] } ] },
|
|
861
|
-
1,
|
|
862
|
-
0,
|
|
863
|
-
],
|
|
864
|
-
},
|
|
865
|
-
},
|
|
866
|
-
},
|
|
867
|
-
{
|
|
868
|
-
$group: {
|
|
869
|
-
_id: { clientId: '$clientId', storeId: '$storeId' },
|
|
870
|
-
clientId: { $first: '$clientId' },
|
|
871
|
-
pendingCount: { $sum: '$pending' },
|
|
872
|
-
totalCount: { $sum: 1 },
|
|
873
|
-
},
|
|
874
|
-
},
|
|
875
|
-
{
|
|
876
|
-
$group: {
|
|
877
|
-
_id: '$clientId',
|
|
878
|
-
clientId: { $first: '$clientId' },
|
|
879
|
-
pendingCount: { $first: { $ifNull: [ '$pendingCount', 0 ] } },
|
|
880
|
-
totalCount: { $first: '$totalCount' },
|
|
881
|
-
},
|
|
882
|
-
},
|
|
883
|
-
];
|
|
884
|
-
|
|
885
|
-
const draftedFiles = [
|
|
886
|
-
{
|
|
887
|
-
$match: {
|
|
888
|
-
$and: [
|
|
889
|
-
{ clientId: { $in: clientList } },
|
|
890
|
-
{ moduleType: { $eq: 'zone' } },
|
|
891
|
-
{ userId: { $eq: inputData.userId } },
|
|
892
|
-
{ createdAt: { $gte: dateRange.start } },
|
|
893
|
-
{ createdAt: { $lte: dateRange.end } },
|
|
894
|
-
],
|
|
895
|
-
},
|
|
896
|
-
},
|
|
897
|
-
{
|
|
898
|
-
$sort: {
|
|
899
|
-
updatedAt: -1,
|
|
900
|
-
},
|
|
901
|
-
},
|
|
902
|
-
{
|
|
903
|
-
$project: {
|
|
904
|
-
clientId: 1,
|
|
905
|
-
isDraft: 1,
|
|
906
|
-
draft: {
|
|
907
|
-
$cond: [ { $eq: [ '$auditStatus', 'drafted' ] }, 1, 0 ],
|
|
908
|
-
},
|
|
909
|
-
inprogress: {
|
|
910
|
-
$cond: [ { $eq: [ '$auditStatus', 'inprogress' ] }, 1, 0 ],
|
|
911
|
-
},
|
|
912
|
-
},
|
|
913
|
-
},
|
|
914
|
-
{
|
|
915
|
-
$group: {
|
|
916
|
-
_id: '$clientId',
|
|
917
|
-
clientId: { $first: '$clientId' },
|
|
918
|
-
isDrafted: { $first: '$isDraft' },
|
|
919
|
-
draftCount: { $sum: '$draft' },
|
|
920
|
-
inprogressCount: { $sum: '$inprogress' },
|
|
921
|
-
},
|
|
922
|
-
},
|
|
923
|
-
];
|
|
924
|
-
|
|
925
|
-
const userAsign = [
|
|
926
|
-
{
|
|
927
|
-
$match: {
|
|
928
|
-
$and: [
|
|
929
|
-
{ userId: { $eq: req.user._id } },
|
|
930
|
-
{ isCompleted: false },
|
|
931
|
-
{ clientId: { $in: clientList } },
|
|
932
|
-
{ moduleType: { $eq: 'zone' } } ],
|
|
933
|
-
},
|
|
934
|
-
},
|
|
935
|
-
{
|
|
936
|
-
$group: {
|
|
937
|
-
_id: '$clientId',
|
|
938
|
-
clientId: { $first: '$clientId' },
|
|
939
|
-
asignedCount: { $sum: 1 },
|
|
940
|
-
},
|
|
941
|
-
},
|
|
942
|
-
{
|
|
943
|
-
$project: {
|
|
944
|
-
_id: 0,
|
|
945
|
-
clientId: 1,
|
|
946
|
-
asignedCount: 1,
|
|
947
|
-
},
|
|
948
|
-
},
|
|
949
|
-
];
|
|
950
|
-
|
|
951
|
-
const clientAssign = [
|
|
952
|
-
{
|
|
953
|
-
$match: {
|
|
954
|
-
$and: [
|
|
955
|
-
{ isCompleted: false },
|
|
956
|
-
{ moduleType: { $eq: 'zone' } },
|
|
957
|
-
{ clientId: { $in: clientList } },
|
|
958
|
-
],
|
|
959
|
-
},
|
|
960
|
-
},
|
|
961
|
-
{
|
|
962
|
-
$group: {
|
|
963
|
-
_id: '$clientId',
|
|
964
|
-
clientId: { $first: '$clientId' },
|
|
965
|
-
ClientAsignedCount: { $sum: 1 },
|
|
966
|
-
},
|
|
967
|
-
},
|
|
968
|
-
{
|
|
969
|
-
$project: {
|
|
970
|
-
_id: 0,
|
|
971
|
-
clientId: 1,
|
|
972
|
-
ClientAsignedCount: { $ifNull: [ '$ClientAsignedCount', 0 ] },
|
|
973
|
-
},
|
|
974
|
-
},
|
|
975
|
-
];
|
|
976
|
-
|
|
977
|
-
const auditDetails = [];// await aggregateAuditClientData( query );
|
|
978
|
-
const auditCount = await aggregateUserAudit( auditFiles );
|
|
979
|
-
const draftedCount = await aggregateUserAudit( draftedFiles );
|
|
980
|
-
const CompletedCount = await aggregateStoreZoneAudit( completedFiles );
|
|
981
|
-
|
|
982
|
-
const clientAssignresult = await aggregateAssignAudit( clientAssign );
|
|
983
|
-
const userAsignCount = await aggregateAssignAudit( userAsign );
|
|
984
|
-
const mergeAll = _.merge(
|
|
985
|
-
_.keyBy( clientDetails, 'clientId' ),
|
|
986
|
-
_.keyBy( auditCount, 'clientId' ),
|
|
987
|
-
_.keyBy( draftedCount, 'clientId' ),
|
|
988
|
-
_.keyBy( clientAssignresult, 'clientId' ),
|
|
989
|
-
);
|
|
990
|
-
const finalResult = _.values( mergeAll );
|
|
991
|
-
const merge = _.values(
|
|
992
|
-
_.merge(
|
|
993
|
-
_.keyBy( finalResult, 'clientId' ),
|
|
994
|
-
_.keyBy( auditDetails, 'clientId' ),
|
|
995
|
-
_.keyBy( userAsignCount, 'clientId' ),
|
|
996
|
-
_.keyBy( CompletedCount, 'clientId' ),
|
|
997
|
-
),
|
|
998
|
-
);
|
|
999
|
-
logger.info( { merge: merge } );
|
|
1000
|
-
let totalStores = 0;
|
|
1001
|
-
for ( let i = 0; i < merge.length; i++ ) {
|
|
1002
|
-
totalStores = totalStores + merge[i].totalFilesCount;
|
|
1003
|
-
|
|
1004
|
-
const pending = await listQueue( merge[i].queueName );
|
|
1005
|
-
|
|
1006
|
-
if ( !pending.statusCode ) {
|
|
1007
|
-
temp.push( {
|
|
1008
|
-
clientId: merge[i].clientId,
|
|
1009
|
-
clientName: merge[i].clientName,
|
|
1010
|
-
completedCount: merge[i].completedCount ?
|
|
1011
|
-
Number( merge[i].completedCount ) :
|
|
1012
|
-
0,
|
|
1013
|
-
pendingByQueue: Number( pending ),
|
|
1014
|
-
pendingByUser:
|
|
1015
|
-
merge[i].pendingCount >= 0 ?
|
|
1016
|
-
Number( merge[i].pendingCount ) +
|
|
1017
|
-
( merge[i].ClientAsignedCount ?
|
|
1018
|
-
Number( merge[i].ClientAsignedCount ) :
|
|
1019
|
-
0 ) :
|
|
1020
|
-
0,
|
|
1021
|
-
totalCount: merge[i].totalFilesCount,
|
|
1022
|
-
queueName: merge[i].queueName,
|
|
1023
|
-
isDraft: merge[i].isDrafted ? merge[i].isDrafted : false,
|
|
1024
|
-
isEnable:
|
|
1025
|
-
Number( pending ) > 0 ||
|
|
1026
|
-
merge[i].isDrafted ||
|
|
1027
|
-
( merge[i].asignedCount && merge[i].asignedCount > 0 ) ||
|
|
1028
|
-
( merge[i].inprogressCount && merge[i].inprogressCount > 0 ) ?
|
|
1029
|
-
true :
|
|
1030
|
-
false,
|
|
1031
|
-
completedRatio: merge[i].totalFilesCount ?
|
|
1032
|
-
merge[i].completedCount ?
|
|
1033
|
-
Math.round(
|
|
1034
|
-
( Number( merge[i].completedCount ) /
|
|
1035
|
-
Number( merge[i].totalFilesCount ) ) *
|
|
1036
|
-
100,
|
|
1037
|
-
) :
|
|
1038
|
-
0 :
|
|
1039
|
-
0,
|
|
1040
|
-
isAssigned:
|
|
1041
|
-
merge[i].asignedCount && merge[i].asignedCount > 0 ? true : false,
|
|
1042
|
-
Assignedcount:
|
|
1043
|
-
merge[i].asignedCount && merge[i].asignedCount > 0 ?
|
|
1044
|
-
merge[i].asignedCount :
|
|
1045
|
-
0,
|
|
1046
|
-
} );
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
if ( inputData.isExport ) {
|
|
1051
|
-
const exportdata = [];
|
|
1052
|
-
temp.forEach( ( element ) => {
|
|
1053
|
-
exportdata.push( {
|
|
1054
|
-
'Client Name': element.clientName,
|
|
1055
|
-
'Client Id': element.clientId,
|
|
1056
|
-
'Completed Percentage': element.completedRatio + ' %',
|
|
1057
|
-
'Pending Queue': element.pendingByQueue,
|
|
1058
|
-
'Pending User': element.pendingByUser,
|
|
1059
|
-
'Completed': element.completedCount,
|
|
1060
|
-
'Draft': element.isDraft ? 1 : 0,
|
|
1061
|
-
'Assigned': element.Assignedcount,
|
|
1062
|
-
} );
|
|
1063
|
-
} );
|
|
1064
|
-
await download( exportdata, res );
|
|
1065
|
-
return;
|
|
1066
|
-
} else {
|
|
1067
|
-
return res.sendSuccess( {
|
|
1068
|
-
result: temp,
|
|
1069
|
-
count: count.length,
|
|
1070
|
-
totalStores: totalStores,
|
|
1071
|
-
} );
|
|
1072
|
-
}
|
|
1073
|
-
} catch ( error ) {
|
|
1074
|
-
const err = error.message || 'Internal Server Error';
|
|
1075
|
-
logger.info( { error: error, message: req.query, function: 'workSpace' } );
|
|
1076
|
-
return res.sendError( err, 500 );
|
|
1077
|
-
}
|
|
1078
|
-
}
|
|
1079
|
-
|
|
1080
|
-
export async function storeMetrics( req, res ) {
|
|
1081
|
-
try {
|
|
1082
|
-
const inputData = req.body;
|
|
1083
|
-
const limit = inputData.limit || 10;
|
|
1084
|
-
const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
|
|
1085
|
-
const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
|
|
1086
|
-
let filter = [
|
|
1087
|
-
{ fileDateISO: { $gte: dateRange.start } },
|
|
1088
|
-
{ fileDateISO: { $lte: dateRange.end } },
|
|
1089
|
-
];
|
|
1090
|
-
if ( inputData.filterByClientId && inputData.filterByClientId?.length > 0 ) {
|
|
1091
|
-
filter.push( { clientId: { $in: inputData.filterByClientId } } );
|
|
1092
|
-
}
|
|
1093
|
-
|
|
1094
|
-
if ( inputData.filterByStoreId && inputData.filterByStoreId?.length > 0 ) {
|
|
1095
|
-
filter.push( { storeId: { $in: inputData.filterByStoreId } } );
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
if ( inputData.filterByStatus && inputData.filterByStatus?.length > 0 ) {
|
|
1099
|
-
filter.push( { status: { $in: inputData.filterByStatus } } );
|
|
1100
|
-
}
|
|
1101
|
-
|
|
1102
|
-
if ( inputData.filterByType && inputData.filterByType?.length > 0 ) {
|
|
1103
|
-
filter.push( { auditType: { $in: inputData.filterByType } } );
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
|
-
const query = [
|
|
1107
|
-
{
|
|
1108
|
-
$match: {
|
|
1109
|
-
$and: filter,
|
|
1110
|
-
},
|
|
1111
|
-
},
|
|
1112
|
-
{
|
|
1113
|
-
$lookup: {
|
|
1114
|
-
from: 'stores',
|
|
1115
|
-
let: { storeId: '$storeId' },
|
|
1116
|
-
pipeline: [
|
|
1117
|
-
{
|
|
1118
|
-
$match: {
|
|
1119
|
-
$expr: {
|
|
1120
|
-
$eq: [ '$storeId', '$$storeId' ],
|
|
1121
|
-
},
|
|
1122
|
-
},
|
|
1123
|
-
},
|
|
1124
|
-
{
|
|
1125
|
-
$project: {
|
|
1126
|
-
storeName: 1,
|
|
1127
|
-
},
|
|
1128
|
-
},
|
|
1129
|
-
], as: 'store',
|
|
1130
|
-
},
|
|
1131
|
-
},
|
|
1132
|
-
{
|
|
1133
|
-
$unwind: {
|
|
1134
|
-
path: '$store', preserveNullAndEmptyArrays: true,
|
|
1135
|
-
},
|
|
1136
|
-
},
|
|
1137
|
-
{
|
|
1138
|
-
$project: {
|
|
1139
|
-
_id: 0,
|
|
1140
|
-
fileDate: 1,
|
|
1141
|
-
storeId: 1,
|
|
1142
|
-
storeName: '$store.storeName',
|
|
1143
|
-
clientName: '',
|
|
1144
|
-
clientId: 1,
|
|
1145
|
-
zoneName: 1,
|
|
1146
|
-
userId: {
|
|
1147
|
-
$arrayElemAt: [ '$userId', { $subtract: [ { $size: '$userId' }, 1 ] } ],
|
|
1148
|
-
},
|
|
1149
|
-
clientId: 1,
|
|
1150
|
-
auditType: 1,
|
|
1151
|
-
beforeCount: 1,
|
|
1152
|
-
afterCount: { $ifNull: [ '$afterCount', null ] },
|
|
1153
|
-
accuracy: { $round: [
|
|
1154
|
-
{ $divide: [
|
|
1155
|
-
{ $multiply: [ { $ifNull: [ '$afterCount', 0 ] }, '$beforeCount' ] }, 100,
|
|
1156
|
-
] }, 2,
|
|
1157
|
-
],
|
|
1158
|
-
|
|
1159
|
-
},
|
|
1160
|
-
timeSpent: 1,
|
|
1161
|
-
status: 1,
|
|
1162
|
-
},
|
|
1163
|
-
},
|
|
1164
|
-
{
|
|
1165
|
-
$lookup: {
|
|
1166
|
-
from: 'users',
|
|
1167
|
-
let: { userId: '$userId' },
|
|
1168
|
-
pipeline: [
|
|
1169
|
-
{
|
|
1170
|
-
$match: {
|
|
1171
|
-
$expr: {
|
|
1172
|
-
$eq: [ '$_id', '$$userId' ],
|
|
1173
|
-
},
|
|
1174
|
-
},
|
|
1175
|
-
},
|
|
1176
|
-
{
|
|
1177
|
-
$project: {
|
|
1178
|
-
_id: 0,
|
|
1179
|
-
userName: 1,
|
|
1180
|
-
userEmail: '$email',
|
|
1181
|
-
},
|
|
1182
|
-
},
|
|
1183
|
-
], as: 'users',
|
|
1184
|
-
},
|
|
1185
|
-
},
|
|
1186
|
-
{
|
|
1187
|
-
$unwind: {
|
|
1188
|
-
path: '$users',
|
|
1189
|
-
preserveNullAndEmptyArrays: true,
|
|
1190
|
-
},
|
|
1191
|
-
},
|
|
1192
|
-
{
|
|
1193
|
-
$lookup: {
|
|
1194
|
-
from: 'clients',
|
|
1195
|
-
let: { clientId: '$clientId' },
|
|
1196
|
-
pipeline: [
|
|
1197
|
-
{
|
|
1198
|
-
$match: {
|
|
1199
|
-
$expr: {
|
|
1200
|
-
$eq: [ '$clientId', '$$clientId' ],
|
|
1201
|
-
},
|
|
1202
|
-
},
|
|
1203
|
-
},
|
|
1204
|
-
{
|
|
1205
|
-
$project: {
|
|
1206
|
-
_id: 0,
|
|
1207
|
-
clientName: 1,
|
|
1208
|
-
},
|
|
1209
|
-
},
|
|
1210
|
-
], as: 'client',
|
|
1211
|
-
},
|
|
1212
|
-
},
|
|
1213
|
-
{
|
|
1214
|
-
$unwind: {
|
|
1215
|
-
path: '$client',
|
|
1216
|
-
preserveNullAndEmptyArrays: true,
|
|
1217
|
-
},
|
|
1218
|
-
},
|
|
1219
|
-
{
|
|
1220
|
-
$project: {
|
|
1221
|
-
_id: 0,
|
|
1222
|
-
fileDate: 1,
|
|
1223
|
-
storeId: 1,
|
|
1224
|
-
zoneName: 1,
|
|
1225
|
-
storeName: '$stores.storeName',
|
|
1226
|
-
userName: '$users.userName',
|
|
1227
|
-
userEmail: '$users.userEmail',
|
|
1228
|
-
clientId: 1,
|
|
1229
|
-
clientName: '$client.clientName',
|
|
1230
|
-
auditType: 1,
|
|
1231
|
-
beforeCount: 1,
|
|
1232
|
-
afterCount: { $ifNull: [ '$afterCount', null ] },
|
|
1233
|
-
accuracy: { $round: [
|
|
1234
|
-
{ $divide: [
|
|
1235
|
-
{ $multiply: [ { $ifNull: [ '$afterCount', 0 ] }, '$beforeCount' ] }, 100,
|
|
1236
|
-
] }, 1,
|
|
1237
|
-
],
|
|
1238
|
-
|
|
1239
|
-
},
|
|
1240
|
-
timeSpent: {
|
|
1241
|
-
|
|
1242
|
-
$cond: [
|
|
1243
|
-
{ $lt: [ '$timeSpent', 60 ] }, // Case 1: If less than 60 seconds
|
|
1244
|
-
{ $concat: [ { $toString: '$timeSpent' }, ' sec' ] },
|
|
1245
|
-
{
|
|
1246
|
-
$cond: [
|
|
1247
|
-
{ $lt: [ '$timeSpent', 3600 ] }, // Case 2: If less than 60 minutes (3600 seconds)
|
|
1248
|
-
{
|
|
1249
|
-
$concat: [
|
|
1250
|
-
{ $toString: { $round: [ { $divide: [ '$timeSpent', 60 ] }, 1 ] } }, // Convert to minutes
|
|
1251
|
-
' min',
|
|
1252
|
-
],
|
|
1253
|
-
},
|
|
1254
|
-
{
|
|
1255
|
-
$concat: [
|
|
1256
|
-
{ $toString: { $round: [ { $divide: [ '$timeSpent', 3600 ] }, 1 ] } }, // Convert to hours
|
|
1257
|
-
' hr',
|
|
1258
|
-
],
|
|
1259
|
-
},
|
|
1260
|
-
],
|
|
1261
|
-
},
|
|
1262
|
-
],
|
|
1263
|
-
|
|
1264
|
-
},
|
|
1265
|
-
status: 1,
|
|
1266
|
-
},
|
|
1267
|
-
},
|
|
1268
|
-
|
|
1269
|
-
];
|
|
1270
|
-
|
|
1271
|
-
if ( inputData.searchValue && inputData.searchValue !== '' ) {
|
|
1272
|
-
query.push( {
|
|
1273
|
-
$match: {
|
|
1274
|
-
$or: [
|
|
1275
|
-
{ clientId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
1276
|
-
{ clientName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
1277
|
-
{ storeId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
1278
|
-
{ storeName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
1279
|
-
{ userName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
1280
|
-
{ status: { $regex: inputData.searchValue, $options: 'i' } },
|
|
1281
|
-
{ auditType: { $regex: inputData.searchValue, $options: 'i' } },
|
|
1282
|
-
{ zoneName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
1283
|
-
|
|
1284
|
-
],
|
|
1285
|
-
},
|
|
1286
|
-
} );
|
|
1287
|
-
}
|
|
1288
|
-
|
|
1289
|
-
if ( inputData.sortColumnName ) {
|
|
1290
|
-
const sortBy = inputData.sortBy || -1;
|
|
1291
|
-
query.push( {
|
|
1292
|
-
$sort: { [inputData.sortColumName]: sortBy },
|
|
1293
|
-
},
|
|
1294
|
-
);
|
|
1295
|
-
}
|
|
1296
|
-
|
|
1297
|
-
const count = await aggregateStoreZoneAudit( query );
|
|
1298
|
-
|
|
1299
|
-
if ( count.length == 0 ) {
|
|
1300
|
-
return res.sendError( 'No data Found', 204 );
|
|
1301
|
-
}
|
|
1302
|
-
if ( !inputData.isExport ) {
|
|
1303
|
-
query.push( {
|
|
1304
|
-
$skip: offset,
|
|
1305
|
-
},
|
|
1306
|
-
{
|
|
1307
|
-
$limit: limit,
|
|
1308
|
-
} );
|
|
1309
|
-
} else {
|
|
1310
|
-
query.push( { $limit: 10000 } );
|
|
1311
|
-
}
|
|
1312
|
-
const result = await aggregateStoreZoneAudit( query );
|
|
1313
|
-
if ( inputData.isExport ) {
|
|
1314
|
-
const exportdata = [];
|
|
1315
|
-
result.forEach( ( element ) => {
|
|
1316
|
-
exportdata.push( {
|
|
1317
|
-
'File Date': element.fileDate,
|
|
1318
|
-
'Store Id': element.storeId,
|
|
1319
|
-
'Store Name': element.storeName,
|
|
1320
|
-
'Client Id': element.clientId,
|
|
1321
|
-
'Client Name': element.clientName,
|
|
1322
|
-
'Zone Name': element.clientName,
|
|
1323
|
-
'User Name': element.userName,
|
|
1324
|
-
'User Email': element.userEmail,
|
|
1325
|
-
'Audit Type': element.auditType,
|
|
1326
|
-
'Accuracy': element.accuracy,
|
|
1327
|
-
'Time Spent': element.timeSpent,
|
|
1328
|
-
'Status': element.status,
|
|
1329
|
-
} );
|
|
1330
|
-
} );
|
|
1331
|
-
await download( exportdata, res );
|
|
1332
|
-
return;
|
|
1333
|
-
}
|
|
1334
|
-
return res.sendSuccess( { result: result, count: count.length } );
|
|
1335
|
-
} catch ( error ) {
|
|
1336
|
-
const err = error.message || 'Internal Server Error';
|
|
1337
|
-
logger.error( { error: error, data: req.body, function: 'storeMetrics' } );
|
|
1338
|
-
return res.sendError( err, 500 );
|
|
1339
|
-
}
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
export async function zoneAuditImages( req, res ) {
|
|
1343
|
-
try {
|
|
1344
|
-
const inputData = req.query;
|
|
1345
|
-
let auditImageDownload = '';
|
|
1346
|
-
switch ( inputData.imageType ) {
|
|
1347
|
-
case 'BC':
|
|
1348
|
-
const auditImageBC = await getZoneAuditImageData( inputData );
|
|
1349
|
-
if ( auditImageBC?.errorCode ) {
|
|
1350
|
-
const error = auditImageBC?.errorMsg;
|
|
1351
|
-
const code = auditImageBC?.errorCode;
|
|
1352
|
-
return res.sendError( { error: error }, code );
|
|
1353
|
-
}
|
|
1354
|
-
|
|
1355
|
-
if ( inputData.export==true ) {
|
|
1356
|
-
auditImageDownload= await zipDownloadImage( auditImageBC );
|
|
1357
|
-
// Set response headers for the zip file
|
|
1358
|
-
res.set( 'Content-Type', 'application/zip' );
|
|
1359
|
-
res.set( 'Content-Disposition', `attachment; filename=${inputData.storeId}_${inputData.fileDate}_${inputData.imageType}_${inputData.zoneName}.zip` );
|
|
1360
|
-
|
|
1361
|
-
// Send the zip file as the API response
|
|
1362
|
-
return res.send( auditImageDownload );
|
|
1363
|
-
}
|
|
1364
|
-
res.sendSuccess( { result: auditImageBC } );
|
|
1365
|
-
|
|
1366
|
-
break;
|
|
1367
|
-
|
|
1368
|
-
case 'AC':
|
|
1369
|
-
|
|
1370
|
-
const [ filterData, auditImageAC ] = await Promise.all( [ getZoneAuditFilterData( inputData ), getZoneAuditImageData( inputData ) ] );
|
|
1371
|
-
|
|
1372
|
-
if ( filterData?.errorCode || auditImageAC?.errorCode ) {
|
|
1373
|
-
const error = filterData?.errorMsg || auditImageAC.errorMsg;
|
|
1374
|
-
const code = filterData?.errorCode || auditImageAC.errorCode;
|
|
1375
|
-
return res.sendError( { error: error }, code );
|
|
1376
|
-
}
|
|
1377
|
-
|
|
1378
|
-
const reauditImg = await getReauditImg( filterData, auditImageAC );
|
|
1379
|
-
|
|
1380
|
-
logger.info( { filterData: filterData } );
|
|
1381
|
-
logger.info( { auditImageAC: auditImageAC } );
|
|
1382
|
-
logger.info( { reauditImg: reauditImg } );
|
|
1383
|
-
if ( inputData.export==true ) {
|
|
1384
|
-
auditImageDownload= await zipDownloadImage( reauditImg );
|
|
1385
|
-
// Set response headers for the zip file
|
|
1386
|
-
res.set( 'Content-Type', 'application/zip' );
|
|
1387
|
-
res.set( 'Content-Disposition', `attachment; filename=${inputData.storeId}_${inputData.fileDate}_${inputData.imageType}.zip` );
|
|
1388
|
-
|
|
1389
|
-
// Send the zip file as the API response
|
|
1390
|
-
return res.send( auditImageDownload );
|
|
1391
|
-
}
|
|
1392
|
-
res.sendSuccess( { result: reauditImg } );
|
|
1393
|
-
break;
|
|
1394
|
-
default:
|
|
1395
|
-
res.sendError( 'No Data Found', 204 );
|
|
1396
|
-
break;
|
|
1397
|
-
}
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
// return res.sendSuccess( { result: inputData } );
|
|
1401
|
-
} catch ( error ) {
|
|
1402
|
-
const err = error.message || 'Internal Server Error';
|
|
1403
|
-
logger.info( { error: error, message: req.body, function: 'beforeCountImages' } );
|
|
1404
|
-
return res.sendError( err, 500 );
|
|
1405
|
-
}
|
|
1406
|
-
}
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
export async function getZoneFilterData( msg ) {
|
|
1410
|
-
try {
|
|
1411
|
-
const bucket = JSON.parse( process.env.BUCKET );
|
|
1412
|
-
const params = {
|
|
1413
|
-
Bucket: `${bucket.auditOutput}`,
|
|
1414
|
-
Key: `${msg.store_id}/${inputData.zoneName}/${msg.curr_date}/${bucketName.masterJsonFile}`,
|
|
1415
|
-
};
|
|
1416
|
-
const mappingFile = await getJsonFileData( params );
|
|
1417
|
-
if ( mappingFile.statusCode ) {
|
|
1418
|
-
return mappingFile;
|
|
1419
|
-
} else {
|
|
1420
|
-
const mappingData = await JSON.parse( mappingFile.toString( 'utf-8' ) );
|
|
1421
|
-
const filterData = await mappingData.person_data.filter(
|
|
1422
|
-
( item ) =>
|
|
1423
|
-
item.temp_ids < 20000 &&
|
|
1424
|
-
item.temp_ids > 0 &&
|
|
1425
|
-
item.person_status === '',
|
|
1426
|
-
);
|
|
1427
|
-
const finalResult = await filterData.map( ( i ) => {
|
|
1428
|
-
return { ...i, img: i.index.concat( '_', i.temp_ids ) };
|
|
1429
|
-
} );
|
|
1430
|
-
return finalResult;
|
|
1431
|
-
}
|
|
1432
|
-
} catch ( error ) {
|
|
1433
|
-
logger.error( {
|
|
1434
|
-
error: error,
|
|
1435
|
-
type: 'MAPPING JSON getFilterData',
|
|
1436
|
-
function: 'getZoneFilterData',
|
|
1437
|
-
} );
|
|
1438
|
-
return error;
|
|
1439
|
-
}
|
|
1440
|
-
}
|
|
1441
|
-
|
|
1442
|
-
export async function getZoneAuditImage( msg ) {
|
|
1443
|
-
try {
|
|
1444
|
-
const bucket = JSON.parse( process.env.BUCKET );
|
|
1445
|
-
const storeId = msg.store_id;
|
|
1446
|
-
const fileDate = msg.curr_date;
|
|
1447
|
-
const files = [];
|
|
1448
|
-
const fetchData = {};
|
|
1449
|
-
fetchData.Bucket = `${bucket.auditInput}`;
|
|
1450
|
-
fetchData.filePath = `${fileDate}/${storeId}/${msg.zone_id}/`;
|
|
1451
|
-
const folderPath = await listFileWithoutLimit( fetchData );
|
|
1452
|
-
if ( folderPath.statusCode == 404 ) {
|
|
1453
|
-
return folderPath;
|
|
1454
|
-
}
|
|
1455
|
-
const auditImages = folderPath;
|
|
1456
|
-
if ( auditImages && auditImages?.length > 0 ) {
|
|
1457
|
-
for ( let i = 0; i < auditImages.length; i++ ) {
|
|
1458
|
-
const img = auditImages[i].Key.split( '/' );
|
|
1459
|
-
const image = img[2].split( '.' );
|
|
1460
|
-
const indexes = image[0].split( '_' );
|
|
1461
|
-
const params = {
|
|
1462
|
-
file_path: auditImages[i].Key,
|
|
1463
|
-
Bucket: fetchData.Bucket,
|
|
1464
|
-
};
|
|
1465
|
-
const data = await signedUrl( params );
|
|
1466
|
-
const mapimg = {
|
|
1467
|
-
img_path: data,
|
|
1468
|
-
img_name: indexes[1],
|
|
1469
|
-
img_id: image[0],
|
|
1470
|
-
};
|
|
1471
|
-
files.push( {
|
|
1472
|
-
img_path: data,
|
|
1473
|
-
img_name: indexes[1],
|
|
1474
|
-
img_id: image[0],
|
|
1475
|
-
selected: false,
|
|
1476
|
-
dropped: false,
|
|
1477
|
-
count: 1,
|
|
1478
|
-
mappedid: [ mapimg ],
|
|
1479
|
-
} );
|
|
1480
|
-
}
|
|
1481
|
-
return files;
|
|
1482
|
-
}
|
|
1483
|
-
} catch ( error ) {
|
|
1484
|
-
logger.error( { error: error, type: 'AWS BUCKET --getAuditImage' } );
|
|
1485
|
-
return error;
|
|
1486
|
-
}
|
|
1487
|
-
}
|
|
1488
|
-
export async function clientMetrics( req, res ) {
|
|
1489
|
-
try {
|
|
1490
|
-
const inputData = req.body;
|
|
1491
|
-
const limit = inputData.limit || 10;
|
|
1492
|
-
const offset = inputData.offset ? ( inputData.offset - 1 ) * limit : 0;
|
|
1493
|
-
const dateRange = await getUTC( new Date( inputData.fromDate ), new Date( inputData.toDate ) );
|
|
1494
|
-
let filter = [
|
|
1495
|
-
{ fileDateISO: { $gte: dateRange.start } },
|
|
1496
|
-
{ fileDateISO: { $lte: dateRange.end } },
|
|
1497
|
-
{ moduleType: { $eq: 'zone' } },
|
|
1498
|
-
|
|
1499
|
-
];
|
|
1500
|
-
if ( inputData?.filterByClient?.length> 0 ) {
|
|
1501
|
-
filter.push(
|
|
1502
|
-
{ clientId: { $in: inputData.filterByClient } },
|
|
1503
|
-
);
|
|
1504
|
-
}
|
|
1505
|
-
const query = [
|
|
1506
|
-
{
|
|
1507
|
-
$match: {
|
|
1508
|
-
$and: filter,
|
|
1509
|
-
},
|
|
1510
|
-
},
|
|
1511
|
-
{
|
|
1512
|
-
$project: {
|
|
1513
|
-
fileDate: 1,
|
|
1514
|
-
clientId: 1,
|
|
1515
|
-
clientName: 1,
|
|
1516
|
-
queueName: 1,
|
|
1517
|
-
installedStore: 1,
|
|
1518
|
-
totalFilesCount: 1,
|
|
1519
|
-
notAssignedCount: { $ifNull: [ 0, 0 ] },
|
|
1520
|
-
clientStatus: { $ifNull: [ '', '' ] },
|
|
1521
|
-
},
|
|
1522
|
-
},
|
|
1523
|
-
{
|
|
1524
|
-
$lookup: {
|
|
1525
|
-
'from': 'storeZoneAudit',
|
|
1526
|
-
'let': { clientId: '$clientId', fileDate: '$fileDate', totalAuditFiles: '$totalAuditFiles' },
|
|
1527
|
-
'pipeline': [
|
|
1528
|
-
|
|
1529
|
-
{
|
|
1530
|
-
$match: {
|
|
1531
|
-
$expr: {
|
|
1532
|
-
$and: [
|
|
1533
|
-
{ $eq: [ '$clientId', '$$clientId' ] },
|
|
1534
|
-
{ $eq: [ '$fileDate', '$$fileDate' ] },
|
|
1535
|
-
],
|
|
1536
|
-
},
|
|
1537
|
-
},
|
|
1538
|
-
},
|
|
1539
|
-
{
|
|
1540
|
-
$project: {
|
|
1541
|
-
completedStores: { $cond: [ { $eq: [ '$status', 'completed' ] }, 1, 0 ] },
|
|
1542
|
-
notAssignedStores: { $cond: [ { $eq: [ '$status', 'not_assign' ] }, 1, 0 ] },
|
|
1543
|
-
inprogressStores: { $cond: [ { $in: [ '$status', [ 'inprogress', 'drafted', 'assigned' ] ] }, 1, 0 ] },
|
|
1544
|
-
},
|
|
1545
|
-
},
|
|
1546
|
-
{
|
|
1547
|
-
$group: {
|
|
1548
|
-
_id: { clientId: '$clientId', fileDate: '$fileDate' },
|
|
1549
|
-
completedStores: { $sum: '$completedStores' },
|
|
1550
|
-
inprogressStores: { $sum: '$inprogressStores' },
|
|
1551
|
-
notAssignedStores: { $sum: '$notAssignedStores' },
|
|
1552
|
-
fileCount: { $sum: 1 },
|
|
1553
|
-
},
|
|
1554
|
-
},
|
|
1555
|
-
{
|
|
1556
|
-
$project: {
|
|
1557
|
-
_id: 0,
|
|
1558
|
-
completedStores: 1,
|
|
1559
|
-
inprogressStores: 1,
|
|
1560
|
-
notAssignedStores: 1,
|
|
1561
|
-
fileCount: 1,
|
|
1562
|
-
completionPercentage: { $ifNull: [
|
|
1563
|
-
{
|
|
1564
|
-
$round: [ {
|
|
1565
|
-
$multiply: [
|
|
1566
|
-
100, {
|
|
1567
|
-
$divide: [
|
|
1568
|
-
'$completedStores', '$$totalAuditFiles',
|
|
1569
|
-
],
|
|
1570
|
-
},
|
|
1571
|
-
],
|
|
1572
|
-
}, 1 ],
|
|
1573
|
-
|
|
1574
|
-
},
|
|
1575
|
-
0,
|
|
1576
|
-
],
|
|
1577
|
-
},
|
|
1578
|
-
},
|
|
1579
|
-
},
|
|
1580
|
-
], 'as': 'storeZoneAudit',
|
|
1581
|
-
},
|
|
1582
|
-
},
|
|
1583
|
-
{
|
|
1584
|
-
$unwind: {
|
|
1585
|
-
path: '$storeZoneAudit',
|
|
1586
|
-
preserveNullAndEmptyArrays: true,
|
|
1587
|
-
},
|
|
1588
|
-
},
|
|
1589
|
-
{
|
|
1590
|
-
$project: {
|
|
1591
|
-
fileDate: 1,
|
|
1592
|
-
clientId: 1,
|
|
1593
|
-
clientName: 1,
|
|
1594
|
-
totalFilesCount: 1,
|
|
1595
|
-
installedStore: 1,
|
|
1596
|
-
fileCount: '$storeZoneAudit.fileCount',
|
|
1597
|
-
notAssignedStores: '$storeZoneAudit.notAssignedStores',
|
|
1598
|
-
inprogressStoresCount: '$storeZoneAudit.inprogressStores',
|
|
1599
|
-
notAssignedCount: 1,
|
|
1600
|
-
clientStatus: { $cond: [ { $eq: [ '$storeZoneAudit.fileCount', 0 ] }, 'not-taken', { $cond: [ { $or: [ { $gt: [ '$notAssignedCount', 0 ] }, { $gt: [ '$storeZoneAudit.notAssignedStores', 0 ] } ] }, 'pending', 'completed' ] } ] },
|
|
1601
|
-
completedStores: '$storeZoneAudit.completedStores',
|
|
1602
|
-
completionPercentage: '$storeZoneAudit.completionPercentage',
|
|
1603
|
-
|
|
1604
|
-
},
|
|
1605
|
-
},
|
|
1606
|
-
];
|
|
1607
|
-
|
|
1608
|
-
if ( inputData.searchValue && inputData.searchValue!== '' ) {
|
|
1609
|
-
query.push( {
|
|
1610
|
-
$match: {
|
|
1611
|
-
$or: [
|
|
1612
|
-
{ clientId: { $regex: inputData.searchValue, $options: 'i' } },
|
|
1613
|
-
{ clientName: { $regex: inputData.searchValue, $options: 'i' } },
|
|
1614
|
-
],
|
|
1615
|
-
|
|
1616
|
-
},
|
|
1617
|
-
} );
|
|
1618
|
-
}
|
|
1619
|
-
if ( inputData.sortColumnName ) {
|
|
1620
|
-
const sortBy = inputData.sortBy || -1;
|
|
1621
|
-
query.push( {
|
|
1622
|
-
$sort: { [inputData.sortColumName]: sortBy },
|
|
1623
|
-
},
|
|
1624
|
-
);
|
|
1625
|
-
}
|
|
1626
|
-
logger.info( { query: query } );
|
|
1627
|
-
const count = [];// await aggregateAuditClientData( query );
|
|
1628
|
-
if ( count.length == 0 ) {
|
|
1629
|
-
return res.sendError( 'No Data Found', 204 );
|
|
1630
|
-
}
|
|
1631
|
-
|
|
1632
|
-
query.push(
|
|
1633
|
-
{ $skip: offset },
|
|
1634
|
-
{ $limit: limit },
|
|
1635
|
-
);
|
|
1636
|
-
if ( inputData.isExport ) {
|
|
1637
|
-
query.push(
|
|
1638
|
-
{
|
|
1639
|
-
$project: {
|
|
1640
|
-
'_id': 0,
|
|
1641
|
-
'File Date': '$fileDate',
|
|
1642
|
-
'Client Name': '$clientName',
|
|
1643
|
-
'Client Id': '$clientId',
|
|
1644
|
-
'Completed Percentage': '$completionPercentage',
|
|
1645
|
-
'Installed Stores': '$installedStore',
|
|
1646
|
-
'Audit Stores': '$totalFilesCount',
|
|
1647
|
-
'Inprogress Stores': '$inprogressStoresCount',
|
|
1648
|
-
'Not Assigned': '$notAssignedCount',
|
|
1649
|
-
'Not Assigned Stores': '$notAssignedStores',
|
|
1650
|
-
'Completed': '$completedStores',
|
|
1651
|
-
'Status': '$clientStatus',
|
|
1652
|
-
},
|
|
1653
|
-
},
|
|
1654
|
-
);
|
|
1655
|
-
}
|
|
1656
|
-
|
|
1657
|
-
logger.info( { query: query } );
|
|
1658
|
-
const result = [];// await aggregateAuditClientData( query );
|
|
1659
|
-
if ( inputData.isExport ) {
|
|
1660
|
-
await download( result, res );
|
|
1661
|
-
return;
|
|
1662
|
-
}
|
|
1663
|
-
return res.sendSuccess( { result: result, count: count.length } );
|
|
1664
|
-
} catch ( error ) {
|
|
1665
|
-
const err = error.error || 'Internal Server Error';
|
|
1666
|
-
logger.info( { error: error, message: req.body, function: 'clientMetrics' } );
|
|
1667
|
-
return res.sendError( err, 500 );
|
|
1668
|
-
}
|
|
1669
|
-
}
|
|
1670
|
-
|
|
1671
|
-
export async function reTrigger( req, res ) {
|
|
1672
|
-
try {
|
|
1673
|
-
const openSearch = JSON.parse( process.env.OPENSEARCH );
|
|
1674
|
-
const inputData = req.body;
|
|
1675
|
-
const sqs = JSON.parse( process.env.SQS );
|
|
1676
|
-
const query ={
|
|
1677
|
-
storeId: inputData.storeId,
|
|
1678
|
-
fileDate: inputData.fileDate,
|
|
1679
|
-
zoneName: inputData.zoneName,
|
|
1680
|
-
};
|
|
1681
|
-
const getQueueName = await findOneClient( { clientId: req.audit.clientId }, { 'auditConfigs': 1 } );
|
|
1682
|
-
logger.info( { queueName: getQueueName.auditConfigs.zoneQueueName } );
|
|
1683
|
-
if ( !getQueueName || getQueueName?.auditConfigs?.zoneQueueName == '' ) {
|
|
1684
|
-
return res.sendError( 'queueName does not create', 400 );
|
|
1685
|
-
}
|
|
1686
|
-
const queueName = getQueueName?.auditConfigs?.zoneQueueName;
|
|
1687
|
-
|
|
1688
|
-
logger.info( { queueName: queueName } );
|
|
1689
|
-
switch ( inputData.triggerType ) {
|
|
1690
|
-
case 'queue':
|
|
1691
|
-
const msg = {
|
|
1692
|
-
store_id: inputData.storeId,
|
|
1693
|
-
curr_date: inputData.fileDate,
|
|
1694
|
-
audit_type: inputData.type,
|
|
1695
|
-
total_count: inputData.totalCount,
|
|
1696
|
-
zone_id: inputData.zoneName,
|
|
1697
|
-
};
|
|
1698
|
-
|
|
1699
|
-
const sqsProduceQueue = {
|
|
1700
|
-
QueueUrl: `${sqs.url}${queueName}`,
|
|
1701
|
-
MessageBody: JSON.stringify( msg ),
|
|
1702
|
-
};
|
|
1703
|
-
const sqsQueue = await sendMessageToQueue( sqsProduceQueue.QueueUrl, sqsProduceQueue.MessageBody );
|
|
1704
|
-
if ( sqsQueue?.errorCode ) {
|
|
1705
|
-
const error = sqsQueue?.errorMsg;
|
|
1706
|
-
const code = sqsQueue?.errorCode;
|
|
1707
|
-
return res.sendError( error, code );
|
|
1708
|
-
} else {
|
|
1709
|
-
if ( req.audit.status !== 'completed' ) {
|
|
1710
|
-
const userRecord ={
|
|
1711
|
-
auditStatus: 'skipped',
|
|
1712
|
-
isDraft: false,
|
|
1713
|
-
};
|
|
1714
|
-
const query ={
|
|
1715
|
-
storeId: inputData.storeId,
|
|
1716
|
-
fileDate: inputData.fileDate,
|
|
1717
|
-
zoneName: inputData.zoneName,
|
|
1718
|
-
auditStatus: req.audit.status,
|
|
1719
|
-
|
|
1720
|
-
};
|
|
1721
|
-
await updateOneUserAudit( query, userRecord );
|
|
1722
|
-
}
|
|
1723
|
-
|
|
1724
|
-
const storeRecord ={
|
|
1725
|
-
status: req.audit.status !== 'completed' ? 'skipped': 'not_assign',
|
|
1726
|
-
};
|
|
1727
|
-
|
|
1728
|
-
await updateOneStoreZoneAudit( query, storeRecord );
|
|
1729
|
-
const logData = {
|
|
1730
|
-
userId: req.user._id,
|
|
1731
|
-
userName: req.user.userName,
|
|
1732
|
-
logType: 'zoneAudit',
|
|
1733
|
-
logSubType: 'reTrigger',
|
|
1734
|
-
logData: {
|
|
1735
|
-
beforeCount: inputData.totalCount,
|
|
1736
|
-
afterCount: '',
|
|
1737
|
-
fileDate: inputData.fileDate,
|
|
1738
|
-
auditType: inputData.type,
|
|
1739
|
-
storeId: inputData.storeId,
|
|
1740
|
-
zoneName: inputData.zoneName,
|
|
1741
|
-
queueName: queueName,
|
|
1742
|
-
message: inputData.comments,
|
|
1743
|
-
triggerBy: req.user.userName,
|
|
1744
|
-
triggerId: req.user._id,
|
|
1745
|
-
triggerType: inputData.triggerType,
|
|
1746
|
-
},
|
|
1747
|
-
};
|
|
1748
|
-
await insertOpenSearchData( openSearch.activityLog, logData );
|
|
1749
|
-
}
|
|
1750
|
-
break;
|
|
1751
|
-
case 'user':
|
|
1752
|
-
const assignUser = await findOneUser( { _id: new mongoose.Types.ObjectId( inputData.userId ) } );
|
|
1753
|
-
const assignUserRecord = {
|
|
1754
|
-
storeId: inputData.storeId,
|
|
1755
|
-
clientId: req.audit.clientId,
|
|
1756
|
-
fileDate: inputData.fileDate,
|
|
1757
|
-
zoneName: inputData.zoneName,
|
|
1758
|
-
fileDateISO: dayjs( inputData.fileDate, 'DD-MM-YYYY' ).format(),
|
|
1759
|
-
auditType: inputData.type,
|
|
1760
|
-
moduleType: 'zone',
|
|
1761
|
-
fileCount: inputData.beforeCount,
|
|
1762
|
-
queueName: queueName,
|
|
1763
|
-
userId: inputData.userId,
|
|
1764
|
-
userName: assignUser.userName,
|
|
1765
|
-
};
|
|
1766
|
-
await createAssignAudit( assignUserRecord );
|
|
1767
|
-
|
|
1768
|
-
if ( req.audit.status !== 'completed' ) {
|
|
1769
|
-
const userRecord ={
|
|
1770
|
-
auditStatus: 'skipped',
|
|
1771
|
-
isDraft: false,
|
|
1772
|
-
};
|
|
1773
|
-
const query ={
|
|
1774
|
-
storeId: inputData.storeId,
|
|
1775
|
-
fileDate: inputData.fileDate,
|
|
1776
|
-
zoneName: inputData.zoneName,
|
|
1777
|
-
auditStatus: req.audit.status,
|
|
1778
|
-
|
|
1779
|
-
};
|
|
1780
|
-
await updateOneUserAudit( query, userRecord );
|
|
1781
|
-
}
|
|
1782
|
-
|
|
1783
|
-
const storeRecord ={
|
|
1784
|
-
status: req.audit.status !== 'completed' ? 'skipped': 'assigned',
|
|
1785
|
-
};
|
|
1786
|
-
|
|
1787
|
-
await updateOneStoreZoneAudit( query, storeRecord );
|
|
1788
|
-
const logData = {
|
|
1789
|
-
userId: req.user._id,
|
|
1790
|
-
userName: req.user.userName,
|
|
1791
|
-
logType: 'zoneAudit',
|
|
1792
|
-
logSubType: 'reTrigger',
|
|
1793
|
-
logData: {
|
|
1794
|
-
beforeCount: inputData.totalCount,
|
|
1795
|
-
afterCount: '',
|
|
1796
|
-
fileDate: inputData.fileDate,
|
|
1797
|
-
auditType: inputData.type,
|
|
1798
|
-
storeId: inputData.storeId,
|
|
1799
|
-
zoneName: inputData.zoneName,
|
|
1800
|
-
queueName: queueName,
|
|
1801
|
-
message: inputData.comments,
|
|
1802
|
-
triggerBy: req.user.userName,
|
|
1803
|
-
triggerId: req.user._id,
|
|
1804
|
-
triggerType: inputData.triggerType,
|
|
1805
|
-
},
|
|
1806
|
-
};
|
|
1807
|
-
await insertOpenSearchData( openSearch.activityLog, logData );
|
|
1808
|
-
}
|
|
1809
|
-
|
|
1810
|
-
return res.sendSuccess( { result: 'The File has been Re Trigger Succesfully' } );
|
|
1811
|
-
} catch ( error ) {
|
|
1812
|
-
const err = error.message;
|
|
1813
|
-
logger.error( { error: error, message: req.body } );
|
|
1814
|
-
return res.sendError( err, 500 );
|
|
1815
|
-
}
|
|
1816
|
-
}
|
|
1817
|
-
|
|
1818
|
-
export async function viewLog( req, res ) {
|
|
1819
|
-
try {
|
|
1820
|
-
// const inputData = req.body;
|
|
1821
|
-
// const result = await
|
|
1822
|
-
} catch ( error ) {
|
|
1823
|
-
const err = error.message;
|
|
1824
|
-
logger.error( { error: error, message: req.body, function: 'viewLog' } );
|
|
1825
|
-
return res.sendError( err, 500 );
|
|
1826
|
-
}
|
|
1827
|
-
}
|