-
Notifications
You must be signed in to change notification settings - Fork 4
/
time_enties.go
190 lines (164 loc) · 5.84 KB
/
time_enties.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package mite
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
)
// -------------------------------------------------------------
// ~ API Mappings
// -------------------------------------------------------------
// TimeEntryGroup mapping to the mite return type
type TimeEntryGroup struct {
Minutes uint64 `json:"minutes"`
Revenue float64 `json:"revenue"`
UserID uint64 `json:"user_id"`
UserName string `json:"user_name"`
From Time `json:"from"`
To Time `json:"to"`
Params map[string]interface{} `json:"time_entries_params"`
}
// TimeEntryCreator is used to create a new time entry
type TimeEntryCreator struct {
DateAt Time `json:"date_at"`
Minutes uint64 `json:"minutes"`
Note string `json:"note"`
UserID uint64 `json:"user_id"`
ProjectID uint64 `json:"project_id"`
ServiceID uint64 `json:"service_id"`
}
// TimeEntry mapping to the mite return type
type TimeEntry struct {
ID uint64 `json:"id"`
DateAt Time `json:"date_at"`
Minutes uint64 `json:"minutes"`
Note string `json:"note"`
UserID uint64 `json:"user_id"`
ProjectID uint64 `json:"project_id"`
ServiceID uint64 `json:"service_id"`
Locked bool `json:"locked"`
// Revenue bool `json:"locked"`
Billable bool `json:"billable"`
HourlyRate uint64 `json:"hourly_rate"`
UserName string `json:"user_name"`
ProjectName string `json:"project_name"`
CustomerID uint64 `json:"customer_id"`
CustomerName string `json:"customer_name"`
ServiceName string `json:"service_name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type getTimeEntryResponseWrapper struct {
TimeEntry *TimeEntry `json:"time_entry"`
}
type getTimeEntriesGroupResponseWrapper struct {
TimeEntry *TimeEntryGroup `json:"time_entry_group"`
}
// -------------------------------------------------------------
// ~ Get
// -------------------------------------------------------------
// GetTimeEntriesGroup return time entry groups for a timerange with filters
func (m *Mite) GetTimeEntriesGroup(from, to time.Time, filters map[string]string) ([]*TimeEntryGroup, error) {
if filters == nil {
return nil, fmt.Errorf("time_entries groups require at least one filter value to be set (filters are nil)")
}
var timeGroupEntries []*getTimeEntriesGroupResponseWrapper
err := m.getAndDecodeFromSuffix("time_entries.json", &timeGroupEntries, filters)
if err != nil {
return nil, err
}
timeEntries := make([]*TimeEntryGroup, len(timeGroupEntries))
// Unwrap all the data
for i, t := range timeGroupEntries {
timeEntries[i] = t.TimeEntry
// fmt.Println("Time Entry", t.TimeEntry)
}
return timeEntries, nil
}
// GetTimeEntries returns arrays for a time range
func (m *Mite) GetTimeEntries(from, to time.Time, filters map[string]string) ([]*TimeEntry, error) {
var timeResp []*getTimeEntryResponseWrapper
err := m.getAndDecodeFromSuffix("time_entries.json", &timeResp, filters)
if err != nil {
return nil, err
}
timeEntries := make([]*TimeEntry, len(timeResp))
// Unwrap all the data
for i, t := range timeResp {
timeEntries[i] = t.TimeEntry
// fmt.Println("Time Entry", t.TimeEntry)
}
return timeEntries, nil
}
// GetTimeEntriesForProjectByService returns a array of time entry groups grouped by service
func (m *Mite) GetTimeEntriesForProjectByService(from, to time.Time, projectID uint64) ([]*TimeEntryGroup, error) {
params := map[string]string{
ParamProjectID: fmt.Sprint(projectID),
ParamGroupBy: "service",
}
return m.GetTimeEntriesGroup(from, to, params)
}
// GetTimeEntry returns a time entry for a id
func (m *Mite) GetTimeEntry(id uint64) (*TimeEntry, error) {
var timeResp *getTimeEntryResponseWrapper
err := m.getAndDecodeFromSuffix("time_entries/"+strconv.FormatUint(id, 10)+".json", &timeResp, nil)
if err != nil {
return nil, err
}
return timeResp.TimeEntry, nil
}
// -------------------------------------------------------------
// ~ Create
// -------------------------------------------------------------
// CreateTimeEntry creates a new time entry
func (m *Mite) CreateTimeEntry(entry *TimeEntryCreator) (*TimeEntry, error) {
reqData := struct {
TimeEntry *TimeEntryCreator `json:"time_entry"`
}{TimeEntry: entry}
resp, errRequest := m.postToMite("/time_entries.json", reqData)
if errRequest != nil {
return nil, errRequest
}
if resp.StatusCode != http.StatusCreated {
return nil, errors.New("Failed to create a time entry: " + resp.Status)
}
var respEntry = &getTimeEntryResponseWrapper{}
// Unmarshal data
err := json.NewDecoder(resp.Body).Decode(respEntry)
if err != nil {
return nil, err
}
return respEntry.TimeEntry, nil
}
// -------------------------------------------------------------
// ~ Update
// -------------------------------------------------------------
// UpdateTimeEntry updates the fields provided in the update struct for a id
func (m *Mite) UpdateTimeEntry(id uint64, update *TimeEntry) error {
// Wrap time entry
wrap := &getTimeEntryResponseWrapper{TimeEntry: update}
resp, errRequest := m.patchAtMite("/time_entries/"+strconv.FormatUint(id, 10)+".json", wrap)
if errRequest != nil {
return errRequest
}
if resp.StatusCode != http.StatusOK {
return errors.New("Failed to create a time entry: " + resp.Status)
}
return nil
}
// -------------------------------------------------------------
// ~ Delete
// -------------------------------------------------------------
// DeleteTimeEntry removes a time entry for a user
func (m *Mite) DeleteTimeEntry(id uint64) error {
resp, errRequest := m.deleteFromMite("/time_entries/"+strconv.FormatUint(id, 10)+".json", nil)
if errRequest != nil {
return errRequest
}
if resp.StatusCode != http.StatusOK {
return errors.New("Failed to create a time entry: " + resp.Status)
}
return nil
}