teqo/src/utilities/sameOriginRequest.ts
Francisco Ignácio Fontoura Solla 7c50e29aa7 B27: combobox + chips + auto-save no popover de Assessores da lista de municípios
Replaces the checkbox-list + "Salvar assessores" button in the
Assessors popover (/campanha/municipios) with removable chips + a
Command combobox, delta-based auto-save (one advisor add/remove per
request, never the whole array), and optimistic UI with request
sequencing to resolve out-of-order responses correctly.

- New POST /campanha/municipios/advisors JSON route (mirrors
  expected-votes/), backed by setMunicipalityAdvisorMembership(Record)
  in actions/municipality.ts (text advisory lock + transaction).
- nextAdvisorIdsAfterMembership moved to
  src/lib/municipalityAdvisorMembership.ts with unit tests.
- Shared isSameOriginRequest (src/utilities/sameOriginRequest.ts) and
  uniqueRelationshipIds (src/utilities/relationship.ts) helpers,
  de-duplicating the same-origin CSRF check and the
  Set+map(relationshipId)+filter pattern across advisor call sites.
- MunicipalityListAdvisorsControl.tsx rewritten: chips, Command
  combobox, per-item pending affordance, rollback on error, and a
  request-sequence guard so only the most recently sent delta's
  response is adopted when concurrent optimistic updates settle.
- e2e coverage for the combobox auto-save flow; setup prewarms the new
  POST-only route to avoid Playwright dev-server HMR aborting the
  first request.
- Plan and roadmap marked as delivered (B27).
2026-07-26 03:31:51 -03:00

15 lines
572 B
TypeScript

/**
* CSRF guard for cookie-authenticated JSON route handlers (auto-save popovers):
* a same-origin browser request either omits `Origin` (same-site navigation)
* or sends one matching the request URL; anything else is rejected before the
* body is even parsed. Shared by every `POST /campanha/**` JSON endpoint.
*/
export const isSameOriginRequest = (request: Request): boolean => {
const origin = request.headers.get('origin')
if (!origin) return true
try {
return new URL(origin).origin === new URL(request.url).origin
} catch {
return false
}
}