mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-04 20:42:05 +00:00
Dispatchers need the design surface from the column and the detail, and an optional evidence file is sent only after the scan passes.
101 lines
3.2 KiB
TypeScript
101 lines
3.2 KiB
TypeScript
import { API_PATHS } from "@/api/api-paths";
|
|
import { apiGet, apiPost, apiPostForm } from "@/api/api";
|
|
import { handleApiResponse } from "@/api/handle-api-response";
|
|
import {
|
|
mapWorkOrderUplift,
|
|
mapWorkOrderUplifts,
|
|
} from "@/domain/work-orders/mappers/work-order-uplift-mapper";
|
|
import type {
|
|
CreateWorkOrderUpliftInput,
|
|
WorkOrderUplift,
|
|
} from "@/domain/work-orders/types/work-order-uplift";
|
|
|
|
function extractUpliftItems(data: unknown): unknown[] {
|
|
if (Array.isArray(data)) {
|
|
return data;
|
|
}
|
|
const record = typeof data === "object" && data != null ? (data as Record<string, unknown>) : {};
|
|
const items = record.items ?? record.Items ?? record.uplifts ?? record.Uplifts;
|
|
return Array.isArray(items) ? items : [];
|
|
}
|
|
|
|
export type UpliftEvidenceSnapshot = {
|
|
id: number;
|
|
scanStatus: string;
|
|
originalFileName: string;
|
|
};
|
|
|
|
function readEvidenceSnapshot(data: unknown): UpliftEvidenceSnapshot {
|
|
const record = handleApiResponse<Record<string, unknown>>(data);
|
|
const id = Number(record.id ?? record.Id);
|
|
return {
|
|
id,
|
|
scanStatus: String(record.scanStatus ?? record.ScanStatus ?? ""),
|
|
originalFileName: String(record.originalFileName ?? record.OriginalFileName ?? ""),
|
|
};
|
|
}
|
|
|
|
export const workOrderUpliftsApi = {
|
|
list: async (workOrderId: string | number): Promise<WorkOrderUplift[]> => {
|
|
const data = await apiGet<unknown>(API_PATHS.workOrder.uplifts(workOrderId));
|
|
const unwrapped = handleApiResponse<unknown>(data);
|
|
return mapWorkOrderUplifts(extractUpliftItems(unwrapped));
|
|
},
|
|
|
|
create: async (
|
|
workOrderId: string | number,
|
|
input: CreateWorkOrderUpliftInput,
|
|
): Promise<WorkOrderUplift> => {
|
|
const body: { amount: number; notes: string; evidenceDocumentId?: number } = {
|
|
amount: input.amount,
|
|
notes: input.notes,
|
|
};
|
|
if (input.evidenceDocumentId != null) {
|
|
body.evidenceDocumentId = input.evidenceDocumentId;
|
|
}
|
|
const data = await apiPost<unknown>(API_PATHS.workOrder.uplifts(workOrderId), body);
|
|
return mapWorkOrderUplift(handleApiResponse(data));
|
|
},
|
|
|
|
uploadEvidence: async (
|
|
workOrderId: string | number,
|
|
file: File,
|
|
): Promise<UpliftEvidenceSnapshot> => {
|
|
const body = new FormData();
|
|
body.append("file", file);
|
|
const data = await apiPostForm<unknown>(API_PATHS.workOrder.upliftEvidence(workOrderId), body);
|
|
return readEvidenceSnapshot(data);
|
|
},
|
|
|
|
getEvidenceStatus: async (
|
|
workOrderId: string | number,
|
|
documentId: number,
|
|
): Promise<UpliftEvidenceSnapshot> => {
|
|
const data = await apiGet<unknown>(
|
|
API_PATHS.workOrder.upliftEvidenceStatus(workOrderId, documentId),
|
|
);
|
|
return readEvidenceSnapshot(data);
|
|
},
|
|
|
|
cancel: async (
|
|
workOrderId: string | number,
|
|
upliftId: string | number,
|
|
): Promise<WorkOrderUplift> => {
|
|
const data = await apiPost<unknown>(
|
|
API_PATHS.workOrder.upliftCancel(workOrderId, upliftId),
|
|
{},
|
|
);
|
|
return mapWorkOrderUplift(handleApiResponse(data));
|
|
},
|
|
|
|
revoke: async (
|
|
workOrderId: string | number,
|
|
upliftId: string | number,
|
|
reason?: string,
|
|
): Promise<WorkOrderUplift> => {
|
|
const data = await apiPost<unknown>(API_PATHS.workOrder.upliftRevoke(workOrderId, upliftId), {
|
|
reason,
|
|
});
|
|
return mapWorkOrderUplift(handleApiResponse(data));
|
|
},
|
|
};
|