Refine today's timesheet handling — skip in-progress shifts

fetchTimesheetShifts: filter out today's shifts with no finish time
(still clocked in), so only completed shifts contribute hours.

runTimesheetSync: if today's row has no completed shifts at all,
skip writing it — leaves rota schedule data in place rather than
overwriting with an empty timesheet row while staff are still working.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-23 12:10:52 +00:00
parent c19460ecb3
commit e3c8f143ef
2 changed files with 14 additions and 1 deletions

View file

@ -105,7 +105,14 @@ export async function runTimesheetSync(from, to) {
const day = new Date(fromDate)
while (day <= toDate) {
const d = fmtDate(day)
dailyRows.push({ date: d, staff: byDate[d] || [], source: 'timesheet' })
const staffForDate = byDate[d] || []
// For today: only write a timesheet row if at least one shift has clocked out.
// An empty row would overwrite rota data with nothing while staff are still working.
if (d === today && staffForDate.length === 0) {
day.setDate(day.getDate() + 1)
continue
}
dailyRows.push({ date: d, staff: staffForDate, source: 'timesheet' })
day.setDate(day.getDate() + 1)
}

View file

@ -85,11 +85,17 @@ export async function fetchTimesheetShifts(from, to, deptIds) {
const filtered = shifts.filter(s => deptIds.includes(String(s.department_id)))
const todayISO = new Date().toLocaleDateString('en-CA', { timeZone: 'Europe/London' })
const byUser = {}
for (const s of filtered) {
const uid = String(s.user_id)
if (!nameMap[uid]) continue
const date = s.date // already 'YYYY-MM-DD'
// Skip today's shifts with no finish time — still clocked in, hours unknown
if (date === todayISO && !s.finish) continue
const hrs = (s.finish && s.start)
? Math.max(0, (s.finish - s.start) / 3600 - (s.break_length || 0) / 60)
: 0