teqo-backup/src/utilities/municipality/municipalityUpdatePageData.ts
Francisco Solla 3d8dbae61f feat(B193): card de município mobile denso com edit-where-you-see e rodapé de atualização expandível
- Card denso (wireframe): posição de voto à direita, barra de cenários com
  marcador do ativo, chips classe/tendência/nível (label à esquerda), 3 grupos
  de avatares com wrap (um por entidade), rodapé Última atualização + chevron
  que expande revelando o último card de atualização + CTA registrar; sem
  atualização → CTA direto; prioridade = borda lateral direita 6px primary.
- Controles ganham prop trigger/triggerClassName (precedente RelationChipCell);
  MunicipalityRelationAvatarStack ganha modo wrap.
- C109 (sheet quebra ao abrir) corrigida: sessões do sheet host keyed por id
  (imune à identidade dos callbacks recriados por render) e footer customizado
  renderizado pelo host dentro da árvore do Drawer (portal perdia o Dialog
  context — reproduzido digitando no sheet).
- Dado: MunicipalityListViewModel.lastUpdate (batch por página, scoped).
- E2E novos (barra/votos, chip nível, borda prioridade, expansão/CTA vazio).
Sem migration/access/Consent.
2026-08-10 11:23:49 -03:00

171 lines
5.3 KiB
TypeScript

import 'server-only'
import type { Payload } from 'payload'
import { relationshipId } from '@/lib/relationship'
import {
municipalityUpdatePolarities,
type MunicipalityUpdatePolarity,
} from '@/lib/schemas/municipalityUpdate'
import type { CampaignUser, MunicipalityUpdate } from '@/payload-types'
import { loadCampaignUserNamesByIds } from '@/utilities/loadNamesByIds'
export const municipalityUpdatesPageSize = 10
export type MunicipalityUpdateViewModel = {
id: number
authorName: string
createdAt: string
body: string | null
polarity: MunicipalityUpdatePolarity
urgent: boolean
activeVolunteers: number | null
newSupports: number | null
adversarySignal: boolean
}
export type MunicipalityUpdateFeedState = {
page: number
}
export const parseMunicipalityUpdateFeedParams = (
searchParams: Record<string, string | string[] | undefined>,
): MunicipalityUpdateFeedState => {
const first = (value: string | string[] | undefined) => (Array.isArray(value) ? value[0] : value)
const rawPage = first(searchParams.updatePage)
const page = rawPage && /^[1-9]\d*$/.test(rawPage) ? Number(rawPage) : 1
return { page }
}
export const loadMunicipalityUpdatesFeed = async (
payload: Payload,
user: CampaignUser,
municipalityID: number,
state: MunicipalityUpdateFeedState,
): Promise<{
updates: MunicipalityUpdateViewModel[]
totalDocs: number
totalPages: number
page: number
}> => {
const result = await payload.find({
collection: 'municipalityUpdate',
where: {
municipality: { equals: municipalityID },
},
depth: 0,
limit: municipalityUpdatesPageSize,
page: state.page,
sort: '-createdAt',
user,
overrideAccess: false,
})
const authorIDs = [
...new Set(
result.docs
.map((update) => relationshipId((update as MunicipalityUpdate).author))
.filter((id): id is number => id !== null),
),
]
const authorNameById = await loadCampaignUserNamesByIds(payload, authorIDs)
return {
updates: result.docs.map((update) => {
const doc = update as MunicipalityUpdate
const polarity = doc.polarity as MunicipalityUpdatePolarity | undefined
return {
id: doc.id,
authorName: authorNameById.get(relationshipId(doc.author) ?? -1) ?? 'Usuário',
createdAt: doc.createdAt,
body: doc.body ?? null,
polarity: polarity && municipalityUpdatePolarities.includes(polarity) ? polarity : 'neutra',
urgent: Boolean(doc.urgent),
activeVolunteers: doc.activeVolunteers ?? null,
newSupports: doc.newSupports ?? null,
adversarySignal: Boolean(doc.adversarySignal),
}
}),
totalDocs: result.totalDocs,
totalPages: result.totalPages,
page: result.page ?? state.page,
}
}
export const loadMunicipalityUpdatesPreview = async (
payload: Payload,
user: CampaignUser,
municipalityID: number,
): Promise<MunicipalityUpdateViewModel[]> =>
(await loadMunicipalityUpdatesFeed(payload, user, municipalityID, { page: 1 })).updates.slice(
0,
3,
)
/**
* B193 — the LAST update per listed municipality, for the mobile card's
* expandable footer. One batch over the visible page ids: docs arrive sorted
* by `-createdAt`, so the first row per municipality is the latest. The read
* runs with `overrideAccess: false`, so `canReadMunicipalityUpdate` scopes it
* to the actor's portfolio exactly like the C89 feed loader.
*/
export const loadMunicipalityLastUpdates = async (
payload: Payload,
user: CampaignUser,
municipalityIDs: readonly number[],
): Promise<ReadonlyMap<number, MunicipalityUpdateViewModel>> => {
const ids = [...new Set(municipalityIDs)]
if (ids.length === 0) return new Map()
const result = await payload.find({
collection: 'municipalityUpdate',
where: { municipality: { in: ids } },
depth: 0,
limit: 0,
pagination: false,
sort: '-createdAt',
select: {
municipality: true,
author: true,
polarity: true,
urgent: true,
activeVolunteers: true,
newSupports: true,
adversarySignal: true,
body: true,
createdAt: true,
},
user,
overrideAccess: false,
})
const authorIDs = [
...new Set(
result.docs
.map((update) => relationshipId((update as MunicipalityUpdate).author))
.filter((id): id is number => id !== null),
),
]
const authorNameById = await loadCampaignUserNamesByIds(payload, authorIDs)
const lastByMunicipality = new Map<number, MunicipalityUpdateViewModel>()
for (const update of result.docs as MunicipalityUpdate[]) {
const municipalityID = relationshipId(update.municipality)
if (municipalityID === null || lastByMunicipality.has(municipalityID)) continue
const polarity = update.polarity as MunicipalityUpdatePolarity | undefined
lastByMunicipality.set(municipalityID, {
id: update.id,
authorName: authorNameById.get(relationshipId(update.author) ?? -1) ?? 'Usuário',
createdAt: update.createdAt,
body: update.body ?? null,
polarity: polarity && municipalityUpdatePolarities.includes(polarity) ? polarity : 'neutra',
urgent: Boolean(update.urgent),
activeVolunteers: update.activeVolunteers ?? null,
newSupports: update.newSupports ?? null,
adversarySignal: Boolean(update.adversarySignal),
})
}
return lastByMunicipality
}