2026-03-30 07:53:42 -05:00
|
|
|
import { workOrderApi } from "../../services/api";
|
2026-04-16 18:22:34 -04:00
|
|
|
import { post } from "../../apiUtil";
|
|
|
|
|
import { API_URL } from "../../constants";
|
2026-01-20 15:05:00 -06:00
|
|
|
|
2026-03-30 07:53:42 -05:00
|
|
|
export const getWorkOrders = async (params = {}) => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await workOrderApi.getAll(params);
|
|
|
|
|
const workOrders = response.data || response;
|
|
|
|
|
return Array.isArray(workOrders) ? workOrders : [];
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching work orders:", error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2026-01-20 15:05:00 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getWorkOrderById = async (id) => {
|
2026-03-30 07:53:42 -05:00
|
|
|
try {
|
2026-04-16 17:37:21 -04:00
|
|
|
const wo = await workOrderApi.getById(id);
|
|
|
|
|
return {
|
|
|
|
|
id: wo.id,
|
2026-04-16 18:42:58 -04:00
|
|
|
internalWONumber: wo.internalWONumber ?? "",
|
2026-04-16 17:37:21 -04:00
|
|
|
workOrderNumber: wo.workerOrderNumber ?? wo.workOrderNumber ?? "",
|
|
|
|
|
title: wo.title ?? wo.workerOrderTitle ?? "",
|
|
|
|
|
location: wo.location ?? "",
|
2026-04-16 17:45:19 -04:00
|
|
|
locationId: wo.locationId != null ? String(wo.locationId) : "",
|
2026-04-16 17:37:21 -04:00
|
|
|
assignedTo: wo.assignee ?? wo.assignedTo ?? "",
|
|
|
|
|
dueDate: wo.dueDate ? wo.dueDate.split("T")[0] : "",
|
|
|
|
|
createdDate: wo.createdDate ? wo.createdDate.split("T")[0] : "",
|
2026-04-16 17:54:21 -04:00
|
|
|
createdDateFull: wo.createdDate ?? "",
|
2026-04-16 17:37:21 -04:00
|
|
|
description: wo.description ?? "",
|
|
|
|
|
priority: wo.priority ?? "",
|
|
|
|
|
status: wo.status ?? "Open",
|
|
|
|
|
po: wo.po ?? "",
|
|
|
|
|
tt: wo.tt ?? "",
|
2026-04-17 10:40:55 -04:00
|
|
|
problem: wo.problem ?? "",
|
|
|
|
|
trade: wo.trade ?? "",
|
|
|
|
|
subTrade: wo.subTrade ?? "",
|
|
|
|
|
vendorNTE: wo.vendorNTE != null ? String(wo.vendorNTE) : "",
|
|
|
|
|
scheduledDate: wo.scheduledDate ? wo.scheduledDate.split("T")[0] : "",
|
|
|
|
|
completedDate: wo.completedDate ? wo.completedDate.split("T")[0] : "",
|
|
|
|
|
source: wo.source ?? "",
|
2026-04-16 17:41:11 -04:00
|
|
|
comments: wo.comments ?? [],
|
2026-04-16 18:09:57 -04:00
|
|
|
auditLog: wo.auditLog ?? [],
|
2026-04-16 17:37:21 -04:00
|
|
|
};
|
2026-03-30 07:53:42 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching work order:", error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2026-01-20 15:05:00 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const addWorkOrder = async (workOrder) => {
|
2026-03-30 07:53:42 -05:00
|
|
|
try {
|
|
|
|
|
return await workOrderApi.create(workOrder);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error creating work order:", error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2026-01-20 15:05:00 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const editWorkOrder = async (workOrder) => {
|
2026-03-30 07:53:42 -05:00
|
|
|
try {
|
2026-04-16 18:12:44 -04:00
|
|
|
return await workOrderApi.update(workOrder.id, {
|
|
|
|
|
Id: workOrder.id,
|
|
|
|
|
WorkerOrderNumber: workOrder.workOrderNumber,
|
|
|
|
|
WorkerOrderTitle: workOrder.title,
|
|
|
|
|
Description: workOrder.description,
|
|
|
|
|
PO: workOrder.po,
|
|
|
|
|
TT: workOrder.tt,
|
|
|
|
|
AssignTo: workOrder.assignedTo || null,
|
|
|
|
|
DueDate: workOrder.dueDate || null,
|
|
|
|
|
Priority: workOrder.priority || null,
|
|
|
|
|
Status: workOrder.status,
|
|
|
|
|
LocationId: workOrder.locationId ? parseInt(workOrder.locationId) : null,
|
2026-04-17 10:40:55 -04:00
|
|
|
Problem: workOrder.problem || null,
|
|
|
|
|
Trade: workOrder.trade || null,
|
|
|
|
|
SubTrade: workOrder.subTrade || null,
|
|
|
|
|
VendorNTE: workOrder.vendorNTE ? parseFloat(workOrder.vendorNTE) : null,
|
|
|
|
|
ScheduledDate: workOrder.scheduledDate || null,
|
|
|
|
|
CompletedDate: workOrder.completedDate || null,
|
|
|
|
|
Source: workOrder.source || null,
|
2026-04-16 18:12:44 -04:00
|
|
|
});
|
2026-03-30 07:53:42 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error updating work order:", error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2026-01-20 15:05:00 -06:00
|
|
|
};
|
|
|
|
|
|
2026-04-16 18:22:34 -04:00
|
|
|
export const addComment = async (workOrderId, text, commentType = "internal") => {
|
|
|
|
|
return post(`${API_URL}/WorkOrder/AddCommentJson`, {
|
|
|
|
|
WorkorderId: workOrderId,
|
|
|
|
|
Text: text,
|
|
|
|
|
CommentType: commentType,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-16 19:04:59 -04:00
|
|
|
export const changeWorkOrderAssignment = async (id, assignTo) => {
|
|
|
|
|
return post(`${API_URL}/WorkOrder/ChangeAssignment?id=${id}&assignTo=${encodeURIComponent(assignTo || "")}`);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const fetchUsers = async () => {
|
|
|
|
|
const { get: apiGet } = await import("../../apiUtil");
|
|
|
|
|
const data = await apiGet(`${API_URL}/User`);
|
|
|
|
|
return (data || []).map((u) => ({ id: u.id ?? u.Id, name: u.name ?? u.Name ?? u.userName ?? "" }));
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-17 10:40:55 -04:00
|
|
|
export const fetchDropdownOptions = async (category, parent) => {
|
|
|
|
|
const { get: apiGet } = await import("../../apiUtil");
|
|
|
|
|
let url = `${API_URL}/DropdownOptions/ByCategory/${encodeURIComponent(category)}`;
|
|
|
|
|
if (parent) url += `?parent=${encodeURIComponent(parent)}`;
|
|
|
|
|
return (await apiGet(url)) || [];
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-16 18:27:00 -04:00
|
|
|
export const changeWorkOrderStatus = async (id, status) => {
|
|
|
|
|
return post(`${API_URL}/WorkOrder/ChangeStatus?id=${id}&status=${encodeURIComponent(status)}`);
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-20 15:05:00 -06:00
|
|
|
export const deleteWorkOrder = async (id) => {
|
2026-03-30 07:53:42 -05:00
|
|
|
try {
|
|
|
|
|
await workOrderApi.delete(id);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error deleting work order:", error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2026-01-20 15:05:00 -06:00
|
|
|
};
|