Files
community-rule/lib/create/groupAnswers.ts
T
adilallo f795951443 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.
2026-11-23 10:40:09 -07:00

101 lines
3.3 KiB
TypeScript

import type { CreateFlowState } from "../../app/(app)/create/types";
import type { RequestedFacets } from "../server/validation/methodFacetsSchemas";
import {
resolveMaturityChipId,
resolveOrgChipId,
resolveScaleChipId,
resolveSizeChipId,
} from "./communityChipIds";
import type { GroupAnswers, Kind, Scale, Stage, TraitKey } from "./deckTypes";
import { KINDS, SCALES, STAGES, TRAIT_KEYS } from "./deckTypes";
function isKind(value: string | null | undefined): value is Kind {
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 {
return (SCALES as readonly string[]).includes(value);
}
function isStage(value: string): value is Stage {
return (STAGES as readonly string[]).includes(value);
}
/** Larger organization and Enterprise both score as Established. */
export function stageForScoring(value: string | null | undefined): Stage | null {
if (!value) return null;
if (value === "enterprise" || value === "larger-organization") return "established";
return isStage(value) ? value : null;
}
/**
* One size, one main kind, every ticked scale, and one maturity answer.
* Extra maturity answers stay on the draft and do not add points.
*/
export function groupAnswersFromRequestedFacets(
facets: RequestedFacets,
traits?: Partial<Record<TraitKey, boolean>>,
): GroupAnswers {
const { mainKind, otherKinds } = kindsFromOrderedIds(facets.orgType ?? []);
const scales = (facets.scale ?? []).filter(isScale);
const maturity = (facets.maturity ?? [])
.map((value) => stageForScoring(value))
.filter((value): value is Stage => value !== null);
return {
size: facets.size?.[0] ?? null,
mainKind,
otherKinds,
scale: scales,
stage: maturity[0] ?? null,
traits,
};
}
function traitRecord(
raw: CreateFlowState["setupTraits"],
): Partial<Record<TraitKey, boolean>> | undefined {
if (!raw) return undefined;
const out: Partial<Record<TraitKey, boolean>> = {};
for (const key of TRAIT_KEYS) {
if (typeof raw[key] === "boolean") out[key] = raw[key];
}
return out;
}
export function groupAnswersFromCreateFlowState(
state: CreateFlowState,
): GroupAnswers {
const size = (state.selectedCommunitySizeIds ?? []).map(resolveSizeChipId)[0] ?? null;
const { mainKind, otherKinds } = kindsFromOrderedIds(
(state.selectedOrganizationTypeIds ?? []).map((id) => resolveOrgChipId(id)),
);
const scale = (state.selectedScaleIds ?? [])
.map(resolveScaleChipId)
.filter(isScale);
const maturity = (state.selectedMaturityIds ?? [])
.map((id) => stageForScoring(resolveMaturityChipId(id)))
.filter((value): value is Stage => value !== null);
const uniqueScale = [...new Set(scale)];
return {
size,
mainKind,
otherKinds,
scale: uniqueScale,
stage: maturity[0] ?? null,
traits: traitRecord(state.setupTraits),
};
}