Fix pay slips 404 when frontend hits legacy API on port 8001.

Default API URL to 8002, align dev proxy, and detect outdated health before listing payslips.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Syazwan 2026-06-01 20:22:00 +08:00
parent 9f246ee31c
commit 19a28bb4c2
4 changed files with 38 additions and 5 deletions

View File

@ -0,0 +1,20 @@
# Quick check: Claim API on 8002 should expose /api/payslips (401), not 404.
$ports = @(8002, 8001)
foreach ($port in $ports) {
$base = "http://127.0.0.1:$port"
try {
$health = Invoke-RestMethod -Uri "$base/api/health" -TimeoutSec 3
$payslip = Invoke-WebRequest -Uri "$base/api/payslips" -TimeoutSec 3 -SkipHttpErrorCheck
$ok = $health.features.payslips -eq $true
$code = $payslip.StatusCode
Write-Host "Port $port : health=$($health.status) payslips=$code payslips_feature=$ok"
if ($port -eq 8002 -and -not $ok) {
Write-Host " -> Restart backend from this repo (backend\start-local.bat)"
}
if ($port -eq 8001 -and $code -eq 404) {
Write-Host " -> Old API on 8001; stop it if frontend should use 8002"
}
} catch {
Write-Host "Port $port : not reachable"
}
}

View File

@ -39,6 +39,7 @@ async def health_check():
"mongodb": "connected" if mongo_healthy else "disconnected",
"database": db.name,
"version": "2.0.0",
"features": {"payslips": True},
}

View File

@ -63,7 +63,7 @@ webpackConfig.devServer = (devServerConfig) => {
devServerConfig.proxy = [
{
context: ["/api", "/uploads"],
target: "http://127.0.0.1:8000",
target: process.env.REACT_APP_BACKEND_URL || "http://127.0.0.1:8002",
changeOrigin: true,
// Excel COM PDF conversion can take longer than the default proxy timeout
proxyTimeout: 180000,

View File

@ -7,7 +7,7 @@ import {
CircleNotch,
X,
} from "@phosphor-icons/react";
import { api, apiErrorText } from "../lib/api";
import { api, apiBaseUrl, apiErrorText } from "../lib/api";
function formatPayslipDate(iso) {
if (!iso) return "—";
@ -46,6 +46,16 @@ export default function PaySlips() {
setLoading(true);
setErr("");
try {
const health = await api.get("/health");
if (!health.data?.features?.payslips) {
setErr(
`Connected API at ${apiBaseUrl} does not include pay slips (likely port 8001 legacy). ` +
"Use http://127.0.0.1:8002 — run backend\\start-local.bat and set REACT_APP_BACKEND_URL in frontend/.env, then restart yarn."
);
setItems([]);
setSelected(null);
return;
}
const r = await api.get("/payslips");
const list = r.data.items || [];
setItems(list);
@ -56,9 +66,11 @@ export default function PaySlips() {
} catch (e) {
const status = e?.response?.status;
if (status === 404) {
setErr(
"Pay slips API is not available. Restart the backend (port 8002) or redeploy the API on Render after pulling the latest code."
);
const hint =
apiBaseUrl.includes(":8001")
? "Your frontend is pointing at port 8001 (old API). Set REACT_APP_BACKEND_URL=http://127.0.0.1:8002 in frontend/.env and restart yarn."
: "Start the Claim API with backend\\start-local.bat (port 8002). If port 8001 is still running, stop that old process.";
setErr(`Pay slips API not found at ${apiBaseUrl}/api/payslips. ${hint}`);
} else {
setErr(apiErrorText(e));
}