mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-13 23:53:05 +00:00
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.
70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import 'server-only'
|
|
|
|
import type { Payload } from 'payload'
|
|
|
|
import { isContactSearchQueryReady, normalizeContactSearchQuery } from '@/lib/contactSearchQuery'
|
|
import { isPopulatedRelationship, relationshipId } from '@/lib/relationship'
|
|
import type { CampaignUser, Contact, Leadership } from '@/payload-types'
|
|
|
|
export type ActivityLeadershipOption = {
|
|
id: number
|
|
label: string
|
|
}
|
|
|
|
const LEADERSHIP_SEARCH_LIMIT = 20
|
|
|
|
const leadershipLabel = (leadership: Leadership): string | null => {
|
|
const id = relationshipId(leadership.id)
|
|
if (!id) return null
|
|
const contact = leadership.contact
|
|
return isPopulatedRelationship<Contact>(contact) ? contact.name : `Liderança #${id}`
|
|
}
|
|
|
|
/**
|
|
* Engaged leaderships visible to the actor (for linking an activity to a leadership).
|
|
* Uses overrideAccess: false so Payload row-level access applies.
|
|
*/
|
|
export const searchActivityLeadershipOptions = async (
|
|
payload: Pick<Payload, 'find'>,
|
|
user: CampaignUser,
|
|
query: string,
|
|
): Promise<ActivityLeadershipOption[]> => {
|
|
const normalizedQuery = typeof query === 'string' ? query : ''
|
|
if (!isContactSearchQueryReady(normalizedQuery)) return []
|
|
|
|
const { trimmed, digits } = normalizeContactSearchQuery(normalizedQuery)
|
|
|
|
const result = await payload.find({
|
|
collection: 'leadership',
|
|
where: {
|
|
and: [
|
|
{ supportStatus: { equals: 'engajado' } },
|
|
{
|
|
or: [
|
|
{ 'contact.name': { contains: trimmed } },
|
|
{ 'contact.phone': { contains: digits || trimmed } },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
depth: 1,
|
|
limit: LEADERSHIP_SEARCH_LIMIT,
|
|
select: {
|
|
contact: true,
|
|
supportStatus: true,
|
|
},
|
|
user,
|
|
overrideAccess: false,
|
|
})
|
|
|
|
return result.docs
|
|
.map((doc) => {
|
|
const leadership = doc as Leadership
|
|
const id = relationshipId(leadership.id)
|
|
const label = leadershipLabel(leadership)
|
|
if (!id || !label) return null
|
|
return { id, label }
|
|
})
|
|
.filter((option): option is ActivityLeadershipOption => option !== null)
|
|
.sort((left, right) => left.label.localeCompare(right.label, 'pt-BR'))
|
|
}
|