diff --git a/PostFundManagement.ConsoleUI/src/pages/GovernancePage.tsx b/PostFundManagement.ConsoleUI/src/pages/GovernancePage.tsx index 23435d3..64ee8aa 100644 --- a/PostFundManagement.ConsoleUI/src/pages/GovernancePage.tsx +++ b/PostFundManagement.ConsoleUI/src/pages/GovernancePage.tsx @@ -67,6 +67,7 @@ import { Instrument, Instruction, Rule, + User, OrganisationType, ActivityStatus, ApprovalStatus, @@ -564,20 +565,70 @@ export const GovernancePage: React.FC = () => { setIsOrgModalOpen(true); }; - const openEditOrg = (org: Organisation) => { + const openEditOrg = async (org: Organisation) => { setEditingOrg(org); - const linkedUserId = org.userId || org.createdBy || usersDirectory[0]?.id || currentUserId; - const matchedUser = usersDirectory.find((u) => u.id === linkedUserId); - orgForm.reset({ - name: org.name || '', - registrationNo: org.registrationNo || '', - email: org.email || '', - idNumber: matchedUser?.idNumber || (org as any).idNumber || '', - organisationUser: linkedUserId, - type: org.type || OrganisationType.Enterprise, - status: org.status ?? ActivityStatus.Active, - }); setIsOrgModalOpen(true); + + // 1. Fetch fresh organisation details from API + let currentOrg = org; + try { + const fresh = await getOrganisation(org.id); + if (fresh) { + currentOrg = fresh; + } + } catch { + // fallback to passed org + } + + // 2. Resolve linked user ID from organisation model + const targetUserId = + currentOrg.userId || + (currentOrg as any).UserId || + (currentOrg as any).organisationUser || + (currentOrg as any).OrganisationUser || + currentOrg.createdBy || + ''; + + // 3. Find user in directory or fetch user record to get cached user profile and idNumber + let matchedUser = usersDirectory.find( + (u) => u.id && String(u.id).toLowerCase() === String(targetUserId).toLowerCase() + ); + + if (!matchedUser && targetUserId) { + try { + const fetchedUser = await getUser(targetUserId); + if (fetchedUser) { + matchedUser = fetchedUser; + } + } catch { + // user fetch failed + } + } + + // 4. Ensure matched user is present in usersDirectory cache for dropdown rendering + if (matchedUser && !usersDirectory.some((u) => u.id === matchedUser!.id)) { + queryClient.setQueryData(['users-directory'], (old = []) => [matchedUser!, ...old]); + } + + // 5. Resolve idNumber from matched user profile or organisation + const resolvedIdNumber = + matchedUser?.idNumber || + currentOrg.idNumber || + (currentOrg as any).IdNumber || + (org as any).idNumber || + ''; + + const selectedUserId = matchedUser?.id || targetUserId || usersDirectory[0]?.id || currentUserId; + + orgForm.reset({ + name: currentOrg.name || '', + registrationNo: currentOrg.registrationNo || '', + email: currentOrg.email || '', + idNumber: resolvedIdNumber, + organisationUser: selectedUserId, + type: currentOrg.type || OrganisationType.Enterprise, + status: currentOrg.status ?? ActivityStatus.Active, + }); }; const openCreatePortfolio = () => { @@ -1448,10 +1499,18 @@ export const GovernancePage: React.FC = () => {