tango-app-api-report 3.1.3 → 3.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-report",
3
- "version": "3.1.3",
3
+ "version": "3.3.1",
4
4
  "description": "report",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -25,8 +25,8 @@
25
25
  "nodemailer": "^6.9.13",
26
26
  "nodemon": "^3.1.0",
27
27
  "swagger-ui-express": "^5.0.0",
28
- "tango-api-schema": "^2.0.111",
29
- "tango-app-api-middleware": "^3.1.19",
28
+ "tango-api-schema": "^2.1.32",
29
+ "tango-app-api-middleware": "^3.1.43",
30
30
  "winston": "^3.12.0",
31
31
  "winston-daily-rotate-file": "^5.0.0"
32
32
  },
@@ -1,5 +1,5 @@
1
1
  import { deleteOneReport, findOneReport, findReport, reportCreate, reportGet, reportGetSingle, reportUpdate } from '../service/report.service.js';
2
- import { fileUpload, getObject, getOpenSearchData, insertOpenSearchData, listFileByPath, logger, sendMessageToQueue } from 'tango-app-api-middleware';
2
+ import { convertTimestampToDateTime, fileUpload, getObject, getOpenSearchData, insertOpenSearchData, listFileByPath, logger, sendMessageToQueue } from 'tango-app-api-middleware';
3
3
  import { findOneUser, getUserNameEmailById } from '../service/user.service.js';
4
4
  import { updateOneBasePriceModel } from '../service/basePrice.service.js';
5
5
  import { aggregateClient } from '../service/client.service.js';
@@ -130,7 +130,6 @@ export async function clientReportList( req, res ) {
130
130
  $match: {
131
131
  $and: [
132
132
  { 'reportConfigs.report': { $eq: true } },
133
- { status: { $eq: 'active' } },
134
133
  { clientId: { $in: inputData.clientId } },
135
134
  ],
136
135
 
@@ -141,6 +140,7 @@ export async function clientReportList( req, res ) {
141
140
  clientId: 1,
142
141
  tangoId: 1,
143
142
  clientName: 1,
143
+ status: 1,
144
144
  daysDifference: 1,
145
145
  datesArray: {
146
146
  $map: {
@@ -166,6 +166,21 @@ export async function clientReportList( req, res ) {
166
166
  clientName: 1,
167
167
  tangoId: 1,
168
168
  fileDate: '$datesArray',
169
+ status: 1,
170
+ isSendEnable: {
171
+ $cond: [
172
+ { $eq: [ '$status', 'active' ] },
173
+ true,
174
+ false,
175
+ ],
176
+ },
177
+ isGenerateEnable: {
178
+ $cond: [
179
+ { $in: [ '$status', [ 'active', 'hold' ] ] },
180
+ true,
181
+ false,
182
+ ],
183
+ },
169
184
  },
170
185
  },
171
186
  ];
@@ -196,6 +211,10 @@ export async function clientReportList( req, res ) {
196
211
  clientId: { $regex: inputData.searchValue, $options: 'i' },
197
212
 
198
213
  },
214
+ {
215
+ status: { $regex: inputData.searchValue, $options: 'i' },
216
+
217
+ },
199
218
  ],
200
219
  },
201
220
  },
