FPSMS-frontend
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

143 linhas
4.6 KiB

  1. "use server";
  2. import { serverFetchString, serverFetchWithNoContent, serverFetchJson } from "@/app/utils/fetchUtil";
  3. import { BASE_API_URL } from "@/config/api";
  4. import { revalidateTag } from "next/cache";
  5. import { WarehouseResult, StockTakeSectionInfo } from "./index";
  6. import { cache } from "react";
  7. export interface WarehouseInputs {
  8. code?: string;
  9. name?: string;
  10. description?: string;
  11. capacity?: number;
  12. store_id?: string;
  13. warehouse?: string;
  14. area?: string;
  15. slot?: string;
  16. order?: string;
  17. stockTakeSection?: string;
  18. stockTakeSectionDescription?: string;
  19. }
  20. export const fetchWarehouseDetail = cache(async (id: number) => {
  21. return serverFetchJson<WarehouseResult>(`${BASE_API_URL}/warehouse/${id}`, {
  22. next: { tags: ["warehouse"] },
  23. });
  24. });
  25. export const createWarehouse = async (data: WarehouseInputs) => {
  26. const newWarehouse = await serverFetchWithNoContent(`${BASE_API_URL}/warehouse/save`, {
  27. method: "POST",
  28. body: JSON.stringify(data),
  29. headers: { "Content-Type": "application/json" },
  30. });
  31. revalidateTag("warehouse");
  32. return newWarehouse;
  33. };
  34. export const editWarehouse = async (id: number, data: WarehouseInputs) => {
  35. // Backend uses the same /warehouse/save POST endpoint for both create and update,
  36. // distinguished by presence of id in the payload.
  37. const updatedWarehouse = await serverFetchWithNoContent(`${BASE_API_URL}/warehouse/save`, {
  38. method: "POST",
  39. body: JSON.stringify({ id, ...data }),
  40. headers: { "Content-Type": "application/json" },
  41. });
  42. revalidateTag("warehouse");
  43. return updatedWarehouse;
  44. };
  45. export const deleteWarehouse = async (id: number) => {
  46. try {
  47. const result = await serverFetchJson<WarehouseResult[]>(`${BASE_API_URL}/warehouse/${id}`, {
  48. method: "DELETE",
  49. headers: { "Content-Type": "application/json" },
  50. });
  51. revalidateTag("warehouse");
  52. return result;
  53. } catch (error) {
  54. console.error("Error deleting warehouse:", error);
  55. revalidateTag("warehouse");
  56. throw error;
  57. }
  58. };
  59. export const importWarehouse = async (data: FormData) => {
  60. const importWarehouse = await serverFetchString<string>(
  61. `${BASE_API_URL}/warehouse/import`,
  62. {
  63. method: "POST",
  64. body: data,
  65. },
  66. );
  67. return importWarehouse;
  68. }
  69. export const importNewWarehouse = async (data: FormData) => {
  70. const importWarehouse = await serverFetchString<string>(
  71. `${BASE_API_URL}/warehouse/importNew`,
  72. {
  73. method: "POST",
  74. body: data,
  75. },
  76. );
  77. return importWarehouse;
  78. }
  79. export const fetchStockTakeSections = cache(async () => {
  80. return serverFetchJson<StockTakeSectionInfo[]>(`${BASE_API_URL}/warehouse/stockTakeSections`, {
  81. next: { tags: ["warehouse"] },
  82. });
  83. });
  84. export const updateSectionDescription = async (section: string, stockTakeSectionDescription: string | null) => {
  85. await serverFetchWithNoContent(
  86. `${BASE_API_URL}/warehouse/section/${encodeURIComponent(section)}/description`,
  87. {
  88. method: "PATCH",
  89. headers: { "Content-Type": "application/json" },
  90. body: JSON.stringify({ stockTakeSectionDescription }),
  91. }
  92. );
  93. revalidateTag("warehouse");
  94. };
  95. export const clearWarehouseSection = async (warehouseId: number) => {
  96. const result = await serverFetchJson<WarehouseResult>(
  97. `${BASE_API_URL}/warehouse/${warehouseId}/clearSection`,
  98. { method: "POST" }
  99. );
  100. revalidateTag("warehouse");
  101. return result;
  102. };
  103. export const getWarehousesBySection = cache(async (stockTakeSection: string) => {
  104. const list = await serverFetchJson<WarehouseResult[]>(`${BASE_API_URL}/warehouse`, {
  105. next: { tags: ["warehouse"] },
  106. });
  107. const items = Array.isArray(list) ? list : [];
  108. return items.filter((w) => w.stockTakeSection === stockTakeSection);
  109. });
  110. export const searchWarehousesForAddToSection = cache(async (
  111. params: { store_id?: string; warehouse?: string; area?: string; slot?: string },
  112. currentSection: string
  113. ) => {
  114. const list = await serverFetchJson<WarehouseResult[]>(`${BASE_API_URL}/warehouse`, {
  115. next: { tags: ["warehouse"] },
  116. });
  117. const items = Array.isArray(list) ? list : [];
  118. const storeId = params.store_id?.trim();
  119. const warehouse = params.warehouse?.trim();
  120. const area = params.area?.trim();
  121. const slot = params.slot?.trim();
  122. return items.filter((w) => {
  123. if (w.stockTakeSection != null && w.stockTakeSection !== currentSection) return false;
  124. if (!w.code) return true;
  125. const parts = w.code.split("-");
  126. if (storeId && parts[0] !== storeId) return false;
  127. if (warehouse && parts[1] !== warehouse) return false;
  128. if (area && parts[2] !== area) return false;
  129. if (slot && parts[3] !== slot) return false;
  130. return true;
  131. });
  132. });