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

76% Statements 19/25
73.33% Branches 11/15
71.42% Functions 5/7
82.6% Lines 19/23

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                                    2209x       2209x 8x 8x 8x           2209x 3x 3x 3x             2209x 3x           3x 3x         3x             2209x 3x             3x 3x         3x      
import type { Statement, StatementList } from "@/objects/Statement";
import { Dictionary } from "@/types/ext";
import { dispositionFilename, saveBlob } from "@/util/download";
import { AxiosRequestConfig } from "axios";
import { APIRouter } from "../ApiRouter";
 
/**
 * Monthly statements (svc-core `/app/statements`): one workspace's month as
 * figures — its spend and what governed it. A closed month is served as it
 * was kept when it ended; the month under way is a draft.
 */
export class StatementRouter extends APIRouter {
  constructor(
    host: string,
    token: string | null = null,
    default_config: AxiosRequestConfig | null = null,
    headers: Dictionary<string> = {},
  ) {
    super("statements", host, token, default_config, headers);
  }
 
  /** The month so far, and every month the workspace has closed. */
  list = async (): Promise<StatementList | null> => {
    const res = await this.api.get<StatementList>(super.endpoint(""));
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  /** One month's statement (`2026-10`). */
  get = async (month: string): Promise<Statement | null> => {
    const res = await this.api.get<Statement>(super.endpoint(month));
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  /** Download one month's statement as a CSV. Resolves false when the
   *  download failed. */
  downloadCsv = async (month: string): Promise<boolean> => {
    const res = await this.api
      .get(super.endpoint(`${month}/csv`), {
        responseType: "blob",
        withCredentials: true,
      })
      .catch(() => null);
    Iif (res?.status !== 200) return false;
    saveBlob(
      res.data as Blob,
      dispositionFilename(res.headers?.["content-disposition"]) ??
        `chainr-statement-${month}.csv`,
    );
    return true;
  };
 
  /** Download the months `from`…`to` (`2026-04`, at most 24) as one CSV for
   *  finance: every figure a row, with its month and the cost centre it is
   *  filed under. For those who govern the workspace. Resolves false when
   *  the download failed. */
  downloadExport = async (from: string, to: string): Promise<boolean> => {
    const res = await this.api
      .get(super.endpoint("export"), {
        params: { from, to },
        responseType: "blob",
        withCredentials: true,
      })
      .catch(() => null);
    Iif (res?.status !== 200) return false;
    saveBlob(
      res.data as Blob,
      dispositionFilename(res.headers?.["content-disposition"]) ??
        `chainr-statements-${from}-to-${to}.csv`,
    );
    return true;
  };
}