Files
PacketStation/frontend/src/api/packets.ts
T

90 lines
5.2 KiB
TypeScript
Raw Normal View History

// Packet endpoints from the phase 2 contract.
import { api, request } from "./client";
import { asList, ifMatch, qs } from "./query";
import type {
AttachPagesBody,
BomLine,
BomPatch,
FieldCas,
Guarded,
LogRow,
PacketCard,
PacketCreate,
PacketHeader,
PacketListQuery,
PacketRead,
ReceiptBody,
ShortBody,
TransitionBody,
WhStageBody,
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`, {}),
/* 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 }),
/* Phase 4: the warehouse. */
/** One BOM line. If-Match is the line's row_version. */
patchBomLine: (id: number, item: number, rowVersion: number, body: BomPatch, g: Guarded = {}) =>
request<PacketRead>(`${base(id)}/bom/${item}`, { method: "PATCH", body: { ...body, ...g }, headers: ifMatch(rowVersion) }),
/** Opens the short on a line, or updates the open one. expect_open says which the caller saw. */
saveShort: (id: number, item: number, body: ShortBody, g: Guarded = {}) =>
api.post<PacketRead>(`${base(id)}/bom/${item}/short`, { ...body, ...g }),
/** "Not short after all": closes the open short. */
clearShort: (id: number, item: number, body: { expect_open: boolean }, g: Guarded = {}) =>
api.delWithBody<PacketRead>(`${base(id)}/bom/${item}/short`, { ...body, ...g }),
receiveShort: (id: number, item: number, body: ReceiptBody, g: Guarded = {}) =>
api.post<PacketRead>(`${base(id)}/bom/${item}/short/receipts`, { ...body, ...g }),
setWhStage: (id: number, body: WhStageBody, g: Guarded = {}) => api.post<PacketRead>(`${base(id)}/wh`, { ...body, ...g }),
};