diff --git a/PostFundManagement.ConsoleUI/src/pages/GovernancePage.tsx b/PostFundManagement.ConsoleUI/src/pages/GovernancePage.tsx index 0e11b09..b53b2bd 100644 --- a/PostFundManagement.ConsoleUI/src/pages/GovernancePage.tsx +++ b/PostFundManagement.ConsoleUI/src/pages/GovernancePage.tsx @@ -62,7 +62,6 @@ import { assignUserRole, getSystemRoles, getCurrentUserTenantId, - getTenantUsers, notifyNewUserCreated, } from '../services/api'; import { Modal } from '../components/ui/Modal'; @@ -78,8 +77,6 @@ import { FundingInstrumentsSection, InstructionsSection } from '../components/go import { RulesSection } from '../components/governance/RulesSection'; import { Organisation, - TenantOrganisation, - TenantUser, Portfolio, Programme, Instrument, @@ -268,42 +265,8 @@ export const GovernancePage: React.FC = () => { }); }, [usersDirectory]); - const isAdmin = - Boolean(isUnrestricted) || - (Array.isArray(roles) && roles.some((r) => r.toLowerCase().trim() === 'admin')) || - (claims?.role && String(claims.role).toLowerCase().includes('admin')); - - const orgTenantId = editingOrg?.tenantId || tenantId || getCurrentUserTenantId() || 1; - - const { data: tenantUsersList = [] } = useQuery({ - queryKey: ['tenant-users', orgTenantId], - queryFn: () => getTenantUsers(orgTenantId), - enabled: isOrgModalOpen && !!editingOrg && !isAdmin, - }); - - useEffect(() => { - if (tenantUsersList.length > 0) { - tenantUsersList.forEach((tu) => { - if (tu.userId && tu.user?.email) { - registerUserEmail(tu.userId, tu.user.email); - } - }); - } - }, [tenantUsersList]); - const modalExecutiveUsers = React.useMemo(() => { - const list: User[] = []; - const seenIds = new Set(); - - const addCandidate = (u?: User | null) => { - if (!u || !u.id) return; - const key = String(u.id).toLowerCase(); - if (seenIds.has(key)) return; - seenIds.add(key); - list.push(u); - }; - - // 1. Always include the organisation's linked user so the select option exists and selects immediately + const list: User[] = [...executiveAndManagerUsers]; const linkedUserId = editingOrg?.userId || editingOrg?.UserId || @@ -312,44 +275,28 @@ export const GovernancePage: React.FC = () => { ''; if (linkedUserId) { - const resolvedEmail = getUserEmail(linkedUserId); - const matched = usersDirectory.find( - (u) => u.id && String(u.id).toLowerCase() === String(linkedUserId).toLowerCase() - ); - addCandidate({ - id: linkedUserId, - email: matched?.email || (resolvedEmail !== '—' && resolvedEmail ? resolvedEmail : linkedUserId), - role: matched?.role || 'Executive', - status: ActivityStatus.Active, - lastLoginAt: null, - }); - } - - // 2. Load Executive role users: Admin sees all executives across tenants; normal tenant user focuses on tenantusers - if (isAdmin) { - usersDirectory - .filter((u) => String(u.role || '').toLowerCase() === 'executive') - .forEach(addCandidate); - } else { - tenantUsersList - .filter((tu) => String(tu.user?.role || '').toLowerCase() === 'executive') - .forEach((tu) => { - if (tu.user) { - addCandidate(tu.user); - } else if (tu.userId) { - addCandidate({ - id: tu.userId, - email: getUserEmail(tu.userId), - role: 'Executive', - status: ActivityStatus.Active, - lastLoginAt: null, - }); - } - }); + const linkedStr = String(linkedUserId).toLowerCase(); + const alreadyIncluded = list.some((u) => u.id && String(u.id).toLowerCase() === linkedStr); + if (!alreadyIncluded) { + const inDirectory = usersDirectory.find((u) => u.id && String(u.id).toLowerCase() === linkedStr); + if (inDirectory) { + list.unshift(inDirectory); + } else { + const email = getUserEmail(linkedUserId); + const hasRealEmail = email && email !== '—' && email.includes('@') && email !== String(linkedUserId); + list.unshift({ + id: String(linkedUserId), + email: hasRealEmail ? email : String(linkedUserId), + role: 'Executive', + status: ActivityStatus.Active, + lastLoginAt: null, + }); + } + } } return list; - }, [isAdmin, usersDirectory, tenantUsersList, editingOrg?.id, editingOrg?.userId]); + }, [executiveAndManagerUsers, usersDirectory, editingOrg?.id, editingOrg?.userId, editingOrg?.organisationUser]); // Quick Add Executive User Subform state const [isQuickAddUserOpen, setIsQuickAddUserOpen] = useState(false); @@ -828,23 +775,29 @@ export const GovernancePage: React.FC = () => { org.createdBy || ''; + let selectedUserId = ''; if (targetUserId) { - const email = getUserEmail(targetUserId); - if (email && email !== '—') { - registerUserEmail(targetUserId, email); + const targetStr = String(targetUserId).toLowerCase(); + const matchedUser = usersDirectory.find( + (u) => + (u.id && String(u.id).toLowerCase() === targetStr) || + (u.email && u.email.toLowerCase() === targetStr) + ); + + if (matchedUser) { + selectedUserId = matchedUser.id; + if (matchedUser.email) { + registerUserEmail(matchedUser.id, matchedUser.email); + } + } else { + selectedUserId = String(targetUserId); + const email = getUserEmail(targetUserId); + if (email && email !== '—' && email.includes('@')) { + registerUserEmail(selectedUserId, email); + } } } - const matchedUser = targetUserId - ? usersDirectory.find( - (u) => - (u.id && String(u.id).toLowerCase() === String(targetUserId).toLowerCase()) || - (u.email && getUserEmail(targetUserId) && u.email.toLowerCase() === getUserEmail(targetUserId).toLowerCase()) - ) - : null; - - const selectedUserId = matchedUser?.id || targetUserId || ''; - orgForm.reset({ name: org.name || '', registrationNo: org.registrationNo || '', @@ -1812,7 +1765,7 @@ export const GovernancePage: React.FC = () => { className="w-full px-3 py-2 text-xs border border-slate-700/80 rounded-xl focus:outline-none focus:ring-1 focus:ring-blue-500 bg-[#090d16] text-white font-medium" > - {(editingOrg ? modalExecutiveUsers : executiveAndManagerUsers).map((u) => ( + {modalExecutiveUsers.map((u) => (