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.
64 lines
2 KiB
TypeScript
64 lines
2 KiB
TypeScript
// ---------------------------------------------------------------------------
|
|
// Supporters
|
|
// ---------------------------------------------------------------------------
|
|
|
|
import type { Access, Where } from 'payload'
|
|
|
|
import { relationshipId } from '@/lib/relationship'
|
|
import { getAccessibleMunicipalityIds } from '@/utilities/access/municipalities'
|
|
import {
|
|
advisorMunicipalityScopeWhere,
|
|
getFreshCampaignUser,
|
|
isCampaignLeader,
|
|
isCampaignUnrestricted,
|
|
isPayloadAdmin,
|
|
} from '@/utilities/access/shared'
|
|
|
|
export const canCreateSupporter: Access = async ({ data, req }) => {
|
|
if (isPayloadAdmin(req.user)) return true
|
|
|
|
const currentUser = await getFreshCampaignUser(req)
|
|
if (isCampaignUnrestricted(currentUser)) return true
|
|
|
|
const municipalityID = relationshipId(data?.municipality)
|
|
if (!municipalityID) return false
|
|
|
|
// Advisor and leader create through the same scope check: the municipality
|
|
// must be one `getAccessibleMunicipalityIds` resolves for THEIR role
|
|
// (administered for the advisor, engaged-leadership-linked for the leader).
|
|
if (currentUser?.role === 'advisor' || isCampaignLeader(currentUser)) {
|
|
const municipalityIDs = await getAccessibleMunicipalityIds(req, currentUser)
|
|
return municipalityIDs?.includes(municipalityID) ?? false
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
export const canReadSupporter: Access = async ({ req }): Promise<boolean | Where> => {
|
|
if (isPayloadAdmin(req.user)) return true
|
|
|
|
const currentUser = await getFreshCampaignUser(req)
|
|
if (!currentUser) return false
|
|
if (isCampaignUnrestricted(currentUser)) return true
|
|
|
|
if (currentUser.role === 'advisor') {
|
|
return advisorMunicipalityScopeWhere(
|
|
'municipality',
|
|
await getAccessibleMunicipalityIds(req, currentUser),
|
|
)
|
|
}
|
|
|
|
if (isCampaignLeader(currentUser)) {
|
|
return {
|
|
createdBy: {
|
|
equals: currentUser.id,
|
|
},
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
export const canManageSupporter: Access = canReadSupporter
|
|
|
|
export const canDeleteSupporter: Access = ({ req }) => isPayloadAdmin(req.user)
|