teqo/src/utilities/homeSearch/searchHomeAdvisors.ts
Cursor Agent 9ae062c77c refactor(lib): close the lib→utilities type-import hole + Local API omission guard
- suggestionCatalog type-imports MunicipalityTerritorialClass from
  lib/territorialClassAnchors (the single source) instead of the
  utilities re-export — direction inversion introduced in Pass 3.
- ESLint src/lib zone: @/utilities|components|app restriction now rejects
  type imports too (allowTypeImports: false); shared types live in lib
  contract modules by policy.
- engagementLevel imports DAY_MS from lib/text (fold missed in P3-K).
- searchHomeAdvisors: delete stale 'Intentional admin bypass' comment —
  the call is overrideAccess: false under the staff gate.
- New guard localApiOverrideAccessConventions.unit.spec.ts: payload calls
  with user must declare overrideAccess (baseline measured: 59 calls, 0
  missing), and runtime dynamic import/require of payload/next server
  modules is banned in utilities (dodges the server-only sweep).
2026-07-31 13:17:10 -03:00

92 lines
2.6 KiB
TypeScript

import 'server-only'
import type { Payload } from 'payload'
import { homeSearchQueryIsActive, normalizeHomeSearchRaw } from '@/lib/campaignHomeSearchContract'
import type { HomeSearchAdvisorHit } from '@/lib/campaignHomeSearchHits'
import { isUnrestrictedCampaignRole } from '@/lib/campaignRoles'
import {
compareHomeSearchNameRelevance,
normalizeHomeSearchName,
} from '@/lib/homeSearchMunicipalityMatch'
import { matchesNormalizedAtWordStart } from '@/lib/wordStartFilter'
import type { CampaignUser } from '@/payload-types'
import { municipalityIdsByAdvisorIds } from '@/utilities/advisorData'
export const searchHomeAdvisors = async (
payload: Payload,
user: CampaignUser,
rawQuery: string,
): Promise<HomeSearchAdvisorHit[]> => {
if (!isUnrestrictedCampaignRole(user.role)) {
return []
}
const query = normalizeHomeSearchRaw(rawQuery)
if (!homeSearchQueryIsActive(query)) {
return []
}
const normalizedQuery = normalizeHomeSearchName(query)
// Under access (`overrideAccess: false`): the unrestricted-staff gate ran
// above, so the advisor directory is only queried for coordinator/candidate.
const result = await payload.find({
collection: 'campaignUser',
where: { role: { equals: 'advisor' } },
depth: 0,
limit: 0,
pagination: false,
sort: 'name',
select: { name: true, phone: true },
user,
overrideAccess: false,
})
const matched = result.docs
.map((doc) => {
const normalizedName = normalizeHomeSearchName(doc.name)
if (!matchesNormalizedAtWordStart(normalizedName, normalizedQuery)) {
return null
}
return {
id: doc.id,
name: doc.name,
phone: doc.phone ?? null,
normalizedName,
}
})
.filter((row): row is NonNullable<typeof row> => row !== null)
if (matched.length === 0) {
return []
}
const advisorIDs = matched.map((row) => row.id)
const municipalityIdsByAdvisor = await municipalityIdsByAdvisorIds(payload, advisorIDs)
return matched
.map((row) => ({
normalizedName: row.normalizedName,
hit: {
id: row.id,
name: row.name,
phone: row.phone,
municipalityCount: municipalityIdsByAdvisor.get(row.id)?.length ?? 0,
},
}))
.sort((left, right) =>
compareHomeSearchNameRelevance(
{
normalizedName: left.normalizedName,
tieBreakDesc: left.hit.municipalityCount,
},
{
normalizedName: right.normalizedName,
tieBreakDesc: right.hit.municipalityCount,
},
normalizedQuery,
),
)
.map((row) => row.hit)
}