teqo/src/utilities/access/demands.ts
Francisco Solla 2cc8f2c14d feat(C143): demandas visíveis apenas para responsáveis explícitos + candidato/coordenador
- campaignDemand.responsibles (hasMany → campaignUser, staff elegível) com
  hook beforeValidate que descarta ids não-staff (fail-closed)
- canRead/UpdateCampaignDemand por membership: assessor só vê demandas onde
  é responsável; unrestricted/admin sempre; leader nunca (fim do escopo
  municipal de demandas)
- criador entra automaticamente como responsável no hook de criação (form,
  wizard, rascunhos C90); migration com backfill created_by_id → responsável
- UI: picker multi-select na criação (nova + wizard) e card de gestão no
  detalhe com save full-replace + advisory lock; sugestões = assessores do
  município; rótulo 'Visível para: Responsáveis · Candidato · Coordenador'
- int tests reescritos para a regra nova + 5 casos novos; e2e novo de
  visibilidade (lista/URL/gestão); fixture e2e com createCampaignDemand
- impl plan + changelog; importMap regenerado com S3_* dummy (OPS69)
2026-08-19 23:55:07 -03:00

57 lines
2.1 KiB
TypeScript

// ---------------------------------------------------------------------------
// Campaign demands
// ---------------------------------------------------------------------------
import type { Access, Where } from 'payload'
import { relationshipId } from '@/lib/relationship'
import { getAccessibleMunicipalityIds } from '@/utilities/access/municipalities'
import {
getFreshCampaignUser,
isCampaignLeader,
isCampaignUnrestricted,
isPayloadAdmin,
} from '@/utilities/access/shared'
export const canCreateCampaignDemand: Access = async ({ data, req }) => {
if (isPayloadAdmin(req.user)) return true
const currentUser = await getFreshCampaignUser(req)
if (!currentUser || isCampaignLeader(currentUser)) return false
if (isCampaignUnrestricted(currentUser)) return true
const municipalityID = relationshipId(data?.municipality)
if (!municipalityID) return false
if (currentUser.role !== 'advisor') return false
const municipalityIDs = await getAccessibleMunicipalityIds(req, currentUser)
return municipalityIDs?.includes(municipalityID) ?? false
}
/**
* C143 — explicit-responsible visibility, fail-closed. An advisor reads ONLY
* demands where they are a listed responsible (the creator is auto-added on
* create); a municipality-related advisor who is not responsible sees nothing
* — not the list, not the URL, not the search (all reads run the collection
* access). Candidate and coordinator (unrestricted) always see everything.
*/
export const canReadCampaignDemand: Access = async ({ req }): Promise<boolean | Where> => {
if (isPayloadAdmin(req.user)) return true
const currentUser = await getFreshCampaignUser(req)
if (!currentUser || isCampaignLeader(currentUser)) return false
if (isCampaignUnrestricted(currentUser)) return true
if (currentUser.role !== 'advisor') return false
return { responsibles: { contains: currentUser.id } }
}
/**
* Staff manage demands they can see — same row scope as read, which is also
* who may manage the `responsibles` list (C143).
*/
export const canUpdateCampaignDemand: Access = canReadCampaignDemand
export const canDeleteCampaignDemand: Access = ({ req }) => isPayloadAdmin(req.user)