45 lines
2.3 KiB
TypeScript
45 lines
2.3 KiB
TypeScript
// Project and unit endpoints from the phase 2 contract.
|
|||
|
|
import { api } from "./client";
|
||
|
|
import { asList, qs } from "./query";
|
||
|
|
import type { FieldCas, Guarded, NumberLookup, Project, ProjectCreate, Unit, UnitsCreate } from "./types";
|
||
|
|
|
||
|
|
export type ProjectShow = "active" | "open" | "hold" | "closed" | "all";
|
||
|
|
|
||
|
|
export interface ProjectFields {
|
||
|
|
name: string | null;
|
||
|
|
customer: string | null;
|
||
|
|
po: string | null;
|
||
|
|
due: string | null;
|
||
|
|
status: Project["status"];
|
||
|
|
notes: string | null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface UnitFields {
|
||
|
|
name: string | null;
|
||
|
|
due: string | null;
|
||
|
|
status: Unit["status"];
|
||
|
|
notes: string | null;
|
||
|
|
}
|
||
|
|
|
||
|
|
const pid = (id: number) => `/api/projects/${encodeURIComponent(String(id))}`;
|
||
|
|
const uid = (id: number) => `/api/units/${encodeURIComponent(String(id))}`;
|
||
|
|
|
||
|
|
export const projectsApi = {
|
||
|
|
list: async (q: string, status: ProjectShow = "active", signal?: AbortSignal): Promise<Project[]> =>
|
||
|
|
asList<Project>(await api.get<unknown>(`/api/projects${qs({ q, status })}`, signal), "projects"),
|
||
|
|
get: (id: number, signal?: AbortSignal) => api.get<Project>(pid(id), signal),
|
||
|
|
create: (body: ProjectCreate & Guarded) => api.post<Project>("/api/projects", body),
|
||
|
|
patch: (id: number, body: FieldCas<ProjectFields>) => api.patch<Project>(pid(id), body),
|
||
|
|
renumber: (id: number, body: { number: string; reason: string } & Guarded) => api.post<Project>(`${pid(id)}/renumber`, body),
|
||
|
|
remove: (id: number, body: { reason: string } & Guarded) => api.delWithBody<null>(pid(id), body),
|
||
|
|
addUnits: async (id: number, body: UnitsCreate & Guarded): Promise<Unit[]> =>
|
||
|
|
asList<Unit>(await api.post<unknown>(`${pid(id)}/units`, body), "units"),
|
||
|
|
units: async (projectId: number, q = "", signal?: AbortSignal): Promise<Unit[]> =>
|
||
|
|
asList<Unit>(await api.get<unknown>(`/api/units${qs({ project_id: projectId, q })}`, signal), "units"),
|
||
|
|
unit: (id: number, signal?: AbortSignal) => api.get<Unit>(uid(id), signal),
|
||
|
|
patchUnit: (id: number, body: FieldCas<UnitFields>) => api.patch<Unit>(uid(id), body),
|
||
|
|
renumberUnit: (id: number, body: { lot: string; reason: string } & Guarded) => api.post<Unit>(`${uid(id)}/renumber`, body),
|
||
|
|
removeUnit: (id: number, body: { reason: string } & Guarded) => api.delWithBody<null>(uid(id), body),
|
||
|
|
lookup: (n: string, signal?: AbortSignal) => api.get<NumberLookup>(`/api/lookup/number${qs({ n })}`, signal),
|
||
|
|
};
|