From 08aba3fb4d76d5090a6d9eebf9f4b768d414af78 Mon Sep 17 00:00:00 2001 From: Nathnael Date: Tue, 25 Aug 2026 13:05:55 +0000 Subject: [PATCH] fix --- .../components/positionSelection.tsx | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/apps/edr-freight-web/backoffice/src/record-management/components/positionSelection.tsx b/apps/edr-freight-web/backoffice/src/record-management/components/positionSelection.tsx index 889552384..b014425e5 100644 --- a/apps/edr-freight-web/backoffice/src/record-management/components/positionSelection.tsx +++ b/apps/edr-freight-web/backoffice/src/record-management/components/positionSelection.tsx @@ -25,6 +25,22 @@ interface BasePosition { isDelegate: boolean; } +/** + * `x-delegated-position-id` names the DELEGATOR's position and the API matches + * it against `position.id`. This used to be set for every selection, carrying + * `employeePositionId` — so it matched nothing, and because the API resolves + * the delegation header *before* the current-position one, it swallowed the + * whole selection: every request ran as the employee's first position no + * matter what the picker said. Only a genuine delegate carries it. + */ +const applyDelegationCookie = (position?: BasePosition | null) => { + if (position?.isDelegate && position.id) { + Cookies.set("delegatedPositionId", position.id); + return; + } + Cookies.remove("delegatedPositionId"); +}; + export const PositionSelect = () => { const { unFilteredUserDetails, @@ -63,26 +79,32 @@ export const PositionSelect = () => { setSelectedPositionId(currentPosition.employeePositionId); } - if (Cookies.get("current-position-id") !== currentPosition.id) { - Cookies.set("current-position-id", currentPosition.id); - } - + // The API matches x-current-position-id against employeePositionId, NOT + // position.id — writing the latter never matches, so the guard silently + // falls back to the employee's first position and the picker does nothing. + // (useAuthUser already self-heals this cookie for the same reason.) if ( currentPosition.employeePositionId && - Cookies.get("delegatedPositionId") !== currentPosition.employeePositionId + Cookies.get("current-position-id") !== currentPosition.employeePositionId ) { - Cookies.set("delegatedPositionId", currentPosition.employeePositionId); + Cookies.set("current-position-id", currentPosition.employeePositionId); } + + applyDelegationCookie(currentPosition); }, [currentPosition, isLoading, selectedPositionId, setSelectedPositionId]); if (isLoading || selectablePositions.length === 0) return null; const handleChange = (value: string) => { - setSelectedPositionId(value); const selected = selectablePositions.find((pos) => pos.id === value); - Cookies.set("current-position-id", value); - Cookies.set("delegatedPositionId", selected?.employeePositionId || ""); + // Both the picker state and the cookie key off employeePositionId — the + // dropdown's own value is position.id, which the API does not match on. + setSelectedPositionId(selected?.employeePositionId ?? value); + if (selected?.employeePositionId) { + Cookies.set("current-position-id", selected.employeePositionId); + } + applyDelegationCookie(selected); // Invalidate relevant queries [