tango-app-api-infra 3.1.21 → 3.1.22

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-infra",
3
- "version": "3.1.21",
3
+ "version": "3.1.22",
4
4
  "description": "infra",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -6,12 +6,18 @@ import { findinfraReason } from '../services/infraReason.service.js';
6
6
  import { aggregateClient, findClient } from '../services/client.service.js';
7
7
  import dayjs from 'dayjs';
8
8
  import { getOpenSearchData } from 'tango-app-api-middleware';
9
+ import { aggregateUserAssignedStore, findUserAssignedStore } from '../services/userAssignedStore.service.js';
10
+
9
11
 
10
12
  export async function infraCard( req, res ) {
11
13
  try {
12
14
  let date = await getUTC( new Date( req.body.fromDate ), new Date( req.body.toDate ) );
13
- let storeCount = await countDocumentsStore( { 'clientId': { $in: req.body.clientId }, 'createdAt': { $lte: date.end }, 'status': 'active' } );
14
- let yettoInstallCount = await aggregateTangoTicket( [
15
+ let storecountQuery ={ 'clientId': { $in: req.body.clientId }, 'createdAt': { $lte: date.end }, 'status': 'active' };
16
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
17
+ storecountQuery = { ...storecountQuery, ...{ storeId: { $in: req.body.permittedStores } } };
18
+ }
19
+ let storeCount = await countDocumentsStore( storecountQuery );
20
+ let yetToInstallQuery = [
15
21
  {
16
22
  $match: {
17
23
  $and: [
@@ -22,18 +28,38 @@ export async function infraCard( req, res ) {
22
28
  ],
23
29
  },
24
30
  },
25
- ] );
31
+ ];
32
+
33
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
34
+ yetToInstallQuery.push(
35
+ {
36
+ $match: {
37
+ 'basicDetails.storeId': { $in: req.body.permittedStores },
38
+ },
39
+ },
40
+ );
41
+ }
42
+ let yettoInstallCount = await aggregateTangoTicket( yetToInstallQuery );
26
43
  storeCount = storeCount-yettoInstallCount.length;
27
- let totalstores = await aggregateStore( [ {
44
+ let totalStoreQuery =[ {
28
45
  $match: {
29
46
  '$and': [
30
47
  { 'clientId': { $in: req.body.clientId } },
31
- { 'edge.firstFile': true },
32
48
  { 'status': 'active' },
33
49
  { 'createdAt': { $lte: date.end } },
34
50
  ],
35
51
  },
36
- } ] );
52
+ } ];
53
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
54
+ totalStoreQuery.push(
55
+ {
56
+ $match: {
57
+ 'storeId': { $in: req.body.permittedStores },
58
+ },
59
+ },
60
+ );
61
+ }
62
+ let totalstores = await aggregateStore( totalStoreQuery );
37
63
  let totalnumberstores = [];
38
64
  for ( let store of totalstores ) {
39
65
  totalnumberstores.push( store.storeId );
@@ -46,7 +72,6 @@ export async function infraCard( req, res ) {
46
72
  { status: { $ne: 'closed' } },
47
73
  { 'basicDetails.clientId': { $in: req.body.clientId } },
48
74
  { 'basicDetails.storeId': { $in: totalnumberstores } },
49
- // { createdAt: { $gte: date.start } },
50
75
  { createdAt: { $lte: date.end } },
51
76
  ],
52
77
  },
@@ -83,7 +108,6 @@ export async function infraCard( req, res ) {
83
108
  { 'ticketDetails.issueStatus': 'identified' },
84
109
  { 'basicDetails.clientId': { $in: req.body.clientId } },
85
110
  { 'basicDetails.storeId': { $in: totalnumberstores } },
86
- // { createdAt: { $gte: date.start } },
87
111
  { createdAt: { $lte: date.end } },
88
112
  ],
89
113
  },
@@ -178,7 +202,7 @@ export async function infraCard( req, res ) {
178
202
  } );
