FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

158 lines
4.5 KiB

  1. "use client";
  2. import axiosInstance from "@/app/(main)/axios/axiosInstance";
  3. import { NEXT_PUBLIC_API_URL } from "@/config/api";
  4. import type {
  5. BomFormatCheckResponse,
  6. BomUploadResponse,
  7. ImportBomItemPayload,
  8. BomCombo,
  9. BomDetailResponse,
  10. EditBomRequest,
  11. } from "./index";
  12. export async function uploadBomFiles(
  13. files: File[]
  14. ): Promise<BomUploadResponse> {
  15. const formData = new FormData();
  16. files.forEach((f) => formData.append("files", f, f.name));
  17. const response = await axiosInstance.post<BomUploadResponse>(
  18. `${NEXT_PUBLIC_API_URL}/bom/import-bom/upload`,
  19. formData,
  20. {
  21. transformRequest: [
  22. (data: unknown, headers?: Record<string, unknown>) => {
  23. if (data instanceof FormData && headers && "Content-Type" in headers) {
  24. delete headers["Content-Type"];
  25. }
  26. return data;
  27. },
  28. ],
  29. }
  30. );
  31. return response.data;
  32. }
  33. export async function checkBomFormat(
  34. batchId: string
  35. ): Promise<BomFormatCheckResponse> {
  36. const response = await axiosInstance.post<BomFormatCheckResponse>(
  37. `${NEXT_PUBLIC_API_URL}/bom/import-bom/format-check`,
  38. { batchId }
  39. );
  40. return response.data;
  41. }
  42. export async function downloadBomFormatIssueLog(
  43. batchId: string,
  44. issueLogFileId: string
  45. ): Promise<Blob> {
  46. const response = await axiosInstance.get(
  47. `${NEXT_PUBLIC_API_URL}/bom/import-bom/format-issue-log`,
  48. {
  49. params: { batchId, issueLogFileId },
  50. responseType: "blob",
  51. }
  52. );
  53. return response.data as Blob;
  54. }
  55. export async function importBom(
  56. batchId: string,
  57. items: ImportBomItemPayload[]
  58. ): Promise<Blob> {
  59. const response = await axiosInstance.post(
  60. `${NEXT_PUBLIC_API_URL}/bom/import-bom`,
  61. { batchId, items },
  62. { responseType: "blob" }
  63. );
  64. return response.data as Blob;
  65. }
  66. import type { BomScoreResult } from "./index";
  67. export const fetchBomScoresClient = async (): Promise<BomScoreResult[]> => {
  68. const response = await axiosInstance.get<BomScoreResult[]>(
  69. `${NEXT_PUBLIC_API_URL}/bom/scores`,
  70. );
  71. return response.data;
  72. };
  73. export async function fetchBomComboClient(): Promise<BomCombo[]> {
  74. const response = await axiosInstance.get<BomCombo[]>(
  75. `${NEXT_PUBLIC_API_URL}/bom/combo`
  76. );
  77. return response.data;
  78. }
  79. export async function fetchBomDetailClient(id: number): Promise<BomDetailResponse> {
  80. const response = await axiosInstance.get<BomDetailResponse>(
  81. `${NEXT_PUBLIC_API_URL}/bom/${id}/detail`
  82. );
  83. return response.data;
  84. }
  85. export async function editBomClient(
  86. id: number,
  87. request: EditBomRequest,
  88. ): Promise<BomDetailResponse> {
  89. const response = await axiosInstance.put<BomDetailResponse>(
  90. `${NEXT_PUBLIC_API_URL}/bom/${id}`,
  91. request,
  92. );
  93. return response.data;
  94. }
  95. export type BomExcelCheckProgress = {
  96. batchId: string;
  97. totalFiles: number;
  98. processedFiles: number;
  99. currentFileName: string | null;
  100. lastUpdateTime: number;
  101. };
  102. export async function getBomFormatProgress(
  103. batchId: string
  104. ): Promise<BomExcelCheckProgress> {
  105. const response = await axiosInstance.get<BomExcelCheckProgress>(
  106. `${NEXT_PUBLIC_API_URL}/bom/import-bom/format-check/progress`,
  107. { params: { batchId } }
  108. );
  109. return response.data;
  110. }
  111. /** Master `equipment` rows for BOM process editor (description/name → code). */
  112. export type EquipmentMasterRow = {
  113. code: string;
  114. name: string;
  115. description: string;
  116. };
  117. /** Master `process` rows for BOM process editor (dropdown by code). */
  118. export type ProcessMasterRow = {
  119. code: string;
  120. name: string;
  121. };
  122. export async function fetchAllEquipmentsMasterClient(): Promise<
  123. EquipmentMasterRow[]
  124. > {
  125. const response = await axiosInstance.get<unknown[]>(
  126. `${NEXT_PUBLIC_API_URL}/Equipment`,
  127. );
  128. const rows = Array.isArray(response.data) ? response.data : [];
  129. return rows.map((r: any) => ({
  130. code: String(r?.code ?? "").trim(),
  131. name: String(r?.name ?? "").trim(),
  132. description: String(r?.description ?? "").trim(),
  133. }));
  134. }
  135. export async function fetchAllProcessesMasterClient(): Promise<
  136. ProcessMasterRow[]
  137. > {
  138. const response = await axiosInstance.get<unknown[]>(
  139. `${NEXT_PUBLIC_API_URL}/Process`,
  140. );
  141. const rows = Array.isArray(response.data) ? response.data : [];
  142. return rows.map((r: any) => ({
  143. code: String(r?.code ?? "").trim(),
  144. name: String(r?.name ?? "").trim(),
  145. }));
  146. }