Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion cmd/timer/start/command.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package start

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/previousnext/tl-go/internal/alias"
"github.com/previousnext/tl-go/internal/db"
"github.com/previousnext/tl-go/internal/model"
"github.com/previousnext/tl-go/internal/service"
"github.com/previousnext/tl-go/internal/util"
)
Expand All @@ -30,7 +32,15 @@ func NewCommand(timerService func() service.TimerEntryServiceInterface, issueSto
desc := strings.Join(args[1:], " ")
description = &desc
}
return timerService().StartTimeEntry(issueKey, description)
prev, err := timerService().StartTimeEntry(issueKey, description)
if err != nil {
return err
}
if prev != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Paused time entry for %s, duration: %s\n", prev.IssueKey, model.FormatDuration(prev.Duration))
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Started time entry for %s\n", issueKey)
return nil
},
}
}
15 changes: 10 additions & 5 deletions internal/service/timer_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type TimerEntryServiceInterface interface {
StartTimeEntry(issueKey string, description *string) error
StartTimeEntry(issueKey string, description *string) (*model.TimerEntry, error)
PauseTimeEntry() error
ResumeTimerEntry(id *uint) error
StopTimeEntry(id *uint) (*model.TimeEntry, error)
Expand All @@ -38,7 +38,7 @@ func NewTimerEntryService(timerEntryStorage db.TimerEntryStorageInterface, timeE
}
}

func (s *TimerEntryService) StartTimeEntry(issueKey string, description *string) error {
func (s *TimerEntryService) StartTimeEntry(issueKey string, description *string) (*model.TimerEntry, error) {
now := s.now()
prev, err := s.timerEntryStorage.FindLatestActiveTimerEntry()
if err == nil && prev != nil {
Expand All @@ -51,10 +51,12 @@ func (s *TimerEntryService) StartTimeEntry(issueKey string, description *string)
prev.PauseTime = now
prev.LastActiveTime = now
if err := s.timerEntryStorage.SaveTimerEntry(prev); err != nil {
return err
return nil, err
}
} else if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
return nil, err
} else {
prev = nil
}

entry := &model.TimerEntry{
Expand All @@ -65,7 +67,10 @@ func (s *TimerEntryService) StartTimeEntry(issueKey string, description *string)
Duration: 0,
Description: description,
}
return s.timerEntryStorage.CreateTimerEntry(entry)
if err := s.timerEntryStorage.CreateTimerEntry(entry); err != nil {
return nil, err
}
return prev, nil
}

func (s *TimerEntryService) PauseTimeEntry() error {
Expand Down
15 changes: 11 additions & 4 deletions internal/service/timer_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ func TestTimerEntryService_TimerWorkflow(t *testing.T) {
return current
}

assert.NoError(t, service.StartTimeEntry("PNX-123", nil))
_, err := service.StartTimeEntry("PNX-123", nil)
assert.NoError(t, err)
assert.NoError(t, service.PauseTimeEntry())
assert.NoError(t, service.ResumeTimerEntry(nil))
timeEntry, err := service.StopTimeEntry(nil)
Expand Down Expand Up @@ -150,7 +151,8 @@ func TestTimerEntryService_StopRoundsToQuarterHour(t *testing.T) {
return current
}

assert.NoError(t, service.StartTimeEntry("PNX-456", nil))
_, err := service.StartTimeEntry("PNX-456", nil)
assert.NoError(t, err)
entry, err := service.StopTimeEntry(nil)
assert.NoError(t, err)
assert.NotNil(t, entry)
Expand Down Expand Up @@ -221,14 +223,19 @@ func TestTimerEntryService_OnlyOneActiveTimer(t *testing.T) {
return current
}

assert.NoError(t, service.StartTimeEntry("PNX-111", nil))
prev, err := service.StartTimeEntry("PNX-111", nil)
assert.NoError(t, err)
assert.Nil(t, prev)
assert.False(t, mockTimer.entry.Paused)
assert.NoError(t, service.StartTimeEntry("PNX-222", nil))
prev, err = service.StartTimeEntry("PNX-222", nil)
assert.NoError(t, err)
assert.NotNil(t, mockTimer.pausedEntry)
assert.True(t, mockTimer.pausedEntry.Paused)
assert.Equal(t, "PNX-111", mockTimer.pausedEntry.IssueKey)
assert.Equal(t, "PNX-222", mockTimer.entry.IssueKey)
assert.False(t, mockTimer.entry.Paused)
assert.NotNil(t, prev)
assert.Equal(t, "PNX-111", prev.IssueKey)
}

func TestTimerEntryService_DeleteTimerEntry(t *testing.T) {
Expand Down