Fixed linked user resolution

This commit is contained in:
2026-10-04 18:04:36 +02:00
parent 84a171d4a4
commit c9deba0427
4 changed files with 110 additions and 24 deletions
@@ -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<User[]>(['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 = () => {
</div>
<select
{...orgForm.register('organisationUser', {
onChange: (e) => {
const selected = usersDirectory.find((u) => u.id === e.target.value);
if (selected?.idNumber && !orgForm.getValues('idNumber')) {
orgForm.setValue('idNumber', selected.idNumber);
onChange: async (e) => {
const selectedId = e.target.value;
let selected = usersDirectory.find((u) => u.id === selectedId);
if (!selected && selectedId) {
try {
selected = await getUser(selectedId);
} catch {
// ignore
}
}
if (selected?.idNumber) {
orgForm.setValue('idNumber', selected.idNumber, { shouldValidate: true });
}
},
})}