179
203
  res.sendSuccess( {
180
204
  total: storeCount,
181
- liveStoreCount: infraStoreCount.length > 0 ? storeCount - infraStoreCount[0].infraCount : 0,
205
+ liveStoreCount: infraStoreCount.length > 0 ? storeCount - infraStoreCount[0].infraCount : storeCount,
182
206
  infraStoreCount: infraStoreCount.length > 0 ? infraStoreCount[0].infraCount : 0,
183
207
  identifiedcount: infraStoreCount.length > 0 ? infraStoreCount[0].identifiedcount : 0,
184
208
  notidentifiedcount: infraStoreCount.length > 0 ? infraStoreCount[0].infraCount - infraStoreCount[0].identifiedcount : 0,
@@ -192,23 +216,22 @@ export async function infraCard( req, res ) {
192
216
  export async function installationCard( req, res ) {
193
217
  try {
194
218
  let date = await getUTC( new Date( req.body.fromDate ), new Date( req.body.toDate ) );
195
- let onboardedCount = await countDocumentsStore( {
196
- '$and': [
197
- { 'clientId': { $in: req.body.clientId } },
198
- { 'createdAt': { $lte: date.end } },
199
- ],
200
- } );
201
- let installedCount = await countDocumentsStore( {
202
- 'clientId': { $in: req.body.clientId },
203
- 'status': 'active',
204
- 'createdAt': { $lte: date.end },
205
- } );
206
- let deactiveCount = await countDocumentsStore( {
207
- 'clientId': { $in: req.body.clientId },
208
- 'status': 'deactive',
209
- 'createdAt': { $lte: date.end },
210
- } );
211
- let yettoInstallCount = await aggregateTangoTicket( [
219
+ let baseQuery = {
220
+ clientId: { $in: req.body.clientId },
221
+ createdAt: { $lte: date.end },
222
+ };
223
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
224
+ baseQuery = { ...baseQuery, ...{ storeId: { $in: req.body.permittedStores } } };
225
+ }
226
+ let onboardQuery = { ...baseQuery };
227
+ let installedQuery = { ...baseQuery, status: 'active' };
228
+ let deactiveQuery = { ...baseQuery, status: 'deactive' };
229
+ let onboardedCount = await countDocumentsStore( onboardQuery );
230
+ let installedCount = await countDocumentsStore( installedQuery );
231
+ let deactiveCount = await countDocumentsStore( deactiveQuery );
232
+
233
+
234
+ let ticketbaseQuery = [
212
235
  {
213
236
  $match: {
214
237
  $and: [
@@ -219,16 +242,21 @@ export async function installationCard( req, res ) {
219
242
  ],
220
243
  },
221
244
  },
222
- ] );
223
- let query = [ {
245
+ ];
246
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
247
+ ticketbaseQuery.push(
248
+ {
249
+ $match: {
250
+ 'basicDetails.storeId': { $in: req.body.permittedStores },
251
+ },
252
+ },
253
+ );
254
+ }
255
+ let yettoInstallCount = await aggregateTangoTicket( ticketbaseQuery );
256
+ let query = ticketbaseQuery;
257
+ query.push( {
224
258
  $match: {
225
- $and: [
226
- { issueType: 'installation' },
227
- { 'status': { $ne: 'closed' } },
228
- { 'basicDetails.clientId': { $in: req.body.clientId } },
229
- { 'ticketDetails.issueStatus': 'identified' },
230
- { createdAt: { $lte: date.end } },
231
- ],
259
+ 'ticketDetails.issueStatus': 'identified',
232
260
  },
233
261
  },
234
262
  {
@@ -272,8 +300,8 @@ export async function installationCard( req, res ) {
272
300
  issueIdentifiedBy: { $last: '$issueIdentifiedBy' },
273
301
  primaryIssue: { $last: '$primaryIssue' },
274
302
  },
275
- },
276
- ];
303
+ } );
304
+
277
305
  let installFailedCount = await aggregateTangoTicket( query );
278
306
 
279
307
  let issueList = await findinfraReason( { parentId: { '$exists': false } } );
@@ -323,8 +351,7 @@ export async function infraIssuesTable( req, res ) {
323
351
  issueStatus = [ 'identified' ];
324
352
  };
325
353
 
326
-
327
- let totalstores = await aggregateStore( [ {
354
+ let totalStoreQuery = [ {
328
355
  $match: {
329
356
  '$and': [
330
357
  { 'clientId': { $in: req.body.clientId } },
@@ -332,7 +359,17 @@ export async function infraIssuesTable( req, res ) {
332
359
  { 'createdAt': { $lte: date.end } },
333
360
  ],
334
361
  },
335
- } ] );
362
+ } ];
363
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
364
+ totalStoreQuery.push(
365
+ {
366
+ $match: {
367
+ 'storeId': { $in: req.body.permittedStores },
368
+ },
369
+ },
370
+ );
371
+ }
372
+ let totalstores = await aggregateStore( totalStoreQuery );
336
373
  let totalnumberstores = [];
337
374
  for ( let store of totalstores ) {
338
375
  totalnumberstores.push( store.storeId );
@@ -348,6 +385,15 @@ export async function infraIssuesTable( req, res ) {
348
385
  ],
349
386
  },
350
387
  } );
388
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
389
+ pendingquery.push(
390
+ {
391
+ $match: {
392
+ 'basicDetails.storeId': { $in: req.body.permittedStores },
393
+ },
394
+ },
395
+ );
396
+ }
351
397
  let pendingstorecount = await aggregateTangoTicket( pendingquery );
352
398
  let installpendingticket = pendingstorecount.map( ( store ) => store.basicDetails.storeId );
353
399
 
@@ -439,6 +485,7 @@ export async function infraIssuesTable( req, res ) {
439
485
  ];
440
486
 
441
487
  let storesQuery = [];
488
+
442
489
  if ( req.body.infrafilterIssue == 'Total Stores' ) {
443
490
  storesQuery.push( {
444
491
  $match: {
@@ -461,7 +508,6 @@ export async function infraIssuesTable( req, res ) {
461
508
  !infraissueStore.includes( data ) &&
462
509
  !installpendingticket.includes( data ),
463
510
  );
464
-
465
511
  storesQuery.push(
466
512
  {
467
513
  $match: {
@@ -471,6 +517,28 @@ export async function infraIssuesTable( req, res ) {
471
517
  },
472
518
  },
473
519
  );
520
+
521
+ if ( infraissueStore&&infraissueStore.length==0 ) {
522
+ storesQuery = [ {
523
+ $match: {
524
+ '$and': [
525
+ { 'clientId': { $in: req.body.clientId } },
526
+ { 'status': 'active' },
527
+ { 'storeId': { $nin: installpendingticket } },
528
+ { 'createdAt': { $lte: date.end } },
529
+ ],
530
+ },
531
+ } ];
532
+ }
533
+ }
534
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
535
+ storesQuery.push(
536
+ {
537
+ $match: {
538
+ 'storeId': { $in: req.body.permittedStores },
539
+ },
540
+ },
541
+ );
474
542
  }
475
543
  const excludedIssues = [ 'Identified Issues', 'Issues Not Identified', 'All Issues' ];
476
544
  if ( req.body.infrafilterIssue && !excludedIssues.includes( req.body.infrafilterIssue ) ) {
@@ -628,6 +696,15 @@ export async function InstallationIssuesTable( req, res ) {
628
696
  ],
629
697
  },
630
698
  } );
699
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
700
+ query.push(
701
+ {
702
+ $match: {
703
+ 'basicDetails.storeId': { $in: req.body.permittedStores },
704
+ },
705
+ },
706
+ );
707
+ }
631
708
  if ( req.body.installtionfilterIssue == 'yettoInstallStores' ) {
632
709
  query.push( { $match: { 'ticketDetails.issueStatus': 'notidentified' } } );
633
710
  }
@@ -687,7 +764,15 @@ export async function InstallationIssuesTable( req, res ) {
687
764
  },
688
765
  );
