All files / src/services rescoreEligibility.ts

100% Statements 47/47
100% Branches 16/16
100% Functions 1/1
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 481x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 9x 13x 5x 5x 4x 2x 2x 5x 4x 4x 4x 3x 2x 1x 3x 13x  
import { FREE_RESCORES_PER_MONTH } from "../models/user";
import type { Fit } from "../models/job";
 
export type ChargeKind = "rescore" | "analysis";
 
export interface RescoreDecisionInput {
  /** The request's rescore intent; absent for an ordinary analysis. */
  intent: { priorFingerprint: string | null } | undefined;
  /** Fingerprint of the profile being sent; absent when no profile is sent. */
  currentFingerprint: string | undefined;
  /**
   * The saved posting's fit: `undefined` when nothing is saved under this
   * key, `null` when the saved posting was never scored.
   */
  savedFit: Fit | null | undefined;
  /** Profiles.fingerprintHistory; only consulted when nothing is saved. */
  fingerprintHistory: string[];
  rescoresUsed: number;
}
 
/**
 * Which counter a request is charged against (research R6). The saved row's
 * fingerprint is authoritative; without one, the claimed prior must be a
 * fingerprint the account really had. An ineligible rescore is never
 * rejected — it is metered as an ordinary analysis, as the spec requires.
 * Takes no tier, so both tiers get the same rule (FR-026).
 */
export function decideRescoreCharge(input: RescoreDecisionInput): ChargeKind {
  const { intent, currentFingerprint, savedFit } = input;
  if (!intent || !currentFingerprint) return "analysis";
  if (input.rescoresUsed >= FREE_RESCORES_PER_MONTH) return "analysis";
 
  if (savedFit !== undefined) {
    // A saved posting with no fit is a first scoring, not a rescore.
    if (savedFit === null) return "analysis";
    return savedFit.profileFingerprint === currentFingerprint
      ? "analysis"
      : "rescore";
  }
 
  const prior = intent.priorFingerprint;
  return prior !== null &&
    prior !== currentFingerprint &&
    input.fingerprintHistory.includes(prior)
    ? "rescore"
    : "analysis";
}