Keep a typed main kind from being ranked as the catalog other kind, and store that catalog kind on the finished Rule.

This commit is contained in:
adilallo committed 2026-11-23 11:07:50 -07:00
1 parent 4b90bce006
commit 3e0181e4de
7 files changed
+174 -48

No files matched your search

+35 -21
View File
@@ -3,6 +3,7 @@ import {
CATALOG_SECTION_IDS,
METHOD_FACET_API_SECTION_IDS,
} from "../../create/customRuleFacets";
import { TYPED_ORG_TYPE_MARKER } from "../../create/deckTypes";
/**
* Zod schemas for the recommendation matrix (CR-88).
@@ -69,18 +70,20 @@ export type OrgTypeValueId = (typeof ORG_TYPE_VALUE_IDS)[number];
export type ScaleValueId = (typeof SCALE_VALUE_IDS)[number];
export type MaturityValueId = (typeof MATURITY_VALUE_IDS)[number];
export const FACET_VALUE_IDS_BY_GROUP: Record<
FacetGroupId,
readonly string[]
> = {
size: SIZE_VALUE_IDS,
orgType: ORG_TYPE_VALUE_IDS,
scale: SCALE_VALUE_IDS,
maturity: MATURITY_VALUE_IDS,
};
export const FACET_VALUE_IDS_BY_GROUP: Record<FacetGroupId, readonly string[]> =
{
size: SIZE_VALUE_IDS,
orgType: ORG_TYPE_VALUE_IDS,
scale: SCALE_VALUE_IDS,
maturity: MATURITY_VALUE_IDS,
};
const sizeValueIdSchema = z.enum(SIZE_VALUE_IDS);
const orgTypeValueIdSchema = z.enum(ORG_TYPE_VALUE_IDS);
const orgTypeRequestValueSchema = z.union([
orgTypeValueIdSchema,
z.literal(TYPED_ORG_TYPE_MARKER),
]);
const scaleValueIdSchema = z.enum(SCALE_VALUE_IDS);
const maturityValueIdSchema = z.enum(MATURITY_VALUE_IDS);
@@ -195,9 +198,10 @@ export type FacetGroupsFile = z.infer<typeof facetGroupsFileSchema>;
* boolean or `{ match, weight? }`). Used by both the seed flattener and the
* scoring helpers.
*/
export function resolveFacetMatch(
v: FacetMatch | undefined,
): { match: boolean; weight: number | null } {
export function resolveFacetMatch(v: FacetMatch | undefined): {
match: boolean;
weight: number | null;
} {
if (v === undefined) return { match: false, weight: null };
if (typeof v === "boolean") return { match: v, weight: null };
return { match: v.match, weight: v.weight ?? null };
@@ -215,8 +219,8 @@ export const requestedFacetsSchema = z
.object({
size: z.array(sizeValueIdSchema).max(SIZE_VALUE_IDS.length).optional(),
orgType: z
.array(orgTypeValueIdSchema)
.max(ORG_TYPE_VALUE_IDS.length)
.array(orgTypeRequestValueSchema)
.max(ORG_TYPE_VALUE_IDS.length + 1)
.optional(),
scale: z.array(scaleValueIdSchema).max(SCALE_VALUE_IDS.length).optional(),
maturity: z
@@ -238,6 +242,7 @@ export function flattenRequestedFacets(
const values = facets[group];
if (!values) continue;
for (const value of values) {
if (value === TYPED_ORG_TYPE_MARKER) continue;
out.push({ group, value });
}
}
@@ -248,11 +253,19 @@ export function flattenRequestedFacets(
* Parse `?facet.size=oneMember&facet.orgType=dao&facet.orgType=nonprofit` into
* a typed `RequestedFacets`. Unknown groups and unknown values are dropped
* silently — the API "never errors on partial facets" (§9.3).
* `facet.orgType=__typed__` is kept, in order, so a typed main kind does not
* promote a later catalog kind to the main kind.
*/
export function parseRequestedFacetsFromSearchParams(
search: URLSearchParams,
): RequestedFacets {
const collected: Record<FacetGroupId, Set<string>> = {
const collected: Record<FacetGroupId, string[]> = {
size: [],
orgType: [],
scale: [],
maturity: [],
};
const seen: Record<FacetGroupId, Set<string>> = {
size: new Set(),
orgType: new Set(),
scale: new Set(),
@@ -262,15 +275,16 @@ export function parseRequestedFacetsFromSearchParams(
if (!rawKey.startsWith("facet.")) continue;
const group = rawKey.slice("facet.".length) as FacetGroupId;
if (!FACET_GROUP_IDS.includes(group)) continue;
const allowed = new Set(FACET_VALUE_IDS_BY_GROUP[group]);
if (allowed.has(rawVal)) {
collected[group].add(rawVal);
}
const allowed = FACET_VALUE_IDS_BY_GROUP[group].includes(rawVal);
const typedMain = group === "orgType" && rawVal === TYPED_ORG_TYPE_MARKER;
if ((!allowed && !typedMain) || seen[group].has(rawVal)) continue;
seen[group].add(rawVal);
collected[group].push(rawVal);
}
const out: RequestedFacets = {};
for (const group of FACET_GROUP_IDS) {
if (collected[group].size > 0) {
out[group] = Array.from(collected[group]) as never;
if (collected[group].length > 0) {
out[group] = collected[group] as never;
}
}
return out;