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

80.95% Statements 17/21
61.53% Branches 8/13
100% Functions 5/5
80.95% Lines 17/21

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                                                    2209x       2209x 8x     8x 8x                     2209x 42x 42x 42x         2209x 3x 3x 3x           2209x               3x         3x 3x          
import {
  ReviewDecision,
  ReviewFlag,
  ReviewFlagList,
  ReviewQuery,
  ReviewSummary,
} from "@/objects/Review";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig } from "axios";
import { APIRouter } from "../ApiRouter";
 
/**
 * The audit reviewer queue.
 *
 * The other half of the `flag_for_review` audit action: the rule raises a
 * flag, and these are the rows somebody works through. Resolution is terminal
 * — there is no reopen, which is what lets the recorded decision stand as an
 * audit answer rather than a setting.
 */
export class ReviewRouter extends APIRouter {
  constructor(
    host: string,
    token: string | null = null,
    default_config: AxiosRequestConfig | null = null,
    headers: Dictionary<string> = {},
  ) {
    super("reviews", host, token, default_config, headers);
  }
 
  /** The queue, newest first. */
  list = async (params?: ReviewQuery): Promise<ReviewFlagList | null> => {
    const res = await this.api.get<ReviewFlagList>(
      super.endpoint("", params as Record<string, unknown> | undefined),
    );
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  /**
   * Counts per status.
   *
   * Declared before `/{flag_id}` on the backend so the literal path is not
   * parsed as a flag id.
   */
  summary = async (): Promise<ReviewSummary | null> => {
    const res = await this.api.get<ReviewSummary>(super.endpoint("summary"));
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  get = async (id: string): Promise<ReviewFlag | null> => {
    const res = await this.api.get<ReviewFlag>(super.endpoint(id));
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  /** Record a decision. Fails with 409 if the flag was already resolved. */
  resolve = async (
    id: string,
    status: ReviewDecision,
    note?: string,
    /** The reviewer's verdict on the flagged answer itself, recorded as its
     *  decision outcome. */
    verdict?: "accepted" | "rejected",
  ): Promise<ReviewFlag | null> => {
    const res = await this.api.patch<ReviewFlag>(super.endpoint(id), {
      status,
      note,
      ...(verdict ? { verdict } : {}),
    });
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
}