♻️ Align week recompute with week close
CloseWeek left closed days that are no workdays out of the total, but recomputeWeek counted every closed day of the week. A closed Saturday therefore changed the total of an already closed week as soon as another day of that week was closed, reopened or marked. Both use the same rule now, with the settings effective on the closed day.
This commit is contained in:
@@ -115,6 +115,18 @@ func recomputeWeek(tx core.App, owner, dayKey string, now time.Time) error {
|
||||
}
|
||||
var workedMs int64
|
||||
for _, day := range closed {
|
||||
// The rule CloseWeek applies: a closed day that is not a workday in the
|
||||
// settings effective on it does not count, for example a Saturday that
|
||||
// was tracked anyway. Without the check the week total changed as soon
|
||||
// as any other day of the week was closed, reopened or marked.
|
||||
dayKey := day.GetString("day_key")
|
||||
set, err := settingsAt(tx, owner, dayKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if set == nil || !isWorkday(set, parseDayKey(dayKey).Weekday()) {
|
||||
continue
|
||||
}
|
||||
workedMs += day.GetInt64("worked_ms")
|
||||
}
|
||||
week.Set("worked_ms", workedMs)
|
||||
|
||||
@@ -244,6 +244,40 @@ func TestWeekSnapshotUpdatesWhenDayReopened(t *testing.T) {
|
||||
wantInt(t, week, "delta_ms", hourMs)
|
||||
}
|
||||
|
||||
func TestRecomputeWeekSkipsClosedNonWorkdays(t *testing.T) {
|
||||
e := setup(t)
|
||||
// A Saturday with tracked time can be closed, but it is no workday.
|
||||
e.entry(daySat, utc(daySat, 9, 0), utc(daySat, 15, 0))
|
||||
if _, err := CloseDay(e.app, e.owner.Id, daySat, testNow); err != nil {
|
||||
t.Fatalf("CloseDay saturday: %v", err)
|
||||
}
|
||||
|
||||
cw, err := CloseWeek(e.app, e.owner.Id, weekKey, testNow)
|
||||
if err != nil {
|
||||
t.Fatalf("CloseWeek: %v", err)
|
||||
}
|
||||
wantInt(t, cw, "worked_ms", 0)
|
||||
wantInt(t, cw, "delta_ms", -fortyH)
|
||||
|
||||
// Closing another day of the week must not add the Saturday to the total.
|
||||
if _, err := MarkDay(e.app, e.owner.Id, dayMon, kindHoliday, testNow.Add(time.Minute)); err != nil {
|
||||
t.Fatalf("MarkDay monday: %v", err)
|
||||
}
|
||||
week := e.load("closed_weeks", cw.Id)
|
||||
wantInt(t, week, "worked_ms", eightH)
|
||||
wantInt(t, week, "delta_ms", eightH-fortyH)
|
||||
wantInt(t, week, "rev", 2)
|
||||
|
||||
// Once Saturday is a workday in the settings effective on it, it counts.
|
||||
e.settings("2026-01-01", 40, 127, "UTC")
|
||||
if err := ReopenDay(e.app, e.owner.Id, dayMon, testNow.Add(2*time.Minute)); err != nil {
|
||||
t.Fatalf("ReopenDay monday: %v", err)
|
||||
}
|
||||
week = e.load("closed_weeks", cw.Id)
|
||||
wantInt(t, week, "worked_ms", 6*hourMs)
|
||||
wantInt(t, week, "rev", 3)
|
||||
}
|
||||
|
||||
func TestDayChangesLeaveAnOpenWeekAlone(t *testing.T) {
|
||||
e := setup(t)
|
||||
if _, err := MarkDay(e.app, e.owner.Id, dayMon, kindHoliday, testNow); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user