mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-14 17:43:04 +00:00
Add denormalized task counters with list perf migration, shared territory FormData parsing, advisory locks on task/update mutations, and a shared contact-search guard; document what is still pending in the C7 plan and roadmap.
141 lines
5.1 KiB
TypeScript
141 lines
5.1 KiB
TypeScript
import 'server-only'
|
|
|
|
import {
|
|
actionPlanKinds,
|
|
actionPlanStatuses,
|
|
type ActionPlanCreateInput,
|
|
type ActionPlanUpdateInput,
|
|
} from '@/lib/schemas/actionPlan'
|
|
import {
|
|
boundedJsonFormValue,
|
|
FormDataBoundaryError,
|
|
nullableRelationshipFormValue,
|
|
optionalFormText,
|
|
repeatedRelationshipFormValues,
|
|
requiredRelationshipFormValue,
|
|
} from '@/lib/formData'
|
|
import { parseBahiaDateTimeInput } from '@/utilities/campaignTime'
|
|
import {
|
|
parseCampaignTerritoryFields,
|
|
validateCampaignTerritoryFormData,
|
|
} from '@/utilities/campaignTerritoryFormData'
|
|
|
|
type ParsedActionPlanTask = {
|
|
title: string
|
|
responsible?: number
|
|
due?: string
|
|
done?: boolean
|
|
}
|
|
|
|
const parseDateTimeFormField = (formData: FormData, field: string): string | undefined => {
|
|
const value = optionalFormText(formData, field)
|
|
if (!value) return undefined
|
|
const iso = parseBahiaDateTimeInput(value)
|
|
if (!iso) throw new FormDataBoundaryError(field, 'Data e horário inválidos.')
|
|
return iso
|
|
}
|
|
|
|
const parseTasksFormData = (formData: FormData): ParsedActionPlanTask[] => {
|
|
const raw = boundedJsonFormValue(formData, 'tasksJson', 20_000)
|
|
if (raw === undefined) return []
|
|
if (!Array.isArray(raw)) {
|
|
throw new FormDataBoundaryError('tasksJson', 'Lista de tarefas inválida.')
|
|
}
|
|
|
|
return raw.map((item, index) => {
|
|
if (typeof item !== 'object' || item === null) {
|
|
throw new FormDataBoundaryError('tasksJson', `Tarefa ${index + 1} inválida.`)
|
|
}
|
|
const record = item as Record<string, unknown>
|
|
const title = typeof record.title === 'string' ? record.title.trim() : ''
|
|
if (!title) {
|
|
throw new FormDataBoundaryError('tasksJson', `Informe o título da tarefa ${index + 1}.`)
|
|
}
|
|
if (title.length > 200) {
|
|
throw new FormDataBoundaryError(
|
|
'tasksJson',
|
|
`O título da tarefa ${index + 1} é muito longo.`,
|
|
)
|
|
}
|
|
const responsible =
|
|
typeof record.responsible === 'number' &&
|
|
Number.isInteger(record.responsible) &&
|
|
record.responsible > 0
|
|
? record.responsible
|
|
: undefined
|
|
const dueRaw = typeof record.due === 'string' ? record.due : undefined
|
|
const due = dueRaw ? (parseBahiaDateTimeInput(dueRaw) ?? undefined) : undefined
|
|
if (dueRaw && !due) {
|
|
throw new FormDataBoundaryError('tasksJson', `Prazo inválido na tarefa ${index + 1}.`)
|
|
}
|
|
const done = typeof record.done === 'boolean' ? record.done : undefined
|
|
|
|
return {
|
|
title,
|
|
...(responsible ? { responsible } : {}),
|
|
...(due ? { due } : {}),
|
|
...(done !== undefined ? { done } : {}),
|
|
}
|
|
})
|
|
}
|
|
|
|
const parseSharedActionPlanFormData = (formData: FormData) => {
|
|
const territory = parseCampaignTerritoryFields(formData)
|
|
const responsible = nullableRelationshipFormValue(formData, 'responsible')
|
|
const leadership = nullableRelationshipFormValue(formData, 'leadership')
|
|
|
|
return {
|
|
title: optionalFormText(formData, 'title') ?? '',
|
|
kind: optionalFormText(formData, 'kind') as (typeof actionPlanKinds)[number],
|
|
status: optionalFormText(formData, 'status') as (typeof actionPlanStatuses)[number] | undefined,
|
|
description: optionalFormText(formData, 'description'),
|
|
startAt: parseDateTimeFormField(formData, 'startAt'),
|
|
endAt: parseDateTimeFormField(formData, 'endAt'),
|
|
deadline: parseDateTimeFormField(formData, 'deadline'),
|
|
...territory,
|
|
responsible,
|
|
leadership,
|
|
tasks: parseTasksFormData(formData),
|
|
}
|
|
}
|
|
|
|
export const parseActionPlanCreateFormData = (formData: FormData): ActionPlanCreateInput => {
|
|
const shared = parseSharedActionPlanFormData(formData)
|
|
validateCampaignTerritoryFormData(shared, { entityLabel: 'plano', requireGeography: false })
|
|
const coordinatorIds = repeatedRelationshipFormValues(formData, 'coordinators')
|
|
|
|
return {
|
|
...shared,
|
|
status: (shared.status ?? 'rascunho') as ActionPlanCreateInput['status'],
|
|
responsible: shared.responsible ?? undefined,
|
|
leadership: shared.leadership ?? undefined,
|
|
coordinators: coordinatorIds.length ? coordinatorIds : undefined,
|
|
}
|
|
}
|
|
|
|
export const parseActionPlanUpdateFormData = (formData: FormData): ActionPlanUpdateInput => {
|
|
const shared = parseSharedActionPlanFormData(formData)
|
|
validateCampaignTerritoryFormData(shared, { entityLabel: 'plano', requireGeography: true })
|
|
const coordinatorIds = repeatedRelationshipFormValues(formData, 'coordinators')
|
|
// Checkboxes only submit when checked, so a hidden marker distinguishes
|
|
// "coordinators section rendered but all unchecked" from "not editable" (coordenador view).
|
|
const hasCoordinators = formData.has('coordinatorsSubmitted')
|
|
|
|
return {
|
|
...shared,
|
|
id: requiredRelationshipFormValue(formData, 'id'),
|
|
status: shared.status as ActionPlanUpdateInput['status'],
|
|
regions: shared.regions,
|
|
cities: shared.cities,
|
|
neighborhoods: shared.neighborhoods,
|
|
locality: shared.locality ?? null,
|
|
territoryNotes: shared.territoryNotes ?? null,
|
|
description: shared.description ?? null,
|
|
startAt: shared.startAt ?? null,
|
|
endAt: shared.endAt ?? null,
|
|
deadline: shared.deadline ?? null,
|
|
responsible: shared.responsible,
|
|
leadership: shared.leadership,
|
|
coordinators: hasCoordinators ? coordinatorIds : undefined,
|
|
}
|
|
}
|