From 3e0181e4de6445f549b3fc97926eb6b661a77400 Mon Sep 17 00:00:00 2001 From: adilallo <39313955+adilallo@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:07:50 -0700 Subject: [PATCH] Keep a typed main kind from being ranked as the catalog other kind, and store that catalog kind on the finished Rule. --- lib/create/buildFacetQueryString.ts | 78 ++++++++++++++------ lib/create/deckTypes.ts | 14 +++- lib/create/publishedRuleAnalytics.ts | 8 +- lib/server/validation/methodFacetsSchemas.ts | 56 ++++++++------ tests/unit/buildFacetQueryString.test.ts | 38 ++++++++++ tests/unit/methodFacetsSchemas.test.ts | 20 +++++ tests/unit/publishedRuleAnalytics.test.ts | 8 ++ 7 files changed, 174 insertions(+), 48 deletions(-) diff --git a/lib/create/buildFacetQueryString.ts b/lib/create/buildFacetQueryString.ts index 0f622d7..2328278 100644 --- a/lib/create/buildFacetQueryString.ts +++ b/lib/create/buildFacetQueryString.ts @@ -6,30 +6,33 @@ import { resolveScaleChipId, resolveSizeChipId, } from "./communityChipIds"; +import { TYPED_ORG_TYPE_MARKER } from "./deckTypes"; const FACET_GROUPS = ["size", "orgType", "scale", "maturity"] as const; type FacetGroupId = (typeof FACET_GROUPS)[number]; -const CHIP_TO_VALUE_BY_GROUP: Record> = - (() => { - const out: Record> = { - size: {}, - orgType: {}, - scale: {}, - maturity: {}, - }; - for (const group of FACET_GROUPS) { - const block = (facetGroups as Record)[group]; - if (block && typeof block === "object" && "values" in block) { - const values = (block as { values: Record }) - .values; - for (const [valueId, entry] of Object.entries(values)) { - out[group][entry.chipId] = valueId; - } +const CHIP_TO_VALUE_BY_GROUP: Record< + FacetGroupId, + Record +> = (() => { + const out: Record> = { + size: {}, + orgType: {}, + scale: {}, + maturity: {}, + }; + for (const group of FACET_GROUPS) { + const block = (facetGroups as Record)[group]; + if (block && typeof block === "object" && "values" in block) { + const values = (block as { values: Record }) + .values; + for (const [valueId, entry] of Object.entries(values)) { + out[group][entry.chipId] = valueId; } } - return out; - })(); + } + return out; +})(); const STATE_KEY_BY_GROUP: Record = { size: "selectedCommunitySizeIds", @@ -45,14 +48,41 @@ const RESOLVE_BY_GROUP: Record string> = { maturity: resolveMaturityChipId, }; -function readChipIds( - state: CreateFlowState, - group: FacetGroupId, -): string[] { +function readChipIds(state: CreateFlowState, group: FacetGroupId): string[] { const value = state[STATE_KEY_BY_GROUP[group]]; return Array.isArray(value) ? (value as string[]) : []; } +function catalogOrgTypeId(chipId: string): string | null { + const resolved = resolveOrgChipId(chipId); + const valueId = + CHIP_TO_VALUE_BY_GROUP.orgType[resolved] ?? + CHIP_TO_VALUE_BY_GROUP.orgType[chipId]; + if (!valueId || valueId === "enterprise") return null; + return valueId; +} + +/** Catalog kinds keep their order. A typed main kind is a leading marker. */ +function appendOrgTypeFacets(params: URLSearchParams, state: CreateFlowState) { + const chipIds = readChipIds(state, "orgType"); + const catalogIds: string[] = []; + const seen = new Set(); + for (const chipId of chipIds) { + const valueId = catalogOrgTypeId(chipId); + if (!valueId || seen.has(valueId)) continue; + seen.add(valueId); + catalogIds.push(valueId); + } + const firstIsCatalog = + chipIds.length > 0 && catalogOrgTypeId(chipIds[0]) !== null; + if (!firstIsCatalog && catalogIds.length > 0) { + params.append("facet.orgType", TYPED_ORG_TYPE_MARKER); + } + for (const valueId of catalogIds) { + params.append("facet.orgType", valueId); + } +} + /** * Build `facet.size=…&facet.orgType=…` query string from Create Community * chip selections. Shared by `/api/create-flow/methods` and @@ -61,6 +91,10 @@ function readChipIds( export function buildFacetQueryString(state: CreateFlowState): string { const params = new URLSearchParams(); for (const group of FACET_GROUPS) { + if (group === "orgType") { + appendOrgTypeFacets(params, state); + continue; + } const valuesById = CHIP_TO_VALUE_BY_GROUP[group]; const resolve = RESOLVE_BY_GROUP[group]; const seen = new Set(); diff --git a/lib/create/deckTypes.ts b/lib/create/deckTypes.ts index 972df0d..2121b4d 100644 --- a/lib/create/deckTypes.ts +++ b/lib/create/deckTypes.ts @@ -27,6 +27,12 @@ export const KINDS = [ ] as const; export type Kind = (typeof KINDS)[number]; +/** + * First `facet.orgType` value when the main kind was typed. + * Later catalog values stay other kinds instead of becoming the main kind. + */ +export const TYPED_ORG_TYPE_MARKER = "__typed__"; + export const SCALES = ["local", "regional", "national", "global"] as const; export type Scale = (typeof SCALES)[number]; @@ -48,7 +54,13 @@ export const TRAIT_KEYS = [ ] as const; export type TraitKey = (typeof TRAIT_KEYS)[number]; -export const DISPLAY_TIERS = ["frame", "safety", "fitted", "floor", "none"] as const; +export const DISPLAY_TIERS = [ + "frame", + "safety", + "fitted", + "floor", + "none", +] as const; export type DisplayTier = (typeof DISPLAY_TIERS)[number]; export type RankTier = Exclude | "answers"; diff --git a/lib/create/publishedRuleAnalytics.ts b/lib/create/publishedRuleAnalytics.ts index 6a49ed3..3c8d7f8 100644 --- a/lib/create/publishedRuleAnalytics.ts +++ b/lib/create/publishedRuleAnalytics.ts @@ -15,13 +15,14 @@ function trimmedLocation(raw: unknown): string | undefined { return trimmed.slice(0, LOCATION_MAX); } -/** Place and main group type stored on a finished Rule for launch counts. */ +/** Place and catalog group type stored on a finished Rule for launch counts. */ export function publishedRuleAnalyticsFromState(state: CreateFlowState): { communityLocation?: string; orgType?: Kind; } { const communityLocation = trimmedLocation(state.communityLocation); - const orgType = groupAnswersFromCreateFlowState(state).mainKind ?? undefined; + const answers = groupAnswersFromCreateFlowState(state); + const orgType = answers.mainKind ?? answers.otherKinds?.[0]; return { ...(communityLocation ? { communityLocation } : {}), ...(orgType ? { orgType } : {}), @@ -37,8 +38,7 @@ export function publishedRuleAnalyticsFromDocument(document: unknown): { } const row = document as Record; const communityLocation = trimmedLocation(row.communityLocation) ?? null; - const orgTypeRaw = - typeof row.orgType === "string" ? row.orgType.trim() : ""; + const orgTypeRaw = typeof row.orgType === "string" ? row.orgType.trim() : ""; const orgType = isKind(orgTypeRaw) ? orgTypeRaw : null; return { communityLocation, orgType }; } diff --git a/lib/server/validation/methodFacetsSchemas.ts b/lib/server/validation/methodFacetsSchemas.ts index fd5eec3..915ca7d 100644 --- a/lib/server/validation/methodFacetsSchemas.ts +++ b/lib/server/validation/methodFacetsSchemas.ts @@ -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 = + { + 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; * 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> = { + const collected: Record = { + size: [], + orgType: [], + scale: [], + maturity: [], + }; + const seen: Record> = { 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; diff --git a/tests/unit/buildFacetQueryString.test.ts b/tests/unit/buildFacetQueryString.test.ts index 4b9b56a..a15e410 100644 --- a/tests/unit/buildFacetQueryString.test.ts +++ b/tests/unit/buildFacetQueryString.test.ts @@ -1,5 +1,11 @@ import { describe, it, expect } from "vitest"; import { buildFacetQueryString } from "../../lib/create/buildFacetQueryString"; +import { TYPED_ORG_TYPE_MARKER } from "../../lib/create/deckTypes"; +import { + groupAnswersFromCreateFlowState, + groupAnswersFromRequestedFacets, +} from "../../lib/create/groupAnswers"; +import { parseRequestedFacetsFromSearchParams } from "../../lib/server/validation/methodFacetsSchemas"; describe("buildFacetQueryString", () => { it("maps community chip ids to facet.* query params", () => { @@ -19,4 +25,36 @@ describe("buildFacetQueryString", () => { it("returns empty string when no selections", () => { expect(buildFacetQueryString({})).toBe(""); }); + + it("keeps a catalog other kind from becoming the main kind when the main kind was typed", () => { + const state = { + selectedCommunitySizeIds: ["sixToTwelve"], + selectedOrganizationTypeIds: ["custom-tenants-union", "workersCoop"], + }; + const params = new URLSearchParams(buildFacetQueryString(state)); + expect(params.getAll("facet.orgType")).toEqual([ + TYPED_ORG_TYPE_MARKER, + "workersCoop", + ]); + const fromState = groupAnswersFromCreateFlowState(state); + const fromQuery = groupAnswersFromRequestedFacets( + parseRequestedFacetsFromSearchParams(params), + ); + expect(fromQuery.mainKind).toBeNull(); + expect(fromQuery.otherKinds).toEqual(["workersCoop"]); + expect(fromQuery.mainKind).toBe(fromState.mainKind); + expect(fromQuery.otherKinds).toEqual(fromState.otherKinds); + }); + + it("does not mark a catalog main kind", () => { + const params = new URLSearchParams( + buildFacetQueryString({ + selectedOrganizationTypeIds: ["workersCoop", "nonprofit"], + }), + ); + expect(params.getAll("facet.orgType")).toEqual([ + "workersCoop", + "nonprofit", + ]); + }); }); diff --git a/tests/unit/methodFacetsSchemas.test.ts b/tests/unit/methodFacetsSchemas.test.ts index 04908ff..94d8ead 100644 --- a/tests/unit/methodFacetsSchemas.test.ts +++ b/tests/unit/methodFacetsSchemas.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import { TYPED_ORG_TYPE_MARKER } from "../../lib/create/deckTypes"; import { flattenRequestedFacets, methodFacetsSchema, @@ -66,6 +67,17 @@ describe("parseRequestedFacetsFromSearchParams", () => { params.append("foo", "bar"); expect(parseRequestedFacetsFromSearchParams(params)).toEqual({}); }); + + it("keeps a typed main-kind marker ahead of catalog kinds", () => { + const params = new URLSearchParams(); + params.append("facet.orgType", TYPED_ORG_TYPE_MARKER); + params.append("facet.orgType", "not-a-kind"); + params.append("facet.orgType", "workersCoop"); + expect(parseRequestedFacetsFromSearchParams(params).orgType).toEqual([ + TYPED_ORG_TYPE_MARKER, + "workersCoop", + ]); + }); }); describe("flattenRequestedFacets", () => { @@ -80,4 +92,12 @@ describe("flattenRequestedFacets", () => { { group: "orgType", value: "dao" }, ]); }); + + it("omits the typed main-kind marker from scoring pairs", () => { + expect( + flattenRequestedFacets({ + orgType: [TYPED_ORG_TYPE_MARKER, "workersCoop"], + }), + ).toEqual([{ group: "orgType", value: "workersCoop" }]); + }); }); diff --git a/tests/unit/publishedRuleAnalytics.test.ts b/tests/unit/publishedRuleAnalytics.test.ts index a973aee..171d9a5 100644 --- a/tests/unit/publishedRuleAnalytics.test.ts +++ b/tests/unit/publishedRuleAnalytics.test.ts @@ -33,4 +33,12 @@ describe("publishedRuleAnalyticsFromState", () => { }), ).toEqual({ communityLocation: "Denver", orgType: "mutualAid" }); }); + + it("records a catalog other kind when the main kind was typed", () => { + expect( + publishedRuleAnalyticsFromState({ + selectedOrganizationTypeIds: ["custom-tenants-union", "workersCoop"], + }), + ).toEqual({ orgType: "workersCoop" }); + }); }); -- 2.53.0