2026-08-10 17:12:10 -03:00
|
|
|
import { API_PATHS } from "@/api/api-paths";
|
2026-09-24 14:30:24 -03:00
|
|
|
import { apiGet, apiPost, apiPostForm } from "@/api/api";
|
2026-08-10 17:12:10 -03:00
|
|
|
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 : [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 14:30:24 -03:00
|
|
|
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 ?? ""),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-10 17:12:10 -03:00
|
|
|
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> => {
|
2026-09-24 14:30:24 -03:00
|
|
|
const body: { amount: number; notes: string; evidenceDocumentId?: number } = {
|
2026-08-10 17:12:10 -03:00
|
|
|
amount: input.amount,
|
|
|
|
|
notes: input.notes,
|
2026-09-24 14:30:24 -03:00
|
|
|
};
|
|
|
|
|
if (input.evidenceDocumentId != null) {
|
|
|
|
|
body.evidenceDocumentId = input.evidenceDocumentId;
|
|
|
|
|
}
|
|
|
|
|
const data = await apiPost<unknown>(API_PATHS.workOrder.uplifts(workOrderId), body);
|
2026-08-10 17:12:10 -03:00
|
|
|
return mapWorkOrderUplift(handleApiResponse(data));
|
|
|
|
|
},
|
|
|
|
|
|
2026-09-24 14:30:24 -03:00
|
|
|
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);
|
|
|
|
|
},
|
|
|
|
|
|
2026-08-10 17:12:10 -03:00
|
|
|
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));
|
|
|
|
|
},
|
|
|
|
|
};
|