tango-app-api-infra 3.0.66-dev → 3.0.68-dev

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.
@@ -9,7 +9,7 @@ import { join } from 'path';
9
9
  import handlebars from 'handlebars';
10
10
  dayjs.extend( utc );
11
11
  dayjs.extend( timezone );
12
- import { sendEmailWithSES } from 'tango-app-api-middleware';
12
+ import { sendEmailWithSES, signedUrl } from 'tango-app-api-middleware';
13
13
  import { createClient, findClient, aggregateClient, findOneClient } from '../services/client.service.js';
14
14
  import { createStore, findStore, updateOneStore, findOneStore } from '../services/store.service.js';
15
15
  import { findTangoTicket, findOneTangoTicket, countDocumentsTangoTicket, aggregateTangoTicket, updateOneTangoTicket } from '../services/tangoTicket.service.js';
@@ -17,7 +17,7 @@ import { findOneGroup } from '../services/group.service.js';
17
17
  import { findinfraReason } from '../services/infraReason.service.js';
18
18
  import { findOneUser } from '../services/user.service.js';
19
19
  import xl from 'excel4node';
20
-
20
+ import { findOneinfraReason } from '../services/infraReason.service.js';
21
21
  export async function migrateClient() {
22
22
  try {
23
23
  let oldclient = [];
@@ -86,7 +86,7 @@ export async function clientList( req, res ) {
86
86
  export async function basicList( req, res ) {
87
87
  try {
88
88
  let storeList = await findStore( { status: 'active' },
89
- { 'storeName': 1, 'storeId': 1, 'clientId': 1, 'storeProfile.open': 1, 'storeProfile.close': 1, 'storeProfile.timeZone': 1 } );
89
+ { 'storeName': 1, 'storeId': 1, 'ticketConfigs': 1, 'clientId': 1, 'storeProfile.open': 1, 'storeProfile.close': 1, 'storeProfile.timeZone': 1 } );
90
90
  res.sendSuccess( storeList );
91
91
  } catch ( error ) {
92
92
  logger.error( { error: error, function: 'basicList' } );
@@ -123,7 +123,7 @@ export async function downStoresList( req, res ) {
123
123
  }
124
124
  export async function openTicketList( req, res ) {
125
125
  try {
126
- let openTicketList = await findTangoTicket( { status: { $ne: 'closed' } }, { ticketId: 1, basicDetails: 1, createdAt: 1, updateAt: 1, ticketDetails: 1 } );
126
+ let openTicketList = await findTangoTicket( { 'issueType': 'infra', 'status': { $ne: 'closed' } }, { issueType: 1, ticketId: 1, basicDetails: 1, createdAt: 1, updateAt: 1, ticketDetails: 1 } );
127
127
  if ( openTicketList.length ) {
128
128
  res.sendSuccess( {
129
129
  count: openTicketList.length,
@@ -200,8 +200,11 @@ export async function updateRefreshTicket( req, res ) {
200
200
  primaryIssue = Issue[0].reasons[0].primaryIssue;
201
201
  }
202
202
  let Uidomain = `${appConfig.url.domain}/manage/stores/infra-ticket?storeId=${getTicket.basicDetails.storeId}`;
203
+ let getclient = await findOneClient( { clientId: getTicket.basicDetails.clientId } );
203
204
  const html = htmlContent( { ...getTicket, Uidomain: Uidomain, primary: primaryIssue, storeName: getTicket.basicDetails.storeName, hibernation: '', spocName: spocName, date: dayjs( getTicket.issueDate ).format( 'YYYY-MM-DD' ), downtimetotal: downtimetotal, Timestamp: Timestamp, domain: appConfig.url.apiDomain } );
204
- await sendEmailWithSES( spocEmail, subject, html, attachments, appConfig.cloud.aws.ses.adminEmail );
205
+ if ( getclient.ticketConfigs.emailAlert && spocEmail && isValidEmail( spocEmail ) ) {
206
+ await sendEmailWithSES( spocEmail, subject, html, attachments, appConfig.cloud.aws.ses.adminEmail );
207
+ }
205
208
  }
206
209
  }
207
210
 
@@ -215,71 +218,77 @@ export async function closeTicket( req, res ) {
215
218
  try {
216
219
  for ( let ticket of req.body.TicketList ) {
217
220
  let getTicket = await findOneTangoTicket( { ticketId: ticket.ticketId } );
218
- if ( ticket.status == 'closed' ) {
219
- getTicket.ticketActivity.push( {
220
- actionType: 'dataRecived',
221
- actionBy: 'Tango',
222
- } );
223
- }
224
- await updateOneTangoTicket( { ticketId: ticket.ticketId },
225
- {
226
- status: ticket.status,
227
- ticketActivity: getTicket.ticketActivity,
228
- issueClosedDate: new Date(),
229
- },
230
- );
231
- let downTimeQuery = {
232
- 'size': 1,
233
- 'query': {
234
- 'bool': {
235
- 'must': [
236
- {
237
- 'term': {
238
- 'doc.date.keyword': dayjs( getTicket.issueDate ).format( 'DD-MM-YYYY' ),
221
+ if ( getTicket ) {
222
+ if ( ticket.status == 'closed' ) {
223
+ getTicket.ticketActivity.push( {
224
+ actionType: 'dataRecived',
225
+ timeStamp: new Date(),
226
+ actionBy: 'Tango',
227
+ } );
228
+ }
229
+ await updateOneTangoTicket( { ticketId: ticket.ticketId },
230
+ {
231
+ status: ticket.status,
232
+ ticketActivity: getTicket.ticketActivity,
233
+ issueClosedDate: new Date(),
234
+ },
235
+ );
236
+ let downTimeQuery = {
237
+ 'size': 1,
238
+ 'query': {
239
+ 'bool': {
240
+ 'must': [
241
+ {
242
+ 'term': {
243
+ 'doc.date.keyword': dayjs( getTicket.issueDate ).format( 'DD-MM-YYYY' ),
244
+ },
239
245
  },
240
- },
241
- {
242
- 'term': {
243
- 'doc.store_id.keyword': getTicket.basicDetails.storeId,
246
+ {
247
+ 'term': {
248
+ 'doc.store_id.keyword': getTicket.basicDetails.storeId,
249
+ },
244
250
  },
245
- },
246
251
 
247
- ],
252
+ ],
248
253
 
254
+ },
249
255
  },
250
- },
251
- };
252
- let downtimetotal;
253
- const downtime = await getOpenSearchData( 'live_downtime_hourly', downTimeQuery );
254
- let streamwiseDowntime = downtime.body.hits.hits.length > 0 ? downtime.body.hits.hits[0]._source.doc.streamwise_downtime : [];
255
- if ( streamwiseDowntime.length > 0 ) {
256
- const sum = streamwiseDowntime.reduce( ( accumulator, currentValue ) => {
257
- return accumulator + currentValue.down_time;
258
- }, 0 );
259
- const average = sum / streamwiseDowntime.length;
260
- downtimetotal = Math.round( average );
261
- } else {
262
- downtimetotal = 0;
263
- }
256
+ };
257
+ let downtimetotal;
258
+ const downtime = await getOpenSearchData( 'live_downtime_hourly', downTimeQuery );
259
+ let streamwiseDowntime = downtime.body.hits.hits.length > 0 ? downtime.body.hits.hits[0]._source.doc.streamwise_downtime : [];
260
+ if ( streamwiseDowntime.length > 0 ) {
261
+ const sum = streamwiseDowntime.reduce( ( accumulator, currentValue ) => {
262
+ return accumulator + currentValue.down_time;
263
+ }, 0 );
264
+ const average = sum / streamwiseDowntime.length;
265
+ downtimetotal = Math.round( average );
266
+ } else {
267
+ downtimetotal = 0;
268
+ }
264
269
 
265
- let Issue = getTicket.ticketActivity.filter( ( a ) => a.actionType == 'issueUpdate' );
266
- let primaryIssue = '';
270
+ let Issue = getTicket.ticketActivity.filter( ( a ) => a.actionType == 'issueUpdate' );
271
+ let primaryIssue = '';
267
272
 
268
- if ( Issue.length > 0 && Issue[0].reasons.length > 0 ) {
269
- primaryIssue = Issue[0].reasons[0].primaryIssue;
270
- }
271
- let Timestamp = dayjs().format( 'YYYY-MM-DD HH:mm' );
272
- const attachments = null;
273
- const subject = `Tango Eye - Infra Ticket Closed for ${getTicket.basicDetails.storeName} `;
274
- const fileContent = readFileSync( join() + '/src/hbs/closeTicekt.hbs', 'utf8' );
275
- const htmlContent = handlebars.compile( fileContent );
276
- let store = await findOneStore( { storeId: getTicket.basicDetails.storeId } );
277
- if ( store.spocDetails && store.spocDetails.length > 0 ) {
278
- let spocEmail = store.spocDetails[0].email;
279
- let spocName = store.spocDetails[0].name;
280
- let Uidomain = `${appConfig.url.domain}/manage/stores/infra-ticket?storeId=${getTicket.basicDetails.storeId}`;
281
- const html = htmlContent( { ...getTicket, Uidomain: Uidomain, primaryIssue: primaryIssue, storeName: getTicket.basicDetails.storeName, spocName: spocName, date: dayjs( getTicket.issueDate ).format( 'YYYY-MM-DD HH:mm' ), downtimetotal: downtimetotal, Timestamp: Timestamp, domain: appConfig.url.apiDomain } );
282
- await sendEmailWithSES( spocEmail, subject, html, attachments, appConfig.cloud.aws.ses.adminEmail );
273
+ if ( Issue.length > 0 && Issue[0].reasons.length > 0 ) {
274
+ primaryIssue = Issue[0].reasons[0].primaryIssue;
275
+ }
276
+ let Timestamp = dayjs().format( 'YYYY-MM-DD HH:mm' );
277
+ const attachments = null;
278
+ const subject = `Tango Eye - Infra Ticket Closed for ${getTicket.basicDetails.storeName} `;
279
+ const fileContent = readFileSync( join() + '/src/hbs/closeTicekt.hbs', 'utf8' );
280
+ const htmlContent = handlebars.compile( fileContent );
281
+ let store = await findOneStore( { storeId: getTicket.basicDetails.storeId } );
282
+ if ( store.spocDetails && store.spocDetails.length > 0 ) {
283
+ let spocEmail = store.spocDetails[0].email;
284
+ let spocName = store.spocDetails[0].name;
285
+ let Uidomain = `${appConfig.url.domain}/manage/stores/infra-ticket?storeId=${getTicket.basicDetails.storeId}`;
286
+ let getclient = await findOneClient( { clientId: getTicket.basicDetails.clientId } );
287
+ const html = htmlContent( { ...getTicket, Uidomain: Uidomain, primaryIssue: primaryIssue, storeName: getTicket.basicDetails.storeName, spocName: spocName, date: dayjs( getTicket.issueDate ).format( 'YYYY-MM-DD HH:mm' ), downtimetotal: downtimetotal, Timestamp: Timestamp, domain: appConfig.url.apiDomain } );
288
+ if ( getclient.ticketConfigs.emailAlert && spocEmail && isValidEmail( spocEmail ) ) {
289
+ await sendEmailWithSES( spocEmail, subject, html, attachments, appConfig.cloud.aws.ses.adminEmail );
290
+ }
291
+ }
283
292
  }
284
293
  }
285
294
 
@@ -289,6 +298,10 @@ export async function closeTicket( req, res ) {
289
298
  res.sendError( error, 500 );
290
299
  }
291
300
  }
301
+ function isValidEmail( email ) {
302
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
303
+ return emailRegex.test( email );
304
+ }
292
305
  export async function emailUserList( req, res ) {
293
306
  try {
294
307
  let clientList = await aggregateClient( [
@@ -433,11 +446,11 @@ export async function emailUserList( req, res ) {
433
446
  export async function infraReportSent( req, res ) {
434
447
  try {
435
448
  let date;
436
- if ( req.body.type == 'start' ) {
437
- date = dayjs().subtract( 1, 'day' ).format( 'YYYY-MM-DD' );
438
- } else if ( req.body.type == 'end' ) {
439
- date = dayjs().format( 'YYYY-MM-DD' );
440
- };
449
+ // if ( req.body.type == 'start' ) {
450
+ // date = dayjs().subtract( 1, 'day' ).format( 'YYYY-MM-DD' );
451
+ // } else if ( req.body.type == 'end' ) {
452
+ date = dayjs().format( 'YYYY-MM-DD' );
453
+ // };
441
454
  let query = [ {
442
455
  $match: {
443
456
  $and: [
@@ -455,6 +468,13 @@ export async function infraReportSent( req, res ) {
455
468
  status: 1,
456
469
  ticketDetails: 1,
457
470
  issueClosedDate: 1,
471
+ otherscomment: {
472
+ $filter: {
473
+ input: '$ticketActivity',
474
+ as: 'item',
475
+ cond: { $eq: [ '$$item.actionType', 'comment' ] },
476
+ },
477
+ },
458
478
  primaryIssue: {
459
479
  $filter: {
460
480
  input: '$ticketActivity',
@@ -464,6 +484,11 @@ export async function infraReportSent( req, res ) {
464
484
  },
465
485
  },
466
486
  },
487
+ {
488
+ $unwind: {
489
+ path: '$otherscomment', preserveNullAndEmptyArrays: true,
490
+ },
491
+ },
467
492
  {
468
493
  $unwind: {
469
494
  path: '$primaryIssue', preserveNullAndEmptyArrays: true,
@@ -487,6 +512,8 @@ export async function infraReportSent( req, res ) {
487
512
  status: 1,
488
513
  ticketDetails: 1,
489
514
  issueClosedDate: 1,
515
+ ommentText: { $ifNull: [ '$primaryIssue.comment', '-' ] },
516
+ otherscomment: { $ifNull: [ '$otherscomment.comment', '-' ] },
490
517
  primaryIssue: { $ifNull: [ '$primaryIssue.reasons.primaryIssue', '-' ] },
491
518
  secondaryIssue: { $ifNull: [ '$primaryIssue.reasons.secondaryIssue.name', '-' ] },
492
519
  },
@@ -497,11 +524,12 @@ export async function infraReportSent( req, res ) {
497
524
  ticketDetails: { $first: '$ticketDetails' },
498
525
  issueClosedDate: { $first: '$issueClosedDate' },
499
526
  basicDetails: { $first: '$basicDetails' },
527
+ commentText: { $last: '$commentText' },
500
528
  primaryIssue: { $last: '$primaryIssue' },
501
529
  secondaryIssue: { $last: '$secondaryIssue' },
530
+ otherscomment: { $last: '$otherscomment' },
502
531
  createdAt: { $first: '$createdAt' },
503
532
  status: { $last: '$status' },
504
-
505
533
  },
506
534
  },
507
535
  ];
@@ -514,18 +542,20 @@ export async function infraReportSent( req, res ) {
514
542
  exportdata.push( {
515
543
  'Client ID': element.basicDetails.clientId,
516
544
  'Client Name': element.basicDetails.clientName,
517
- 'Created Date & Time': dayjs( element.createdAt ).format( 'YYYY-MM-DD HH:mm A' ),
518
- 'Store ID': element.basicDetails.clientName,
519
- 'Store Name': element.basicDetails.clientName,
545
+ 'Ticket Created Date & Time': dayjs( element.createdAt ).tz( 'Asia/Kolkata' ).format( 'YYYY-MM-DD HH:mm A' ),
546
+ 'Store ID': element.basicDetails.storeId,
547
+ 'Store Name': element.basicDetails.storeName,
520
548
  'Issue ': element.primaryIssue,
521
549
  'Secondary Issue': element.secondaryIssue,
522
- 'Issue Date & Time': dayjs( element.date ).format( 'YYYY-MM-DD' ),
523
550
  'Status ': element.status,
524
551
  'Responded By': clientuser && clientuser.userName ? clientuser.userName : '-',
525
552
  'Resolved By': tangouser && tangouser.userName ? tangouser.userName : '-',
526
- 'Closed Date & Time': element.issueClosedDate?dayjs( element.issueClosedDate ).format( 'YYYY-MM-DD HH:mm A' ):'-',
527
- 'Latest Comment': '',
528
- 'Activity Log': '',
553
+ 'Ticket Closed Date & Time': element.issueClosedDate ? dayjs( element.issueClosedDate ).tz( 'Asia/Kolkata' ).format( 'YYYY-MM-DD HH:mm A' ) : '-',
554
+ 'Latest Comment': element.otherscomment?element.otherscomment:element.commentText,
555
+ 'Activity Log': {
556
+ label: 'Link',
557
+ url: `${appConfig.url.domain+'/manage/stores/infra-ticket?storeId='+element.basicDetails.storeId}`,
558
+ },
529
559
  } );
530
560
  }
531
561
 
@@ -537,7 +567,7 @@ export async function infraReportSent( req, res ) {
537
567
  ],
538
568
  } );
539
569
  let client = await findOneClient( { clientId: req.body.clientId } );
540
- let avgDownTime = client.ticketConfigs.infraDownTime*60;
570
+ let avgDownTime = client.ticketConfigs.infraDownTime * 60;
541
571
  let issueList = await findinfraReason( { parentId: { '$exists': false } } );
542
572
  const categoryCounts = {};
543
573
  let response;
@@ -561,31 +591,14 @@ export async function infraReportSent( req, res ) {
561
591
  } ) );
562
592
  }
563
593
  let reportdate = dayjs().format( 'YYYY-MM-DD' );
564
- let attachments= null;
565
- if ( exportdata.length>0 ) {
566
- const wb = new xl.Workbook();
567
- const ws = wb.addWorksheet( 'Daily report' );
568
- const headers = Object.keys( exportdata[0] );
569
-
570
- for ( let i = 0; i < headers.length; i++ ) {
571
- ws.cell( 1, i + 1 ).string( headers[i] );
572
- };
573
- for ( let i = 0; i < exportdata.length; i++ ) {
574
- const dataRow = exportdata[i];
575
- for ( let j = 0; j < headers.length; j++ ) {
576
- const header = headers[j];
577
- const value = dataRow[header];
578
- ws.cell( i + 2, j + 1 ).string( value?.toString() );
579
- }
580
- }
581
- let buffer = await wb.writeToBuffer();
594
+ let attachments = null;
595
+ let buffer = await download( exportdata );
596
+ attachments = {
597
+ filename: `dailyInfraReport- ${reportdate}.xlsx`,
598
+ content: buffer,
599
+ contentType: 'application/xlsx', // e.g., 'application/pdf'
600
+ };
582
601
 
583
- attachments = {
584
- filename: `dailyInfraReport- ${reportdate}.xlsx`,
585
- content: buffer,
586
- contentType: 'application/xlsx', // e.g., 'application/pdf'
587
- };
588
- }
589
602
 
590
603
  const subject = `Daily Digest - Infra Downtime Report - ${reportdate}`;
591
604
  const fileContent = readFileSync( join() + '/src/hbs/dailyInfraReport.hbs', 'utf8' );
@@ -594,10 +607,451 @@ export async function infraReportSent( req, res ) {
594
607
 
595
608
 
596
609
  const html = htmlContent( { ...req.body, Uidomain: Uidomain, issueCount: issueCount, avgDownTime: avgDownTime, reportdate: reportdate, content: response, date: date, domain: appConfig.url.apiDomain } );
597
- const result = await sendEmailWithSES( req.body.email, subject, html, attachments, appConfig.cloud.aws.ses.adminEmail );
598
- res.sendSuccess( result );
610
+ if ( isValidEmail( req.body.email ) ) {
611
+ const result = await sendEmailWithSES( req.body.email, subject, html, attachments, appConfig.cloud.aws.ses.adminEmail );
612
+ res.sendSuccess( result );
613
+ }
599
614
  } catch ( error ) {
600
615
  logger.error( { error: error, function: 'infraReportSent' } );
601
616
  res.sendError( error, 500 );
602
617
  }
603
618
  }
619
+ export async function spocmailchange() {
620
+ let storelist = await findStore( {} );
621
+ for ( let store of storelist ) {
622
+ if ( store && store.spocDetails ) {
623
+ let spoclist = [];
624
+ for ( let spoc of store.spocDetails ) {
625
+ spoc.email = spoc.email.replace( '@', '1@' ),
626
+ spoclist.push( spoc );
627
+ }
628
+ await updateOneStore( { storeId: store.storeId }, { spocDetails: spoclist } );
629
+ }
630
+ }
631
+ }
632
+
633
+
634
+ export async function camAngleChangeReport( req, res ) {
635
+ try {
636
+ const currentDate = dayjs();
637
+
638
+
639
+ const previousDay = currentDate.subtract( 1, 'day' );
640
+
641
+
642
+ const formattedPreviousDay = previousDay.format( 'DD-MM-YYYY' );
643
+ const angleChange = await getOpenSearchData( 'camera-angle-change',
644
+ {
645
+ 'query': {
646
+ 'bool': {
647
+ 'must': [
648
+ {
649
+ 'term': {
650
+ 'date.keyword': formattedPreviousDay,
651
+ },
652
+ },
653
+ {
654
+ 'term': {
655
+ 'client_id.keyword': req.body.clientId,
656
+ },
657
+ },
658
+ ],
659
+ },
660
+ },
661
+ } );
662
+ if ( angleChange.body.hits.hits.length > 0 ) {
663
+ const exportdata = [];
664
+
665
+ for ( let camera of angleChange.body.hits.hits ) {
666
+ let result = camera._source;
667
+
668
+ if ( result && result.cameraAngleChangeStatus && result.camera_info.length > 0 ) {
669
+ for ( let stream of result.camera_info ) {
670
+ let params = {
671
+ Bucket: appConfig.cloud.aws.bucket.cameraAngle,
672
+ file_path: stream.path,
673
+ };
674
+ let Image = await signedUrl( params );
675
+
676
+
677
+ let findStore = await findOneStore( { storeId: result.store_id } );
678
+
679
+
680
+ exportdata.push( {
681
+ 'Store Name': findStore.storeName,
682
+ 'Store Code': findStore.storeId,
683
+ 'Date Changed': formattedPreviousDay,
684
+ 'ImageURL': {
685
+ url: Image,
686
+ label: 'Angle changed Image',
687
+ },
688
+ } );
689
+ }
690
+ }
691
+ }
692
+ let buffer = await download( exportdata );
693
+
694
+ let attachments = {
695
+ filename: `Camera angle change- ${formattedPreviousDay}.xlsx`,
696
+ content: buffer,
697
+ contentType: 'application/xlsx', // e.g., 'application/pdf'
698
+ };
699
+ let subject = `Camera Angle Modified - ${formattedPreviousDay}`;
700
+ let html = `<div>We wanted to inform you that the camera angle in your stores has been adjusted recently.</div>`;
701
+ let result = await sendEmailWithSES( req.body.toMail, subject, html, attachments, appConfig.cloud.aws.ses.adminEmail );
702
+ if ( result ) {
703
+ res.sendSuccess( 'Email send successfully' );
704
+ }
705
+ } else {
706
+ res.sendSuccess( 'No changes in camera Angle' );
707
+ }
708
+ } catch ( error ) {
709
+ logger.error( { error: error, function: 'camAngleChangeList' } );
710
+ res.sendError( error, 500 );
711
+ }
712
+ }
713
+
714
+
715
+ export async function download( data ) {
716
+ const wb = new xl.Workbook();
717
+ const ws = wb.addWorksheet( 'Worksheet Name' );
718
+ const headers = Object.keys( data[0] );
719
+
720
+ for ( let i = 0; i < headers.length; i++ ) {
721
+ ws.cell( 1, i + 1 ).string( headers[i] );
722
+ };
723
+ for ( let i = 0; i < data.length; i++ ) {
724
+ const dataRow = data[i];
725
+ for ( let j = 0; j < headers.length; j++ ) {
726
+ const header = headers[j];
727
+ const value = dataRow[header];
728
+ if ( value&& typeof value === 'object' && value.hasOwnProperty( 'label' ) && value.hasOwnProperty( 'url' ) ) {
729
+ ws.cell( i + 2, j + 1 ).link( value.url );
730
+ ws.cell( i + 2, j + 1 ).string( value.label );
731
+ } else {
732
+ ws.cell( i + 2, j + 1 ).string( value?.toString() );
733
+ }
734
+ }
735
+ }
736
+ return await wb.writeToBuffer();
737
+ }
738
+ export async function edgeApplogsCheck( req, res ) {
739
+ try {
740
+ let finalresult= [];
741
+ let ticketList = await findTangoTicket( { 'issueDate': new Date( req.body.issueDate ), 'status': { $ne: 'closed' }, 'ticketDetails.issueStatus': 'notidentified', 'issueType': 'infra' } );
742
+ for ( let ticket of ticketList ) {
743
+ req.body.date = dayjs( ticket.createdAt ).format( 'YYYY-MM-DD' );
744
+ let errorLog = {
745
+ 'size': 500,
746
+ 'query': {
747
+ 'bool': {
748
+ 'must': [
749
+ {
750
+ 'range': {
751
+ 'log_code': {
752
+ 'gte': 1000,
753
+ },
754
+ },
755
+ },
756
+ {
757
+ 'term': {
758
+ 'store_date.keyword': dayjs( ticket.createdAt ).format( 'DD-MM-YYYY' ),
759
+ },
760
+ },
761
+ {
762
+ 'term': {
763
+ 'storeId.keyword': ticket.basicDetails.storeId,
764
+ },
765
+ },
766
+
767
+ ],
768
+
769
+ },
770
+ },
771
+ 'sort': [
772
+ { 'timestamp': { 'order': 'desc' } },
773
+ ],
774
+ };
775
+ const errorLogList = await getOpenSearchData( 'edgeapp_systemlogs', errorLog );
776
+
777
+ let result = [];
778
+
779
+ for ( let error of errorLogList.body.hits.hits ) {
780
+ if ( error._source.log_code == '1003' ) {
781
+ let logCheck = {
782
+ code: error._source.log_code,
783
+ edgelog: error._source.data,
784
+ };
785
+ result.push( logCheck );
786
+ }
787
+ // if ( error._source.log_code == '1001' ) {
788
+ // let logCheck = {
789
+ // code: error._source.log_code,
790
+ // edgelog: error._source.data,
791
+ // };
792
+ // result.push( logCheck );
793
+ // }
794
+
795
+ // if ( error._source.log_code == '1004' ) {
796
+ // let logCheck = {
797
+ // code: error._source.log_code,
798
+ // edgelog: error._source.data,
799
+
800
+ // };
801
+ // result.push( logCheck );
802
+ // }
803
+ if ( error._source.log_code == '1005' ) {
804
+ const bytes = error._source.data.upload_Speed.split( '.' )[0];
805
+ const megabytes = bytesToMB( bytes );
806
+ if ( megabytes < 2 ) {
807
+ let logCheck = {
808
+ code: error._source.log_code,
809
+ edgelog: error._source.data,
810
+
811
+ };
812
+ result.push( logCheck );
813
+ }
814
+ }
815
+
816
+
817
+ if ( error._source.log_code == '1011' ) {
818
+ let logCheck = {
819
+ code: error._source.log_code,
820
+ edgelog: error._source.data,
821
+
822
+ };
823
+ result.push( logCheck );
824
+ }
825
+ if ( error._source.log_code == '1022' ) {
826
+ let logCheck = {
827
+ code: error._source.log_code,
828
+ edgelog: error._source.data,
829
+
830
+ };
831
+ result.push( logCheck );
832
+ }
833
+ if ( error._source.log_code == '1024' ) {
834
+ let logCheck = {
835
+ code: error._source.log_code,
836
+ edgelog: error._source.data,
837
+
838
+ };
839
+ result.push( logCheck );
840
+ }
841
+ if ( error._source.log_code == '1025' ) {
842
+ let logCheck = {
843
+ code: error._source.log_code,
844
+ edgelog: error._source.data,
845
+
846
+ };
847
+ result.push( logCheck );
848
+ }
849
+
850
+ if ( error._source.log_code == '1000' ) {
851
+ let logCheck = {
852
+ code: error._source.log_code,
853
+ edgelog: error._source.data,
854
+
855
+ };
856
+ result.push( logCheck );
857
+ }
858
+
859
+ if ( error._source.log_code == '1007' ) {
860
+ let logCheck = {
861
+ code: error._source.log_code,
862
+ edgelog: error._source.data,
863
+
864
+ };
865
+ result.push( logCheck );
866
+ }
867
+ if ( error._source.log_code == '1034' ) {
868
+ let logCheck = {
869
+ code: error._source.log_code,
870
+ edgelog: error._source.data,
871
+
872
+ };
873
+ result.push( logCheck );
874
+ }
875
+
876
+ if ( Number( error._source.log_code ) > 2000 ) {
877
+ let logCheck = {
878
+ code: error._source.log_code,
879
+ edgelog: error._source.data,
880
+ };
881
+ result.push( logCheck );
882
+ }
883
+ }
884
+ let findissueEdgeApp = {};
885
+ if ( result.length>0 ) {
886
+ for ( let findissue of result ) {
887
+ const istTimestamp = dayjs.utc( ticket.createdAt ).tz( 'Asia/Kolkata' ).format( 'HH:mm:ss' );
888
+
889
+ if ( findissue.code == '1003' ) {
890
+ const occurringTimeParsed = dayjs( `${req.body.date} ${findissue.edgelog.occuringTime}` );
891
+ const ticketCreatedParsed = dayjs( `${req.body.date} ${istTimestamp}` );
892
+ const isOccurringTimeEarlier = occurringTimeParsed.isBefore( ticketCreatedParsed );
893
+ if ( isOccurringTimeEarlier ) {
894
+ findissueEdgeApp = {
895
+ ticketId: ticket.ticketId,
896
+ edgelog: findissue,
897
+ storeId: ticket.basicDetails.storeId,
898
+ primary: 'System Issues',
899
+ secondary: [ 'Malware or Viruses' ],
900
+ };
901
+ updateIssue( findissueEdgeApp );
902
+ finalresult.push( findissueEdgeApp );
903
+ }
904
+ } else if ( findissue.code == '1024' ) {
905
+ const occurringTimeParsed = dayjs( `${req.body.date} ${findissue.edgelog.data.occuringTime}` );
906
+ const ticketCreatedParsed = dayjs( `${req.body.date} ${istTimestamp}` );
907
+ const isOccurringTimeEarlier = occurringTimeParsed.isBefore( ticketCreatedParsed );
908
+
909
+ const existsInArray = finalresult.some( ( item ) =>
910
+ item.ticketId === ticket.ticketId,
911
+ );
912
+ if ( isOccurringTimeEarlier&&!existsInArray ) {
913
+ findissueEdgeApp = {
914
+ ticketId: ticket.ticketId,
915
+ storeId: ticket.basicDetails.storeId,
916
+ edgelog: findissue.edgelog.data,
917
+ primary: 'Camera Issues',
918
+ secondary: [ 'Camera Not working/RTSP port not working' ],
919
+ };
920
+ updateIssue( findissueEdgeApp );
921
+ finalresult.push( findissueEdgeApp );
922
+ }
923
+ } else if ( findissue.code == '1011' ) {
924
+ const occurringTimeParsed = dayjs( `${req.body.date} ${findissue.edgelog.occuringTime}` );
925
+ const ticketCreatedParsed = dayjs( `${req.body.date} ${istTimestamp}` );
926
+ const isOccurringTimeEarlier = occurringTimeParsed.isBefore( ticketCreatedParsed );
927
+
928
+ if ( isOccurringTimeEarlier ) {
929
+ findissueEdgeApp = {
930
+ ticketId: ticket.ticketId,
931
+ edgelog: findissue,
932
+ storeId: ticket.basicDetails.storeId,
933
+ primary: 'System Issues',
934
+ secondary: [ 'System is in Sleep Mode' ],
935
+ };
936
+ updateIssue( findissueEdgeApp );
937
+ finalresult.push( findissueEdgeApp );
938
+ }
939
+ } else if ( findissue.code ==='1005' ) {
940
+ const occurringTimeParsed = dayjs( `${req.body.date} ${findissue.edgelog.occuringTime}` );
941
+ const ticketCreatedParsed = dayjs( `${req.body.date} ${istTimestamp}` );
942
+ const isOccurringTimeEarlier = occurringTimeParsed.isBefore( ticketCreatedParsed );
943
+ if ( isOccurringTimeEarlier ) {
944
+ findissueEdgeApp = {
945
+ ticketId: ticket.ticketId,
946
+ edgelog: findissue,
947
+ storeId: ticket.basicDetails.storeId,
948
+ primary: 'Internet Issues',
949
+ secondary: [ 'Slow Internet Speed' ],
950
+ };
951
+ updateIssue( findissueEdgeApp );
952
+ finalresult.push( findissueEdgeApp );
953
+ }
954
+ }
955
+ }
956
+ }
957
+ }
958
+ res.sendSuccess( { count: finalresult.length, result: finalresult } );
959
+ } catch ( error ) {
960
+ logger.error( { error: error, function: 'camAngleChangeList' } );
961
+ res.sendError( error, 500 );
962
+ }
963
+ }
964
+ function bytesToMB( bytes ) {
965
+ return bytes / ( 1024 * 1024 );
966
+ }
967
+
968
+ export async function updateIssue( data ) {
969
+ try {
970
+ let Ticket = await findOneTangoTicket(
971
+ {
972
+ ticketId: data.ticketId,
973
+ },
974
+ );
975
+
976
+ data.issueType = Ticket.issueType;
977
+ data.basicDetails = Ticket.basicDetails;
978
+ data.ticketDetails = Ticket.ticketDetails;
979
+ data.ticketActivity = Ticket.ticketActivity;
980
+ if ( data.primary && data.secondary && data.secondary.length ) {
981
+ let primaryReason = await findOneinfraReason( { name: data.primary } );
982
+ if ( !primaryReason ) {
983
+ return res.sendError( 'Primary Reason Not exists in database', 500 );
984
+ }
985
+ const secondary = [];
986
+ const steptoReslove = [];
987
+ for ( let i = 0; i < data.secondary.length; i++ ) {
988
+ let secondaryReason = await findOneinfraReason( { name: data.secondary[i] } );
989
+ if ( !secondaryReason ) {
990
+ return res.sendError( `secondary Reason - ${data.secondary[i]} Not exists in database`, 500 );
991
+ }
992
+ secondary.push( {
993
+ name: secondaryReason.name,
994
+ } );
995
+ let resolveSteps = [];
996
+ for ( let i = 0; i < secondaryReason.stepstoResolve.length; i++ ) {
997
+ resolveSteps.push( {
998
+ name: secondaryReason.stepstoResolve[i].name,
999
+ } );
1000
+ }
1001
+ steptoReslove.push( {
1002
+ primaryIssue: secondaryReason.name,
1003
+ secondaryIsssue: [ ...resolveSteps ],
1004
+ } );
1005
+ }
1006
+
1007
+ data.ticketActivity.push( {
1008
+ actionType: 'issueUpdate',
1009
+ actionBy: 'automated',
1010
+ IdentifiedBy: 'Tango',
1011
+ timeStamp: new Date(),
1012
+ reasons: [ {
1013
+ primaryIssue: primaryReason.name,
1014
+ secondaryIssue: secondary,
1015
+ } ],
1016
+ },
1017
+ );
1018
+ } else {
1019
+ if ( data.primary == 'Application Issues' ) {
1020
+ data.ticketActivity.push( {
1021
+ actionType: 'issueUpdate',
1022
+ actionBy: 'automated',
1023
+ timeStamp: new Date(),
1024
+ IdentifiedBy: 'Tango',
1025
+ reasons: [ {
1026
+ primaryIssue: data.primary,
1027
+ secondaryIssue: [],
1028
+ } ],
1029
+ },
1030
+ );
1031
+ }
1032
+ }
1033
+ if ( data.issueType == 'infra' ) {
1034
+ let client = await findOneClient( { clientId: data.basicDetails.clientId }, { ticketConfigs: 1 } );
1035
+ let statusCheckAlertTime = dayjs().add( client.ticketConfigs.statusCheckAlert, 'hours' ).format( 'YYYY-MM-DD hh:mm' );
1036
+ data.ticketActivity.push( {
1037
+ actionType: 'statusCheck',
1038
+ timeStamp: statusCheckAlertTime,
1039
+ actionBy: 'Tango',
1040
+ IdentifiedBy: 'Tango',
1041
+ timeStamp: new Date(),
1042
+ statusCheckAlertTime: statusCheckAlertTime,
1043
+ } );
1044
+ }
1045
+ let query = {
1046
+ 'ticketActivity': data.ticketActivity,
1047
+ 'ticketDetails.issueIdentifiedDate': new Date(),
1048
+ 'ticketDetails.issueStatus': 'identified',
1049
+ 'status': 'inprogress',
1050
+ };
1051
+ await updateOneTangoTicket( { ticketId: data.ticketId }, query );
1052
+ } catch ( error ) {
1053
+ logger.error( { error: error, function: 'updateAutomaticIssue' } );
1054
+ res.sendError( error, 500 );
1055
+ }
1056
+ }
1057
+