All files / src/services/api/routers DecisionRouter.tsx

78.57% Statements 11/14
44.44% Branches 4/9
100% Functions 4/4
84.61% Lines 11/13

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                                            2209x                 2209x 6x 6x 6x               2209x       3x       3x         2209x       3x       3x      
import {
  Decision,
  DecisionCaseCreate,
  DecisionCaseCreated,
  DecisionOutcome,
  OutcomeCreate,
} from "@/objects/Decision";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig, isAxiosError } from "axios";
import { APIRouter } from "../ApiRouter";
 
/**
 * The decision record: one governed turn, assembled from everything it
 * recorded (svc-core `GET /app/decisions/:id`).
 */
export class DecisionRouter extends APIRouter {
  constructor(
    host: string,
    token: string | null = null,
    default_config: AxiosRequestConfig | null = null,
    headers: Dictionary<string> = {},
  ) {
    super("decisions", host, token, default_config, headers);
  }
 
  /**
   * The record of the turn with this id. Null when there is none to read —
   * svc-core answers 404 alike for an id no turn has and for a turn the
   * caller neither had nor reviews — so the page says "not found" rather
   * than showing an error.
   */
  get = async (id: string): Promise<Decision | null> => {
    try {
      const res = await this.api.get<Decision>(super.endpoint(id));
      return super.validRes(res) ? res.data : null;
    } catch (e) {
      if (isAxiosError(e) && e.response?.status === 404) return null;
      throw e;
    }
  };
 
  /** Give the turn a verdict. A changed mind is a new verdict. */
  recordOutcome = async (
    id: string,
    body: OutcomeCreate,
  ): Promise<DecisionOutcome> => {
    const res = await this.api.post<DecisionOutcome>(
      super.endpoint(`${id}/outcome`),
      body,
    );
    return res.data;
  };
 
  /** Add the turn to a dataset as a case; refused (409) until it has a
   *  verdict. */
  addToDataset = async (
    id: string,
    body: DecisionCaseCreate,
  ): Promise<DecisionCaseCreated> => {
    const res = await this.api.post<DecisionCaseCreated>(
      super.endpoint(`${id}/case`),
      body,
    );
    return res.data;
  };
}