2026-10-16 09:31:45 -06:00
|
|
|
// Packet endpoints from the phase 2 contract.
|
|
|
|
|
import { api, request } from "./client";
|
|
|
|
|
import { asList, ifMatch, qs } from "./query";
|
|
|
|
|
import type {
|
|
|
|
|
AttachPagesBody,
|
|
|
|
|
BomLine,
|
|
|
|
|
FieldCas,
|
|
|
|
|
Guarded,
|
|
|
|
|
LogRow,
|
|
|
|
|
PacketCard,
|
|
|
|
|
PacketCreate,
|
|
|
|
|
PacketHeader,
|
|
|
|
|
PacketListQuery,
|
|
|
|
|
PacketRead,
|
|
|
|
|
TransitionBody,
|
|
|
|
|
WorkOrder,
|
|
|
|
|
} from "./types";
|
|
|
|
|
|
|
|
|
|
const base = (id: number) => `/api/packets/${encodeURIComponent(String(id))}`;
|
|
|
|
|
|
|
|
|
|
export const packetsApi = {
|
|
|
|
|
list: async (q: PacketListQuery = {}, signal?: AbortSignal): Promise<PacketCard[]> =>
|
|
|
|
|
asList<PacketCard>(await api.get<unknown>(`/api/packets${qs({ limit: 500, ...q })}`, signal), "packets"),
|
|
|
|
|
get: (id: number, signal?: AbortSignal) => api.get<PacketRead>(base(id), signal),
|
|
|
|
|
log: async (id: number, opts: { limit?: number; before?: number } = {}, signal?: AbortSignal): Promise<LogRow[]> =>
|
|
|
|
|
asList<LogRow>(await api.get<unknown>(`${base(id)}/log${qs({ limit: opts.limit ?? 300, before: opts.before })}`, signal), "log"),
|
|
|
|
|
create: (body: PacketCreate) => api.post<PacketRead>("/api/packets", body),
|
|
|
|
|
patch: (id: number, body: FieldCas<PacketHeader>) => api.patch<PacketRead>(base(id), body),
|
|
|
|
|
transition: (id: number, body: TransitionBody) => api.post<PacketRead>(`${base(id)}/transitions`, body),
|
|
|
|
|
putWorkOrders: (id: number, version: number, work_orders: WorkOrder[], extra: Guarded = {}) =>
|
|
|
|
|
request<PacketRead>(`${base(id)}/work-orders`, { method: "PUT", body: { work_orders, ...extra }, headers: ifMatch(version) }),
|
|
|
|
|
putBom: (id: number, version: number, bom: BomLine[], extra: Guarded = {}) =>
|
|
|
|
|
request<PacketRead>(`${base(id)}/bom`, { method: "PUT", body: { bom, ...extra }, headers: ifMatch(version) }),
|
|
|
|
|
attachPages: (id: number, body: AttachPagesBody) => api.post<PacketRead>(`${base(id)}/pages`, body),
|
|
|
|
|
removePages: (id: number, n: number, body: { reason?: string } & Guarded = {}) =>
|
|
|
|
|
api.delWithBody<PacketRead>(`${base(id)}/pages/${n}`, body),
|
|
|
|
|
addDoc: (id: number, body: { asset_id: string; kind: string; title: string } & Guarded) =>
|
|
|
|
|
api.post<PacketRead>(`${base(id)}/docs`, body),
|
|
|
|
|
removeDoc: (id: number, docId: number, extra: Guarded = {}) =>
|
|
|
|
|
api.delWithBody<PacketRead>(`${base(id)}/docs/${docId}`, extra),
|
|
|
|
|
move: (id: number, body: { unit_id: number; reason?: string } & Guarded) => api.post<PacketRead>(`${base(id)}/move`, body),
|
|
|
|
|
remove: (id: number, body: { reason: string } & Guarded) => api.delWithBody<PacketRead | null>(base(id), body),
|
|
|
|
|
setVclosed: (id: number, closed: boolean, extra: Guarded = {}) =>
|
|
|
|
|
api.post<PacketRead>(`${base(id)}/vclosed`, { closed, ...extra }),
|
|
|
|
|
databook: (id: number) => api.post<PacketRead>(`${base(id)}/databook`, {}),
|
2026-10-16 14:45:38 -06:00
|
|
|
|
|
|
|
|
/* Phase 3: VISUAL packets. */
|
|
|
|
|
revApprove: (id: number, body: { reason: string } & Guarded) => api.post<PacketRead>(`${base(id)}/rev-approve`, body),
|
|
|
|
|
handDrawing: (id: number, body: { asset_id: string; reason: string } & Guarded) =>
|
|
|
|
|
api.post<PacketRead>(`${base(id)}/hand/drawing`, body),
|
|
|
|
|
/** Multipart cannot carry JSON, so the signature rides as sign_factor and sign_secret. */
|
|
|
|
|
handBom: (id: number, file: File, reason: string, extra: Guarded = {}) => {
|
|
|
|
|
const fd = new FormData();
|
|
|
|
|
fd.append("file", file, file.name);
|
|
|
|
|
fd.append("reason", reason);
|
|
|
|
|
if (extra.sign) {
|
|
|
|
|
fd.append("sign_factor", extra.sign.factor);
|
|
|
|
|
fd.append("sign_secret", extra.sign.secret);
|
|
|
|
|
}
|
|
|
|
|
if (extra.it_reason) fd.append("it_reason", extra.it_reason);
|
|
|
|
|
return request<PacketRead>(`${base(id)}/hand/bom`, { method: "POST", body: fd });
|
|
|
|
|
},
|
|
|
|
|
removeHandBom: (id: number, extra: Guarded = {}) => api.delWithBody<PacketRead>(`${base(id)}/hand/bom`, extra),
|
|
|
|
|
|
|
|
|
|
/* Change orders. */
|
|
|
|
|
coApprove: (id: number, no: number, extra: Guarded = {}) => api.post<PacketRead>(`${base(id)}/change-orders/${no}/approve`, extra),
|
|
|
|
|
coReject: (id: number, no: number, body: { reason: string } & Guarded) =>
|
|
|
|
|
api.post<PacketRead>(`${base(id)}/change-orders/${no}/reject`, body),
|
|
|
|
|
coHold: (id: number, no: number, hold: boolean, extra: Guarded = {}) =>
|
|
|
|
|
api.post<PacketRead>(`${base(id)}/change-orders/${no}/hold`, { hold, ...extra }),
|
2026-10-16 09:31:45 -06:00
|
|
|
};
|