Skip to content

Commit

Permalink
Add mite tracker support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 5, 2019
1 parent 58e8bbe commit f43cd10
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 33 deletions.
7 changes: 7 additions & 0 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type TimeEntryApi interface {
DeleteTimeEntry(id string) error
}

type TrackerApi interface {
Tracker() (*TrackingTimeEntry, error)
StartTracker(id string) (*TrackingTimeEntry, *StoppedTimeEntry, error)
StopTracker(id string) (*StoppedTimeEntry, error)
}

type CustomerApi interface{}

type ProjectApi interface {
Expand All @@ -36,6 +42,7 @@ type UserApi interface{}
type Api interface {
AccountApi
TimeEntryApi
TrackerApi
CustomerApi
ProjectApi
ServiceApi
Expand Down
86 changes: 53 additions & 33 deletions mite/tracker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mite

import (
"math"
"fmt"
"strconv"
"time"
)
Expand All @@ -17,49 +17,69 @@ type StoppedTimeEntry struct {
Duration time.Duration
}

type Tracker struct {
Tracking TrackingTimeEntry
Stopped StoppedTimeEntry
type trackerResponse struct {
Tracker struct {
TrackingTimeEntry *struct {
Id int `json:"id"`
Minutes int `json:"minutes"`
Since time.Time `json:"since"`
} `json:"tracking_time_entry"`
StoppedTimeEntry *struct {
Id string `json:"id"`
Minutes int `json:"minutes"`
} `json:"stopped_time_entry"`
} `json:"tracker"`
}

type TrackerCommand struct {
Id string
Duration time.Duration
Since time.Time
}
func (r *trackerResponse) toTrackingTimeEntry() *TrackingTimeEntry {
if r.Tracker.TrackingTimeEntry == nil {
return nil
}

func (c *TrackerCommand) toRequest() *trackerRequest {
i, err := strconv.Atoi(c.Id)
if err != nil {
panic(err)
return &TrackingTimeEntry{
Id: strconv.Itoa(r.Tracker.TrackingTimeEntry.Id),
Duration: time.Duration(r.Tracker.TrackingTimeEntry.Minutes) * time.Minute,
Since: r.Tracker.TrackingTimeEntry.Since,
}
}

r := &trackerRequest{}
r.Tracker.TrackingTimeEntry.Id = i
r.Tracker.TrackingTimeEntry.Minutes = int(math.Floor(math.Round(c.Duration.Minutes()))) // BOGUS
r.Tracker.TrackingTimeEntry.Since = c.Since
func (r *trackerResponse) toStoppedTimeEntry() *StoppedTimeEntry {
if r.Tracker.StoppedTimeEntry == nil {
return nil
}

return r
return &StoppedTimeEntry{
Id: strconv.Itoa(r.Tracker.TrackingTimeEntry.Id),
Duration: time.Duration(r.Tracker.TrackingTimeEntry.Minutes) * time.Minute,
}
}

type trackerRequest struct {
Tracker struct {
TrackingTimeEntry struct {
Id int `json:"id"`
Minutes int `json:"minutes"`
Since time.Time `json:"since"`
} `json:"tracking_time_entry"`
} `json:"tracker"`
}
func (a *api) Tracker() (*TrackingTimeEntry, error) {
tr := trackerResponse{}
err := a.get("/tracker.json", &tr)
if err != nil {
return nil, err
}

func (a *api) Tracker() (*Tracker, error) {
return &Tracker{}, nil
return tr.toTrackingTimeEntry(), nil
}

func (a *api) StartTracker(command TrackerCommand) (*Tracker, error) {
return &Tracker{}, nil
func (a *api) StartTracker(id string) (*TrackingTimeEntry, *StoppedTimeEntry, error) {
tr := &trackerResponse{}
err := a.patch(fmt.Sprintf("/tracker/%s.json", id), nil, tr)
if err != nil {
return nil, nil, err
}

return tr.toTrackingTimeEntry(), tr.toStoppedTimeEntry(), nil
}

func (a *api) StopTracker(id string) (*Tracker, error) {
return &Tracker{}, nil
func (a *api) StopTracker(id string) (*StoppedTimeEntry, error) {
tr := &trackerResponse{}
err := a.delete(fmt.Sprintf("/tracker/%s.json", id), tr)
if err != nil {
return nil, err
}

return tr.toStoppedTimeEntry(), nil
}

0 comments on commit f43cd10

Please sign in to comment.