mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-17 04:23:07 +00:00
Drops signalSource/triangulated from municipalityUpdate (migration simplify_municipality_signal_fields), rewrites signal-type labels for the field team, and treats any adversary signal as confirmed in E11 (P1 triage 1). Also fixes the stale post-B43 leader-home e2e and lowers a drifted bypass pin.
118 lines
3.4 KiB
TypeScript
118 lines
3.4 KiB
TypeScript
import 'server-only'
|
|
|
|
import type { Payload } from 'payload'
|
|
|
|
import { relationshipId } from '@/lib/relationship'
|
|
import type {
|
|
MunicipalitySignalType,
|
|
MunicipalityUpdateKind,
|
|
} from '@/lib/schemas/municipalityUpdate'
|
|
import { municipalityUpdateKinds } 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
|
|
kind: MunicipalityUpdateKind
|
|
authorName: string
|
|
createdAt: string
|
|
worked: string | null
|
|
failed: string | null
|
|
needs: string | null
|
|
body: string | null
|
|
activeVolunteers: number | null
|
|
newSupports: number | null
|
|
signalType: MunicipalitySignalType | null
|
|
}
|
|
|
|
export type MunicipalityUpdateFeedState = {
|
|
kind?: MunicipalityUpdateKind
|
|
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 rawKind = first(searchParams.updateKind)
|
|
const rawPage = first(searchParams.updatePage)
|
|
const page = rawPage && /^[1-9]\d*$/.test(rawPage) ? Number(rawPage) : 1
|
|
|
|
return {
|
|
...(municipalityUpdateKinds.includes(rawKind as MunicipalityUpdateKind)
|
|
? { kind: rawKind as MunicipalityUpdateKind }
|
|
: {}),
|
|
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: {
|
|
and: [
|
|
{ municipality: { equals: municipalityID } },
|
|
...(state.kind ? [{ kind: { equals: state.kind } }] : []),
|
|
],
|
|
},
|
|
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
|
|
return {
|
|
id: doc.id,
|
|
kind: doc.kind as MunicipalityUpdateKind,
|
|
authorName: authorNameById.get(relationshipId(doc.author) ?? -1) ?? 'Usuário',
|
|
createdAt: doc.createdAt,
|
|
worked: doc.worked ?? null,
|
|
failed: doc.failed ?? null,
|
|
needs: doc.needs ?? null,
|
|
body: doc.body ?? null,
|
|
activeVolunteers: doc.activeVolunteers ?? null,
|
|
newSupports: doc.newSupports ?? null,
|
|
signalType: doc.signalType ?? null,
|
|
}
|
|
}),
|
|
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,
|
|
)
|