mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-05 10:42:04 +00:00
62 lines
2 KiB
TypeScript
62 lines
2 KiB
TypeScript
|
|
import { API_PATHS } from "@/api/api-paths";
|
||
|
|
import { apiGet, apiPost } 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 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 data = await apiPost<unknown>(API_PATHS.workOrder.uplifts(workOrderId), {
|
||
|
|
amount: input.amount,
|
||
|
|
notes: input.notes,
|
||
|
|
});
|
||
|
|
return mapWorkOrderUplift(handleApiResponse(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));
|
||
|
|
},
|
||
|
|
};
|