689
766
  let storesQuery = [];
690
-
767
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
768
+ storesQuery.push(
769
+ {
770
+ $match: {
771
+ 'storeId': { $in: req.body.permittedStores },
772
+ },
773
+ },
774
+ );
775
+ }
691
776
  if ( req.body.installtionfilterIssue == 'onboardedStores' ) {
692
777
  storesQuery.push( {
693
778
  $match: {
@@ -1017,7 +1102,7 @@ export async function hourWiseDownstores( req, res ) {
1017
1102
  };
1018
1103
 
1019
1104
 
1020
- let totalstores = await aggregateStore( [ {
1105
+ let totalStoreQuery = [ {
1021
1106
  $match: {
1022
1107
  '$and': [
1023
1108
  { 'clientId': { $in: req.body.clientId } },
@@ -1025,7 +1110,17 @@ export async function hourWiseDownstores( req, res ) {
1025
1110
  { 'createdAt': { $lte: date.end } },
1026
1111
  ],
1027
1112
  },
1028
- } ] );
1113
+ } ];
1114
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
1115
+ totalStoreQuery.push(
1116
+ {
1117
+ $match: {
1118
+ 'storeId': { $in: req.body.permittedStores },
1119
+ },
1120
+ },
1121
+ );
1122
+ }
1123
+ let totalstores = await aggregateStore( totalStoreQuery );
1029
1124
  let totalnumberstores = [];
1030
1125
  for ( let store of totalstores ) {
1031
1126
  totalnumberstores.push( store.storeId );
@@ -1041,6 +1136,15 @@ export async function hourWiseDownstores( req, res ) {
1041
1136
  ],
1042
1137
  },
1043
1138
  } );
1139
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
1140
+ pendingquery.push(
1141
+ {
1142
+ $match: {
1143
+ 'basicDetails.storeId': { $in: req.body.permittedStores },
1144
+ },
1145
+ },
1146
+ );
1147
+ }
1044
1148
  let pendingstorecount = await aggregateTangoTicket( pendingquery );
1045
1149
  let installpendingticket = pendingstorecount.map( ( store ) => store.basicDetails.storeId );
1046
1150
 
@@ -1164,6 +1268,27 @@ export async function hourWiseDownstores( req, res ) {
1164
1268
  },
1165
1269
  },
1166
1270
  );
