tango-app-api-analysis-traffic 3.0.0-alpha.2 → 3.0.0-alpha.3

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-analysis-traffic",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "Traffic Analysis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -414,6 +414,7 @@ export const headerLocationsV1 = async ( req, res ) => {
414
414
  {
415
415
  $match: {
416
416
  $and: [
417
+ { clientId: { $eq: getClientId } },
417
418
  { storeId: { $in: totalStores } },
418
419
  ],
419
420
  },
@@ -452,67 +453,58 @@ export const headerLocationsV1 = async ( req, res ) => {
452
453
 
453
454
  export const headerGroupsV1 = async ( req, res ) => {
454
455
  try {
455
- // let requestData = req.body;
456
+ let requestData = req.body;
456
457
  let getUserEmail = req.user.email;
457
- let groupQuery = [
458
- {
459
- $match: {
460
- $and: [
461
- { userEmail: { $eq: getUserEmail } },
462
- { assignedType: { $eq: 'group' } },
463
- ],
464
- },
465
- },
466
- {
467
- $group: {
468
- _id: '$assignedValue',
469
- },
470
- },
471
- {
472
- $lookup: {
473
- from: 'groups',
474
- let: { groupId: { $toObjectId: '$_id' } },
475
- pipeline: [
476
- {
477
- $match: {
478
- $expr: {
479
- $eq: [ '$_id', '$$groupId' ],
480
- },
481
- },
482
- },
483
- {
484
- $project: {
485
- _id: 0,
486
- groupName: 1,
487
- },
488
- },
489
- ], as: 'groups',
490
- },
491
- },
492
- {
493
- $unwind: {
494
- path: '$groups', preserveNullAndEmptyArrays: true,
495
- },
496
- },
497
- {
498
- $unwind: {
499
- path: '$groups.groupName', preserveNullAndEmptyArrays: true,
458
+ const assignedQuery = {
459
+ clientId: requestData.clientId,
460
+ userEmail: getUserEmail,
461
+ };
462
+ let groupIds;
463
+ const getAssignedType = await findOneUserAssignedStore( assignedQuery );
464
+ if ( getAssignedType ) {
465
+ if ( getAssignedType.userType == 'client' ) {
466
+ if ( getAssignedType.assignedType == 'allstores' ) {
467
+ groupIds = await getGroupIds( requestData.clientId );
468
+ } else if ( getAssignedType.assignedType == 'group' ) {
469
+ groupIds = await getAssignedGroupIds( requestData.clientId, getUserEmail );
470
+ }
471
+ } else if ( getAssignedType.userType == 'tango' ) {
472
+ groupIds = await getGroupIds( requestData.clientId );
473
+ } else {
474
+ groupIds = [];
475
+ }
476
+ } else {
477
+ return res.sendError( 'No User Assign', 400 );
478
+ }
479
+
480
+ if ( groupIds && groupIds.length > 0 ) {
481
+ let groupQuery = [
482
+ {
483
+ $match: {
484
+ $and: [
485
+ { clientId: { $eq: requestData.clientId } },
486
+ { _id: { $in: groupIds } },
487
+ ],
488
+ },
500
489
  },
501
- },
502
- {
503
- $project: {
504
- _id: 0,
505
- groupName: '$groups.groupName',
490
+ {
491
+ $project: {
492
+ _id: 0,
493
+ groupName: 1,
494
+ },
506
495
  },
507
- },
508
- ];
509
- const groupList = await aggregateUserAssignedStore( groupQuery );
510
- if ( groupList && groupList.length > 0 ) {
511
- return res.sendSuccess( { groupData: groupList } );
496
+ ];
497
+ const groupResult = await aggregateGroup( groupQuery );
498
+ if ( groupResult && groupResult.length > 0 ) {
499
+ return res.sendSuccess( { groupData: groupResult } );
500
+ } else {
501
+ return res.sendError( 'No Group', 400 );
502
+ }
512
503
  } else {
513
504
  return res.sendError( 'No Group', 400 );
514
505
  }
515
506
  } catch ( error ) {
507
+ console.log( 'error =>', error );
516
508
  logger.error( { error: error, message: req.query, function: 'trafficCards' } );
517
509
  return res.sendError( { error: error }, 500 );
518
510
  }
@@ -607,7 +599,7 @@ export const headerStoresV1 = async ( req, res ) => {
607
599
 
608
600
  const storeList = await aggregateStore( storeQuery );
609
601
  if ( storeList && storeList.length > 0 ) {
610
- return res.sendSuccess( storeList );
602
+ return res.sendSuccess( { storesData: storeList } );
611
603
  } else {
612
604
  return res.sendError( 'No Stores', 400 );
613
605
  }
@@ -660,16 +652,12 @@ async function getAllStores( getUserEmail, getClientId ) {
660
652
  return false;
661
653
  }
662
654
  } else if ( getAssignedType.userType == 'tango' ) {
663
- if ( getAssignedType.assignedType && getAssignedType.assignedType !='' ) {
664
- let overAllStores = [];
665
- let getAllS = await getAssignedAllStores( getClientId );
666
- if ( getAllS && getAllS.length >0 ) {
667
- overAllStores = getAllS;
668
- }
669
- return overAllStores;
670
- } else {
671
- return false;
655
+ let overAllStores = [];
656
+ let getAllS = await getAssignedAllStores( getClientId );
657
+ if ( getAllS && getAllS.length >0 ) {
658
+ overAllStores = getAllS;
672
659
  }
660
+ return overAllStores;
673
661
  } else {
674
662
  return false;
675
663
  }
@@ -903,3 +891,77 @@ async function getLocationStores( userClientId, cityList ) {
903
891
  logger.error( { error: error, message: data, function: 'getLocationStores' } );
904
892
  }
905
893
  }
894
+
895
+ async function getGroupIds( userClientId ) {
896
+ try {
897
+ if ( userClientId && userClientId !='' ) {
898
+ let groupQuery = [
899
+ {
900
+ $match: {
901
+ $and: [
902
+ { clientId: { $eq: userClientId } },
903
+ ],
904
+ },
905
+ },
906
+ {
907
+ $group: {
908
+ _id: null,
909
+ groupName: { $push: '$_id' },
910
+ },
911
+ },
912
+ ];
913
+ const groupGroupIds = await aggregateGroup( groupQuery );
914
+ if ( groupGroupIds && groupGroupIds.length>0 && groupGroupIds[0]?.groupName.length > 0 ) {
915
+ let uniqueGroupIds = [ ...new Set( groupGroupIds[0].groupName ) ];
916
+ return uniqueGroupIds;
917
+ } else {
918
+ return false;
919
+ }
920
+ } else {
921
+ return false;
922
+ }
923
+ } catch ( error ) {
924
+ console.log( 'getGroupIds error =>', error );
925
+ logger.error( { error: error, message: data, function: 'getGroupIds' } );
926
+ }
927
+ }
928
+
929
+ async function getAssignedGroupIds( userClientId, getUserEmail ) {
930
+ try {
931
+ if ( userClientId && userClientId !='' && getUserEmail && getUserEmail !='' ) {
932
+ let groupQuery = [
933
+ {
934
+ $match: {
935
+ $and: [
936
+ { clientId: { $eq: userClientId } },
937
+ { userEmail: { $eq: getUserEmail } },
938
+ { assignedType: { $eq: 'group' } },
939
+ ],
940
+ },
941
+ },
942
+ {
943
+ $group: {
944
+ _id: null,
945
+ groupName: { $push: '$assignedValue' },
946
+ },
947
+ },
948
+ ];
949
+ const groupStoreList = await aggregateUserAssignedStore( groupQuery );
950
+ if ( groupStoreList && groupStoreList.length>0 && groupStoreList[0]?.groupName.length > 0 ) {
951
+ let uniqueGroupIdsData = [ ...new Set( groupStoreList[0].groupName ) ];
952
+ let uniqueGroupIds = [];
953
+ for ( let i = 0; i < uniqueGroupIdsData.length; i++ ) {
954
+ uniqueGroupIds.push( new ObjectId( uniqueGroupIdsData[i] ) );
955
+ }
956
+ return uniqueGroupIds;
957
+ } else {
958
+ return false;
959
+ }
960
+ } else {
961
+ return false;
962
+ }
963
+ } catch ( error ) {
964
+ console.log( 'getAssignedGroupIds error =>', error );
965
+ logger.error( { error: error, message: data, function: 'getAssignedGroupIds' } );
966
+ }
967
+ }