mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-16 20:13:04 +00:00
- stateDeputyListUrl.ts / stateDeputyListFilters.ts: contrato de URL/filtro próprio (sort/dir, filtro de Partido multi + sentinela "Sem partido"), espelhando municipalityListUrl.ts (paginação/resolveListUrl) e territoryListUrl.ts (sort keys/labels) - stateDeputyData.ts: where/sort resolvidos no banco + facet de partido (loadStateDeputyPartyFacet), select estreito na query principal - StateDeputySortableHead / StateDeputyHeaderFilter / StateDeputyFilters: wrappers finos sobre o chrome compartilhado (shared/CampaignSortableHead, CampaignHeaderFilterPopover), paridade mobile incluída - dobradinhas/page.tsx: redirect canônico em duas fases, CampaignTable sempre montada com caption + empty state, resumo de ordenação aria-live - Correção pós-entrega (design review): "Sem partido" no mobile + caption da tabela + copy plural do empty state - /simplify (1ª rodada): buildStateDeputyPartyOptions compartilhado, remove discriminante filterParam redundante, dedupe/if mortos em stateDeputyListUrl.ts - /simplify (2ª rodada, pós-rebase): remove cast/type morto em stateDeputyData.ts, stateDeputyColumns usa StateDeputyFilterOption em vez de duplicar a forma, inlinea parsePartiesParam; extrai truncatedNamesLabel compartilhado em campaignListUrl.ts (3º call site — municipalityListFilters e territoryListFilters tinham a mesma função com nomes distintos) Gate verde (tsc, lint, format, check:cycles, 396 testes, build); Aikido 0 achados nos 7 arquivos tocados nesta rodada. Docs: docs/plans/ordenacao-filtros-lista-dobradinhas.md, docs/roadmap.md, .cursor/rules/projects/nucleos-eleitorais.mdc.
266 lines
7 KiB
TypeScript
266 lines
7 KiB
TypeScript
import 'server-only'
|
|
|
|
import type { Payload } from 'payload'
|
|
|
|
import type { CampaignUser, Leadership, StateDeputy } from '@/payload-types'
|
|
import { relationshipId } from '@/utilities/relationship'
|
|
import {
|
|
buildStateDeputyListWhere,
|
|
NO_PARTY_FILTER_VALUE,
|
|
resolveStateDeputyListPayloadSort,
|
|
resolveStateDeputyListSort,
|
|
stateDeputyPageSize,
|
|
type StateDeputyListState,
|
|
} from '@/utilities/stateDeputyListUrl'
|
|
|
|
export type StateDeputyRowViewModel = {
|
|
id: number
|
|
name: string
|
|
slug: string
|
|
party: string | null
|
|
municipalityCount: number
|
|
leadershipCount: number
|
|
}
|
|
|
|
/** Values still reachable under the OTHER active filters (the Partido popover). */
|
|
export type StateDeputyListFilterFacets = {
|
|
parties: string[]
|
|
hasNoParty: boolean
|
|
}
|
|
|
|
/**
|
|
* Respects the search (an OTHER filter, from the popover's point of view) but
|
|
* drops the party filter itself — same contract as
|
|
* `loadMunicipalityListFilterFacets`: a selected value is unioned in so it
|
|
* stays visible to undo even if the search would otherwise hide it.
|
|
*/
|
|
const loadStateDeputyPartyFacet = async (
|
|
payload: Payload,
|
|
user: CampaignUser,
|
|
state: StateDeputyListState,
|
|
): Promise<StateDeputyListFilterFacets> => {
|
|
const result = await payload.find({
|
|
collection: 'stateDeputy',
|
|
where: buildStateDeputyListWhere({ ...state, parties: undefined }),
|
|
depth: 0,
|
|
limit: 0,
|
|
pagination: false,
|
|
select: { party: true },
|
|
user,
|
|
overrideAccess: false,
|
|
})
|
|
|
|
const availableParties = new Set<string>(
|
|
(state.parties ?? []).filter((party) => party !== NO_PARTY_FILTER_VALUE),
|
|
)
|
|
let hasNoParty = (state.parties ?? []).includes(NO_PARTY_FILTER_VALUE)
|
|
|
|
for (const doc of result.docs) {
|
|
if (doc.party) availableParties.add(doc.party)
|
|
else hasNoParty = true
|
|
}
|
|
|
|
return {
|
|
parties: [...availableParties].sort((left, right) => left.localeCompare(right, 'pt-BR')),
|
|
hasNoParty,
|
|
}
|
|
}
|
|
|
|
export const loadStateDeputyListPageData = async (
|
|
payload: Payload,
|
|
user: CampaignUser,
|
|
state: StateDeputyListState,
|
|
): Promise<{
|
|
rows: StateDeputyRowViewModel[]
|
|
totalDocs: number
|
|
totalPages: number
|
|
filterFacets: StateDeputyListFilterFacets
|
|
}> => {
|
|
const { sort, dir } = resolveStateDeputyListSort(state)
|
|
const [result, filterFacets] = await Promise.all([
|
|
payload.find({
|
|
collection: 'stateDeputy',
|
|
where: buildStateDeputyListWhere(state),
|
|
depth: 0,
|
|
limit: stateDeputyPageSize,
|
|
page: state.page,
|
|
sort: resolveStateDeputyListPayloadSort(sort, dir),
|
|
select: { name: true, slug: true, party: true },
|
|
user,
|
|
overrideAccess: false,
|
|
}),
|
|
loadStateDeputyPartyFacet(payload, user, state),
|
|
])
|
|
|
|
const stateDeputyIDs = result.docs.map((doc) => doc.id)
|
|
const municipalityCounts = new Map<number, number>()
|
|
const leadershipCounts = new Map<number, number>()
|
|
|
|
if (stateDeputyIDs.length) {
|
|
const [municipalities, leaderships] = await Promise.all([
|
|
payload.find({
|
|
collection: 'municipality',
|
|
where: { stateDeputies: { in: stateDeputyIDs } },
|
|
depth: 0,
|
|
limit: 0,
|
|
pagination: false,
|
|
select: { stateDeputies: true },
|
|
overrideAccess: true,
|
|
}),
|
|
payload.find({
|
|
collection: 'leadership',
|
|
where: { stateDeputies: { in: stateDeputyIDs } },
|
|
depth: 0,
|
|
limit: 0,
|
|
pagination: false,
|
|
select: { stateDeputies: true },
|
|
overrideAccess: true,
|
|
}),
|
|
])
|
|
|
|
for (const municipality of municipalities.docs) {
|
|
for (const deputy of municipality.stateDeputies ?? []) {
|
|
const id = relationshipId(deputy)
|
|
if (id !== null && stateDeputyIDs.includes(id)) {
|
|
municipalityCounts.set(id, (municipalityCounts.get(id) ?? 0) + 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const leadership of leaderships.docs) {
|
|
for (const deputy of leadership.stateDeputies ?? []) {
|
|
const id = relationshipId(deputy)
|
|
if (id !== null && stateDeputyIDs.includes(id)) {
|
|
leadershipCounts.set(id, (leadershipCounts.get(id) ?? 0) + 1)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
rows: result.docs.map((doc) => ({
|
|
id: doc.id,
|
|
name: doc.name,
|
|
slug: doc.slug,
|
|
party: doc.party ?? null,
|
|
municipalityCount: municipalityCounts.get(doc.id) ?? 0,
|
|
leadershipCount: leadershipCounts.get(doc.id) ?? 0,
|
|
})),
|
|
totalDocs: result.totalDocs,
|
|
totalPages: result.totalPages,
|
|
filterFacets,
|
|
}
|
|
}
|
|
|
|
export type StateDeputySummary = {
|
|
id: number
|
|
name: string
|
|
slug: string
|
|
party: string | null
|
|
}
|
|
|
|
export type StateDeputyDetailViewModel = StateDeputySummary & {
|
|
notes: string | null
|
|
municipalities: Array<{ id: number; name: string; slug: string }>
|
|
leaderships: Array<{ id: number; name: string }>
|
|
}
|
|
|
|
export const loadStateDeputyDetail = async (
|
|
payload: Payload,
|
|
user: CampaignUser,
|
|
slug: string,
|
|
): Promise<StateDeputyDetailViewModel | null> => {
|
|
const result = await payload.find({
|
|
collection: 'stateDeputy',
|
|
where: { slug: { equals: slug } },
|
|
depth: 0,
|
|
limit: 1,
|
|
pagination: false,
|
|
user,
|
|
overrideAccess: false,
|
|
})
|
|
const stateDeputy = result.docs[0] as StateDeputy | undefined
|
|
if (!stateDeputy) return null
|
|
|
|
const [municipalities, leaderships] = await Promise.all([
|
|
payload.find({
|
|
collection: 'municipality',
|
|
where: { stateDeputies: { in: [stateDeputy.id] } },
|
|
depth: 0,
|
|
limit: 0,
|
|
pagination: false,
|
|
sort: 'name',
|
|
select: { name: true, slug: true },
|
|
user,
|
|
overrideAccess: false,
|
|
}),
|
|
payload.find({
|
|
collection: 'leadership',
|
|
where: { stateDeputies: { in: [stateDeputy.id] } },
|
|
depth: 1,
|
|
limit: 0,
|
|
pagination: false,
|
|
sort: 'createdAt',
|
|
user,
|
|
overrideAccess: false,
|
|
}),
|
|
])
|
|
|
|
return {
|
|
id: stateDeputy.id,
|
|
name: stateDeputy.name,
|
|
slug: stateDeputy.slug,
|
|
party: stateDeputy.party ?? null,
|
|
notes: stateDeputy.notes ?? null,
|
|
municipalities: municipalities.docs.map((municipality) => ({
|
|
id: municipality.id,
|
|
name: municipality.name,
|
|
slug: municipality.slug,
|
|
})),
|
|
leaderships: leaderships.docs.map((leadership) => {
|
|
const contact = leadership.contact as Leadership['contact']
|
|
return {
|
|
id: leadership.id,
|
|
name:
|
|
typeof contact === 'object' && contact !== null && 'name' in contact
|
|
? String(contact.name)
|
|
: 'Contato',
|
|
}
|
|
}),
|
|
}
|
|
}
|
|
|
|
export const loadStateDeputySummaries = async (
|
|
payload: Payload,
|
|
ids: number[],
|
|
): Promise<StateDeputySummary[]> => {
|
|
if (ids.length === 0) return []
|
|
|
|
const result = await payload.find({
|
|
collection: 'stateDeputy',
|
|
where: { id: { in: ids } },
|
|
depth: 0,
|
|
limit: 0,
|
|
pagination: false,
|
|
sort: 'name',
|
|
select: { name: true, slug: true, party: true },
|
|
overrideAccess: true,
|
|
})
|
|
|
|
const byId = new Map(
|
|
result.docs.map((doc) => [
|
|
doc.id,
|
|
{
|
|
id: doc.id,
|
|
name: doc.name,
|
|
slug: doc.slug,
|
|
party: doc.party ?? null,
|
|
},
|
|
]),
|
|
)
|
|
|
|
return ids.flatMap((id) => {
|
|
const summary = byId.get(id)
|
|
return summary ? [summary] : []
|
|
})
|
|
}
|