// 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; order_pin?: string | null; order_pin_reason?: string | null; } export interface UnitFields { name: string | null; due: string | null; status: Unit["status"]; notes: string | null; order_pin?: string | null; order_pin_reason?: 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 => asList(await api.get(`/api/projects${qs({ q, status })}`, signal), "projects"), get: (id: number, signal?: AbortSignal) => api.get(pid(id), signal), create: (body: ProjectCreate & Guarded) => api.post("/api/projects", body), patch: (id: number, body: FieldCas) => api.patch(pid(id), body), renumber: (id: number, body: { number: string; reason: string } & Guarded) => api.post(`${pid(id)}/renumber`, body), remove: (id: number, body: { reason: string } & Guarded) => api.delWithBody(pid(id), body), addUnits: async (id: number, body: UnitsCreate & Guarded): Promise => asList(await api.post(`${pid(id)}/units`, body), "units"), units: async (projectId: number, q = "", signal?: AbortSignal): Promise => asList(await api.get(`/api/units${qs({ project_id: projectId, q })}`, signal), "units"), unit: (id: number, signal?: AbortSignal) => api.get(uid(id), signal), patchUnit: (id: number, body: FieldCas) => api.patch(uid(id), body), renumberUnit: (id: number, body: { lot: string; reason: string } & Guarded) => api.post(`${uid(id)}/renumber`, body), removeUnit: (id: number, body: { reason: string } & Guarded) => api.delWithBody(uid(id), body), lookup: (n: string, signal?: AbortSignal) => api.get(`/api/lookup/number${qs({ n })}`, signal), };