Fix: Sync Apply Group buttons with text input and automatically authorize group assets in backend getAssets query

This commit is contained in:
kenilkb 2026-07-16 12:52:26 +05:30
parent ed60948be4
commit 9170b607fe
2 changed files with 72 additions and 16 deletions

View File

@ -177,19 +177,43 @@ export class AssetService {
return []; return [];
} }
return await prisma.asset.findMany({ // Support comma-separated multiple groups
where: { const userGroups = user.partnerGroup
status: 'published', ? user.partnerGroup.split(',').map(s => s.trim().toLowerCase())
sharedWith: { : [];
some: {
organizationId: user.organizationId, const whereClause: any = {
OR: [ status: 'published',
{ userId: null }, OR: [
{ userId: user.id } {
] sharedWith: {
some: {
organizationId: user.organizationId,
OR: [
{ userId: null },
{ userId: user.id }
]
}
} }
} }
}, ]
};
if (userGroups.length > 0) {
whereClause.OR.push({
assetGroups: {
some: {
name: {
in: userGroups,
mode: 'insensitive'
}
}
}
});
}
return await prisma.asset.findMany({
where: whereClause,
include: { include: {
sharedWith: { sharedWith: {
include: { include: {

View File

@ -163,11 +163,43 @@ export const DirectoryPage: React.FC = () => {
getAssetGroups().then(setAssetGroups).catch(console.error); getAssetGroups().then(setAssetGroups).catch(console.error);
}, []); }, []);
const handleToggleInviteGroup = (groupName: string, select: boolean) => {
setPartnerGroup(prev => {
const groups = prev ? prev.split(',').map(s => s.trim()) : [];
if (select) {
if (!groups.some(g => g.toLowerCase() === groupName.toLowerCase())) {
groups.push(groupName);
}
} else {
const filtered = groups.filter(g => g.toLowerCase() !== groupName.toLowerCase());
return filtered.join(', ');
}
return groups.join(', ');
});
};
const handleToggleEditGroup = (groupName: string, select: boolean) => {
setEditGroup(prev => {
const groups = prev ? prev.split(',').map(s => s.trim()) : [];
if (select) {
if (!groups.some(g => g.toLowerCase() === groupName.toLowerCase())) {
groups.push(groupName);
}
} else {
const filtered = groups.filter(g => g.toLowerCase() !== groupName.toLowerCase());
return filtered.join(', ');
}
return groups.join(', ');
});
};
const renderAssetSelector = ( const renderAssetSelector = (
selectedIds: string[], selectedIds: string[],
setSelectedIds: React.Dispatch<React.SetStateAction<string[]>>, setSelectedIds: React.Dispatch<React.SetStateAction<string[]>>,
searchQuery: string, searchQuery: string,
setSearchQuery: React.Dispatch<React.SetStateAction<string>> setSearchQuery: React.Dispatch<React.SetStateAction<string>>,
onToggleGroup?: (groupName: string, select: boolean) => void
) => { ) => {
const filtered = allAssets.filter(asset => const filtered = allAssets.filter(asset =>
asset.title.toLowerCase().includes(searchQuery.toLowerCase()) asset.title.toLowerCase().includes(searchQuery.toLowerCase())
@ -181,8 +213,6 @@ export const DirectoryPage: React.FC = () => {
} }
}; };
return ( return (
<div className="space-y-2"> <div className="space-y-2">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
@ -220,6 +250,7 @@ export const DirectoryPage: React.FC = () => {
const groupAssetIds = g.assets.map(a => a.id); const groupAssetIds = g.assets.map(a => a.id);
if (hasAll) { if (hasAll) {
setSelectedIds(prev => prev.filter(id => !groupAssetIds.includes(id))); setSelectedIds(prev => prev.filter(id => !groupAssetIds.includes(id)));
if (onToggleGroup) onToggleGroup(g.name, false);
} else { } else {
setSelectedIds(prev => { setSelectedIds(prev => {
const newIds = [...prev]; const newIds = [...prev];
@ -228,6 +259,7 @@ export const DirectoryPage: React.FC = () => {
}); });
return newIds; return newIds;
}); });
if (onToggleGroup) onToggleGroup(g.name, true);
} }
}} }}
className={`px-2 py-0.5 rounded text-[9px] font-bold transition-all border cursor-pointer ${hasAll className={`px-2 py-0.5 rounded text-[9px] font-bold transition-all border cursor-pointer ${hasAll
@ -1233,7 +1265,7 @@ export const DirectoryPage: React.FC = () => {
</span> </span>
)} )}
</div> </div>
{renderAssetSelector(selectedAssetIds, setSelectedAssetIds, inviteAssetSearch, setInviteAssetSearch)} {renderAssetSelector(selectedAssetIds, setSelectedAssetIds, inviteAssetSearch, setInviteAssetSearch, handleToggleInviteGroup)}
</div> </div>
{inviteResult?.error && ( {inviteResult?.error && (
@ -1397,7 +1429,7 @@ export const DirectoryPage: React.FC = () => {
</span> </span>
)} )}
</div> </div>
{renderAssetSelector(editAssetIds, setEditAssetIds, editAssetSearch, setEditAssetSearch)} {renderAssetSelector(editAssetIds, setEditAssetIds, editAssetSearch, setEditAssetSearch, handleToggleEditGroup)}
</div> </div>
<div className="flex items-center gap-3 pt-2"> <div className="flex items-center gap-3 pt-2">