tango-app-api-analysis-traffic 3.0.0-alpha.9 → 3.0.0-beta.2

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.
@@ -1,13 +1,16 @@
1
- import { logger, download } from 'tango-app-api-middleware';
1
+ import { logger, download, signedUrl } from 'tango-app-api-middleware';
2
2
  import * as clientService from '../services/clients.services.js';
3
3
  import {
4
4
  aggregateStore,
5
+ findOneStore,
5
6
  } from '../services/stores.service.js';
6
7
  import { aggregateUserAssignedStore, findOneUserAssignedStore } from '../services/userAssignedStore.service.js';
8
+ import { aggregateCluster } from '../services/cluster.service.js';
7
9
  import { aggregateGroup } from '../services/group.service.js';
8
10
  import dayjs from 'dayjs';
9
11
  import mongoose from 'mongoose';
10
12
  const ObjectId = mongoose.Types.ObjectId;
13
+ import { findCamera } from '../services/camera.service.js';
11
14
 
12
15
  // Lamda Service Call //
13
16
  async function LamdaServiceCall( url, data ) {
@@ -27,8 +30,8 @@ async function LamdaServiceCall( url, data ) {
27
30
  const json = await response.json();
28
31
  return json;
29
32
  } catch ( error ) {
30
- console.log( 'error =>', error );
31
33
  logger.error( { error: error, message: data, function: 'LamdaServiceCall' } );
34
+ return false;
32
35
  }
33
36
  }
34
37
 
