tango-app-api-infra 3.9.5-vms.62 → 3.9.5-vms.64

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.9.5-vms.62",
3
+ "version": "3.9.5-vms.64",
4
4
  "description": "infra",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -564,11 +564,13 @@ export async function ticketCreation( req, res, next ) {
564
564
  $match: {
565
565
  clientId: getstoreName.clientId,
566
566
  role: 'admin',
567
+ isActive: true,
567
568
  },
568
569
  },
569
570
  ];
570
571
 
571
572
  const finduserList = await aggregateUser( userQuery );
573
+ console.log( '🚀 ~ ticketCreation ~ finduserList:', finduserList.length );
572
574
 
573
575
  const createdOn = dayjs().format( 'DD MMM YYYY' );
574
576
  const title = `${getstoreName?.storeName} Have raised a ticket for a Footfall Mismatch`;
@@ -594,10 +596,12 @@ export async function ticketCreation( req, res, next ) {
594
596
  m.name === 'reviewer' && ( m.isAdd === true || m.isEdit === true ),
595
597
  ),
596
598
  );
599
+ console.log( '🚀 ~ ticketCreation ~ ticketsFeature:', ticketsFeature );
597
600
 
598
601
  if ( !ticketsFeature ) return;
599
602
 
600
603
  const notifyUser = await getAssinedStore( userData, req.body.storeId );
604
+ console.log( '🚀 ~ ticketCreation ~ notifyUser:', notifyUser );
601
605
  if ( !notifyUser || !userData?.fcmToken ) return;
602
606
 
603
607
  await sendPushNotification( title, description, userData.fcmToken, Data );
@@ -1260,11 +1264,12 @@ export async function ticketReview( req, res, next ) {
1260
1264
  $match: {
1261
1265
  clientId: getstoreName.clientId,
1262
1266
  role: 'admin',
1267
+ isActive: true,
1263
1268
  },
1264
1269
  },
1265
1270
  ];
1266
1271
  let finduserList = await aggregateUser( userQuery );
1267
- console.log( '🚀 ~ ticketReview ~ finduserList:', finduserList );
1272
+ console.log( '🚀 ~ ticketReview ~ finduserList:', finduserList.length );
1268
1273
 
1269
1274
  // return;
1270
1275
  for ( let userData of finduserList ) {
@@ -1759,6 +1764,7 @@ export async function ticketApprove( req, res, next ) {
1759
1764
  $match: {
1760
1765
  clientId: getstoreName.clientId,
1761
1766
  role: 'admin',
1767
+ isActive: true,
1762
1768
  },
1763
1769
  },
1764
1770
  ];
@@ -1808,71 +1814,85 @@ export async function ticketApprove( req, res, next ) {
1808
1814
  }
1809
1815
  }
1810
1816
 
