From e0b0f2f275ae7555d5ecbbc2f0128926e690d335 Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Tue, 21 Jul 2026 15:30:17 +0000 Subject: [PATCH] Fix shift hours overcounting automatic breaks + add version-check banner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Workforce's automatic_break_length is a payroll deduction setting, not a scheduling field — applying it when no explicit breaks are present was silently shortening all shifts (e.g. a 2h shift showing as 1.5h). Now only deducts explicit break records from s.breaks. Also adds BUILD_VERSION to /health and an UpdateBanner that prompts staff to reload when a new deploy lands. Co-Authored-By: Claude Sonnet 4.6 --- backend/src/index.js | 3 +- backend/src/lib/workforce.js | 2 -- frontend/src/App.tsx | 16 ++++++--- frontend/src/components/UpdateBanner.tsx | 44 ++++++++++++++++++++++++ frontend/src/hooks/useVersionCheck.ts | 43 +++++++++++++++++++++++ 5 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 frontend/src/components/UpdateBanner.tsx create mode 100644 frontend/src/hooks/useVersionCheck.ts diff --git a/backend/src/index.js b/backend/src/index.js index 8725d40..bbfb25a 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -7,11 +7,12 @@ import { configRoutes } from './routes/config.js' import { workforceRoutes } from './routes/workforce.js' const app = Fastify({ logger: true, trustProxy: true }) +const startedAt = Date.now() await app.register(cookie) await app.register(cors, { origin: process.env.CORS_ORIGIN || false, credentials: true }) -app.get('/health', async () => ({ status: 'healthy' })) +app.get('/health', async () => ({ status: 'healthy', version: process.env.BUILD_VERSION || String(startedAt) })) await app.register(bookingRoutes) await app.register(configRoutes) diff --git a/backend/src/lib/workforce.js b/backend/src/lib/workforce.js index fb08e55..a533236 100644 --- a/backend/src/lib/workforce.js +++ b/backend/src/lib/workforce.js @@ -95,8 +95,6 @@ export async function fetchShifts(from, to, deptIds) { let breakHrs = 0 if (Array.isArray(s.breaks) && s.breaks.length) { breakHrs = s.breaks.reduce((sum, b) => sum + (b.finish - b.start) / 3600, 0) - } else { - breakHrs = (s.automatic_break_length ?? 0) / 60 } const shiftHrs = Math.max(0, (s.finish - s.start) / 3600 - breakHrs) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 471bd86..0e6f108 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,5 +1,7 @@ import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom' import { AuthGate } from './components/AuthGate' +import { UpdateBanner } from './components/UpdateBanner' +import { useVersionCheck } from './hooks/useVersionCheck' import { Layout } from './components/Layout' import { Planner } from './pages/Planner' import { Settings } from './pages/CategorySettings' @@ -20,11 +22,15 @@ function AppRoutes({ user }: { user: User }) { } export default function App() { + const updateAvailable = useVersionCheck('/hk-planner/health') return ( - - - {user => } - - + <> + + + {user => } + + + + ) } diff --git a/frontend/src/components/UpdateBanner.tsx b/frontend/src/components/UpdateBanner.tsx new file mode 100644 index 0000000..b492c67 --- /dev/null +++ b/frontend/src/components/UpdateBanner.tsx @@ -0,0 +1,44 @@ +import { RefreshCw } from 'lucide-react' + +export function UpdateBanner({ visible }: { visible: boolean }) { + if (!visible) return null + return ( +
+ A new version is available. + +
+ ) +} diff --git a/frontend/src/hooks/useVersionCheck.ts b/frontend/src/hooks/useVersionCheck.ts new file mode 100644 index 0000000..e4fed28 --- /dev/null +++ b/frontend/src/hooks/useVersionCheck.ts @@ -0,0 +1,43 @@ +import { useEffect, useState } from 'react' + +const POLL_MS = 2 * 60 * 1000 + +export function useVersionCheck(healthUrl: string) { + const [updateAvailable, setUpdateAvailable] = useState(false) + + useEffect(() => { + let seenVersion: string | null = null + + async function check() { + try { + const res = await fetch(healthUrl, { cache: 'no-store' }) + if (!res.ok) return + const data = await res.json() + const v: string | undefined = data.version + if (!v) return + if (seenVersion === null) { + seenVersion = v + } else if (v !== seenVersion) { + setUpdateAvailable(true) + } + } catch { + // network error — skip silently + } + } + + check() + const interval = setInterval(check, POLL_MS) + + function onVisible() { + if (document.visibilityState === 'visible') check() + } + document.addEventListener('visibilitychange', onVisible) + + return () => { + clearInterval(interval) + document.removeEventListener('visibilitychange', onVisible) + } + }, [healthUrl]) + + return updateAvailable +}