Files
twenty/packages
Sonarly Claude Code 4122987433 fix(server): handle missing role references gracefully in flat role target cache builder
https://sonarly.com/issue/15476?type=bug

View create/update/delete operations trigger a background error during cache invalidation because the flat role target map builder throws when a roleTarget references a role with missing or null `universalIdentifier`. The mutation succeeds but the UI hangs briefly waiting for cache invalidation to resolve.

Fix: The fix addresses the root cause in two cache services that build flat role target maps from database entities:

**1. `workspace-flat-role-target-map-cache.service.ts`** — The `computeForCache` method now pre-filters roleTargets before passing them to `fromRoleTargetEntityToFlatRoleTarget`. RoleTargets whose `roleId` or `applicationId` reference entities that either don't exist or have null `universalIdentifier` are skipped with a WARN-level log. This prevents the `FlatEntityMapsException` from being thrown during cache recomputation, which was crashing every view mutation on upgraded instances.

**2. `workspace-flat-role-target-by-agent-id.service.ts`** — Same fix applied to the agent-specific role target cache, which has the identical vulnerability. Without this fix, any operation that triggers `flatRoleTargetByAgentIdMaps` recomputation would also crash.

The validation in `fromRoleTargetEntityToFlatRoleTarget` is intentionally preserved — it serves as a safety net for programming errors where the caller passes incomplete maps. The pre-filtering in `computeForCache` handles the data integrity scenario (orphaned/incomplete data from upgrades) at the appropriate layer.
2026-03-17 04:14:45 +00:00
..