1271
+ if ( infraissueStore&&infraissueStore.length==0 ) {
1272
+ storesQuery = [ {
1273
+ $match: {
1274
+ '$and': [
1275
+ { 'clientId': { $in: req.body.clientId } },
1276
+ { 'status': 'active' },
1277
+ { 'storeId': { $nin: installpendingticket } },
1278
+ { 'createdAt': { $lte: date.end } },
1279
+ ],
1280
+ },
1281
+ } ];
1282
+ }
1283
+ }
1284
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
1285
+ storesQuery.push(
1286
+ {
1287
+ $match: {
1288
+ 'storeId': { $in: req.body.permittedStores },
1289
+ },
1290
+ },
1291
+ );
1167
1292
  }
1168
1293
  const excludedIssues = [ 'Identified Issues', 'Issues Not Identified', 'All Issues' ];
1169
1294
  if ( req.body.infrafilterIssue && !excludedIssues.includes( req.body.infrafilterIssue ) ) {
@@ -1278,6 +1403,7 @@ export async function hourWiseDownstores( req, res ) {
1278
1403
  for ( const store of storesList ) {
1279
1404
  data.storeId = store.storeId;
1280
1405
  data.storeName = store.storeName;
1406
+ data.clientName = store.clientName;
1281
1407
  data.primaryIssue = store.primaryIssue;
1282
1408
  let storedata = await downStoresCheck( data, inputData );
1283
1409
  result.push( storedata[0] );
@@ -1363,6 +1489,9 @@ export async function ticketCountSplit( req, res ) {
1363
1489
  { issueType: { $in: [ 'highcount', 'lowcount' ] } },
1364
1490
 
1365
1491
  ];
1492
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
1493
+ filter.push( { 'basicDetails.storeId': { $in: req.body.permittedStores } } );
1494
+ }
1366
1495
  if ( req.user.userType === 'client' ) {
1367
1496
  filter.push( { 'dataMismatch.showToClient': true } );
1368
1497
  }
@@ -1441,6 +1570,9 @@ export async function overViewTable( req, res ) {
1441
1570
  'basicDetails.clientId': { $in: inputData.clientId },
1442
1571
  },
1443
1572
  ];
1573
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
1574
+ filter.push( { 'basicDetails.storeId': { $in: req.body.permittedStores } } );
1575
+ }
1444
1576
  if ( req.user.userType === 'client' ) {
1445
1577
  filter.push( { 'dataMismatch.showToClient': true } );
1446
1578
  }
@@ -1536,7 +1668,9 @@ export async function ticketCount( req, res ) {
1536
1668
  },
1537
1669
 
1538
1670
  ];
1539
-
1671
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
1672
+ filter.push( { 'basicDetails.storeId': { $in: req.body.permittedStores } } );
1673
+ }
1540
1674
  if ( req.user.userType === 'client' ) {
1541
1675
  filter.push( { 'dataMismatch.showToClient': true } );
1542
1676
  }
@@ -1598,3 +1732,108 @@ export async function ticketCount( req, res ) {
1598
1732
  res.sendError( 'Internal Server Error', 500 );
1599
1733
  }
1600
1734
  };
