From 30eb8517d1f421a91cff2d7b566bb2760332bc96 Mon Sep 17 00:00:00 2001 From: "vluk@2fi-solutions.com.hk" Date: Sun, 18 Jan 2026 19:26:41 +0800 Subject: [PATCH] refining the data syn --- .../api/settings/m18ImportTesting/actions.ts | 52 ++++++++++++- .../M18ImportTesting/M18ImportTesting.tsx | 75 ++++++++++++++++++- 2 files changed, 124 insertions(+), 3 deletions(-) diff --git a/src/app/api/settings/m18ImportTesting/actions.ts b/src/app/api/settings/m18ImportTesting/actions.ts index 6af117b..0797b59 100644 --- a/src/app/api/settings/m18ImportTesting/actions.ts +++ b/src/app/api/settings/m18ImportTesting/actions.ts @@ -53,19 +53,67 @@ export const testM18ImportDo = async (data: M18ImportDoForm) => { }; export const testM18ImportPq = async (data: M18ImportPqForm) => { + const token = localStorage.getItem("accessToken"); + return serverFetchWithNoContent(`${BASE_API_URL}/m18/pq`, { method: "POST", body: JSON.stringify(data), - headers: { "Content-Type": "application/json" }, + headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}`, }, + }); }; export const testM18ImportMasterData = async ( data: M18ImportMasterDataForm, ) => { + const token = localStorage.getItem("accessToken"); return serverFetchWithNoContent(`${BASE_API_URL}/m18/master-data`, { method: "POST", body: JSON.stringify(data), - headers: { "Content-Type": "application/json" }, + headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}`, }, }); }; + +export const triggerScheduler = async (type: 'po' | 'do1' | 'do2' | 'master-data' | 'refresh-cron') => { + try { + // IMPORTANT: 'refresh-cron' is a direct endpoint /api/scheduler/refresh-cron + // Others are /api/scheduler/trigger/{type} + const path = type === 'refresh-cron' + ? 'refresh-cron' + : `trigger/${type}`; + + const url = `${BASE_API_URL}/scheduler/${path}`; + + console.log("Fetching URL:", url); + + const response = await serverFetchWithNoContent(url, { + method: "GET", + cache: "no-store", + }); + + if (!response.ok) throw new Error(`Failed: ${response.status}`); + + return await response.text(); + } catch (error) { + console.error("Scheduler Action Error:", error); + return null; + } +}; + +export const refreshCronSchedules = async () => { + // Simply reuse the triggerScheduler logic to avoid duplication + // or call serverFetch directly as shown below: + try { + const response = await serverFetchWithNoContent(`${BASE_API_URL}/scheduler/refresh-cron`, { + method: "GET", + cache: "no-store", + }); + + if (!response.ok) throw new Error(`Failed to refresh: ${response.status}`); + + return await response.text(); + } catch (error) { + console.error("Refresh Cron Error:", error); + return "Refresh failed. Check server logs."; + } +}; \ No newline at end of file diff --git a/src/components/M18ImportTesting/M18ImportTesting.tsx b/src/components/M18ImportTesting/M18ImportTesting.tsx index 0b46d54..f2ddaad 100644 --- a/src/components/M18ImportTesting/M18ImportTesting.tsx +++ b/src/components/M18ImportTesting/M18ImportTesting.tsx @@ -8,7 +8,7 @@ import { testM18ImportMasterData, testM18ImportDo, } from "@/app/api/settings/m18ImportTesting/actions"; -import { Card, CardContent, Grid, Stack, Typography } from "@mui/material"; +import { Card, CardContent, Grid, Stack, Typography, Button } from "@mui/material"; import React, { BaseSyntheticEvent, FormEvent, @@ -22,6 +22,8 @@ import M18ImportPq from "./M18ImportPq"; import { dateTimeStringToDayjs } from "@/app/utils/formatUtil"; import M18ImportMasterData from "./M18ImportMasterData"; import M18ImportDo from "./M18ImportDo"; +import { PlayArrow, Refresh as RefreshIcon } from "@mui/icons-material"; +import { triggerScheduler, refreshCronSchedules } from "@/app/api/settings/m18ImportTesting/actions"; interface Props {} @@ -166,9 +168,80 @@ const M18ImportTesting: React.FC = ({}) => { // [], // ); + const handleManualTrigger = async (type: any) => { + setIsLoading(true); + setLoadingType(`Manual ${type}`); + try { + const result = await triggerScheduler(type); + if (result) alert(result); + } catch (error) { + console.error(error); + alert("Trigger failed. Check server logs."); + } finally { + setIsLoading(false); + } + }; + + const handleRefreshSchedules = async () => { + // Re-use the manual trigger logic which we know works + await handleManualTrigger('refresh-cron'); + }; + return ( + {t("Manual Scheduler Triggers")} + + + + + + + + +
+ + + {t("Status: ")} + {isLoading ? t(`Processing ${loadingType}...`) : t("Ready")} + + {t("Status: ")} {isLoading ? t(`Importing ${loadingType}...`) : t("Ready to import")}