FPSMS-frontend
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

257 líneas
7.3 KiB

  1. "use server";
  2. // import { serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
  3. // import { BASE_API_URL } from "@/config/api";
  4. import {
  5. serverFetchJson,
  6. serverFetchWithNoContent,
  7. } from "../../utils/fetchUtil";
  8. import { BASE_API_URL } from "../../../config/api";
  9. import { revalidateTag } from "next/cache";
  10. import { cache } from "react";
  11. export interface ShopAndTruck{
  12. id: number;
  13. name: String;
  14. code: String;
  15. addr1: String;
  16. addr2: String;
  17. addr3: String;
  18. contactNo: number;
  19. type: String;
  20. contactEmail: String;
  21. contactName: String;
  22. truckLanceCode: String;
  23. DepartureTime: String;
  24. LoadingSequence?: number | null;
  25. districtReference: Number;
  26. Store_id: Number;
  27. remark?: String | null;
  28. truckId?: number;
  29. }
  30. export interface Shop{
  31. id: number;
  32. name: String;
  33. code: String;
  34. addr3: String;
  35. }
  36. export interface Truck{
  37. id?: number;
  38. truckLanceCode: String;
  39. departureTime: String | number[];
  40. loadingSequence: number;
  41. districtReference: Number;
  42. storeId: Number | String;
  43. remark?: String | null;
  44. shopName?: String | null;
  45. shopCode?: String | null;
  46. }
  47. export interface SaveTruckLane {
  48. id: number;
  49. truckLanceCode: string;
  50. departureTime: string;
  51. loadingSequence: number;
  52. districtReference: number;
  53. storeId: string;
  54. remark?: string | null;
  55. }
  56. export interface DeleteTruckLane {
  57. id: number;
  58. }
  59. export interface UpdateTruckShopDetailsRequest {
  60. id: number;
  61. shopId?: number | null;
  62. shopName: string | null;
  63. shopCode: string | null;
  64. loadingSequence: number;
  65. remark?: string | null;
  66. }
  67. export interface SaveTruckRequest {
  68. id?: number | null;
  69. store_id: string;
  70. truckLanceCode: string;
  71. departureTime: string;
  72. shopId: number;
  73. shopName: string;
  74. shopCode: string;
  75. loadingSequence: number;
  76. districtReference?: number | null;
  77. remark?: string | null;
  78. }
  79. export interface CreateTruckWithoutShopRequest {
  80. store_id: string;
  81. truckLanceCode: string;
  82. departureTime: string;
  83. loadingSequence?: number;
  84. districtReference?: number | null;
  85. remark?: string | null;
  86. }
  87. export interface MessageResponse {
  88. id: number | null;
  89. name: string | null;
  90. code: string | null;
  91. type: string;
  92. message: string;
  93. errorPosition: string | null;
  94. entity: Truck | null;
  95. }
  96. export const fetchAllShopsAction = cache(async (params?: Record<string, string | number | null>) => {
  97. const endpoint = `${BASE_API_URL}/shop/combo/allShop`;
  98. const qs = params
  99. ? Object.entries(params)
  100. .filter(([, v]) => v !== null && v !== undefined && String(v).trim() !== "")
  101. .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
  102. .join("&")
  103. : "";
  104. const url = qs ? `${endpoint}?${qs}` : endpoint;
  105. return serverFetchJson<ShopAndTruck[]>(url, {
  106. method: "GET",
  107. headers: { "Content-Type": "application/json" },
  108. });
  109. });
  110. export const findTruckLaneByShopIdAction = cache(async (shopId: number | string) => {
  111. const endpoint = `${BASE_API_URL}/truck/findTruckLane/${shopId}`;
  112. return serverFetchJson<Truck[]>(endpoint, {
  113. method: "GET",
  114. headers: { "Content-Type": "application/json" },
  115. });
  116. });
  117. export const updateTruckLaneAction = async (data: SaveTruckLane) => {
  118. const endpoint = `${BASE_API_URL}/truck/updateTruckLane`;
  119. return serverFetchJson<MessageResponse>(endpoint, {
  120. method: "POST",
  121. body: JSON.stringify(data),
  122. headers: { "Content-Type": "application/json" },
  123. });
  124. };
  125. export const deleteTruckLaneAction = async (data: DeleteTruckLane) => {
  126. const endpoint = `${BASE_API_URL}/truck/deleteTruckLane`;
  127. return serverFetchJson<MessageResponse>(endpoint, {
  128. method: "POST",
  129. body: JSON.stringify(data),
  130. headers: { "Content-Type": "application/json" },
  131. });
  132. };
  133. export const createTruckAction = async (data: SaveTruckRequest) => {
  134. const endpoint = `${BASE_API_URL}/truck/createTruckInShop`;
  135. return serverFetchJson<MessageResponse>(endpoint, {
  136. method: "POST",
  137. body: JSON.stringify(data),
  138. headers: { "Content-Type": "application/json" },
  139. });
  140. };
  141. export const findAllUniqueTruckLaneCombinationsAction = cache(async () => {
  142. const endpoint = `${BASE_API_URL}/truck/findAllUniqueTruckLanceCodeAndRemarkCombinations`;
  143. return serverFetchJson<Truck[]>(endpoint, {
  144. method: "GET",
  145. headers: { "Content-Type": "application/json" },
  146. });
  147. });
  148. export const findAllShopsByTruckLanceCodeAndRemarkAction = cache(async (truckLanceCode: string, remark: string) => {
  149. const endpoint = `${BASE_API_URL}/truck/findAllFromShopAndTruckByTruckLanceCodeAndRemarkAndDeletedFalse`;
  150. const url = `${endpoint}?truckLanceCode=${encodeURIComponent(truckLanceCode)}&remark=${encodeURIComponent(remark)}`;
  151. return serverFetchJson<ShopAndTruck[]>(url, {
  152. method: "GET",
  153. headers: { "Content-Type": "application/json" },
  154. });
  155. });
  156. export const findAllShopsByTruckLanceCodeAction = cache(async (truckLanceCode: string) => {
  157. const endpoint = `${BASE_API_URL}/truck/findAllFromShopAndTruckByTruckLanceCodeAndDeletedFalse`;
  158. const url = `${endpoint}?truckLanceCode=${encodeURIComponent(truckLanceCode)}`;
  159. return serverFetchJson<ShopAndTruck[]>(url, {
  160. method: "GET",
  161. headers: { "Content-Type": "application/json" },
  162. });
  163. });
  164. export const findAllByTruckLanceCodeAndDeletedFalseAction = cache(async (truckLanceCode: string) => {
  165. const endpoint = `${BASE_API_URL}/truck/findAllByTruckLanceCodeAndDeletedFalse`;
  166. const url = `${endpoint}?truckLanceCode=${encodeURIComponent(truckLanceCode)}`;
  167. return serverFetchJson<Truck[]>(url, {
  168. method: "GET",
  169. headers: { "Content-Type": "application/json" },
  170. });
  171. });
  172. export const updateTruckShopDetailsAction = async (data: UpdateTruckShopDetailsRequest) => {
  173. const endpoint = `${BASE_API_URL}/truck/updateTruckShopDetails`;
  174. return serverFetchJson<MessageResponse>(endpoint, {
  175. method: "POST",
  176. body: JSON.stringify(data),
  177. headers: { "Content-Type": "application/json" },
  178. });
  179. };
  180. export const createTruckWithoutShopAction = async (data: CreateTruckWithoutShopRequest) => {
  181. const endpoint = `${BASE_API_URL}/truck/createTruckWithoutShop`;
  182. return serverFetchJson<MessageResponse>(endpoint, {
  183. method: "POST",
  184. body: JSON.stringify(data),
  185. headers: { "Content-Type": "application/json" },
  186. });
  187. };
  188. export const findAllUniqueShopNamesAndCodesFromTrucksAction = cache(async () => {
  189. const endpoint = `${BASE_API_URL}/truck/findAllUniqueShopNamesAndCodesFromTrucks`;
  190. return serverFetchJson<Array<{ name: string; code: string }>>(endpoint, {
  191. method: "GET",
  192. headers: { "Content-Type": "application/json" },
  193. });
  194. });
  195. export const findAllUniqueRemarksFromTrucksAction = cache(async () => {
  196. const endpoint = `${BASE_API_URL}/truck/findAllUniqueRemarksFromTrucks`;
  197. return serverFetchJson<string[]>(endpoint, {
  198. method: "GET",
  199. headers: { "Content-Type": "application/json" },
  200. });
  201. });
  202. export const findAllUniqueShopCodesFromTrucksAction = cache(async () => {
  203. const endpoint = `${BASE_API_URL}/truck/findAllUniqueShopCodesFromTrucks`;
  204. return serverFetchJson<string[]>(endpoint, {
  205. method: "GET",
  206. headers: { "Content-Type": "application/json" },
  207. });
  208. });
  209. export const findAllUniqueShopNamesFromTrucksAction = cache(async () => {
  210. const endpoint = `${BASE_API_URL}/truck/findAllUniqueShopNamesFromTrucks`;
  211. return serverFetchJson<string[]>(endpoint, {
  212. method: "GET",
  213. headers: { "Content-Type": "application/json" },
  214. });
  215. });