1735
+ export async function checkPermission( req, res, next ) {
1736
+ try {
1737
+ let result =[];
1738
+ if ( req.user.role !== 'superadmin' && req.user.userType == 'client' ) {
1739
+ const assignedUser = await findUserAssignedStore( { userEmail: req.user.email } );
1740
+ if ( assignedUser.length == 0 ) {
1741
+ return res.sendSuccess( { result: [], count: 0 } );
1742
+ }
1743
+ switch ( assignedUser[0].assignedType ) {
1744
+ case 'store':
1745
+ const assignedUserQuery = [
1746
+ {
1747
+ $match: {
1748
+ userEmail: { $eq: req.user.email },
1749
+ },
1750
+
1751
+ },
1752
+ {
1753
+ $group: {
1754
+ _id: null,
1755
+ storeList: { $push: '$assignedValue' },
1756
+ },
1757
+ },
1758
+ {
1759
+ $project: {
1760
+ storeList: 1,
1761
+ },
1762
+ },
1763
+ ];
1764
+ result = await aggregateUserAssignedStore( assignedUserQuery );
1765
+ break;
1766
+
1767
+ case 'group':
1768
+ const assignedQuery = [
1769
+ {
1770
+ $match: {
1771
+ userEmail: { $eq: req.user.email },
1772
+ },
1773
+
1774
+ },
1775
+ {
1776
+ $project: {
1777
+ assignedValue: {
1778
+ $toObjectId: '$assignedValue',
1779
+ },
1780
+ },
1781
+ },
1782
+ {
1783
+ $lookup: {
1784
+ from: 'groups',
1785
+ let: { groupId: '$assignedValue' },
1786
+ pipeline: [
1787
+ {
1788
+ $match: {
1789
+ $expr: {
1790
+ $eq: [ '$_id', '$$groupId' ],
1791
+ },
1792
+ },
1793
+ },
1794
+ {
1795
+ $project: {
1796
+ _id: 0,
1797
+ storeList: 1,
1798
+ },
1799
+ },
1800
+ ], as: 'groups',
1801
+ },
1802
+ },
1803
+ {
1804
+ $unwind: {
1805
+ path: '$groups', preserveNullAndEmptyArrays: true,
1806
+ },
1807
+ },
1808
+ {
1809
+ $unwind: {
1810
+ path: '$groups.storeList', preserveNullAndEmptyArrays: true,
1811
+ },
1812
+ },
1813
+ {
1814
+ $group: {
1815
+ _id: null,
1816
+ stores: { $push: '$groups.storeList' },
1817
+ },
1818
+ },
1819
+ {
1820
+ $project: {
1821
+ _id: 0,
1822
+ storeList: '$stores',
1823
+ },
1824
+ },
1825
+ ];
1826
+ result = await aggregateUserAssignedStore( assignedQuery );
1827
+ break;
1828
+ }
1829
+ if ( result&&result.length>0 ) {
1830
+ const uniqueArray = [ ...new Set( result[0].storeList ) ];
1831
+ req.body.permittedStores =uniqueArray;
1832
+ }
1833
+ }
1834
+ next();
1835
+ } catch ( error ) {
1836
+ logger.error( { error: error, function: 'checkPermission' } );
1837
+ res.sendError( 'Internal Server Error', 500 );
1838
+ }
1839
+ };
@@ -14,6 +14,7 @@ import { findOneGroup } from '../services/group.service.js';
14
14
  import htmlpdf from 'html-pdf-node';
15
15
  import mongoose from 'mongoose';
16
16
  import _ from 'lodash';
17
+ import { aggregateUserAssignedStore, findUserAssignedStore } from '../services/userAssignedStore.service.js';
17
18
  import { aggregateCamera } from '../services/camera.service.js';