1811
-
1812
1817
  export async function getAssinedStore( user, storeId ) {
1813
- if ( user && user.userType === 'client' && user.role !== 'superadmin' ) {
1814
- let storeIds = new Set( user.assignedStores?.map( ( store ) => store.storeId ) );
1815
-
1816
- // Fetch clusters and teams in parallel
1817
- const [ clustersList, teamsList ] = await Promise.all( [
1818
- findcluster( { clientId: user.clientId, Teamlead: { $elemMatch: { email: user.email } } } ),
1819
- findteams( { clientId: user.clientId, Teamlead: { $elemMatch: { email: user.email } } } ),
1820
- ] );
1821
-
1822
- // Process clusters
1823
- if ( clustersList.length > 0 ) {
1824
- for ( let cluster of clustersList ) {
1825
- cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
1826
- }
1827
- }
1818
+ if ( !user || user.userType !== 'client' || user.role === 'superadmin' ) {
1819
+ return;
1820
+ }
1828
1821
 
1829
- // Process teams
1830
- if ( teamsList.length > 0 ) {
1831
- for ( let team of teamsList ) {
1832
- for ( let user of team.users ) {
1833
- let findUser = await findOneUser( { _id: user.userId } );
1834
- if ( findUser && findUser.assignedStores?.length > 0 ) {
1835
- findUser.assignedStores.forEach( ( store ) => storeIds.add( store.storeId ) );
1836
- }
1822
+ const clientId = user.clientId;
1823
+ const storeIds = new Set(
1824
+ user.assignedStores?.map( ( store ) => store.storeId ) ?? [],
1825
+ );
1837
1826
 
1838
- // Fetch clusters for the user
1839
- let userClustersList = await findcluster( { clientId: user.clientId, Teamlead: { $elemMatch: { email: findUser.email } } } );
1840
- if ( userClustersList.length > 0 ) {
1841
- for ( let cluster of userClustersList ) {
1842
- cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
1843
- }
1844
- }
1845
- }
1846
- }
1827
+ const addClusterStores = ( clusters ) => {
1828
+ if ( !clusters?.length ) return;
1829
+ for ( const cluster of clusters ) {
1830
+ cluster.stores?.forEach( ( store ) => storeIds.add( store.storeId ) );
1847
1831
  }
1848
- let TeamMember = await findteams( { clientId: user.clientId, users: { $elemMatch: { email: user.email } } } );
1849
- if ( TeamMember && TeamMember.length > 0 ) {
1850
- for ( let team of TeamMember ) {
1851
- let clusterList = await findcluster( { clientId: user.clientId, teams: { $elemMatch: { name: team.teamName } } } );
1852
- if ( clusterList.length > 0 ) {
1853
- for ( let cluster of clusterList ) {
1854
- cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
1855
- }
1856
- }
1857
- }
1858
- }
1859
- let TeamLeader = await findteams( { clientId: user.clientId, Teamlead: { $elemMatch: { email: user.email } } } );
1860
- if ( TeamLeader && TeamLeader.length > 0 ) {
1861
- for ( let team of TeamLeader ) {
1862
- let clusterList = await findcluster( { clientId: user.clientId, teams: { $elemMatch: { name: team.teamName } } } );
1863
- if ( clusterList.length > 0 ) {
1864
- for ( let cluster of clusterList ) {
1865
- cluster.stores.forEach( ( store ) => storeIds.add( store.storeId ) );
1866
- }
1867
- }
1868
- }
1832
+ };
1833
+
1834
+ // Fetch all top-level data in parallel
1835
+ const [ clustersList, teamsList, teamMemberList ] = await Promise.all( [
1836
+ findcluster( {
1837
+ clientId,
1838
+ Teamlead: { $elemMatch: { email: user.email } },
1839
+ } ),
1840
+ findteams( {
1841
+ clientId,
1842
+ Teamlead: { $elemMatch: { email: user.email } },
1843
+ } ),
1844
+ findteams( {
1845
+ clientId,
1846
+ users: { $elemMatch: { email: user.email } },
1847
+ } ),
1848
+ ] );
1849
+
1850
+ // 1) Clusters where this user is Teamlead
1851
+ addClusterStores( clustersList );
1852
+
1853
+ // 2) Teams where this user is Teamlead → their users + their clusters
1854
+ if ( teamsList?.length ) {
1855
+ for ( const team of teamsList ) {
1856
+ if ( !team.users?.length ) continue;
1857
+
1858
+ await Promise.all(
1859
+ team.users.map( async ( teamUser ) => {
1860
+ const foundUser = await findOneUser( { _id: teamUser.userId } );
1861
+ if ( !foundUser ) return;
1862
+
1863
+ // Direct assigned stores of that user
1864
+ if ( foundUser.assignedStores?.length ) {
1865
+ foundUser.assignedStores.forEach( ( store ) =>
1866
+ storeIds.add( store.storeId ),
1867
+ );
1868
+ }
1869
+
1870
+ // Clusters where this user is Teamlead
1871
+ const userClustersList = await findcluster( {
1872
+ clientId,
1873
+ Teamlead: { $elemMatch: { email: foundUser.email } },
1874
+ } );
1875
+ addClusterStores( userClustersList );
1876
+ } ),
1877
+ );
1869
1878
  }
1870
- // Convert Set back to Array if needed
1871
- let assignedStores = Array.from( storeIds );
1872
- if ( assignedStores.includes( storeId ) ) {
1873
- return true;
1874
- } else {
1875
- return true;
1879
+ }
1880
+
1881
+ // 3) Teams where this user is a member → clusters by teamName
1882
+ if ( teamMemberList?.length ) {
1883
+ for ( const team of teamMemberList ) {
1884
+ const clusterList = await findcluster( {
1885
+ clientId,
1886
+ teams: { $elemMatch: { name: team.teamName } },
1887
+ } );
1888
+ addClusterStores( clusterList );
1876
1889
  }
1877
1890
  }
1891
+
1892
+ const assignedStores = Array.from( storeIds );
1893
+
1894
+ // Previously you returned `true` in both branches.
1895
+ // Assuming you actually want to check membership:
1896
+ return assignedStores.includes( storeId );
1878
1897
  }
1898
+