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
|
@@ -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
|
|
1814
|
-
|
|
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
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
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
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
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
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
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
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
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
|
+
|