18
19
  export async function createTicket( req, res ) {
19
20
  try {
@@ -72,7 +73,6 @@ export async function createTicket( req, res ) {
72
73
  downtimetotal = 0;
73
74
  }
74
75
 
75
- console.log( req.body );
76
76
  let create = await createTangoTicket( req.body );
77
77
  let Timestamp = dayjs().format( 'YYYY-MM-DD HH:mm' );
78
78
  const attachments = null;
@@ -242,11 +242,11 @@ export async function updateTicketIssue( req, res ) {
242
242
  if ( req.body.issueType === 'infra' ) {
243
243
  let client = await findOneClient( { clientId: req.body.basicDetails.clientId } );
244
244
  let refreshdate = dayjs().add( client.ticketConfigs.refreshAlert, 'days' );
245
- console.log( new Date( dayjs( refreshdate ).format( 'YYYY-MM-DD' ) ) );
245
+
246
246
  query = { ...query, 'ticketDetails.ticketRefreshTime': new Date( dayjs( refreshdate ).format( 'YYYY-MM-DD' ) ),
247
247
  };
248
248
  }
249
- console.log( query );
249
+
250
250
  let updateTicket = await updateOneTangoTicket( { ticketId: req.body.ticketId }, query );
251
251
 
252
252
  if ( req.body.ticketDetails.ticketType === 'refreshticket' ) {
@@ -937,50 +937,61 @@ function inWords( num ) {
937
937
  export async function allCounts( req, res ) {
938
938
  try {
939
939
  let date = await getUTC( new Date( req.body.fromDate ), new Date( req.body.toDate ) );
940
- let countQuery=[
941
- {
942
- $match: {
943
- $and: [
944
- { 'basicDetails.clientId': { $in: req.body.clientId } },
945
- { createdAt: { $gte: date.start } },
946
- { createdAt: { $lte: date.end } },
940
+ let countQuery=[];
941
+
942
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
943
+ countQuery.push(
944
+ {
945
+ $match: {
946
+ 'basicDetails.storeId': { $in: req.body.permittedStores },
947
+ },
948
+ },
949
+ );
950
+ }
951
+
952
+ countQuery.push( {
953
+ $match: {
954
+ $and: [
955
+ { 'basicDetails.clientId': { $in: req.body.clientId } },
956
+ { createdAt: { $gte: date.start } },
957
+ { createdAt: { $lte: date.end } },
958
+ ],
959
+ },
960
+ },
961
+ {
962
+ $project: {
963
+ issueType: 1,
964
+ installationCount: {
965
+ $cond: [ { $and: [ { $eq: [ '$issueType', 'installation' ] } ] }, 1, 0,
947
966
  ],
948
967
  },
949
- },
950
- {
951
- $project: {
952
- issueType: 1,
953
- installationCount: {
954
- $cond: [ { $and: [ { $eq: [ '$issueType', 'installation' ] } ] }, 1, 0,
955
- ],
956
- },
957
- infraCount: {
958
- $cond: [ { $and: [ { $eq: [ '$issueType', 'infra' ] } ] }, 1, 0,
959
- ],
960
- },
961
- datamismatchCount: {
962
- $cond: [ { $or: [ { $eq: [ '$issueType', 'highcount' ] }, { $eq: [ '$issueType', 'lowcount' ] } ] }, 1, 0,
963
- ],
968
+ infraCount: {
969
+ $cond: [ { $and: [ { $eq: [ '$issueType', 'infra' ] } ] }, 1, 0,
970
+ ],
971
+ },
972
+ datamismatchCount: {
973
+ $cond: [ { $or: [ { $eq: [ '$issueType', 'highcount' ] }, { $eq: [ '$issueType', 'lowcount' ] } ] }, 1, 0,
974
+ ],
964
975
 
965
- },
966
- matCount: {
967
- $cond: [ { $and: [ { $eq: [ '$issueType', 'mat' ] } ] }, 1, 0,
968
- ],
969
- },
976
+ },
977
+ matCount: {
978
+ $cond: [ { $and: [ { $eq: [ '$issueType', 'mat' ] } ] }, 1, 0,
979
+ ],
970
980
  },
971
981
  },
972
- {
973
- $group: {
974
- _id: null,
975
- installationCount: { $sum: '$installationCount' },
976
- infraCount: { $sum: '$infraCount' },
977
- datamismatchCount: { $sum: '$datamismatchCount' },
978
- matCount: { $sum: '$matCount' },
982
+ },
983
+ {
984
+ $group: {
985
+ _id: null,
986
+ installationCount: { $sum: '$installationCount' },
987
+ infraCount: { $sum: '$infraCount' },
988
+ datamismatchCount: { $sum: '$datamismatchCount' },
989
+ matCount: { $sum: '$matCount' },
979
990
 
980
991
 
981
- },
982
992
  },
983
- ];
993
+ } );
994
+
984
995
 
985
996
  if ( req.user.userType === 'client' ) {
986
997
  countQuery.forEach( ( item ) => {
@@ -1022,6 +1033,15 @@ export async function infraTable( req, res ) {
1022
1033
  },
1023
1034
  } );
1024
1035
  }
1036
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
1037
+ query.push(
1038
+ {
1039
+ $match: {
1040
+ 'basicDetails.storeId': { $in: req.body.permittedStores },
1041
+ },
1042
+ },
1043
+ );
1044
+ }
1025
1045
  query.push( {
1026
1046
  $match: {
1027
1047
  $and: [
@@ -1311,6 +1331,15 @@ export async function installationTable( req, res ) {
1311
1331
  },
1312
1332
  } );
1313
1333
  }
1334
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
1335
+ query.push(
1336
+ {
1337
+ $match: {
1338
+ 'basicDetails.storeId': { $in: req.body.permittedStores },
1339
+ },
1340
+ },
1341
+ );
1342
+ }
1314
1343
 
1315
1344
  query.push( {
1316
1345
  $match: {
@@ -1798,6 +1827,11 @@ export async function dataMismatchTable( req, res ) {
1798
1827
  },
1799
1828
 
1800
1829
  ];
1830
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
1831
+ countFilter.push(
1832
+ { 'basicDetails.storeId': { $in: req.body.permittedStores } },
1833
+ );
1834
+ }
1801
1835
 
1802
1836
  if ( req.user.userType === 'client' ) {
1803
1837
  countFilter.push( { 'dataMismatch.showToClient': true } );
@@ -1863,7 +1897,11 @@ export async function dataMismatchTable( req, res ) {
1863
1897
  { 'basicDetails.clientId': { $in: inputData.clientId } },
1864
1898
  { issueType: { $in: [ 'highcount', 'lowcount' ] } },
1865
1899
  ];
1866
-
1900
+ if ( req.body.permittedStores && req.body.permittedStores.length > 0 ) {
1901
+ filter.push(
1902
+ { 'basicDetails.storeId': { $in: req.body.permittedStores } },
1903
+ );
1904
+ }
1867
1905
  if ( req.user.userType === 'client' ) {
1868
1906
  filter.push( { 'dataMismatch.showToClient': true } );
1869
1907
  }
@@ -2067,3 +2105,109 @@ export async function dataMismatchTable( req, res ) {
2067
2105
  res.sendError( 'Internal Server Error', 500 );
2068
2106
  }
2069
2107
  };
2108
+
2109
+ export async function checkPermission( req, res, next ) {
2110
+ try {
2111
+ let result =[];
2112
+ if ( req.user.role !== 'superadmin' && req.user.userType == 'client' ) {
2113
+ const assignedUser = await findUserAssignedStore( { userEmail: req.user.email } );
2114
+ if ( assignedUser.length == 0 ) {
2115
+ return res.sendSuccess( { result: [], count: 0 } );
2116
+ }
2117
+ switch ( assignedUser[0].assignedType ) {
2118
+ case 'store':
2119
+ const assignedUserQuery = [
2120
+ {
2121
+ $match: {
2122
+ userEmail: { $eq: req.user.email },
2123
+ },
2124
+
2125
+ },
2126
+ {
2127
+ $group: {
2128
+ _id: null,
2129
+ storeList: { $push: '$assignedValue' },
2130
+ },
2131
+ },
2132
+ {
2133
+ $project: {
2134
+ storeList: 1,
2135
+ },
2136
+ },
2137
+ ];
2138
+ result = await aggregateUserAssignedStore( assignedUserQuery );
2139
+ break;
2140
+
2141
+ case 'group':
2142
+ const assignedQuery = [
2143
+ {
2144
+ $match: {
2145
+ userEmail: { $eq: req.user.email },
2146
+ },
2147
+
2148
+ },
2149
+ {
2150
+ $project: {
2151
+ assignedValue: {
2152
+ $toObjectId: '$assignedValue',
2153
+ },
2154
+ },
2155
+ },
2156
+ {
2157
+ $lookup: {
2158
+ from: 'groups',
2159
+ let: { groupId: '$assignedValue' },
2160
+ pipeline: [
2161
+ {
2162
+ $match: {
2163
+ $expr: {
2164
+ $eq: [ '$_id', '$$groupId' ],
2165
+ },
2166
+ },
2167
+ },
2168
+ {
2169
+ $project: {
2170
+ _id: 0,
2171
+ storeList: 1,
2172
+ },
2173
+ },
2174
+ ], as: 'groups',
2175
+ },
2176
+ },
2177
+ {
2178
+ $unwind: {
2179
+ path: '$groups', preserveNullAndEmptyArrays: true,
2180
+ },
2181
+ },
2182
+ {
2183
+ $unwind: {
2184
+ path: '$groups.storeList', preserveNullAndEmptyArrays: true,
2185
+ },
2186
+ },
2187
+ {
2188
+ $group: {
2189
+ _id: null,
2190
+ stores: { $push: '$groups.storeList' },
2191
+ },
2192
+ },
2193
+ {
2194
+ $project: {
2195
+ _id: 0,
2196
+ storeList: '$stores',
2197
+ },
2198
+ },
2199
+ ];
2200
+ result = await aggregateUserAssignedStore( assignedQuery );
2201
+ break;
2202
+ }
2203
+ if ( result&&result.length>0 ) {
2204
+ const uniqueArray = [ ...new Set( result[0].storeList ) ];
2205
+ req.body.permittedStores =uniqueArray;
2206
+ }
2207
+ }
2208
+ next();
2209
+ } catch ( error ) {
2210
+ logger.error( { error: error, function: 'checkPermission' } );
2211
+ res.sendError( 'Internal Server Error', 500 );
2212
+ }
2213
+ };
@@ -1,7 +1,7 @@
1
1
 
2
2
  import express from 'express';
3
3
  import { isAllowedSessionHandler, authorize } from 'tango-app-api-middleware';
4
- import { infraCard, installationCard, InstallationIssuesTable, infraIssuesTable, hourWiseDownClients, hourWiseDownstores, ticketCountSplit, overViewTable, ticketCount } from '../controllers/clientInfra.controller.js';
4
+ import { infraCard, installationCard, checkPermission, InstallationIssuesTable, infraIssuesTable, hourWiseDownClients, hourWiseDownstores, ticketCountSplit, overViewTable, ticketCount } from '../controllers/clientInfra.controller.js';
5
5
 
6
6
  export const clientInfraRouter = express.Router();
7
7
 
@@ -9,19 +9,19 @@ export const clientInfraRouter = express.Router();
9
9
  clientInfraRouter.post( '/infraCard', isAllowedSessionHandler, authorize( {
10
10
  userType: [ 'client', 'tango' ], access: [
11
11
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
12
- } ), infraCard );
12
+ } ), checkPermission, infraCard );
13
13
  clientInfraRouter.post( '/infraIssuesTable', isAllowedSessionHandler, authorize( {
14
14
  userType: [ 'client', 'tango' ], access: [
15
15
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
16
- } ), infraIssuesTable );
16
+ } ), checkPermission, infraIssuesTable );
17
17
  clientInfraRouter.post( '/installationCard', isAllowedSessionHandler, authorize( {
18
18
  userType: [ 'client', 'tango' ], access: [
19
19
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
20
- } ), installationCard );
20
+ } ), checkPermission, installationCard );
21
21
  clientInfraRouter.post( '/InstallationIssuesTable', isAllowedSessionHandler, authorize( {
22
22
  userType: [ 'client', 'tango' ], access: [
23
23
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
24
- } ), InstallationIssuesTable );
24
+ } ), checkPermission, InstallationIssuesTable );
25
25
  clientInfraRouter.post( '/hourWiseDownClients', isAllowedSessionHandler, authorize( {
26
26
  userType: [ 'client', 'tango' ], access: [
27
27
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
@@ -29,17 +29,17 @@ clientInfraRouter.post( '/hourWiseDownClients', isAllowedSessionHandler, authori
29
29
  clientInfraRouter.post( '/hourWiseDownstores', isAllowedSessionHandler, authorize( {
30
30
  userType: [ 'client', 'tango' ], access: [
31
31
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
32
- } ), hourWiseDownstores );
32
+ } ), checkPermission, hourWiseDownstores );
33
33
 
34
34
  clientInfraRouter.post( '/ticket-count', isAllowedSessionHandler, authorize( {
35
35
  userType: [ 'client', 'tango' ], access: [
36
36
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
37
- } ), ticketCount );
37
+ } ), checkPermission, ticketCount );
38
38
  clientInfraRouter.post( '/ticket-count-split', isAllowedSessionHandler, authorize( {
39
39
  userType: [ 'client', 'tango' ], access: [
40
40
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
41
- } ), ticketCountSplit );
41
+ } ), checkPermission, ticketCountSplit );
42
42
  clientInfraRouter.post( '/overview-table', isAllowedSessionHandler, authorize( {
43
43
  userType: [ 'client', 'tango' ], access: [
44
44
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
45
- } ), overViewTable );
45
+ } ), checkPermission, overViewTable );
@@ -2,7 +2,7 @@
2
2
  import express from 'express';
