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

89.47% Statements 34/38
63.63% Branches 7/11
100% Functions 11/11
89.47% Lines 34/38

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                      2209x       2209x   2209x 44x 44x 44x         2209x 4x 4x 4x           2209x       3x 3x 3x             2209x 4x 4x         2209x         3x       3x       2209x 3x         2209x 3x 3x     2209x       3x 3x     2209x 3x     2209x 3x   3x 3x          
import {
  CreatedInvite,
  Organisation,
  OrganisationInvite,
  OrganisationMember,
  OrganisationRole,
  AccessMap,
} from "@/objects/Organisation";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig } from "axios";
import { HistoricalAPIRouter } from "../HistoricalApiRouter";
 
export class OrganisationRouter extends HistoricalAPIRouter {
  constructor(
    host: string,
    token: string | null = null,
    default_config: AxiosRequestConfig<any> | null = null,
    headers: Dictionary<string> = {},
  ) {
    super("organisations", host, token, default_config, headers);
  }
 
  /** This router addresses a single organisation as /organisations/id/{id}. */
  protected historyBasePath = "id";
 
  list = async () => {
    const res = await this.api.get(this.prefix);
    Eif (super.validRes(res)) {
      return res.data as Organisation[];
    }
    return null;
  };
 
  id = async (val: string) => {
    const res = await this.api.get(super.endpoint(`id/${val}`));
    Eif (super.validRes(res)) {
      return res.data as Organisation;
    }
    return null;
  };
 
  /** Change what an admin may change of it — its cost centre, here. */
  update = async (
    id: string,
    body: Partial<Pick<Organisation, "name" | "cost_centre">>,
  ): Promise<Organisation | null> => {
    const res = await this.api.put(super.endpoint(`id/${id}`), body);
    Eif (super.validRes(res)) {
      return res.data as Organisation;
    }
    return null;
  };
 
  // ── Members ───────────────────────────────────────────────────────────
 
  members = async (id: string): Promise<OrganisationMember[]> => {
    const res = await this.api.get(super.endpoint(`id/${id}/members`));
    return res.data as OrganisationMember[];
  };
 
  /** Change a member's role, their access per area, or both. Throws with
   *  the server's reason (e.g. the last admin stepping down). */
  updateMember = async (
    id: string,
    userId: string,
    change: { role?: OrganisationRole; access?: AccessMap },
  ): Promise<OrganisationMember> => {
    const res = await this.api.patch(
      super.endpoint(`id/${id}/members/${userId}`),
      change,
    );
    return res.data as OrganisationMember;
  };
 
  /** Remove a member, or leave when `userId` is the caller's own. */
  removeMember = async (id: string, userId: string): Promise<void> => {
    await this.api.delete(super.endpoint(`id/${id}/members/${userId}`));
  };
 
  // ── Invites ───────────────────────────────────────────────────────────
 
  invites = async (id: string): Promise<OrganisationInvite[]> => {
    const res = await this.api.get(super.endpoint(`id/${id}/invites`));
    return res.data as OrganisationInvite[];
  };
 
  createInvite = async (
    id: string,
    body: { email: string; role: OrganisationRole; access?: AccessMap },
  ): Promise<CreatedInvite> => {
    const res = await this.api.post(super.endpoint(`id/${id}/invites`), body);
    return res.data as CreatedInvite;
  };
 
  revokeInvite = async (id: string, inviteId: string): Promise<void> => {
    await this.api.delete(super.endpoint(`id/${id}/invites/${inviteId}`));
  };
 
  new = async (organisation: Organisation) => {
    const res = await this.api.post(`${this.prefix}/`, organisation);
    // console.log(res);
    Eif (super.validRes(res)) {
      return res; // as Project;
    }
    return null;
  };
}