@@ -223,6 +242,7 @@ export async function clientReportList( req, res ) {
223
242
  export async function generateReport( req, res ) {
224
243
  try {
225
244
  const sqs = JSON.parse( process.env.SQS );
245
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
226
246
  const inputData = req.body;
227
247
  const sqsProduceQueue = {
228
248
  QueueUrl: `${sqs.url}${sqs.generateReport}`,
@@ -237,19 +257,20 @@ export async function generateReport( req, res ) {
237
257
  if ( sqsQueue.MessageId ) {
238
258
  const log = {
239
259
  userName: req.user.userName,
240
- Date: new Date(),
241
260
  userId: req.user._id,
242
261
  logType: 'report',
243
262
  logSubType: 'generateReport',
244
263
  logData: {
245
- clientName: req.report?.clientName,
264
+ // clientName: req.report?.clientName,
246
265
  fileDate: inputData.fileDate,
247
266
  reportName: req.report?.reportConfigs?.reportName,
248
- clientId: inputData.ClientId,
267
+ clientId: inputData.clientId,
249
268
  sqsMessageId: sqsQueue.MessageId,
250
269
  },
270
+ createdAt: Date.now(),
251
271
  };
252
- await insertOpenSearchData( 'tango-retail-activity-logs', log );
272
+ logger.info( { auditLog: openSearch.auditLog, openSearch: openSearch } );
273
+ await insertOpenSearchData( openSearch.auditLog, log );
253
274
 
254
275
  return res.sendSuccess( { result: 'Report has been Initiated Sucessfully' } );
255
276
  }
@@ -263,6 +284,7 @@ export async function generateReport( req, res ) {
263
284
  export async function uploadManualReport( req, res ) {
264
285
  try {
265
286
  const bucket = JSON.parse( process.env.BUCKET );
287
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
266
288
  const inputData = req.body;
267
289
  const name = inputData.fileName;
268
290
  const regex = /^(\d{2}-\d{2}-\d{4})_(.*?)\.([a-zA-Z0-9]+)$/;
@@ -301,7 +323,6 @@ export async function uploadManualReport( req, res ) {
301
323
  }
302
324
  const log = {
303
325
  userName: user.userName,
304
- Date: new Date(),
305
326
  userId: req.user._id,
306
327
  logType: 'report',
307
328
  logSubType: 'uploadReport',
@@ -310,9 +331,10 @@ export async function uploadManualReport( req, res ) {
310
331
  reportName: req.report?.reportConfigs?.reportName,
311
332
  clientId: data.clientId,
312
333
  },
334
+ createdAt: Date.now(),
313
335
  };
314
336
  logger.info( { log: log } );
315
- // await insertOpenSearchData( 'tango-retail-activity-logs', log );
337
+ await insertOpenSearchData( openSearch.auditLog, log );
316
338
  return res.sendSuccess( { result: 'uploaded Sucessfully' } );
317
339
  } else {
318
340
  return res.sendError( 'File Name is Incorrect', 400 );
@@ -378,6 +400,7 @@ export async function dowloadReport( req, res ) {
378
400
  export async function sendReport( req, res ) {
379
401
  try {
380
402
  const bucket = JSON.parse( process.env.BUCKET );
403
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
381
404
  const sqs = JSON.parse( process.env.SQS );
382
405
  const sesConfig = JSON.parse( process.env.SES );
383
406
  const inputData = req.body;
@@ -388,7 +411,7 @@ export async function sendReport( req, res ) {
388
411
  let result = [];
389
412
  let file = {};
390
413
  let sendMailCount = 0;
391
- let subject = 'Fwd: Tango Eye\'s Customer Intelligence Report - ' + inputData.fileDate;
414
+ let subject = 'Tango Eye\'s Customer Intelligence Report - ' + inputData.fileDate;
392
415
  let messageBody = `<br>Hi Team,<br><br>&nbsp;Please find attached Tango RetailEye report on customer footfall and shopping patterns for ${inputData.fileDate}.<br><br>&nbsp;Thanks <br>&nbsp;Team Tango`;
393
416
  let UserattachmentDetails = [];
394
417
  for ( const data of getReportList ) {
@@ -443,6 +466,7 @@ export async function sendReport( req, res ) {
443
466
  QueueUrl: `${sqs.url}${sqs.sendReport}`,
444
467
  MessageBody: JSON.stringify( {
445
468
  file_date: inputData.fileDate,
469
+ reportName: req.report?.reportConfigs?.reportName,
446
470
  client_name: req.report?.reportConfigs?.reportName,
447
471
  client_id: Number( inputData.clientId ),
448
472
  file_name: regionalKey,
@@ -454,7 +478,6 @@ export async function sendReport( req, res ) {
454
478
  logger.info( 'successfully Pushed to SQS ' + `${JSON.stringify( sqsQueue )}`, 'Send Report' );
455
479
  const log = {
456
480
  userName: user.userName,
457
- Date: new Date(),
458
481
  userId: user._id,
459
482
  logType: 'report',
460
483
  logSubType: 'sendReport',
@@ -462,10 +485,12 @@ export async function sendReport( req, res ) {
462
485
  fileDate: inputData.fileDate,
463
486
  reportName: req.report?.reportConfigs?.reportName,
464
487
  clientId: getReportList[0].clientId,
488
+ sqsMessageId: sqsQueue.MessageId,
465
489
  },
490
+ createdAt: Date.now(),
466
491
  };
467
492
  logger.info( { log: log } );
468
- // await insertOpenSearchData( 'tango-retail-activity-logs', log );
493
+ await insertOpenSearchData( openSearch.auditLog, log );
469
494
  return res.sendSuccess( { result: 'Report has been Sent Successfully' } );
470
495
  } else {
471
496
  return res.sendError( ' Message does not Sent & Report has been Sent Successfully', 500 );
@@ -480,7 +505,9 @@ export async function sendReport( req, res ) {
480
505
  export async function getReportLog( req, res ) {
481
506
  try {
482
507
  const inputData = req.body;
483
- const log = await getOpenSearchData( 'tango-retail-activity-logs',
508
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
509
+ const logData =[];
510
+ const reportLog = await getOpenSearchData( openSearch.auditLog,
484
511
  {
485
512
  'size': 100,
486
513
  'query': {
@@ -496,6 +523,11 @@ export async function getReportLog( req, res ) {
496
523
  'logData.fileDate.keyword': inputData.fileDate,
497
524
  },
498
525
  },
526
+ {
527
+ 'term': {
528
+ 'logData.clientId.keyword': inputData.clientId,
529
+ },
530
+ },
499
531
 
500
532
  ],
501
533
 
@@ -503,10 +535,24 @@ export async function getReportLog( req, res ) {
503
535
  },
504
536
 
505
537
  } );
506
- return res.sendSuccess( { result: log } );
538
+ logger.info( { reportLog: reportLog } );
539
+ if ( reportLog.body.hits.hits.length > 0 ) {
540
+ const sourcesArray = reportLog.body.hits.hits.map( ( hit ) => hit._source );
541
+ for ( const i of sourcesArray ) { // Loop through sourcesArray, not result._doc.camera
542
+ const convertedTime = await convertTimestampToDateTime( i.createdAt );
543
+ logData.push( {
544
+ ...i, // Spread the individual log object
545
+ time: convertedTime, // Add the converted time
546
+ } );
547
+ }
548
+ return res.sendSuccess( { result: logData } );
549
+ } else {
550
+ return res.sendError( 'No data found', 204 );
551
+ }
507
552
  } catch ( error ) {
553
+ const err = error.message || 'Internal Server Error';
508
554
  logger.info( { error: error } );
509
- return res.sendError( error, 500 );
555
+ return res.sendError( err, 500 );
510
556
  }
511
557
  }
512
558
  export async function updateBasePrice( req, res ) {
@@ -3,10 +3,10 @@ import express from 'express';
3
3
  import { authorize, isAllowedSessionHandler, validate } from 'tango-app-api-middleware';
4
4
  import { addReportValid, clientListTableValid, deleteReportValid, downloadReportValid, generateReportValid, getReportListValid, getReportLogValid, getReportValid, getSingleReportValid, sendReportValid, updateReportValid } from '../dtos/report.dtos.js';
5
5
  import { clientReportList, createReport, deleteReport, dowloadReport, generateReport, getReport, getReportList, getReportLog, getSingleReport, sendReport, updateBasePrice, updateReport, uploadManualReport } from '../controllers/report.controllers.js';
6
- import { isFileExist, isFolderExist, isReportEnable } from '../validations/report.validations.js';
6
+ import { isFileExist, isFolderExist, isReportEnable, isSendReportEnable } from '../validations/report.validations.js';
7
7
 
8
8
  export const reportRouter = express.Router();
9
-
9
+ // report setting configurations
10
10
  reportRouter.post( '/create-report/:id', isAllowedSessionHandler, authorize( { userType: [ 'tango' ], access: [
11
11
  { featureName: 'settings', name: 'configuration', permissions: [ 'isEdit' ] },
12
12
  ] } ), validate( addReportValid ), createReport );
@@ -29,43 +29,43 @@ reportRouter.get( '/delete-report/:id', isAllowedSessionHandler, authorize( { us
29
29
  // admin report's overview
30
30
  reportRouter.post( '/client-list-table', isAllowedSessionHandler,
31
31
  authorize( { userType: [ 'tango' ], access: [
32
- { featureName: 'settings', name: 'configuration', permissions: [ 'isView' ] },
32
+ { featureName: 'manage', name: 'reports', permissions: [ 'isView' ] },
33
33
  ] } ),
34
34
  validate( clientListTableValid ), clientReportList );
35
35
 
36
36
  reportRouter.post( '/generate-report', isAllowedSessionHandler,
37
37
  authorize( { userType: [ 'tango' ], access: [
38
- { featureName: 'settings', name: 'configuration', permissions: [ 'isEdit' ] },
38
+ { featureName: 'manage', name: 'reports', permissions: [ 'isEdit' ] },
39
39
  ] } ),
40
40
  validate( generateReportValid ), isReportEnable, generateReport );
41
41
 
42
42
  reportRouter.post( '/upload-manual-report', isAllowedSessionHandler,
43
43
  authorize( { userType: [ 'tango' ], access: [
44
- { featureName: 'settings', name: 'configuration', permissions: [ 'isEdit' ] },
44
+ { featureName: 'manage', name: 'reports', permissions: [ 'isEdit' ] },
45
45
  ] } ),
46
46
  isReportEnable, uploadManualReport );
47
47
 
48
48
  reportRouter.get( '/get-report-list', isAllowedSessionHandler,
49
49
  authorize( { userType: [ 'tango' ], access: [
50
- { featureName: 'settings', name: 'configuration', permissions: [ 'isView' ] },
50
+ { featureName: 'manage', name: 'reports', permissions: [ 'isView' ] },
51
51
  ] } ),
52
52
  validate( getReportListValid ), isReportEnable, isFolderExist, getReportList );
53
53
 
54
54
  reportRouter.get( '/download-report', isAllowedSessionHandler,
55
55
  authorize( { userType: [ 'tango' ], access: [
56
- { featureName: 'settings', name: 'configuration', permissions: [ 'isEdit' ] },
56
+ { featureName: 'manage', name: 'reports', permissions: [ 'isEdit' ] },
57
57
  ] } ),
58
- validate( downloadReportValid ), isFileExist, dowloadReport );
58
+ validate( downloadReportValid ), isReportEnable, isFileExist, dowloadReport );
59
59
 
60
60
  reportRouter.post( '/send-report', isAllowedSessionHandler,
61
61
  authorize( { userType: [ 'tango' ], access: [
62
- { featureName: 'settings', name: 'configuration', permissions: [ 'isEdit' ] },
62
+ { featureName: 'manage', name: 'reports', permissions: [ 'isEdit' ] },
63
63
  ] } ),
64
- validate( sendReportValid ), isReportEnable, sendReport );
64
+ validate( sendReportValid ), isSendReportEnable, sendReport );
65
65
 
66
66
  reportRouter.post( '/get-report-log', isAllowedSessionHandler,
67
67
  authorize( { userType: [ 'tango' ], access: [
68
- { featureName: 'settings', name: 'configuration', permissions: [ 'isView' ] },
68
+ { featureName: 'manage', name: 'reports', permissions: [ 'isView' ] },
69
69
  ] } ),
70
70
  validate( getReportLogValid ), getReportLog );
71
71
 
@@ -3,11 +3,26 @@ import { findOneClient } from '../service/client.service.js';
3
3
 
4
4
 
5
5
  export async function isReportEnable( req, res, next ) {
6
+ try {
7
+ const inputData = req.method !== 'GET'? req.body: req.query;
8
+ const getReportName = await findOneClient( { 'clientId': inputData.clientId, 'status': { $in: [ 'active', 'hold' ] }, 'reportConfigs.report': true }, { '_id': 0, 'reportConfigs.reportName': 1, 'clientName': 1 } );
9
+ if ( !getReportName ) {
10
+ return res.sendError( 'This action was forbidden', 403 );
11
+ }
12
+ req.report = getReportName;
13
+ return next();
14
+ } catch ( error ) {
15
+ logger.error( { error: error, message: req.body, function: 'isReportEnable' } );
16
+ return res.sendError( error, 500 );
17
+ }
18
+ }
19
+
20
+ export async function isSendReportEnable( req, res, next ) {
6
21
  try {
7
22
  const inputData = req.method !== 'GET'? req.body: req.query;
8
23
  const getReportName = await findOneClient( { 'clientId': inputData.clientId, 'status': 'active', 'reportConfigs.report': true }, { '_id': 0, 'reportConfigs.reportName': 1, 'clientName': 1 } );
9
24
  if ( !getReportName ) {
10
- return res.sendError( 'reportName is not there in the client', 204 );
25
+ return res.sendError( 'This action was forbidden', 403 );
11
26
  }
12
27
  req.report = getReportName;
13
28
  return next();