3
3
  import { isAllowedSessionHandler, authorize, validate } from 'tango-app-api-middleware';
4
4
  import { validateDetails, bulkvalidateDetails, validateTicket, bulkvalidateTicket, validateTicketstatus, ticketExists, infraReasonExists, InfrastepstoResolve, InfraAlert } from '../validations/infra.validation.js';
5
- import { createTicket, bulkcreateTicket, updateStatus, createReason, PrimaryReasons, matTable, removeAttachment,
5
+ import { createTicket, bulkcreateTicket, checkPermission, updateStatus, createReason, PrimaryReasons, matTable, removeAttachment,
6
6
  secondaryReason, updateTicketIssue, viewTicket, AlertTicketReply, uploadAttachments, getInfraIssues,
7
7
  updateInstallationTicket, emailUserList, saveInfraEmailConfig, invoice, allCounts, infraTable, dataMismatchTable, storeFilter, assignTicket, installationTable } from '../controllers/infra.controllers.js';
8
8
  import { getInfraIssueValid } from '../dtos/infra.dtos.js';
@@ -63,15 +63,15 @@ infraRouter.post( '/invoice', invoice );
63
63
  infraRouter.post( '/allCounts', isAllowedSessionHandler, authorize( {
64
64
  userType: [ 'client', 'tango' ], access: [
65
65
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
66
- } ), allCounts );
66
+ } ), checkPermission, allCounts );
67
67
  infraRouter.post( '/infraTable', isAllowedSessionHandler, authorize( {
68
68
  userType: [ 'client', 'tango' ], access: [
69
69
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
70
- } ), infraTable );
70
+ } ), checkPermission, infraTable );
71
71
  infraRouter.post( '/installationTable', isAllowedSessionHandler, authorize( {
72
72
  userType: [ 'client', 'tango' ], access: [
73
73
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
74
- } ), installationTable );
74
+ } ), checkPermission, installationTable );
75
75
  infraRouter.post( '/matTable', isAllowedSessionHandler, authorize( {
76
76
  userType: [ 'client', 'tango' ], access: [
77
77
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
@@ -79,7 +79,7 @@ infraRouter.post( '/matTable', isAllowedSessionHandler, authorize( {
79
79
  infraRouter.post( '/dataMismatchTable', isAllowedSessionHandler, authorize( {
80
80
  userType: [ 'client', 'tango' ], access: [
81
81
  { featureName: 'manage', name: 'tickets', permissions: [ 'isView' ] } ],
82
- } ), dataMismatchTable );
82
+ } ), checkPermission, dataMismatchTable );
83
83
  infraRouter.post( '/assignTicket', isAllowedSessionHandler, authorize( {
84
84
  userType: [ 'client', 'tango' ], access: [
85
85
  { featureName: 'manage', name: 'tickets', permissions: [ 'isEdit', 'isView' ] } ],