@@ -36,11 +39,12 @@ async function LamdaServiceCall( url, data ) {
36
39
  export const cardsFunnelV1 = async ( req, res ) => {
37
40
  try {
38
41
  let reqestData = req.body;
39
- let getClientData = await clientService.findOne( { clientId: reqestData.clientId } );
42
+ let getClientData = await getClientConfig( reqestData.clientId );
40
43
  if ( !getClientData ) {
41
44
  return res.sendError( 'Invalid Client Id', 400 );
42
45
  }
43
- reqestData.currency = getClientData.paymentInvoice?.currencyType || 'INR';
46
+ reqestData.featureConfigs = getClientData.featureConfigs;
47
+ reqestData.currency = getClientData.paymentInvoice?.currencyType || 'inr';
44
48
  reqestData.revenue = getClientData.averageTransactionValue || '0';
45
49
  let LamdaURL = 'https://55mojecvuvtphucgsalx5jtyki0untzp.lambda-url.ap-south-1.on.aws/';
46
50
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
@@ -59,9 +63,41 @@ export const cardsFunnelV1 = async ( req, res ) => {
59
63
  }
60
64
  };
61
65
 
66
+ export const funnelV1 = async ( req, res ) => {
67
+ try {
68
+ let reqestData = req.body;
69
+ let getClientData = await getClientConfig( reqestData.clientId );
70
+ if ( !getClientData ) {
71
+ return res.sendError( 'Invalid Client Id', 400 );
72
+ }
73
+ reqestData.featureConfigs = getClientData.featureConfigs;
74
+ reqestData.currency = getClientData.paymentInvoice?.currencyType || 'inr';
75
+ reqestData.revenue = getClientData.averageTransactionValue || '0';
76
+ let LamdaURL = 'https://k3mv4jr6w7er4tn3ku2kkdgdha0ovyog.lambda-url.ap-south-1.on.aws/';
77
+ let resultData = await LamdaServiceCall( LamdaURL, reqestData );
78
+ if ( resultData ) {
79
+ if ( resultData.status_code == '200' ) {
80
+ return res.sendSuccess( resultData );
81
+ } else {
82
+ return res.sendError( 'No Content', 204 );
83
+ }
84
+ } else {
85
+ return res.sendError( 'No Content', 204 );
86
+ }
87
+ } catch ( error ) {
88
+ logger.error( { error: error, message: req.query, function: 'funnelV1' } );
89
+ return res.sendError( { error: error }, 500 );
90
+ }
91
+ };
92
+
62
93
  export const cardsGraphsV1 = async ( req, res ) => {
63
94
  try {
64
95
  let reqestData = req.body;
96
+ let getClientData = await getClientConfig( reqestData.clientId );
97
+ if ( !getClientData ) {
98
+ return res.sendError( 'Invalid Client Id', 400 );
99
+ }
100
+ reqestData.featureConfigs = getClientData.featureConfigs;
65
101
  let LamdaURL = 'https://xvsz4gd4erdlmvhv33hhyot3ui0xzitq.lambda-url.ap-south-1.on.aws/';
66
102
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
67
103
  if ( resultData ) {
@@ -82,6 +118,11 @@ export const cardsGraphsV1 = async ( req, res ) => {
82
118
  export const recapVideoV1 = async ( req, res ) => {
83
119
  try {
84
120
  let reqestData = req.body;
121
+ let getClientData = await getClientConfig( reqestData.clientId );
122
+ if ( !getClientData ) {
123
+ return res.sendError( 'Invalid Client Id', 400 );
124
+ }
125
+ reqestData.featureConfigs = getClientData.featureConfigs;
85
126
  let LamdaURL = 'https://u7xzph4jkl72sbefz2xx5rjw540rocck.lambda-url.ap-south-1.on.aws/';
86
127
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
87
128
  if ( resultData ) {
@@ -103,6 +144,11 @@ export const densityDwellV1 = async ( req, res ) => {
103
144
  try {
104
145
  let reqestData = req.body;
105
146
  reqestData.hourFormat = 12;
147
+ let getClientData = await getClientConfig( reqestData.clientId );
148
+ if ( !getClientData ) {
149
+ return res.sendError( 'Invalid Client Id', 400 );
150
+ }
151
+ reqestData.featureConfigs = getClientData.featureConfigs;
106
152
  let LamdaURL = 'https://wh2d4dkgsao5kbwpjxbmchcjja0cxjhv.lambda-url.ap-south-1.on.aws/';
107
153
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
108
154
  if ( resultData ) {
@@ -123,6 +169,13 @@ export const densityDwellV1 = async ( req, res ) => {
123
169
  export const overallCardsV1 = async ( req, res ) => {
124
170
  try {
125
171
  let reqestData = req.body;
172
+ let getClientData = await getClientConfig( reqestData.clientId );
173
+ if ( !getClientData ) {
174
+ return res.sendError( 'Invalid Client Id', 400 );
175
+ }
176
+ reqestData.featureConfigs = getClientData.featureConfigs;
177
+ reqestData.currency = getClientData.paymentInvoice?.currencyType || 'inr';
178
+ reqestData.revenue = getClientData.averageTransactionValue || '0';
126
179
  let LamdaURL = 'https://dugu3ghkgalnpaydf2wdjp37240pzcmy.lambda-url.ap-south-1.on.aws/';
127
180
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
128
181
  if ( resultData ) {
@@ -144,6 +197,11 @@ export const overallHourlyChartV1 = async ( req, res ) => {
144
197
  try {
145
198
  let reqestData = req.body;
146
199
  reqestData.hourFormat = 12;
200
+ let getClientData = await getClientConfig( reqestData.clientId );
201
+ if ( !getClientData ) {
202
+ return res.sendError( 'Invalid Client Id', 400 );
203
+ }
204
+ reqestData.featureConfigs = getClientData.featureConfigs;
147
205
  let LamdaURL = 'https://p3xcs56mkjj4sfugvsibhyto3i0gdbda.lambda-url.ap-south-1.on.aws/';
148
206
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
149
207
  if ( resultData ) {
@@ -164,6 +222,11 @@ export const overallHourlyChartV1 = async ( req, res ) => {
164
222
  export const overallChartV1 = async ( req, res ) => {
165
223
  try {
166
224
  let reqestData = req.body;
225
+ let getClientData = await getClientConfig( reqestData.clientId );
226
+ if ( !getClientData ) {
227
+ return res.sendError( 'Invalid Client Id', 400 );
228
+ }
229
+ reqestData.featureConfigs = getClientData.featureConfigs;
167
230
  let LamdaURL = 'https://wtcllrsyec4iwkx6sg7xi2b7se0seawx.lambda-url.ap-south-1.on.aws/';
168
231
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
169
232
  if ( resultData ) {
@@ -184,6 +247,11 @@ export const overallChartV1 = async ( req, res ) => {
184
247
  export const singleStoreChartV1 = async ( req, res ) => {
185
248
  try {
186
249
  let reqestData = req.body;
250
+ let getClientData = await getClientConfig( reqestData.clientId );
251
+ if ( !getClientData ) {
252
+ return res.sendError( 'Invalid Client Id', 400 );
253
+ }
254
+ reqestData.featureConfigs = getClientData.featureConfigs;
187
255
  let LamdaURL = 'https://j2mqa4qhmku3e7htuptsjvpumi0qkhkf.lambda-url.ap-south-1.on.aws/';
188
256
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
189
257
  if ( resultData ) {
@@ -204,6 +272,11 @@ export const singleStoreChartV1 = async ( req, res ) => {
204
272
  export const demographicChartV1 = async ( req, res ) => {
205
273
  try {
206
274
  let reqestData = req.body;
275
+ let getClientData = await getClientConfig( reqestData.clientId );
276
+ if ( !getClientData ) {
277
+ return res.sendError( 'Invalid Client Id', 400 );
278
+ }
279
+ reqestData.featureConfigs = getClientData.featureConfigs;
207
280
  let LamdaURL = 'https://7mslk3sde3m663mrgmwlc4rfou0alphk.lambda-url.ap-south-1.on.aws/';
208
281
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
209
282
  if ( resultData ) {
@@ -224,6 +297,11 @@ export const demographicChartV1 = async ( req, res ) => {
224
297
  export const buyerChartV1 = async ( req, res ) => {
225
298
  try {
226
299
  let reqestData = req.body;
300
+ let getClientData = await getClientConfig( reqestData.clientId );
301
+ if ( !getClientData ) {
302
+ return res.sendError( 'Invalid Client Id', 400 );
303
+ }
304
+ reqestData.featureConfigs = getClientData.featureConfigs;
227
305
  let LamdaURL = 'https://2i76wug6swytd7fej3d2fvd2xa0ujxhz.lambda-url.ap-south-1.on.aws/';
228
306
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
229
307
  if ( resultData ) {
@@ -245,11 +323,16 @@ export const footfallDirectoryFoldersV1 = async ( req, res ) => {
245
323
  try {
246
324
  let reqestData = req.body;
247
325
  reqestData.hourFormat = 12;
326
+ let getClientData = await getClientConfig( reqestData.clientId );
327
+ if ( !getClientData ) {
328
+ return res.sendError( 'Invalid Client Id', 400 );
329
+ }
330
+ reqestData.featureConfigs = getClientData.featureConfigs;
248
331
  let LamdaURL = 'https://waxlhd7lfdlmyrkrdyv77najka0ayihq.lambda-url.ap-south-1.on.aws/';
249
332
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
250
333
  if ( resultData ) {
251
334
  if ( resultData.status_code == '200' ) {
252
- return res.sendSuccess( resultData );
335
+ return res.sendSuccess( { ...resultData, isFootfallAuditStores: getClientData?.isFootfallAuditStores } );
253
336
  } else {
254
337
  return res.sendError( 'No Content', 204 );
255
338
  }
@@ -265,11 +348,16 @@ export const footfallDirectoryFoldersV1 = async ( req, res ) => {
265
348
  export const footfallDirectoryV1 = async ( req, res ) => {
266
349
  try {
267
350
  let reqestData = req.body;
351
+ let getClientData = await getClientConfig( reqestData.clientId );
352
+ if ( !getClientData ) {
353
+ return res.sendError( 'Invalid Client Id', 400 );
354
+ }
355
+ reqestData.featureConfigs = getClientData.featureConfigs;
268
356
  let LamdaURL = 'https://waxlhd7lfdlmyrkrdyv77najka0ayihq.lambda-url.ap-south-1.on.aws/';
269
357
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
270
358
  if ( resultData ) {
271
359
  if ( resultData.status_code == '200' ) {
272
- return res.sendSuccess( resultData );
360
+ return res.sendSuccess( { ...resultData, isFootfallAuditStores: getClientData?.isFootfallAuditStores } );
273
361
  } else {
274
362
  return res.sendError( 'No Content', 204 );
275
363
  }
@@ -285,8 +373,35 @@ export const footfallDirectoryV1 = async ( req, res ) => {
285
373
  export const summaryTableV1 = async ( req, res ) => {
286
374
  try {
287
375
  let reqestData = req.body;
376
+ let getClientData = await getClientConfig( reqestData.clientId );
377
+ if ( !getClientData ) {
378
+ return res.sendError( 'Invalid Client Id', 400 );
379
+ }
380
+ reqestData.featureConfigs = getClientData.featureConfigs;
288
381
  let LamdaURL = 'https://fpmvquqpi7xw6hpaalxa5oa6pi0cvbql.lambda-url.ap-south-1.on.aws/';
289
- let resultData = await LamdaServiceCall( LamdaURL, reqestData );
382
+ let resultData;
383
+ if ( reqestData.export ) {
384
+ let limit = 10000;
385
+ let count = 0;
386
+ for ( let i=0; i<=limit; i += 1000 ) {
387
+ reqestData.limit = 1000;
388
+ reqestData.offset = count;
389
+ let data = await LamdaServiceCall( LamdaURL, reqestData );
390
+ count ++;
391
+ if ( data.status_code == '200' && data.summaryData.length ) {
392
+ data = {
393
+ 'status_code': '200',
394
+ 'totalCount': resultData?.totalCount ? resultData?.totalCount + data.totalCount : data.totalCount,
395
+ 'summaryData': resultData?.summaryData ? [ ...resultData?.summaryData, ...data.summaryData ] : data.summaryData,
396
+ };
397
+ resultData = data;
398
+ } else {
399
+ break;
400
+ }
401
+ }
402
+ } else {
403
+ resultData = await LamdaServiceCall( LamdaURL, reqestData );
404
+ }
290
405
  if ( resultData ) {
291
406
  if ( resultData.status_code == '200' ) {
292
407
  if ( reqestData.export ) {
@@ -294,12 +409,26 @@ export const summaryTableV1 = async ( req, res ) => {
294
409
  const exportdata = [];
295
410
  resultData.summaryData.forEach( ( element ) => {
296
411
  exportdata.push( {
297
- 'storeName': element.storeName,
298
- 'storeId': element.storeId,
299
- 'potentialBuyersCount': element.potentialBuyersCount,
300
- 'bouncedCounte': element.bouncedCount,
301
- 'engagersCount': element.engagersCount,
302
- 'conversionRate': element.conversionRate,
412
+ 'Store Name': element.storeName,
413
+ 'Store ID': element.storeId,
414
+ ...( req.body.storeId.length ==1 ) ? { Date: element['date'] }:{},
415
+ 'Footfall': element.footfallCount,
416
+ 'Bounced Count': element.bouncedCount,
417
+ 'Engagers': element.engagersCount,
418
+ 'MissedOpportunity': element.missedOpportunityCount,
419
+ 'Potential Buyers': element.potentialBuyersCount,
420
+ 'Conversion Rate': element.conversionRate,
421
+ 'NOB Count': element.NOBCount,
422
+ 'Avg.Dwell Time': element.avgDwellTime,
423
+ 'Avg.Infra DownTime': element.avgInfraDowntime,
424
+ 'Below12': element.below12,
425
+ '13-19': element['13-19'],
426
+ '20-30': element['20-30'],
427
+ '31-45': element['31-45'],
428
+ '46-59': element['46-59'],
429
+ '60 above': element['60 above'],
430
+ 'Male': element.male,
431
+ 'Female': element.female,
303
432
  } );
304
433
  } );
305
434
  return await download( exportdata, res );
@@ -324,6 +453,11 @@ export const summaryTableV1 = async ( req, res ) => {
324
453
  export const footfallTrendV1 = async ( req, res ) => {
325
454
  try {
326
455
  let reqestData = req.body;
456
+ let getClientData = await getClientConfig( reqestData.clientId );
457
+ if ( !getClientData ) {
458
+ return res.sendError( 'Invalid Client Id', 400 );
459
+ }
460
+ reqestData.featureConfigs = getClientData.featureConfigs;
327
461
  let LamdaURL = 'https://l5uacls7kfyham5d7pd3i6c6pq0pflem.lambda-url.ap-south-1.on.aws/';
328
462
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
329
463
  if ( resultData ) {
@@ -344,6 +478,11 @@ export const footfallTrendV1 = async ( req, res ) => {
344
478
  export const storeOperationV1 = async ( req, res ) => {
345
479
  try {
346
480
  let reqestData = req.body;
481
+ let getClientData = await getClientConfig( reqestData.clientId );
482
+ if ( !getClientData ) {
483
+ return res.sendError( 'Invalid Client Id', 400 );
484
+ }
485
+ reqestData.featureConfigs = getClientData.featureConfigs;
347
486
  let LamdaURL = 'https://mezoc2jy25t73v6eit57s3px6i0hxkwk.lambda-url.ap-south-1.on.aws/';
348
487
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
349
488
  if ( resultData ) {
@@ -364,10 +503,39 @@ export const storeOperationV1 = async ( req, res ) => {
364
503
  export const performanceMatrixV1 = async ( req, res ) => {
365
504
  try {
366
505
  let reqestData = req.body;
506
+ let getClientData = await getClientConfig( reqestData.clientId );
507
+ if ( !getClientData ) {
508
+ return res.sendError( 'Invalid Client Id', 400 );
509
+ }
510
+ reqestData.featureConfigs = getClientData.featureConfigs;
367
511
  let LamdaURL = 'https://5l3pjpivqpyboludmbridailpu0axrov.lambda-url.ap-south-1.on.aws/';
368
512
  let resultData = await LamdaServiceCall( LamdaURL, reqestData );
369
513
  if ( resultData ) {
370
514
  if ( resultData.status_code == '200' ) {
515
+ let data = [];
516
+ resultData.performanceData.forEach( ( item, index ) => {
517
+ let findprevIndex = data.findLastIndex( ( ele ) => ele.position == item.position );
518
+ if ( findprevIndex != -1 ) {
519
+ item.top = data[findprevIndex].top + 7;
520
+ item.left = data[findprevIndex].left + 5;
521
+ } else {
522
+ if ( item.position == 'High_Low' ) {
523
+ item.left = 5;
524
+ item.top = 10;
525
+ } else if ( item.position == 'High_High' ) {
526
+ item.left = 50;
527
+ item.top = 10;
528
+ } else if ( item.position == 'Low_Low' ) {
529
+ item.left = 5;
530
+ item.top = 55;
531
+ } else {
532
+ item.left = 50;
533
+ item.top = 55;
534
+ }
535
+ }
536
+ data.push( item );
537
+ } );
538
+ resultData.performanceData = data;
371
539
  return res.sendSuccess( resultData );
372
540
  } else {
373
541
  return res.sendError( 'No Content', 204 );
@@ -381,6 +549,31 @@ export const performanceMatrixV1 = async ( req, res ) => {
381
549
  }
382
550
  };
383
551
 
552
+ export const zoneDwellTimeSplitV1 = async ( req, res ) => {
553
+ try {
554
+ let reqestData = req.body;
555
+ let getClientData = await getClientConfig( reqestData.clientId );
556
+ if ( !getClientData ) {
557
+ return res.sendError( 'Invalid Client Id', 400 );
558
+ }
559
+ reqestData.featureConfigs = getClientData.featureConfigs;
560
+ let LamdaURL = 'https://7tfzazsi6lcejnjdzijv7bwg7y0yxhdx.lambda-url.ap-south-1.on.aws/';
561
+ let resultData = await LamdaServiceCall( LamdaURL, reqestData );
562
+ if ( resultData ) {
563
+ if ( resultData.status_code == '200' ) {
564
+ return res.sendSuccess( resultData );
565
+ } else {
566
+ return res.sendError( 'No Content', 204 );
567
+ }
568
+ } else {
569
+ return res.sendError( 'No Content', 204 );
570
+ }
571
+ } catch ( error ) {
572
+ logger.error( { error: error, message: req.query, function: 'trafficCards' } );
573
+ return res.sendError( { error: error }, 500 );
574
+ }
575
+ };
576
+
384
577
  export const storesMapV1 = async ( req, res ) => {
385
578
  try {
386
579
  let reqestData = req.body;
@@ -392,8 +585,8 @@ export const storesMapV1 = async ( req, res ) => {
392
585
  $and: [
393
586
  { 'storeProfile.latitude': { $exists: true } },
394
587
  { 'storeProfile.latitude': { $gt: 0 } },
395
- { 'storeProfile.latitude': { $exists: true } },
396
- { 'storeProfile.latitude': { $gt: 0 } },
588
+ { 'storeProfile.longitude': { $exists: true } },
589
+ { 'storeProfile.longitude': { $gt: 0 } },
397
590
  ],
398
591
  },
399
592
  },
@@ -404,10 +597,12 @@ export const storesMapV1 = async ( req, res ) => {
404
597
  'storeName': 1,
405
598
  'appId': 1,
406
599
  'storeProfile': 1,
600
+ 'lat': '$storeProfile.latitude',
601
+ 'lng': '$storeProfile.longitude',
407
602
  'spocDetails': { $arrayElemAt: [ '$spocDetails', 0 ] },
408
603
  'businessType': 1,
409
604
  'storeType': 1,
410
- 'avgFootfall': '100',
605
+ 'avgFootfall': '',
411
606
  'avgWeekdayFootfall': '',
412
607
  'avgWeekendFootfall': '',
413
608
  'hourlyFootfall': '',
@@ -428,43 +623,12 @@ export const storesMapV1 = async ( req, res ) => {
428
623
  return res.sendError( 'No Content', 204 );
429
624
  } else {
430
625
  if ( getStores.length >0 ) {
431
- reqestData.fromDate = reqestData.storeDate;
432
- reqestData.toDate = reqestData.storeDate;
433
- let hourlyLamdaURL = 'https://ksxkjnhwwzvrgp7ngttw2ribsy0twqip.lambda-url.ap-south-1.on.aws/';
434
- let hourlyResultData = await LamdaServiceCall( hourlyLamdaURL, reqestData );
435
- if ( hourlyResultData && hourlyResultData.status_code == '200' && hourlyResultData.hourData.length>0 ) {
436
- for ( let i = 0; i < getStores.length; i++ ) {
437
- for ( let j = 0; j < hourlyResultData.hourData.length; j++ ) {
438
- if ( getStores[i].storeId == hourlyResultData.hourData[j].storeId ) {
439
- getStores[i].hourlyFootfall = hourlyResultData.hourData[j].hourlyFootfall;
440
- getStores[i].openTime = hourlyResultData.hourData[j].openTime;
441
- getStores[i].closeTime = hourlyResultData.hourData[j].closeTime;
442
- }
443
- }
444
- }
445
- }
446
- let fromDate = new Date( dayjs( reqestData.fromDate ).subtract( 30, 'days' ) );
447
- reqestData.fromDate = dayjs( fromDate ).format( 'YYYY-MM-DD' );
448
- let footfallLamdaURL = 'https://3ircml3r6dm7fbiif3ti2wwdee0zqhyb.lambda-url.ap-south-1.on.aws/';
449
- let footfallResultData = await LamdaServiceCall( footfallLamdaURL, reqestData );
450
- if ( footfallResultData && footfallResultData.status_code == '200' && footfallResultData.avgData.length>0 ) {
451
- for ( let k = 0; k < getStores.length; k++ ) {
452
- for ( let l = 0; l < footfallResultData.avgData.length; l++ ) {
453
- if ( getStores[k].storeId == footfallResultData.avgData[l].storeId ) {
454
- getStores[k].avgWeekendFootfall = footfallResultData.avgData[l].avgWeekendFootfall;
455
- getStores[k].avgWeekdayFootfall = footfallResultData.avgData[l].avgWeekdayFootfall;
456
- getStores[k].avgFootfall = footfallResultData.avgData[l].avgFootfall;
457
- }
458
- }
459
- }
460
- }
461
626
  return res.sendSuccess( { storeData: getStores } );
462
627
  } else {
463
628
  return res.sendError( 'No Content', 204 );
464
629
  }
465
630
  }
466
631
  } catch ( error ) {
467
- console.log( 'storesMapV1 error =>', error );
468
632
  logger.error( { error: error, message: req.query, function: 'storesMapV1' } );
469
633
  return res.sendError( { error: error }, 500 );
470
634
  }
@@ -519,7 +683,56 @@ export const headerLocationsV1 = async ( req, res ) => {
519
683
  return res.sendError( { error: error }, 500 );
520
684
  }
521
685
  };
522
-
686
+ export const headerLocationsV2 = async ( req, res ) => {
687
+ try {
688
+ let reqestData = req.body;
689
+ let getUserEmail = req.user.email;
690
+ let getUserType = req.user.userType;
691
+ let getRole = req.user.role;
692
+ let getClientId = reqestData.clientId;
693
+ let totalStores = await getStoresList( getUserEmail, getClientId, getUserType, getRole, req );
694
+ if ( totalStores && totalStores.length>0 ) {
695
+ let storeQuery = [
696
+ {
697
+ $match: {
698
+ $and: [
699
+ { clientId: { $eq: getClientId } },
700
+ { storeId: { $in: totalStores } },
701
+ ],
702
+ },
703
+ },
704
+ {
705
+ $group: {
706
+ _id: '$storeProfile.city',
707
+ },
708
+ },
709
+ {
710
+ $project: {
711
+ _id: 0,
712
+ city: '$_id',
713
+ },
714
+ },
715
+ {
716
+ $sort: {
717
+ city: -1,
718
+ },
719
+ },
720
+ ];
721
+ const cityList = await aggregateStore( storeQuery );
722
+ if ( cityList && cityList.length > 0 ) {
723
+ let city = cityList.filter( ( data ) => data.city!='' );
724
+ return res.sendSuccess( { locationData: city } );
725
+ } else {
726
+ return res.sendError( 'No City', 400 );
727
+ }
728
+ } else {
729
+ return res.sendError( 'No stores', 400 );
730
+ }
731
+ } catch ( error ) {
732
+ logger.error( { error: error, message: req.query, function: 'trafficCards' } );
733
+ return res.sendError( { error: error }, 500 );
734
+ }
735
+ };
523
736
  export const headerGroupsV1 = async ( req, res ) => {
524
737
  try {
525
738
  let requestData = req.body;
@@ -528,7 +741,7 @@ export const headerGroupsV1 = async ( req, res ) => {
528
741
  let getRole = req.user.role;
529
742
  let groupIds;
530
743
  if ( requestData.city && requestData.city.length>0 ) {
531
- let getGroupIds = await getCityStores( requestData.clientId, getUserEmail, getRole, requestData.city, getUserType );
744
+ let getGroupIds = await getCityStores( requestData.clientId, getUserEmail, getRole, requestData.city, getUserType, req );
532
745
  if ( getGroupIds && getGroupIds.length>0 ) {
533
746
  groupIds = getGroupIds;
534
747
  } else {
@@ -590,12 +803,150 @@ export const headerGroupsV1 = async ( req, res ) => {
590
803
  return res.sendError( 'No Group', 400 );
591
804
  }
592
805
  } catch ( error ) {
593
- console.log( 'error =>', error );
594
- logger.error( { error: error, message: req.query, function: 'trafficCards' } );
806
+ logger.error( { error: error, message: req.query, function: 'headerGroupsV1' } );
595
807
  return res.sendError( { error: error }, 500 );
596
808
  }
597
809
  };
810
+ export async function headerClustersV2( req, res ) {
811
+ try {
812
+ let requestData = req.body;
813
+ let getUserEmail = req.user.email;
814
+ let getUserType = req.user.userType;
815
+ let getRole = req.user.role;
816
+ let clusterNames=[];
817
+ if ( requestData.city && requestData.city.length>0 ) {
818
+ let getcityclustes = await getCityClusters( requestData.clientId, getUserEmail, getRole, requestData.city, getUserType, req );
819
+ if ( getcityclustes && getcityclustes.length>0 ) {
820
+ clusterNames = getcityclustes;
821
+ } else {
822
+ clusterNames = [];
823
+ }
824
+ } else {
825
+ if ( getUserType == 'tango' ) {
826
+ clusterNames = await getclusterList( requestData.clientId, getUserType, getRole, req );
827
+ } else if ( getUserType == 'client' ) {
828
+ if ( getRole == 'superadmin' ) {
829
+ clusterNames = await getclusterList( requestData.clientId, getUserType, getRole, req );
830
+ } else {
831
+ clusterNames = await getclusterList( requestData.clientId, getUserType, getRole, req );
832
+ }
833
+ }
834
+ }
835
+ if ( clusterNames.length>0 ) {
836
+ const dataObjects = clusterNames.map( ( item ) => ( { groupName: item } ) );
837
+ res.sendSuccess( { groupData: dataObjects } );
838
+ } else {
839
+ res.sendSuccess( { groupData: [] } );
840
+ }
841
+ } catch ( error ) {
842
+ logger.error( { error: error, message: req.query, function: 'headerClustersV2' } );
843
+ return res.sendError( { error: error }, 500 );
844
+ }
845
+ }
846
+
847
+
848
+ async function getCityClusters( userClientId, getUserEmail, getRole, requestCity, getUserType, req ) {
849
+ try {
850
+ if ( userClientId && userClientId !='' && getUserEmail && getUserEmail !='', getRole && getRole !='', requestCity && requestCity.length>0 ) {
851
+ let cityStores = await getLocationStores( userClientId, requestCity, req );
852
+ if ( cityStores && cityStores.length > 0 ) {
853
+ let storeGroups = await getClusterStoresIds( userClientId, cityStores, getRole, getUserType, getUserEmail, req );
854
+ if ( storeGroups && storeGroups.length > 0 ) {
855
+ return storeGroups;
856
+ } else {
857
+ return false;
858
+ }
859
+ } else {
860
+ return false;
861
+ }
862
+ } else {
863
+ return false;
864
+ }
865
+ } catch ( error ) {
866
+ logger.error( { error: error, function: 'getCityClusters' } );
867
+ return false;
868
+ }
869
+ }
870
+ async function getClusterStoresIds( userClientId, storeIds, getRole, getUserType, getUserEmail, req ) {
871
+ try {
872
+ if ( userClientId && userClientId !='' && storeIds && storeIds !='' && getRole && getRole !='' && getUserType && getUserType !='', getUserEmail && getUserEmail !='' ) {
873
+ let clusterQuery = [];
874
+ if ( getUserType == 'client' ) {
875
+ if ( getRole == 'superadmin' ) {
876
+ clusterQuery = [
877
+ {
878
+ $match: {
879
+ $and: [
880
+ { clientId: { $eq: userClientId } },
881
+ { 'stores.storeId': { $in: storeIds } },
882
+ ],
883
+ },
884
+ },
885
+ {
886
+ $group: {
887
+ _id: null,
888
+ clusterName: { $push: '$clusterName' },
889
+ },
890
+ },
891
+ ];
892
+ } else {
893
+ let getAssignedGroupIds = [
894
+ {
895
+ $match: {
896
+ $and: [
897
+ { clientId: { $eq: userClientId } },
898
+ { 'stores.storeId': { $in: storeIds } },
899
+ { 'Teamlead.email': req.user.email },
900
+ ],
901
+ },
902
+ },
903
+ {
904
+ $group: {
905
+ _id: null,
906
+ clusterName: { $push: '$clusterName' },
907
+ },
908
+ },
909
+ ];
910
+ const assignedclusters = await aggregateCluster( getAssignedGroupIds );
911
+ if ( assignedclusters && assignedclusters.length>0 && assignedclusters[0]?.clusterName.length > 0 ) {
912
+ let uniqueclusters = [ ...new Set( assignedclusters[0].clusterName ) ];
913
+ return uniqueclusters;
914
+ }
915
+ }
916
+ } else if ( getUserType == 'tango' ) {
917
+ clusterQuery = [
918
+ {
919
+ $match: {
920
+ $and: [
921
+ { clientId: { $eq: userClientId } },
922
+ { 'stores.storeId': { $in: storeIds } },
923
+ ],
924
+ },
925
+ },
926
+ {
927
+ $group: {
928
+ _id: null,
929
+ clusterName: { $push: '$clusterName' },
930
+ },
931
+ },
932
+ ];
933
+ }
598
934
 
935
+ const assignedclusters = await aggregateCluster( clusterQuery );
936
+ if ( assignedclusters && assignedclusters.length>0 && assignedclusters[0]?.clusterName.length > 0 ) {
937
+ let uniqueclusters = [ ...new Set( assignedclusters[0].clusterName ) ];
938
+ return uniqueclusters;
939
+ } else {
940
+ return false;
941
+ }
942
+ } else {
943
+ return false;
944
+ }
945
+ } catch ( error ) {
946
+ logger.error( { error: error, function: 'getGroupIds' } );
947
+ return false;
948
+ }
949
+ }
599
950
  export const headerStoresV1 = async ( req, res ) => {
600
951
  try {
601
952
  let reqestData = req.body;
@@ -695,11 +1046,144 @@ export const headerStoresV1 = async ( req, res ) => {
695
1046
  return res.sendError( 'No Stores', 400 );
696
1047
  }
697
1048
  } catch ( error ) {
698
- console.log( 'headerStoresV1 =>', error );
699
1049
  logger.error( { error: error, message: req.query, function: 'headerStoresV1' } );
700
1050
  return res.sendError( { error: error }, 500 );
701
1051
  }
702
1052
  };
1053
+ export const headerStoresV2 = async ( req, res ) => {
1054
+ try {
1055
+ let reqestData = req.body;
1056
+ let getUserEmail = req.user.email;
1057
+ let getUserType = req.user.userType;
1058
+ let getRole = req.user.role;
1059
+ let getClientId = reqestData.clientId;
1060
+ let totalStores = await getStoresList( getUserEmail, getClientId, getUserType, getRole, req );
1061
+ if ( totalStores && totalStores.length>0 ) {
1062
+ let storeQuery = [];
1063
+ if ( reqestData.city.length>0 && reqestData.clusters.length>0 ) {
1064
+ let unqueCityStores = await getLocationStores( getClientId, reqestData.city, req );
1065
+ let unqueGroupStores = await getClusterStores( getClientId, reqestData.clusters );
1066
+ storeQuery = [
1067
+ {
1068
+ $match: {
1069
+ $and: [
1070
+ { storeId: { $in: totalStores } },
1071
+ { storeId: { $in: unqueCityStores } },
1072
+ { storeId: { $in: unqueGroupStores } },
1073
+ ],
1074
+ },
1075
+ },
1076
+ {
1077
+ $project: {
1078
+ _id: 0,
1079
+ storeId: '$storeId',
1080
+ storeName: '$storeName',
1081
+ },
1082
+ },
1083
+ ];
1084
+ } else if ( reqestData.city.length>0 ) {
1085
+ let uniqueCityStores = await getLocationStores( getClientId, reqestData.city, req );
1086
+ storeQuery = [
1087
+ {
1088
+ $match: {
1089
+ $and: [
1090
+ { storeId: { $in: totalStores } },
1091
+ { storeId: { $in: uniqueCityStores } },
1092
+ ],
1093
+ },
1094
+ },
1095
+ {
1096
+ $project: {
1097
+ _id: 0,
1098
+ storeId: '$storeId',
1099
+ storeName: '$storeName',
1100
+ },
1101
+ },
1102
+ ];
1103
+ } else if ( reqestData.clusters.length>0 ) {
1104
+ let uniqueclusterStores = await getClusterStores( getClientId, reqestData.clusters );
1105
+ storeQuery = [
1106
+ {
1107
+ $match: {
1108
+ $and: [
1109
+ { storeId: { $in: totalStores } },
1110
+ { storeId: { $in: uniqueclusterStores } },
1111
+ ],
1112
+ },
1113
+ },
1114
+ {
1115
+ $project: {
1116
+ _id: 0,
1117
+ storeId: '$storeId',
1118
+ storeName: '$storeName',
1119
+ },
1120
+ },
1121
+ ];
1122
+ } else {
1123
+ let totalStores = await getStoresList( getUserEmail, getClientId, getUserType, getRole, req );
1124
+ storeQuery = [
1125
+ {
1126
+ $match: {
1127
+ $and: [
1128
+ { storeId: { $in: totalStores } },
1129
+ ],
1130
+ },
1131
+ },
1132
+ {
1133
+ $project: {
1134
+ _id: 0,
1135
+ storeId: '$storeId',
1136
+ storeName: '$storeName',
1137
+ },
1138
+ },
1139
+ ];
1140
+ }
1141
+
1142
+ const storeList = await aggregateStore( storeQuery );
1143
+ if ( storeList && storeList.length > 0 ) {
1144
+ return res.sendSuccess( { storesData: storeList } );
1145
+ } else {
1146
+ return res.sendError( 'No Stores', 400 );
1147
+ }
1148
+ } else {
1149
+ return res.sendError( 'No Stores', 400 );
1150
+ }
1151
+ } catch ( error ) {
1152
+ logger.error( { error: error, message: req.query, function: 'headerStoresV2' } );
1153
+ return res.sendError( { error: error }, 500 );
1154
+ }
1155
+ };
1156
+
1157
+
1158
+ async function getStoresList( getUserEmail, getClientId, getUserType, getRole, req ) {
1159
+ try {
1160
+ if ( getUserEmail && getUserEmail !='' && getClientId && getClientId !='' && getUserType && getUserType !='' && getRole && getRole!='' ) {
1161
+ let overAllStores = [];
1162
+ if ( getUserType == 'tango' ) {
1163
+ let getAllS = await getAssignedAllStores( getClientId );
1164
+ if ( getAllS && getAllS.length >0 ) {
1165
+ overAllStores = getAllS;
1166
+ }
1167
+ return overAllStores;
1168
+ } else if ( getUserType == 'client' ) {
1169
+ if ( getRole == 'superadmin' ) {
1170
+ let getAllS = await getAssignedAllStores( getClientId );
1171
+ if ( getAllS && getAllS.length >0 ) {
1172
+ overAllStores = getAllS;
1173
+ }
1174
+ return overAllStores;
1175
+ } else {
1176
+ return req.body.assignedStores;
1177
+ }
1178
+ }
1179
+ } else {
1180
+ return false;
1181
+ }
1182
+ } catch ( error ) {
1183
+ logger.error( { error: error, message: req.query, function: 'getAllStores' } );
1184
+ }
1185
+ };
1186
+
703
1187
 
704
1188
  async function getAllStores( getUserEmail, getClientId, getUserType, getRole ) {
705
1189
  try {
@@ -760,7 +1244,6 @@ async function getAllStores( getUserEmail, getClientId, getUserType, getRole ) {
760
1244
  return false;
761
1245
  }
762
1246
  } catch ( error ) {
763
- console.log( 'getAllStores =>', error );
764
1247
  logger.error( { error: error, message: req.query, function: 'getAllStores' } );
765
1248
  }
766
1249
  };
@@ -796,8 +1279,8 @@ async function getAssignedStores( clientId, userEmail, assignedType ) {
796
1279
  return false;
797
1280
  }
798
1281
  } catch ( error ) {
799
- console.log( 'getAssignedStores error =>', error );
800
1282
  logger.error( { error: error, message: data, function: 'getAssignedStores' } );
1283
+ return false;
801
1284
  }
802
1285
  }
803
1286
 
@@ -863,8 +1346,8 @@ async function getAssignedGroupStores( clientId, userEmail, assignedType ) {
863
1346
  return false;
864
1347
  }
865
1348
  } catch ( error ) {
866
- console.log( 'getAssignedGroupStores error =>', error );
867
1349
  logger.error( { error: error, message: data, function: 'getAssignedGroupStores' } );
1350
+ return false;
868
1351
  }
869
1352
  }
870
1353
 
@@ -898,8 +1381,61 @@ async function getAssignedAllStores( userClientId ) {
898
1381
  return false;
899
1382
  }
900
1383
  } catch ( error ) {
901
- console.log( 'getAssignedAllStores error =>', error );
902
1384
  logger.error( { error: error, message: data, function: 'getAssignedAllStores' } );
1385
+ return false;
1386
+ }
1387
+ }
1388
+
1389
+
1390
+ async function getClusterStores( userClientId, ClusterList ) {
1391
+ try {
1392
+ if ( userClientId && userClientId !='' && ClusterList && ClusterList.length >0 ) {
1393
+ let ClusterQuery = [
1394
+ {
1395
+ $match: {
1396
+ $and: [
1397
+ { clientId: { $eq: userClientId } },
1398
+ { clusterName: { $in: ClusterList } },
1399
+ ],
1400
+ },
1401
+ },
1402
+ {
1403
+ $unwind: {
1404
+ path: '$storeList', preserveNullAndEmptyArrays: true,
1405
+ },
1406
+ },
1407
+ {
1408
+ $group: {
1409
+ _id: null,
1410
+ storesList: { $push: '$stores' },
1411
+ },
1412
+ },
1413
+ {
1414
+ $project: {
1415
+ storesList: {
1416
+ $reduce: {
1417
+ input: '$storesList', // Merge arrays using $reduce
1418
+ initialValue: [],
1419
+ in: { $concatArrays: [ '$$value', '$$this' ] },
1420
+ },
1421
+ },
1422
+ },
1423
+ },
1424
+ ];
1425
+ const clusterStoreList = await aggregateCluster( ClusterQuery );
1426
+ if ( clusterStoreList && clusterStoreList.length>0 && clusterStoreList[0]?.storesList.length > 0 ) {
1427
+ let storeIds = clusterStoreList[0].storesList.map( ( data ) => data.storeId );
1428
+ let uniqueStores = [ ...new Set( storeIds ) ];
1429
+ return uniqueStores;
1430
+ } else {
1431
+ return false;
1432
+ }
1433
+ } else {
1434
+ return false;
1435
+ }
1436
+ } catch ( error ) {
1437
+ logger.error( { error: error, message: data, function: 'getClusterStores' } );
1438
+ return false;
903
1439
  }
904
1440
  }
905
1441
 
@@ -938,21 +1474,26 @@ async function getGroupStores( userClientId, groupList ) {
938
1474
  return false;
939
1475
  }
940
1476
  } catch ( error ) {
941
- console.log( 'getGroupStores error =>', error );
942
1477
  logger.error( { error: error, message: data, function: 'getGroupStores' } );
1478
+ return false;
943
1479
  }
944
1480
  }
945
1481
 
946
- async function getLocationStores( userClientId, cityList ) {
1482
+ async function getLocationStores( userClientId, cityList, req ) {
947
1483
  try {
948
1484
  if ( userClientId && userClientId !='' && cityList && cityList.length >0 ) {
1485
+ let filter = [
1486
+ { clientId: { $eq: userClientId } },
1487
+ { 'storeProfile.city': { $in: cityList } },
1488
+ ];
1489
+ if ( req.body.assignedStores&&req.body.assignedStores.length>0 ) {
1490
+ filter.push( { storeId: { $in: req.body.assignedStores } } );
1491
+ }
1492
+
949
1493
  let storeQuery = [
950
1494
  {
951
1495
  $match: {
952
- $and: [
953
- { clientId: { $eq: userClientId } },
954
- { 'storeProfile.city': { $in: cityList } },
955
- ],
1496
+ $and: filter,
956
1497
  },
957
1498
  },
958
1499
  {
@@ -972,6 +1513,48 @@ async function getLocationStores( userClientId, cityList ) {
972
1513
  if ( cityStoreList && cityStoreList.length>0 && cityStoreList[0]?.stores.length > 0 ) {
973
1514
  let uniqueStores = [ ...new Set( cityStoreList[0].stores ) ];
974
1515
  return uniqueStores;
1516
+ } else {
1517
+ return [];
1518
+ }
1519
+ } else {
1520
+ return [];
1521
+ }
1522
+ } catch ( error ) {
1523
+ logger.error( { error: error, function: 'getLocationStores' } );
1524
+ return false;
1525
+ }
1526
+ }
1527
+ async function getclusterList( userClientId, getUserType, getRole, req ) {
1528
+ try {
1529
+ if ( userClientId && userClientId !='' ) {
1530
+ let filter = [
1531
+ { clientId: { $eq: userClientId } },
1532
+ ];
1533
+ if ( req.body.assignedStores&&req.body.assignedStores.length>0 ) {
1534
+ filter.push( { 'stores.storeId': { $in: req.body.assignedStores } } );
1535
+ }
1536
+ if ( getUserType == 'client'&&getRole!='superadmin' ) {
1537
+ filter.push( { 'Teamlead.email': req.user.email } );
1538
+ }
1539
+
1540
+
1541
+ let clusterQuery = [
1542
+ {
1543
+ $match: {
1544
+ $and: filter,
1545
+ },
1546
+ },
1547
+ {
1548
+ $group: {
1549
+ _id: null,
1550
+ clusterName: { $push: '$clusterName' },
1551
+ },
1552
+ },
1553
+ ];
1554
+ const clusterIds = await aggregateCluster( clusterQuery );
1555
+ if ( clusterIds && clusterIds.length>0 && clusterIds[0]?.clusterName.length > 0 ) {
1556
+ let uniqueclusterIds = [ ...new Set( clusterIds[0].clusterName ) ];
1557
+ return uniqueclusterIds;
975
1558
  } else {
976
1559
  return false;
977
1560
  }
@@ -979,11 +1562,10 @@ async function getLocationStores( userClientId, cityList ) {
979
1562
  return false;
980
1563
  }
981
1564
  } catch ( error ) {
982
- console.log( 'getLocationStores error =>', error );
983
- logger.error( { error: error, message: data, function: 'getLocationStores' } );
1565
+ logger.error( { error: error, function: 'getclusterList' } );
1566
+ return false;
984
1567
  }
985
1568
  }
986
-
987
1569
  async function getGroupIds( userClientId ) {
988
1570
  try {
989
1571
  if ( userClientId && userClientId !='' ) {
@@ -1013,8 +1595,8 @@ async function getGroupIds( userClientId ) {
1013
1595
  return false;
1014
1596
  }
1015
1597
  } catch ( error ) {
1016
- console.log( 'getGroupIds error =>', error );
1017
1598
  logger.error( { error: error, message: data, function: 'getGroupIds' } );
1599
+ return false;
1018
1600
  }
1019
1601
  }
1020
1602
 
@@ -1053,15 +1635,15 @@ async function getAssignedGroupIds( userClientId, getUserEmail ) {
1053
1635
  return false;
1054
1636
  }
1055
1637
  } catch ( error ) {
1056
- console.log( 'getAssignedGroupIds error =>', error );
1057
1638
  logger.error( { error: error, message: data, function: 'getAssignedGroupIds' } );
1639
+ return false;
1058
1640
  }
1059
1641
  }
1060
1642
 
1061
- async function getCityStores( userClientId, getUserEmail, getRole, requestCity, getUserType ) {
1643
+ async function getCityStores( userClientId, getUserEmail, getRole, requestCity, getUserType, req ) {
1062
1644
  try {
1063
1645
  if ( userClientId && userClientId !='' && getUserEmail && getUserEmail !='', getRole && getRole !='', requestCity && requestCity.length>0 ) {
1064
- let cityStores = await getLocationStores( userClientId, requestCity );
1646
+ let cityStores = await getLocationStores( userClientId, requestCity, req );
1065
1647
  if ( cityStores && cityStores.length > 0 ) {
1066
1648
  let storeGroups = await getGroupStoresIds( userClientId, cityStores, getRole, getUserType, getUserEmail );
1067
1649
  if ( storeGroups && storeGroups.length > 0 ) {
@@ -1076,8 +1658,8 @@ async function getCityStores( userClientId, getUserEmail, getRole, requestCity,
1076
1658
  return false;
1077
1659
  }
1078
1660
  } catch ( error ) {
1079
- console.log( 'getAssignedGroupIds error =>', error );
1080
1661
  logger.error( { error: error, message: data, function: 'getAssignedGroupIds' } );
1662
+ return false;
1081
1663
  }
1082
1664
  }
1083
1665
 
@@ -1176,45 +1758,173 @@ async function getGroupStoresIds( userClientId, storeIds, getRole, getUserType,
1176
1758
  return false;
1177
1759
  }
1178
1760
  } catch ( error ) {
1179
- console.log( 'getGroupIds error =>', error );
1180
1761
  logger.error( { error: error, message: data, function: 'getGroupIds' } );
1762
+ return false;
1181
1763
  }
1182
1764
  }
1183
1765
 
1184
- export async function isAllowedClient( req, res, next ) {
1766
+ async function getClientConfig( clientId ) {
1767
+ try {
1768
+ let getClientData = await clientService.findOne( { clientId: clientId }, { 'paymentInvoice.currencyType': 1, 'averageTransactionValue': 1, 'featureConfigs.billableCalculation': 1, 'featureConfigs.missedOpportunityCalculation': 1, 'featureConfigs.conversionCalculation': 1, 'featureConfigs.open': 1, 'featureConfigs.close': 1, 'isFootfallAuditStores': 1 } );
1769
+ if ( !getClientData ) {
1770
+ return false;
1771
+ }
1772
+ return getClientData;
1773
+ } catch ( error ) {
1774
+ logger.error( { error: error, message: data, function: 'getClientConfig' } );
1775
+ return false;
1776
+ }
1777
+ }
1778
+
1779
+ export const getMySubscription = async ( req, res ) => {
1185
1780
  try {
1186
1781
  let reqestData = req.body;
1187
- let getUserEmail = req.user.email;
1188
- let getUserType = req.user.userType;
1189
- let getClientId = req.user.clientId;
1190
- let getRole = req.user.role;
1191
- if ( getUserType == 'tango' ) {
1192
- if ( getRole == 'superadmin' ) {
1193
- next();
1194
- } else {
1195
- const assignedQuery = {
1196
- userEmail: getUserEmail,
1197
- assignedType: 'client',
1198
- assignedValue: reqestData.clientId,
1782
+ let getClientData = await clientService.findOne( { clientId: reqestData.clientId }, { planDetails: 1 } );
1783
+ if ( getClientData ) {
1784
+ if ( getClientData.planDetails && getClientData.planDetails.product && getClientData.planDetails.product.length>0 ) {
1785
+ let clientSubscription = {};
1786
+ let clientProducts = {
1787
+ tangoTraffic: false,
1788
+ tangoZone: false,
1789
+ tangoTrax: false,
1199
1790
  };
1200
- const getAssignedType = await findOneUserAssignedStore( assignedQuery );
1201
- if ( getAssignedType ) {
1202
- next();
1791
+ clientSubscription.subscriptionType = getClientData.planDetails.subscriptionType;
1792
+ let actualProduct = getClientData.planDetails.product;
1793
+ for ( let clientIndex = 0; clientIndex < actualProduct.length; clientIndex++ ) {
1794
+ if ( actualProduct[clientIndex].productName == 'tangoTraffic' ) {
1795
+ clientProducts.tangoTraffic = true;
1796
+ }
1797
+ if ( actualProduct[clientIndex].productName == 'tangoZone' ) {
1798
+ clientProducts.tangoZone = true;
1799
+ }
1800
+ if ( actualProduct[clientIndex].tangoTrax == 'tangoTrax' ) {
1801
+ clientProducts.tangoTrax = true;
1802
+ }
1803
+ }
1804
+ clientSubscription.product = clientProducts;
1805
+ let storeSubscription = {};
1806
+ let storeProducts = {
1807
+ tangoTraffic: false,
1808
+ tangoZone: false,
1809
+ tangoTrax: false,
1810
+ };
1811
+ if ( reqestData.storeId && reqestData.storeId.length>0 ) {
1812
+ let getStoreData = await findOneStore( { clientId: reqestData.clientId, storeId: { $in: reqestData.storeId[0] } }, { product: 1 } );
1813
+ if ( getStoreData ) {
1814
+ for ( let storeIndex = 0; storeIndex < getStoreData.product.length; storeIndex++ ) {
1815
+ if ( getStoreData.product[storeIndex] == 'tangoTraffic' ) {
1816
+ storeProducts.tangoTraffic = true;
1817
+ }
1818
+ if ( getStoreData.product[storeIndex] == 'tangoZone' ) {
1819
+ storeProducts.tangoZone = true;
1820
+ }
1821
+ if ( getStoreData.product[storeIndex] == 'tangoTrax' ) {
1822
+ storeProducts.tangoTrax = true;
1823
+ }
1824
+ }
1825
+ storeSubscription.product = storeProducts;
1826
+ } else {
1827
+ storeSubscription.product = [];
1828
+ }
1203
1829
  } else {
1204
- return res.sendError( 'Client Not Assigned', 400 );
1830
+ storeSubscription.product = [];
1205
1831
  }
1206
- }
1207
- } else if ( getUserType == 'client' ) {
1208
- if ( getClientId == reqestData.clientId ) {
1209
- next();
1832
+ let resultData = { clientSubscription, storeSubscription };
1833
+ return res.sendSuccess( resultData );
1210
1834
  } else {
1211
- return res.sendError( 'Client Not Assigned', 400 );
1835
+ return res.sendError( 'Not Subscribe', 400 );
1212
1836
  }
1213
1837
  } else {
1214
- return res.sendError( 'Client Not Assigned', 400 );
1838
+ return res.sendError( 'Invalid Client Id', 400 );
1215
1839
  }
1216
1840
  } catch ( error ) {
1217
- logger.error( { error: error, function: 'isAllowedClient' } );
1218
- return res.sendError( error, 500 );
1841
+ logger.error( { error: error, message: req.query, function: 'getMySubscription' } );
1842
+ return res.sendError( { error: error }, 500 );
1219
1843
  }
1220
- }
1844
+ };
1845
+
1846
+ export const getStoreMapData = async ( req, res ) => {
1847
+ try {
1848
+ let reqestData = req.body;
1849
+ if ( reqestData.storeId && reqestData.storeId.length > 0 ) {
1850
+ let getClientData = await getClientConfig( reqestData.clientId );
1851
+ if ( !getClientData ) {
1852
+ return res.sendError( 'Invalid Client Id', 400 );
1853
+ }
1854
+ reqestData.featureConfigs = getClientData.featureConfigs;
1855
+ let cameraBaseImage = '';
1856
+ const bucket= JSON.parse( process.env.BUCKET );
1857
+ if ( bucket && bucket.baseImage && bucket.baseImage != '' ) {
1858
+ const camera = await findCamera( { storeId: reqestData.storeId[0], isUp: true, isActivated: true }, { thumbnailImage: 1 } );
1859
+ if ( camera && camera.thumbnailImage && camera.thumbnailImage != '' ) {
1860
+ const params = {
1861
+ file_path: camera.thumbnailImage,
1862
+ Bucket: bucket.baseImage,
1863
+ };
1864
+ cameraBaseImage = await signedUrl( params );
1865
+ }
1866
+ }
1867
+ reqestData.fromDate = reqestData.storeDate;
1868
+ reqestData.toDate = reqestData.storeDate;
1869
+ let avgData = {
1870
+ 'avgFootfall': '',
1871
+ 'avgWeekdayFootfall': '',
1872
+ 'avgWeekendFootfall': '',
1873
+ };
1874
+ let hourlyData = {
1875
+ 'hourlyFootfall': '',
1876
+ 'openTime': '',
1877
+ 'closeTime': '',
1878
+ };
1879
+ let hourlyLamdaURL = 'https://ksxkjnhwwzvrgp7ngttw2ribsy0twqip.lambda-url.ap-south-1.on.aws/';
1880
+ let hourlyResultData = await LamdaServiceCall( hourlyLamdaURL, reqestData );
1881
+ if ( hourlyResultData && hourlyResultData.hourData.length>0 ) {
1882
+ hourlyData.hourlyFootfall = hourlyResultData.hourData[0].hourlyFootfall;
1883
+ hourlyData.openTime = hourlyResultData.hourData[0].openTime;
1884
+ hourlyData.closeTime = hourlyResultData.hourData[0].closeTime;
1885
+ }
1886
+ let fromDate = new Date( dayjs( reqestData.fromDate ).subtract( 30, 'days' ) );
1887
+ reqestData.fromDate = dayjs( fromDate ).format( 'YYYY-MM-DD' );
1888
+ let footfallLamdaURL = 'https://3ircml3r6dm7fbiif3ti2wwdee0zqhyb.lambda-url.ap-south-1.on.aws/';
1889
+ let footfallResultData = await LamdaServiceCall( footfallLamdaURL, reqestData );
1890
+ if ( footfallResultData && footfallResultData.avgData&&footfallResultData.avgData.length>0 ) {
1891
+ avgData.avgFootfall = footfallResultData.avgData[0].avgFootfall;
1892
+ avgData.avgWeekdayFootfall = footfallResultData.avgData[0].avgWeekdayFootfall;
1893
+ avgData.avgWeekendFootfall = footfallResultData.avgData[0].avgWeekendFootfall;
1894
+ }
1895
+ return res.sendSuccess( { cameraBaseImage: cameraBaseImage, hourlyData: hourlyData, avgData: avgData } );
1896
+ } else {
1897
+ return res.sendError( 'Store Id rewuired', 400 );
1898
+ }
1899
+ } catch ( error ) {
1900
+ logger.error( { error: error, message: req.query, function: 'getMySubscription' } );
1901
+ return res.sendError( { error: error }, 500 );
1902
+ }
1903
+ };
1904
+
1905
+ // async function getGeocodedAddress( lat, lng ) {
1906
+ // try {
1907
+ // const apiKey = 'AIzaSyDlOezgwQO0JviD0aizrCuN1FY9tcWfR3o'; // Use this if you're using dotenv
1908
+ // const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${apiKey}`;
1909
+
1910
+ // const requestOptions = {
1911
+ // method: 'GET',
1912
+ // headers: {
1913
+ // 'Content-Type': 'application/json',
1914
+ // },
1915
+ // };
1916
+ // const response = await fetch( url, requestOptions );
1917
+ // console.log( 'response =>', response );
1918
+ // if ( !response.ok ) {
1919
+ // throw new Error( `Response status: ${response.status}` );
1920
+ // return false;
1921
+ // }
1922
+ // const json = await response.json();
1923
+ // return json;
1924
+ // } catch ( error ) {
1925
+ // console.log( 'getGeocodedAddress error =>', error );
1926
+ // logger.error( { error: error, message: data, function: 'getGeocodedAddress' } );
1927
+ // }
1928
+ // }
1929
+ // let getGEO = await getGeocodedAddress( 12.900260100404893, 80.23384232089138 );
1930
+ // console.log( 'getGEO =>', getGEO );