Fold duplicated campanha access, loaders, list/filter/form shells, and scripts into shared modules; reorganize utilities by domain; stabilize e2e; and document prevention-first audit workflow so the same smells cannot creep back in.
67 lines
2 KiB
TypeScript
67 lines
2 KiB
TypeScript
'use server'
|
|
|
|
import type { Payload } from 'payload'
|
|
|
|
import {
|
|
ORGANIZATION_CONFLICT_MESSAGE,
|
|
ORGANIZATION_STAFF_MESSAGE,
|
|
organizationCreateSchema,
|
|
organizationUpdateSchema,
|
|
type OrganizationCreateInput,
|
|
type OrganizationUpdateInput,
|
|
} from '@/lib/schemas/organization'
|
|
import type { CampaignUser } from '@/payload-types'
|
|
import { getCampaignActionContext } from '@/utilities/campaignActionContext'
|
|
import { runStaffEntityMutation, type StaffEntityPolicy } from '@/utilities/campaignEntityActions'
|
|
import { hookFilledCreateData } from '@/utilities/hookFilledData'
|
|
|
|
const organizationPolicy: StaffEntityPolicy = {
|
|
staffMessage: ORGANIZATION_STAFF_MESSAGE,
|
|
conflictPattern: /organization_(name|slug)|duplicate key/i,
|
|
conflictMessage: ORGANIZATION_CONFLICT_MESSAGE,
|
|
}
|
|
|
|
const createOrganizationRecord = async (
|
|
payload: Payload,
|
|
actor: CampaignUser,
|
|
input: OrganizationCreateInput,
|
|
) => {
|
|
const data = organizationCreateSchema.parse(input)
|
|
return runStaffEntityMutation(payload, actor, organizationPolicy, (currentActor) =>
|
|
payload.create({
|
|
collection: 'organization',
|
|
data: hookFilledCreateData<'organization'>(data),
|
|
depth: 0,
|
|
user: currentActor,
|
|
overrideAccess: false,
|
|
}),
|
|
)
|
|
}
|
|
|
|
const updateOrganizationRecord = async (
|
|
payload: Payload,
|
|
actor: CampaignUser,
|
|
input: OrganizationUpdateInput,
|
|
) => {
|
|
const { id, ...data } = organizationUpdateSchema.parse(input)
|
|
return runStaffEntityMutation(payload, actor, organizationPolicy, (currentActor) =>
|
|
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)
|
|
}
|