Prevent duplicate tenant memberships on import
All checks were successful
Build and Push Docker Images / build-backend (push) Successful in 37s
Build and Push Docker Images / build-frontend (push) Successful in 1m18s
Build and Push Docker Images / build-website (push) Successful in 17s
Build and Push Docker Images / build-docs (push) Successful in 16s

This commit is contained in:
2026-07-20 19:06:01 +02:00
parent 720c70845e
commit 37d08ee984
4 changed files with 119 additions and 4 deletions

View File

@@ -660,10 +660,16 @@ export default async function adminRoutes(server: FastifyInstance) {
})
.from(authUserRoles),
]);
const uniqueMembershipRows = Array.from(
new Map(membershipRows.map((membership) => [
`${membership.tenant_id}:${membership.user_id}`,
membership,
])).values()
);
const users = userRows.map((user) => {
const profiles = profileRows.filter((profile) => profile.user_id === user.id);
const memberships = membershipRows.filter((membership) => membership.user_id === user.id);
const memberships = uniqueMembershipRows.filter((membership) => membership.user_id === user.id);
const roleAssignments = roleAssignmentRows.filter((assignment) => assignment.user_id === user.id);
const preferredProfile = profiles.find((profile) => profile.active) || profiles[0];
const fallbackName = deriveNameFromEmail(user.email);
@@ -683,7 +689,7 @@ export default async function adminRoutes(server: FastifyInstance) {
const tenantsWithCounts = tenantRows.map((tenant) => ({
...tenant,
user_count: membershipRows.filter((membership) => membership.tenant_id === tenant.id).length,
user_count: uniqueMembershipRows.filter((membership) => membership.tenant_id === tenant.id).length,
}));
return {
@@ -691,7 +697,7 @@ export default async function adminRoutes(server: FastifyInstance) {
tenants: tenantsWithCounts,
roles: roleRows,
unassignedProfiles: profileRows.filter((profile) => !profile.user_id),
memberships: membershipRows,
memberships: uniqueMembershipRows,
roleAssignments: roleAssignmentRows,
};
} catch (err) {