From 13933dad7dbd7d92aa56b01ea252ecfb3c38848e Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Mon, 10 Feb 2025 14:05:56 -0600 Subject: [PATCH] refactor(rotation): enhance calcAdvance to handle multiple rotation versions and return error for unknown versions --- engine/rotationmanager/advance.go | 15 ++++++++++----- engine/rotationmanager/updaterotation.go | 5 ++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/engine/rotationmanager/advance.go b/engine/rotationmanager/advance.go index b3647bc9f5..38333605ef 100644 --- a/engine/rotationmanager/advance.go +++ b/engine/rotationmanager/advance.go @@ -23,15 +23,20 @@ type rotState struct { } // calcAdvance will calculate rotation advancement if it is required. If not, nil is returned -func calcAdvance(ctx context.Context, t time.Time, rot *rotation.Rotation, state rotState, partCount int) *advance { +func calcAdvance(ctx context.Context, t time.Time, rot *rotation.Rotation, state rotState, partCount int) (*advance, error) { var mustUpdate bool origPos := state.Position // get next shift start time newStart := rot.EndTime(state.ShiftStart) - if state.Version == 1 { + switch state.Version { + case 1: newStart = calcVersion1EndTime(rot, state.ShiftStart) mustUpdate = true + case 2: + // no-op + default: + return nil, fmt.Errorf("unknown rotation version (supported: 1,2): %d", state.Version) } if state.Position >= partCount { @@ -49,10 +54,10 @@ func calcAdvance(ctx context.Context, t time.Time, rot *rotation.Rotation, state // If migrating from version 1 to 2 without changing // who's on-call do so silently. silent: state.Version == 1 && state.Position == origPos, - } + }, nil } // in the future, so nothing to do yet - return nil + return nil, nil } if !newStart.After(t.Add(-15 * time.Minute)) { @@ -79,5 +84,5 @@ func calcAdvance(ctx context.Context, t time.Time, rot *rotation.Rotation, state return &advance{ id: rot.ID, newPosition: state.Position, - } + }, nil } diff --git a/engine/rotationmanager/updaterotation.go b/engine/rotationmanager/updaterotation.go index c1b08c9ea3..326773048b 100644 --- a/engine/rotationmanager/updaterotation.go +++ b/engine/rotationmanager/updaterotation.go @@ -78,7 +78,10 @@ func (db *DB) updateRotation(ctx context.Context, j *river.Job[UpdateArgs]) erro Position: int(row.Position.Int32), Version: int(row.Version.Int32), } - adv := calcAdvance(ctx, row.Now, &r, s, len(row.Participants)) + adv, err := calcAdvance(ctx, row.Now, &r, s, len(row.Participants)) + if err != nil { + return fmt.Errorf("calc advance: %w", err) + } if adv == nil { // no advancement needed return nil