useOperationApi-BiBzgQM3.mjs 5.69 KB
import { z as createSharedComposable, A as useApiGateway } from './server.mjs';

const OPERATION_TASK_STATUS_VALUES = [
  "pending",
  "in_progress",
  "pending_test",
  "failed",
  "rework",
  "completed",
  "skipped"
];
const OPERATION_TASK_ACTION_VALUES = [
  "start",
  "complete_assembly",
  "complete_rework",
  "submit_test",
  "skip"
];
function isRecord(value) {
  return typeof value === "object" && value !== null;
}
function normalizeString(value, fallback = "") {
  return typeof value === "string" ? value : fallback;
}
function normalizeNumber(value, fallback = 0) {
  const num = Number(value);
  return Number.isFinite(num) ? num : fallback;
}
function isStatus(value) {
  return typeof value === "string" && OPERATION_TASK_STATUS_VALUES.includes(value);
}
function isAction(value) {
  return typeof value === "string" && OPERATION_TASK_ACTION_VALUES.includes(value);
}
function isResult(value) {
  return value === "pending" || value === "pass" || value === "fail";
}
function normalizeTasks(payload) {
  if (!Array.isArray(payload)) {
    return [];
  }
  return payload.filter(isRecord).map((item) => {
    const auditEvents = Array.isArray(item.auditEvents) ? item.auditEvents : [];
    return {
      id: normalizeNumber(item.id),
      workOrderNo: normalizeString(item.workOrderNo),
      sn: normalizeString(item.sn),
      stepName: normalizeString(item.stepName),
      workstation: normalizeString(item.workstation),
      device: normalizeString(item.device),
      operator: normalizeString(item.operator),
      startedAt: normalizeString(item.startedAt, void 0),
      endedAt: normalizeString(item.endedAt, void 0),
      result: isResult(item.result) ? item.result : "pending",
      status: isStatus(item.status) ? item.status : "pending",
      nextAction: normalizeString(item.nextAction),
      auditEvents: auditEvents.filter(isRecord).map((event) => ({
        id: normalizeString(event.id),
        action: isAction(event.action) ? event.action : "start",
        fromStatus: isStatus(event.fromStatus) ? event.fromStatus : null,
        toStatus: isStatus(event.toStatus) ? event.toStatus : "pending",
        operator: normalizeString(event.operator),
        at: normalizeString(event.at),
        remark: normalizeString(event.remark, void 0),
        evidencePath: normalizeString(event.evidencePath, void 0),
        nextAction: normalizeString(event.nextAction)
      }))
    };
  }).filter((item) => item.id > 0 && item.sn.length > 0);
}
function normalizeMutation(payload, fallbackMessage) {
  if (!isRecord(payload)) {
    return {
      success: false,
      errorCode: "REQUEST_FAILED",
      message: fallbackMessage
    };
  }
  return {
    success: Boolean(payload.success),
    errorCode: typeof payload.errorCode === "string" ? payload.errorCode : null,
    message: typeof payload.message === "string" && payload.message.length > 0 ? payload.message : fallbackMessage,
    state: isStatus(payload.state) ? payload.state : void 0,
    nextAction: typeof payload.nextAction === "string" ? payload.nextAction : void 0
  };
}
function normalizeUpload(payload, fallbackMessage) {
  if (!isRecord(payload)) {
    return {
      success: false,
      errorCode: "REQUEST_FAILED",
      message: fallbackMessage
    };
  }
  return {
    success: Boolean(payload.success),
    errorCode: typeof payload.errorCode === "string" ? payload.errorCode : null,
    message: typeof payload.message === "string" && payload.message.length > 0 ? payload.message : fallbackMessage,
    relativePath: typeof payload.relativePath === "string" && payload.relativePath.length > 0 ? payload.relativePath : void 0
  };
}
function buildQuery(query) {
  const params = new URLSearchParams();
  if (query.workstation) {
    params.set("workstation", query.workstation);
  }
  if (query.stepName) {
    params.set("stepName", query.stepName);
  }
  if (query.operator) {
    params.set("operator", query.operator);
  }
  if (query.status) {
    params.set("status", query.status);
  }
  if (query.sn) {
    params.set("sn", query.sn);
  }
  const search = params.toString();
  return search.length > 0 ? `?${search}` : "";
}
const _useOperationApi = () => {
  const api = useApiGateway();
  const getOperationTasks = async (query) => {
    const result = await api.request(`/api/operations${buildQuery(query)}`, {
      method: "GET"
    });
    return normalizeTasks(result);
  };
  const runOperationAction = async (id, action, payload) => {
    if (!isAction(action)) {
      return {
        success: false,
        errorCode: "VALIDATION_ERROR",
        message: "工序任务动作不支持。"
      };
    }
    const actionPath = `/api/operations/${id}/actions/${action}`;
    try {
      const result = await api.request(actionPath, {
        method: "POST",
        body: payload,
        remotePath: actionPath
      });
      return normalizeMutation(result, "工序任务状态更新失败。");
    } catch {
      return {
        success: false,
        errorCode: "REQUEST_FAILED",
        message: "工序任务状态更新失败。"
      };
    }
  };
  const uploadOperationEvidence = async (file) => {
    const formData = new FormData();
    formData.append("file", file);
    try {
      const result = await api.request("/api/files/operation-evidence", {
        method: "POST",
        body: formData
      });
      return normalizeUpload(result, "附件上传失败。");
    } catch {
      return {
        success: false,
        errorCode: "REQUEST_FAILED",
        message: "附件上传失败。"
      };
    }
  };
  return {
    getOperationTasks,
    runOperationAction,
    uploadOperationEvidence
  };
};
const useOperationApi = createSharedComposable(_useOperationApi);

export { useOperationApi as u };
//# sourceMappingURL=useOperationApi-BiBzgQM3.mjs.map