This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentries.go
197 lines (177 loc) · 6.97 KB
/
entries.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
191
192
193
194
195
196
197
package clockogo
import (
"github.com/google/go-querystring/query"
"net/http"
"strconv"
)
type EntriesAPI struct {
client *Client
}
type EntryType uint8
const (
TimeEntry EntryType = iota + 1
LumpsumValue
LumpsumService
)
type BillableType uint8
const (
NotBillable BillableType = iota
Billable
AlreadyBilled
)
type BudgetType string
const (
Strict BudgetType = "strict"
StrictCompleted BudgetType = "strict-completed"
StrictIncomplete BudgetType = "strict-incomplete"
Soft BudgetType = "soft"
SoftCompleted BudgetType = "soft-completed"
SoftIncomplete BudgetType = "soft-incomplete"
Without BudgetType = "without"
WithoutStrict BudgetType = "without-strict"
)
type Entry struct {
Id *int `json:"id" url:"-"`
CustomersId int `json:"customers_id" url:"customers_id"`
ProjectsId *int `json:"projects_id" url:"projects_id,omitempty"`
UsersId *int `json:"users_id" url:"users_id,omitempty"`
Billable *BillableType `json:"billable" url:"billable"`
TextsId *int `json:"texts_id" url:"-"`
TimeSince ISO8601UTC `json:"time_since" url:"time_since"`
TimeUntil *ISO8601UTC `json:"time_until" url:"time_until,omitempty"`
TimeInsert *ISO8601UTC `json:"time_insert" url:"-"`
TimeLastChange *ISO8601UTC `json:"time_last_change" url:"-"`
CustomersName *string `json:"customers_name" url:"-"`
ProjectsName *string `json:"projects_name" url:"-"`
UsersName *string `json:"users_name" url:"-"`
Text *string `json:"text" url:"text,omitempty"`
Revenue *float64 `json:"revenue" url:"-"`
Type EntryType `json:"type" url:"type"`
ServicesId *int `json:"services_id" url:"services_id,omitempty"`
Duration *int `json:"duration" url:"duration,omitempty"`
Offset *int `json:"offset" url:"-"`
Clocked *bool `json:"clocked" url:"-"`
ClockedOffline *bool `json:"clocked_offline" url:"-"`
TimeClockedSince *ISO8601UTC `json:"time_clocked_since" url:"-"`
TimeLastChangeWorktime *ISO8601UTC `json:"time_last_change_worktime" url:"-"`
HourlyRate *float64 `json:"hourly_rate" url:"hourly_rate,omitempty"`
ServicesName *string `json:"services_name" url:"-"`
Lumpsum *float64 `json:"lumpsum" url:"lumpsum,omitempty"`
LumpsumServicesId *int `json:"lumpsum_services_id" url:"lumpsum_services_id,omitempty"`
LumpsumServicesAmount *float64 `json:"lumpsum_services_amount" url:"lumpsum_services_amount,omitempty"`
LumpsumServicesPrice *float64 `json:"lumpsum_services_price" url:"-"`
}
func NewTimeEntry(customersId int, servicesId int, billable BillableType, timeSince ISO8601UTC, timeUntil ISO8601UTC) Entry {
return Entry{
Type: TimeEntry,
CustomersId: customersId,
ServicesId: &servicesId,
Billable: &billable,
TimeSince: timeSince,
TimeUntil: &timeUntil,
}
}
func NewLumpsumValueEntry(customersId int, servicesId int, lumpsum float64, billable BillableType, timeSince ISO8601UTC) Entry {
return Entry{
Type: LumpsumValue,
CustomersId: customersId,
ServicesId: &servicesId,
Lumpsum: &lumpsum,
Billable: &billable,
TimeSince: timeSince,
}
}
func NewLumpsumServiceEntry(customersId int, lumpsumServicesId int, lumpsumServicesAmount float64, billable BillableType, timeSince ISO8601UTC) Entry {
return Entry{
Type: LumpsumService,
CustomersId: customersId,
LumpsumServicesId: &lumpsumServicesId,
LumpsumServicesAmount: &lumpsumServicesAmount,
Billable: &billable,
TimeSince: timeSince,
}
}
type Entries struct {
Paging `json:"paging"`
Filter interface{} `json:"filter"`
Entries []Entry `json:"entries"`
}
type EntriesListParams struct {
TimeSince ISO8601UTC `url:"time_since"`
TimeUntil ISO8601UTC `url:"time_until"`
FilterUsersId int `url:"filter[users_id],omitempty"`
FilterCustomersId int `url:"filter[customers_id],omitempty"`
FilterProjectsId int `url:"filter[projects_id],omitempty"`
FilterServicesId int `url:"filter[services_id],omitempty"`
FilterLumpsumServicesId int `url:"filter[lumpsum_services_id],omitempty"`
FilterBillable BillableType `url:"filter[billable],omitempty"`
FilterText string `url:"filter[text],omitempty"`
FilterTextsId int `url:"filter[texts_id],omitempty"`
FilterBudgetType BudgetType `url:"filter[budget_type],omitempty"`
CalcAlsoRevenuesForProjectsWithHardBudget bool `url:"calc_also_revenues_for_projects_with_hard_budget,omitempty"`
EnhancedList bool `url:"enhanced_list,omitempty"`
Page int `url:"page,omitempty"`
}
func (api EntriesAPI) List(q EntriesListParams) (*Entries, error) {
params, err := query.Values(&q)
if err != nil {
return nil, err
}
req, err := api.client.auth.NewRequest(http.MethodGet, BaseURL+"/api/v2/entries?"+params.Encode(), nil)
var data Entries
err = api.client.Do(req, &data)
if err != nil {
return nil, err
}
return &data, nil
}
type SingleEntry struct {
Entry `json:"entry"`
}
func (api EntriesAPI) Read(id int) (*Entry, error) {
req, err := api.client.auth.NewRequest(http.MethodGet, BaseURL+"/api/v2/entries/"+strconv.Itoa(id), nil)
var data SingleEntry
err = api.client.Do(req, &data)
if err != nil {
return nil, err
}
return &data.Entry, nil
}
func (api EntriesAPI) Create(entry Entry) (*Entry, error) {
params, err := query.Values(&entry)
if err != nil {
return nil, err
}
req, err := api.client.auth.NewRequest(http.MethodPost, BaseURL+"/api/v2/entries?"+params.Encode(), nil)
var data SingleEntry
err = api.client.Do(req, &data)
if err != nil {
return nil, err
}
return &data.Entry, nil
}
func (api EntriesAPI) Update(entry Entry) (*Entry, error) {
params, err := query.Values(&entry)
if err != nil {
return nil, err
}
req, err := api.client.auth.NewRequest(http.MethodPut, BaseURL+"/api/v2/entries/"+strconv.Itoa(*entry.Id)+"?"+params.Encode(), nil)
var data SingleEntry
err = api.client.Do(req, &data)
if err != nil {
return nil, err
}
return &data.Entry, nil
}
func (api EntriesAPI) Delete(id int) error {
req, err := api.client.auth.NewRequest(http.MethodDelete, BaseURL+"/api/v2/entries/"+strconv.Itoa(id), nil)
if err != nil {
return err
}
var data interface{}
err = api.client.Do(req, data)
if err != nil {
return err
}
return nil
}