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

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.63",
4
4
  "description": "infra",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -1808,71 +1808,85 @@ export async function ticketApprove( req, res, next ) {
1808
1808
  }
1809
1809
  }
1810
1810
 
1811
-
1812
1811
  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
- }
1812
+ if ( !user || user.userType !== 'client' || user.role === 'superadmin' ) {
1813
+ return;
1814
+ }
1828
1815
 
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
- }
1816
+ const clientId = user.clientId;
1817
+ const storeIds = new Set(
1818
+ user.assignedStores?.map( ( store ) => store.storeId ) ?? [],
1819
+ );
1837
1820
 
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
- }
1821
+ const addClusterStores = ( clusters ) => {
1822
+ if ( !clusters?.length ) return;
1823
+ for ( const cluster of clusters ) {
1824
+ cluster.stores?.forEach( ( store ) => storeIds.add( store.storeId ) );
1847
1825
  }
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
- }
1826
+ };
1827
+
1828
+ // Fetch all top-level data in parallel
1829
+ const [ clustersList, teamsList, teamMemberList ] = await Promise.all( [
1830
+ findcluster( {
1831
+ clientId,
1832
+ Teamlead: { $elemMatch: { email: user.email } },
1833
+ } ),
1834
+ findteams( {
1835
+ clientId,
1836
+ Teamlead: { $elemMatch: { email: user.email } },
1837
+ } ),
1838
+ findteams( {
1839
+ clientId,
1840
+ users: { $elemMatch: { email: user.email } },
1841
+ } ),
1842
+ ] );
1843
+
1844
+ // 1) Clusters where this user is Teamlead
1845
+ addClusterStores( clustersList );
1846
+
1847
+ // 2) Teams where this user is Teamlead → their users + their clusters
1848
+ if ( teamsList?.length ) {
1849
+ for ( const team of teamsList ) {
1850
+ if ( !team.users?.length ) continue;
1851
+
1852
+ await Promise.all(
1853
+ team.users.map( async ( teamUser ) => {
1854
+ const foundUser = await findOneUser( { _id: teamUser.userId } );
1855
+ if ( !foundUser ) return;
1856
+
1857
+ // Direct assigned stores of that user
1858
+ if ( foundUser.assignedStores?.length ) {
1859
+ foundUser.assignedStores.forEach( ( store ) =>
1860
+ storeIds.add( store.storeId ),
1861
+ );
1862
+ }
1863
+
1864
+ // Clusters where this user is Teamlead
1865
+ const userClustersList = await findcluster( {
1866
+ clientId,
1867
+ Teamlead: { $elemMatch: { email: foundUser.email } },
1868
+ } );
1869
+ addClusterStores( userClustersList );
1870
+ } ),
1871
+ );
1869
1872
  }
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;
1873
+ }
1874
+
1875
+ // 3) Teams where this user is a member → clusters by teamName
1876
+ if ( teamMemberList?.length ) {
1877
+ for ( const team of teamMemberList ) {
1878
+ const clusterList = await findcluster( {
1879
+ clientId,
1880
+ teams: { $elemMatch: { name: team.teamName } },
1881
+ } );
1882
+ addClusterStores( clusterList );
1876
1883
  }
1877
1884
  }
1885
+
1886
+ const assignedStores = Array.from( storeIds );
1887
+
1888
+ // Previously you returned `true` in both branches.
1889
+ // Assuming you actually want to check membership:
1890
+ return assignedStores.includes( storeId );
1878
1891
  }
1892
+