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

92.15% Statements 47/51
63.63% Branches 7/11
100% Functions 16/16
92.15% Lines 47/51

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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148                                                  2209x     2209x 45x 45x 45x         2209x 6x 6x 6x           2209x       3x 3x 3x             2209x 4x 4x     2209x       3x     3x       2209x       3x 3x     2209x 3x     2209x       3x     3x         2209x 4x 4x         2209x         3x       3x     2209x 3x     2209x 3x 3x     2209x       3x 3x     2209x 3x     2209x 3x   3x 3x          
import { HistoricalAPIRouter } from "../HistoricalApiRouter";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig } from "axios";
import { Project } from "@/objects/Project";
import {
  InheritanceCandidate,
  InheritanceDeclaration,
  InheritanceDependent,
  ProjectInheritance,
} from "@/objects/Inheritance";
import {
  CreatedInvite,
  OrganisationInvite,
  OrganisationMember,
  OrganisationRole,
  AccessMap,
} from "@/objects/Organisation";
 
export class ProjectRouter extends HistoricalAPIRouter {
  constructor(
    host: string,
    token: string | null = null,
    default_config: AxiosRequestConfig<any> | null = null,
    headers: Dictionary<string> = {},
  ) {
    super("projects", host, token, default_config, headers);
  }
 
  list = async () => {
    const res = await this.api.get(this.prefix);
    Eif (super.validRes(res)) {
      return res.data as Project[];
    }
    return null;
  };
 
  id = async (val: string) => {
    const res = await this.api.get(super.endpoint(val));
    Eif (super.validRes(res)) {
      return res.data as Project;
    }
    return null;
  };
 
  /** Change a project's name, description or cost centre. */
  update = async (
    id: string,
    body: Partial<Pick<Project, "name" | "description" | "cost_centre">>,
  ): Promise<Project | null> => {
    const res = await this.api.patch(super.endpoint(id), body);
    Eif (super.validRes(res)) {
      return res.data as Project;
    }
    return null;
  };
 
  // ── What the project inherits from its parent ─────────────────────────
 
  inheritance = async (id: string): Promise<ProjectInheritance> => {
    const res = await this.api.get(super.endpoint(`${id}/inheritance`));
    return res.data as ProjectInheritance;
  };
 
  inheritanceCandidates = async (
    id: string,
    kind: string,
  ): Promise<InheritanceCandidate[]> => {
    const res = await this.api.get(super.endpoint(`${id}/inheritance/candidates`), {
      params: { kind },
    });
    return res.data as InheritanceCandidate[];
  };
 
  /** Inherit one resource, or (no `resource_id`) every one of a kind. */
  declareInheritance = async (
    id: string,
    body: { kind: string; resource_id?: string | null },
  ): Promise<InheritanceDeclaration> => {
    const res = await this.api.post(super.endpoint(`${id}/inheritance`), body);
    return res.data as InheritanceDeclaration;
  };
 
  withdrawInheritance = async (id: string, declarationId: string) => {
    await this.api.delete(super.endpoint(`${id}/inheritance/${declarationId}`));
  };
 
  inheritanceDependents = async (
    id: string,
    declarationId: string,
  ): Promise<InheritanceDependent[]> => {
    const res = await this.api.get(
      super.endpoint(`${id}/inheritance/${declarationId}/dependents`),
    );
    return res.data as InheritanceDependent[];
  };
 
  // ── Members and invites: the same routes organisations have ───────────
 
  members = async (id: string): Promise<OrganisationMember[]> => {
    const res = await this.api.get(super.endpoint(`${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}/members/${userId}`),
      change,
    );
    return res.data as OrganisationMember;
  };
 
  removeMember = async (id: string, userId: string): Promise<void> => {
    await this.api.delete(super.endpoint(`${id}/members/${userId}`));
  };
 
  invites = async (id: string): Promise<OrganisationInvite[]> => {
    const res = await this.api.get(super.endpoint(`${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}/invites`), body);
    return res.data as CreatedInvite;
  };
 
  revokeInvite = async (id: string, inviteId: string): Promise<void> => {
    await this.api.delete(super.endpoint(`${id}/invites/${inviteId}`));
  };
 
  new = async (project: Project) => {
    const res = await this.api.post(this.prefix, project);
    // console.log(res);
    Eif (super.validRes(res)) {
      return res; // as Project;
    }
    return null;
  };
}