Fix shift hours overcounting automatic breaks + add version-check banner

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 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-21 15:30:17 +00:00
parent ba23d59b3d
commit e0b0f2f275
5 changed files with 100 additions and 8 deletions

View file

@ -7,11 +7,12 @@ import { configRoutes } from './routes/config.js'
import { workforceRoutes } from './routes/workforce.js' import { workforceRoutes } from './routes/workforce.js'
const app = Fastify({ logger: true, trustProxy: true }) const app = Fastify({ logger: true, trustProxy: true })
const startedAt = Date.now()
await app.register(cookie) await app.register(cookie)
await app.register(cors, { origin: process.env.CORS_ORIGIN || false, credentials: true }) 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(bookingRoutes)
await app.register(configRoutes) await app.register(configRoutes)

View file

@ -95,8 +95,6 @@ export async function fetchShifts(from, to, deptIds) {
let breakHrs = 0 let breakHrs = 0
if (Array.isArray(s.breaks) && s.breaks.length) { if (Array.isArray(s.breaks) && s.breaks.length) {
breakHrs = s.breaks.reduce((sum, b) => sum + (b.finish - b.start) / 3600, 0) 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) const shiftHrs = Math.max(0, (s.finish - s.start) / 3600 - breakHrs)

View file

@ -1,5 +1,7 @@
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom' import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { AuthGate } from './components/AuthGate' import { AuthGate } from './components/AuthGate'
import { UpdateBanner } from './components/UpdateBanner'
import { useVersionCheck } from './hooks/useVersionCheck'
import { Layout } from './components/Layout' import { Layout } from './components/Layout'
import { Planner } from './pages/Planner' import { Planner } from './pages/Planner'
import { Settings } from './pages/CategorySettings' import { Settings } from './pages/CategorySettings'
@ -20,11 +22,15 @@ function AppRoutes({ user }: { user: User }) {
} }
export default function App() { export default function App() {
const updateAvailable = useVersionCheck('/hk-planner/health')
return ( return (
<BrowserRouter basename="/hk-planner"> <>
<AuthGate> <BrowserRouter basename="/hk-planner">
{user => <AppRoutes user={user} />} <AuthGate>
</AuthGate> {user => <AppRoutes user={user} />}
</BrowserRouter> </AuthGate>
</BrowserRouter>
<UpdateBanner visible={updateAvailable} />
</>
) )
} }

View file

@ -0,0 +1,44 @@
import { RefreshCw } from 'lucide-react'
export function UpdateBanner({ visible }: { visible: boolean }) {
if (!visible) return null
return (
<div style={{
position: 'fixed',
bottom: 0,
left: 0,
right: 0,
zIndex: 9999,
background: 'var(--sidebar)',
color: 'var(--text-light)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
gap: '12px',
padding: '10px 16px',
fontSize: '14px',
boxShadow: '0 -2px 8px rgba(0,0,0,0.3)',
}}>
<span>A new version is available.</span>
<button
onClick={() => window.location.reload()}
style={{
display: 'flex',
alignItems: 'center',
gap: '6px',
background: 'var(--accent)',
color: 'var(--sidebar)',
border: 'none',
borderRadius: '4px',
padding: '6px 14px',
fontWeight: 600,
cursor: 'pointer',
fontSize: '13px',
}}
>
<RefreshCw size={14} strokeWidth={1.75} />
Reload
</button>
</div>
)
}

View file

@ -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
}