Treat a typed main kind as no kind so recommendations still fall back to size.
A custom kind no longer empties a job of eligible cards, and other catalog kinds stay ranking hints instead of becoming the main kind.
This commit is contained in:
1 parent
dad8661268
commit
f795951443
5 files changed
+116
-16
No files matched your search
@@ -12,6 +12,7 @@ import jobsFile from "../../data/create/customRule/_jobs.json";
|
|||||||
import membershipFacets from "../../data/create/customRule/membership.json";
|
import membershipFacets from "../../data/create/customRule/membership.json";
|
||||||
import type {
|
import type {
|
||||||
CardEngineEntry,
|
CardEngineEntry,
|
||||||
|
CardFacets,
|
||||||
DeckEngine,
|
DeckEngine,
|
||||||
DeckId,
|
DeckId,
|
||||||
DeckRecommendation,
|
DeckRecommendation,
|
||||||
@@ -53,20 +54,24 @@ interface Evaluated {
|
|||||||
otherKindHits: number;
|
otherKindHits: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Normalizes setup answers. A group with other kinds and no main kind gets the first as main. */
|
/** A typed or missing main kind stays unset so eligibility falls back to size. */
|
||||||
export function normalizeAnswers(answers: GroupAnswers): Required<GroupAnswers> {
|
export function normalizeAnswers(answers: GroupAnswers): Required<GroupAnswers> {
|
||||||
const others = [...(answers.otherKinds ?? [])];
|
const mainKind = answers.mainKind ?? null;
|
||||||
const mainKind = answers.mainKind ?? others.shift() ?? null;
|
|
||||||
return {
|
return {
|
||||||
size: answers.size ?? null,
|
size: answers.size ?? null,
|
||||||
mainKind,
|
mainKind,
|
||||||
otherKinds: others.filter((k) => k !== mainKind),
|
otherKinds: [...(answers.otherKinds ?? [])].filter((k) => k !== mainKind),
|
||||||
scale: [...new Set(answers.scale ?? [])],
|
scale: [...new Set(answers.scale ?? [])],
|
||||||
stage: answers.stage ?? null,
|
stage: answers.stage ?? null,
|
||||||
traits: answers.traits ?? {},
|
traits: answers.traits ?? {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function kindFits(orgType: CardFacets["orgType"], kind: Kind | null): boolean {
|
||||||
|
if (!kind || !(kind in orgType)) return true;
|
||||||
|
return orgType[kind];
|
||||||
|
}
|
||||||
|
|
||||||
function sizeBandOf(size: string | null, data: DeckData): SizeBand | null {
|
function sizeBandOf(size: string | null, data: DeckData): SizeBand | null {
|
||||||
if (!size) return null;
|
if (!size) return null;
|
||||||
const band = data.config.sizeAnswers[size];
|
const band = data.config.sizeAnswers[size];
|
||||||
@@ -86,7 +91,7 @@ function evaluate(
|
|||||||
if (!f || !entry) throw new Error(`Card ${id} is missing from the facet or jobs file`);
|
if (!f || !entry) throw new Error(`Card ${id} is missing from the facet or jobs file`);
|
||||||
const traits = entry.traits ?? {};
|
const traits = entry.traits ?? {};
|
||||||
const fitsSize = band ? f.size[band] : true;
|
const fitsSize = band ? f.size[band] : true;
|
||||||
const fitsKind = a.mainKind ? f.orgType[a.mainKind] : true;
|
const fitsKind = kindFits(f.orgType, a.mainKind);
|
||||||
const openedBy = (traits.opens ?? []).find((k) => ticked.has(k));
|
const openedBy = (traits.opens ?? []).find((k) => ticked.has(k));
|
||||||
const isBeside = (traits.beside ?? []).length > 0;
|
const isBeside = (traits.beside ?? []).length > 0;
|
||||||
// Each facet group counts once. Scale earns its point only if the card fits
|
// Each facet group counts once. Scale earns its point only if the card fits
|
||||||
@@ -96,7 +101,7 @@ function evaluate(
|
|||||||
const fitsAnswer = (traits.fits ?? []).find((k) => ticked.has(k));
|
const fitsAnswer = (traits.fits ?? []).find((k) => ticked.has(k));
|
||||||
const matched: MatchedFacet[] = [];
|
const matched: MatchedFacet[] = [];
|
||||||
if (band && fitsSize) matched.push("size");
|
if (band && fitsSize) matched.push("size");
|
||||||
if (a.mainKind && fitsKind) matched.push("kind");
|
if (a.mainKind && a.mainKind in f.orgType && fitsKind) matched.push("kind");
|
||||||
if (fitsScale) matched.push("scale");
|
if (fitsScale) matched.push("scale");
|
||||||
if (fitsStage) matched.push("stage");
|
if (fitsStage) matched.push("stage");
|
||||||
return {
|
return {
|
||||||
|
|||||||
+23
-10
@@ -9,8 +9,21 @@ import {
|
|||||||
import type { GroupAnswers, Kind, Scale, Stage, TraitKey } from "./deckTypes";
|
import type { GroupAnswers, Kind, Scale, Stage, TraitKey } from "./deckTypes";
|
||||||
import { KINDS, SCALES, STAGES, TRAIT_KEYS } from "./deckTypes";
|
import { KINDS, SCALES, STAGES, TRAIT_KEYS } from "./deckTypes";
|
||||||
|
|
||||||
function isKind(value: string): value is Kind {
|
function isKind(value: string | null | undefined): value is Kind {
|
||||||
return (KINDS as readonly string[]).includes(value);
|
return value != null && (KINDS as readonly string[]).includes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** First id is the main kind only when it is a catalog kind. A typed kind is no kind. */
|
||||||
|
function kindsFromOrderedIds(ids: readonly string[]): {
|
||||||
|
mainKind: Kind | null;
|
||||||
|
otherKinds: Kind[];
|
||||||
|
} {
|
||||||
|
const mainKind = isKind(ids[0]) ? ids[0] : null;
|
||||||
|
const otherKinds = ids
|
||||||
|
.slice(1)
|
||||||
|
.filter(isKind)
|
||||||
|
.filter((kind, index, all) => kind !== mainKind && all.indexOf(kind) === index);
|
||||||
|
return { mainKind, otherKinds };
|
||||||
}
|
}
|
||||||
|
|
||||||
function isScale(value: string): value is Scale {
|
function isScale(value: string): value is Scale {
|
||||||
@@ -36,15 +49,15 @@ export function groupAnswersFromRequestedFacets(
|
|||||||
facets: RequestedFacets,
|
facets: RequestedFacets,
|
||||||
traits?: Partial<Record<TraitKey, boolean>>,
|
traits?: Partial<Record<TraitKey, boolean>>,
|
||||||
): GroupAnswers {
|
): GroupAnswers {
|
||||||
const kinds = (facets.orgType ?? []).filter(isKind);
|
const { mainKind, otherKinds } = kindsFromOrderedIds(facets.orgType ?? []);
|
||||||
const scales = (facets.scale ?? []).filter(isScale);
|
const scales = (facets.scale ?? []).filter(isScale);
|
||||||
const maturity = (facets.maturity ?? [])
|
const maturity = (facets.maturity ?? [])
|
||||||
.map((value) => stageForScoring(value))
|
.map((value) => stageForScoring(value))
|
||||||
.filter((value): value is Stage => value !== null);
|
.filter((value): value is Stage => value !== null);
|
||||||
return {
|
return {
|
||||||
size: facets.size?.[0] ?? null,
|
size: facets.size?.[0] ?? null,
|
||||||
mainKind: kinds[0] ?? null,
|
mainKind,
|
||||||
otherKinds: kinds.slice(1),
|
otherKinds,
|
||||||
scale: scales,
|
scale: scales,
|
||||||
stage: maturity[0] ?? null,
|
stage: maturity[0] ?? null,
|
||||||
traits,
|
traits,
|
||||||
@@ -66,9 +79,9 @@ export function groupAnswersFromCreateFlowState(
|
|||||||
state: CreateFlowState,
|
state: CreateFlowState,
|
||||||
): GroupAnswers {
|
): GroupAnswers {
|
||||||
const size = (state.selectedCommunitySizeIds ?? []).map(resolveSizeChipId)[0] ?? null;
|
const size = (state.selectedCommunitySizeIds ?? []).map(resolveSizeChipId)[0] ?? null;
|
||||||
const kinds = (state.selectedOrganizationTypeIds ?? [])
|
const { mainKind, otherKinds } = kindsFromOrderedIds(
|
||||||
.map((id) => resolveOrgChipId(id))
|
(state.selectedOrganizationTypeIds ?? []).map((id) => resolveOrgChipId(id)),
|
||||||
.filter(isKind);
|
);
|
||||||
const scale = (state.selectedScaleIds ?? [])
|
const scale = (state.selectedScaleIds ?? [])
|
||||||
.map(resolveScaleChipId)
|
.map(resolveScaleChipId)
|
||||||
.filter(isScale);
|
.filter(isScale);
|
||||||
@@ -78,8 +91,8 @@ export function groupAnswersFromCreateFlowState(
|
|||||||
const uniqueScale = [...new Set(scale)];
|
const uniqueScale = [...new Set(scale)];
|
||||||
return {
|
return {
|
||||||
size,
|
size,
|
||||||
mainKind: kinds[0] ?? null,
|
mainKind,
|
||||||
otherKinds: kinds.slice(1).filter((kind, index, all) => all.indexOf(kind) === index),
|
otherKinds,
|
||||||
scale: uniqueScale,
|
scale: uniqueScale,
|
||||||
stage: maturity[0] ?? null,
|
stage: maturity[0] ?? null,
|
||||||
traits: traitRecord(state.setupTraits),
|
traits: traitRecord(state.setupTraits),
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { deckData, recommendDeck } from "../../lib/create/deckRecommend";
|
||||||
|
import type { Kind } from "../../lib/create/deckTypes";
|
||||||
|
|
||||||
|
describe("recommendDeck typed main kind", () => {
|
||||||
|
const sizeOnly = {
|
||||||
|
size: "sixToTwelve",
|
||||||
|
mainKind: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
it("treats a typed main kind as no kind so eligibility falls back to size", () => {
|
||||||
|
const typed = recommendDeck(deckData("decisionApproaches"), {
|
||||||
|
size: "sixToTwelve",
|
||||||
|
mainKind: "streetMedicCrew" as Kind,
|
||||||
|
});
|
||||||
|
const none = recommendDeck(deckData("decisionApproaches"), sizeOnly);
|
||||||
|
const coop = recommendDeck(deckData("decisionApproaches"), {
|
||||||
|
size: "sixToTwelve",
|
||||||
|
mainKind: "workersCoop",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(typed.fallback).toBe(false);
|
||||||
|
expect(typed.recommended.map((card) => card.id)).toEqual(
|
||||||
|
none.recommended.map((card) => card.id),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
typed.recommended.some(
|
||||||
|
(card) =>
|
||||||
|
(card.reason.kind === "fit" || card.reason.kind === "opened") &&
|
||||||
|
card.reason.matched.includes("kind"),
|
||||||
|
),
|
||||||
|
).toBe(false);
|
||||||
|
expect(
|
||||||
|
coop.recommended.some(
|
||||||
|
(card) =>
|
||||||
|
(card.reason.kind === "fit" || card.reason.kind === "opened") &&
|
||||||
|
card.reason.matched.includes("kind"),
|
||||||
|
),
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps other catalog kinds as ranking hints, not as the main kind", () => {
|
||||||
|
const typedWithOther = recommendDeck(deckData("decisionApproaches"), {
|
||||||
|
size: "sixToTwelve",
|
||||||
|
mainKind: null,
|
||||||
|
otherKinds: ["workersCoop"],
|
||||||
|
});
|
||||||
|
const none = recommendDeck(deckData("decisionApproaches"), sizeOnly);
|
||||||
|
|
||||||
|
expect(typedWithOther.fallback).toBe(false);
|
||||||
|
expect(new Set(typedWithOther.recommended.map((card) => card.job))).toEqual(
|
||||||
|
new Set(none.recommended.map((card) => card.job)),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
typedWithOther.recommended.some(
|
||||||
|
(card) =>
|
||||||
|
(card.reason.kind === "fit" || card.reason.kind === "opened") &&
|
||||||
|
card.reason.matched.includes("kind"),
|
||||||
|
),
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -19,6 +19,15 @@ describe("maturity and chip aliases", () => {
|
|||||||
expect(answers.mainKind).toBe("workersCoop");
|
expect(answers.mainKind).toBe("workersCoop");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("treats a typed main kind as no kind and keeps catalog kinds as other kinds", () => {
|
||||||
|
const answers = groupAnswersFromCreateFlowState({
|
||||||
|
selectedCommunitySizeIds: ["sixToTwelve"],
|
||||||
|
selectedOrganizationTypeIds: ["custom-tenants-union", "workersCoop"],
|
||||||
|
});
|
||||||
|
expect(answers.mainKind).toBeNull();
|
||||||
|
expect(answers.otherKinds).toEqual(["workersCoop"]);
|
||||||
|
});
|
||||||
|
|
||||||
it("passes ticked setup answers through as traits", () => {
|
it("passes ticked setup answers through as traits", () => {
|
||||||
const answers = groupAnswersFromCreateFlowState({
|
const answers = groupAnswersFromCreateFlowState({
|
||||||
setupTraits: { employer: true, records: false },
|
setupTraits: { employer: true, records: false },
|
||||||
|
|||||||
@@ -55,6 +55,17 @@ describe("listMethodRecommendations", () => {
|
|||||||
expect(findManyMock).not.toHaveBeenCalled();
|
expect(findManyMock).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("still ranks when only size is set and the main kind is missing", async () => {
|
||||||
|
const result = await listMethodRecommendations({
|
||||||
|
section: "decisionApproaches",
|
||||||
|
facets: {
|
||||||
|
size: ["sixToTwelve"],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(result?.rankedSlugs.length).toBeGreaterThan(0);
|
||||||
|
expect(findManyMock).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
it("uses only the main kind for eligibility", async () => {
|
it("uses only the main kind for eligibility", async () => {
|
||||||
const result = await listMethodRecommendations({
|
const result = await listMethodRecommendations({
|
||||||
section: "decisionApproaches",
|
section: "decisionApproaches",
|
||||||
|
|||||||
Reference in new issue
Block a user