mirror of
https://github.com/fsolla/teqo.git
synced 2026-12-04 23:33:06 +00:00
Rename the operational unit "Praça"/"Núcleo" to "Município" (code identifier `municipality`) and land the 2026-07-23 course changes: - New `candidate` role with unrestricted visibility (`isCampaignStaff` now includes it) alongside coordinator/advisor; leader is locked down to the supporter contact tool only. - Demandas become staff-only; dobradinhas modeled via a `stateDeputy` entity. - Camaçari consolidated to a single município (ZE 170/171 aggregated); Salvador keeps its 19 zone municípios. - Map moves to the staff dashboard; `/campanha/municipios` is list/overview. - Vote pledges keep the declared vs estimated (scenario) asymmetry; leaders never see estimates. Destructive migration for the campaign vertical (drops nucleus/plaza-era tables, wipes campaign rows, seeds 435 municípios). Public-site data untouched. Includes the post-delivery /simplify cleanups: shared reloadStaffActor / reloadCoordinatorActor helpers, req.context cache for getFreshCampaignUser, parallelized dashboard loads, candidate dashboard-copy fix, and dead leader-dashboard / roleKind code removal.
72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
'use server'
|
|
|
|
import type { Payload } from 'payload'
|
|
|
|
import {
|
|
organizationCreateSchema,
|
|
organizationUpdateSchema,
|
|
type OrganizationCreateInput,
|
|
type OrganizationUpdateInput,
|
|
} from '@/lib/schemas/organization'
|
|
import type { CampaignUser } from '@/payload-types'
|
|
import { getCampaignActionContext, reloadStaffActor } from '@/utilities/campaignActionContext'
|
|
|
|
const assertStaffActor = (payload: Payload, actor: CampaignUser): Promise<CampaignUser> =>
|
|
reloadStaffActor(payload, actor, 'Somente a coordenação e a assessoria gerenciam organizações.')
|
|
|
|
const isUniqueOrganizationConflict = (error: unknown): boolean => {
|
|
const message = error instanceof Error ? error.message : String(error)
|
|
return /organization_(name|slug)|duplicate key/i.test(message)
|
|
}
|
|
|
|
export const createOrganizationRecord = async (
|
|
payload: Payload,
|
|
actor: CampaignUser,
|
|
input: OrganizationCreateInput,
|
|
) => {
|
|
const data = organizationCreateSchema.parse(input)
|
|
const currentActor = await assertStaffActor(payload, actor)
|
|
|
|
try {
|
|
return await payload.create({
|
|
collection: 'organization',
|
|
data: data as never,
|
|
depth: 0,
|
|
user: currentActor,
|
|
overrideAccess: false,
|
|
})
|
|
} catch (error) {
|
|
if (isUniqueOrganizationConflict(error)) {
|
|
throw new Error('Já existe uma organização com este nome.')
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export const updateOrganizationRecord = async (
|
|
payload: Payload,
|
|
actor: CampaignUser,
|
|
input: OrganizationUpdateInput,
|
|
) => {
|
|
const { id, ...data } = organizationUpdateSchema.parse(input)
|
|
const currentActor = await assertStaffActor(payload, actor)
|
|
|
|
return payload.update({
|
|
collection: 'organization',
|
|
id,
|
|
data,
|
|
depth: 0,
|
|
user: currentActor,
|
|
overrideAccess: false,
|
|
})
|
|
}
|
|
|
|
export const createOrganization = async (input: OrganizationCreateInput) => {
|
|
const { payload, actor } = await getCampaignActionContext()
|
|
return createOrganizationRecord(payload, actor, input)
|
|
}
|
|
|
|
export const updateOrganization = async (input: OrganizationUpdateInput) => {
|
|
const { payload, actor } = await getCampaignActionContext()
|
|
return updateOrganizationRecord(payload, actor, input)
|
|
}
|