teqo/src/utilities/access/googleCalendarSync.ts
Francisco Solla 2f247d40a6 fix(C115): revisão /simplify — relógio com baseline de espelho, reopen sem re-cancelamento, hardening do webhook
- lastMirroredChangeAt (activity, migration): clock rule compara contra a
  última mudança espelhada, não updatedAt (task toggle não silencia edição
  pendente do Google)
- snapshot exclui ids que o motor deletou (removedEventIds separado do
  remoteEventIds): reopen pós-cancelamento não vira re-cancelamento fantasma
- cancelamento do Google respeita o relógio: cancel mais novo cancela; Teqo
  mais novo (reopen) limpa o trashed e recria
- escrita reversa sem req.user: autoria do registro correta + sem gate 403
  do deputado; hook de sync pula a própria escrita reversa (sem passada aninhada)
- canal: segredo rotaciona por criação; novo canal persistido antes do stop
  do antigo; identity fields read:false; create admin-only; leitura prefere
  linha configurada; pre-filter do webhook antes do bootstrap do Payload
- testes: all-day ida-e-volta, task-toggle, reopen, slug congelado, e2e dos
  caminhos 200, render tests do dialog/feed; asserções herméticas a
  atividades ambiente dos arquivos irmãos (DB compartilhado)
2026-08-11 22:42:05 -03:00

55 lines
2.2 KiB
TypeScript

import type { Access, FieldAccess } from 'payload'
import {
getFreshCampaignUser,
isCampaignStaff,
isPayloadAdmin,
payloadAdminOnly,
} from '@/utilities/access/shared'
/**
* C114+C115 — access for the `googleCalendarSync` configuration/state
* collection. Staff (coordinator/advisor/candidate) read the sync state
* (pill) and toggle `disabledAt`; only Payload admin sets the operational
* `calendarId` and creates the row (the single-row invariant is enforced at
* the read: the configured row wins); the system state fields are written
* solely by the sync engine through an admin bypass — never by a user path.
* The push-channel identity fields (id/resource/secret — the webhook
* credential set) are unreadable to any user path, admin included: the
* engine and the webhook read them with the admin bypass.
*/
export const canReadGoogleCalendarSync: Access = async ({ req }) => {
if (isPayloadAdmin(req.user)) return true
return isCampaignStaff(await getFreshCampaignUser(req))
}
export const canCreateGoogleCalendarSync: Access = payloadAdminOnly
export const canUpdateGoogleCalendarSync: Access = async ({ req }) => {
if (isPayloadAdmin(req.user)) return true
return isCampaignStaff(await getFreshCampaignUser(req))
}
export const canDeleteGoogleCalendarSync: Access = payloadAdminOnly
/** `calendarId` — operational identity, admin-only (env-level trust). */
export const canSetGoogleCalendarSyncConfigField: FieldAccess = ({ req }) =>
isPayloadAdmin(req.user)
/** `disabledAt` — staff may pause/resume the sync from the agenda. */
export const canSetGoogleCalendarSyncDisabled: FieldAccess = async ({ req }) => {
if (isPayloadAdmin(req.user)) return true
return isCampaignStaff(await getFreshCampaignUser(req))
}
/** System state fields — only the sync engine writes them (overrideAccess). */
export const canSetGoogleCalendarSyncSystemField: FieldAccess = () => false
/**
* C115 — the webhook credential set never leaves the engine: field-level
* `read` denies every user path (the engine/webhook read with the admin
* bypass). A future server action reading the config row would otherwise
* hand out full push-forging authority to any staff member.
*/
export const canReadGoogleCalendarSyncIdentityField: FieldAccess = () => false