2 Commits
@@ -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,93 +265,38 @@ 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<TenantUser[]>({
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(() => {
let list: User[] = [];
if (isAdmin) {
list = usersDirectory.filter((u) => String(u.role || '').toLowerCase() === 'executive');
} else {
list = tenantUsersList
.map((tu) => tu.user || ({ id: tu.userId, email: getUserEmail(tu.userId), role: 'Executive' } as User))
.filter((u) => String(u.role || '').toLowerCase() === 'executive');
}
const currentLinkedUserId =
const list: User[] = [...executiveAndManagerUsers];
const linkedUserId =
editingOrg?.userId ||
editingOrg?.UserId ||
editingOrg?.organisationUser ||
editingOrg?.OrganisationUser ||
'';
if (currentLinkedUserId) {
const alreadyIncluded = list.some(
(u) => u.id && String(u.id).toLowerCase() === String(currentLinkedUserId).toLowerCase()
);
if (linkedUserId) {
const linkedStr = String(linkedUserId).toLowerCase();
const alreadyIncluded = list.some((u) => u.id && String(u.id).toLowerCase() === linkedStr);
if (!alreadyIncluded) {
const resolvedEmail = getUserEmail(currentLinkedUserId);
const matchedInDir = usersDirectory.find(
(u) => u.id && String(u.id).toLowerCase() === String(currentLinkedUserId).toLowerCase()
);
list = [
{
id: currentLinkedUserId,
email: matchedInDir?.email || (resolvedEmail !== '—' && resolvedEmail ? resolvedEmail : currentLinkedUserId),
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,
},
...list,
];
});
}
}
}
return list;
}, [isAdmin, usersDirectory, tenantUsersList, editingOrg]);
useEffect(() => {
if (isOrgModalOpen && editingOrg) {
const targetUserId =
editingOrg.userId ||
editingOrg.UserId ||
editingOrg.organisationUser ||
editingOrg.OrganisationUser ||
'';
if (targetUserId) {
const targetEmail = getUserEmail(targetUserId);
const matched = modalExecutiveUsers.find(
(u) =>
(u.id && String(u.id).toLowerCase() === String(targetUserId).toLowerCase()) ||
(u.email && targetEmail && targetEmail !== '—' && u.email.toLowerCase() === targetEmail.toLowerCase())
);
const resolvedId = matched?.id || targetUserId;
orgForm.setValue('organisationUser', resolvedId, { shouldValidate: true, shouldDirty: false });
}
}
}, [isOrgModalOpen, editingOrg, modalExecutiveUsers]);
}, [executiveAndManagerUsers, usersDirectory, editingOrg?.id, editingOrg?.userId, editingOrg?.organisationUser]);
// Quick Add Executive User Subform state
const [isQuickAddUserOpen, setIsQuickAddUserOpen] = useState(false);
@@ -833,22 +775,28 @@ export const GovernancePage: React.FC = () => {
org.createdBy ||
'';
let selectedUserId = '';
if (targetUserId) {
const email = getUserEmail(targetUserId);
if (email && email !== '—') {
registerUserEmail(targetUserId, email);
}
}
const matchedUser = targetUserId
? usersDirectory.find(
const targetStr = String(targetUserId).toLowerCase();
const matchedUser = usersDirectory.find(
(u) =>
(u.id && String(u.id).toLowerCase() === String(targetUserId).toLowerCase()) ||
(u.email && getUserEmail(targetUserId) && u.email.toLowerCase() === getUserEmail(targetUserId).toLowerCase())
)
: null;
(u.id && String(u.id).toLowerCase() === targetStr) ||
(u.email && u.email.toLowerCase() === targetStr)
);
const selectedUserId = matchedUser?.id || targetUserId || '';
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);
}
}
}
orgForm.reset({
name: org.name || '',
@@ -1817,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"
>
<option value="">-- Select linked executive or manager account --</option>
{(editingOrg ? modalExecutiveUsers : executiveAndManagerUsers).map((u) => (
{modalExecutiveUsers.map((u) => (
<option key={u.id} value={u.id}>
{u.email || u.id} ({u.role || 'Executive'})
</option>