This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
leaves.go
219 lines (181 loc) · 6.37 KB
/
leaves.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package factorial
import "encoding/json"
const (
leaveTypeURL = "/api/v1/leave_types"
leaveURL = "/api/v1/leaves"
)
// LeaveType contains all the leave type information
type LeaveType struct {
ID int `json:"id"`
Accrues bool `json:"accrues"` // Whether leaves with this type accrue holidays
Active bool `json:"active"` // Whether leaves whit this type can be created
ApprovalRequired bool `json:"approval_required"` // Whether leaves with this type require approval from timeoff managers
Attachment bool `json:"attachment"` // Whether leaves with this type accept attachments
Color string `json:"color"` // Identifying color of this leave type
Identifier string `json:"identifier"` // Slug identifying the type of leave type. Only "custom" leave types can be created or modified via the API
Name string `json:"name"`
Visibility bool `json:"visibility"` // Whether this leave type is visibile to regular employees
Workable bool `json:"workable"` // Whether leaves with this type count as working days
}
// CreateLeaveTypeRequest keeps the information needed
// for create a new leave type
type CreateLeaveTypeRequest struct {
Accrues bool `json:"accrues,omitempty"`
Active bool `json:"active,omitempty"`
ApprovalRequired bool `json:"approval_required,omitempty"`
Attachment bool `json:"attachment,omitempty"`
Color string `json:"color"`
Name string `json:"name"`
Visibility bool `json:"visibility,omitempty"`
Workable bool `json:"workable,omitempty"`
}
// UpdateLeaveTypeRequest keeps the information needed
// for update a leave type
type UpdateLeaveTypeRequest struct {
Accrues bool `json:"accrues,omitempty"`
Active bool `json:"active,omitempty"`
ApprovalRequired bool `json:"approval_required,omitempty"`
Attachment bool `json:"attachment,omitempty"`
Color string `json:"color,omitempty"`
Name string `json:"name,omitempty"`
Visibility bool `json:"visibility,omitempty"`
Workable bool `json:"workable,omitempty"`
}
// CreateLeaveType creates a new leave type.
// Restricted to admin users.
func (c Client) CreateLeaveType(lt CreateLeaveTypeRequest) (LeaveType, error) {
var leaveType LeaveType
bytes, err := json.Marshal(lt)
if err != nil {
return leaveType, err
}
resp, err := c.post(leaveTypeURL, bytes)
if err != nil {
return leaveType, err
}
if err := json.NewDecoder(resp.Body).Decode(&leaveType); err != nil {
return leaveType, err
}
return leaveType, nil
}
// ListLeaveTypes gets all leave types in your company.
func (c Client) ListLeaveTypes() ([]LeaveType, error) {
var leaveTypes []LeaveType
resp, err := c.get(leaveTypeURL, nil)
if err != nil {
return leaveTypes, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&leaveTypes); err != nil {
return leaveTypes, err
}
return leaveTypes, nil
}
// UpdateLeaveType update the given leave type id with the given
// request data
func (c Client) UpdateLeaveType(id string, lt UpdateLeaveTypeRequest) (LeaveType, error) {
var leaveType LeaveType
bytes, err := json.Marshal(lt)
if err != nil {
return leaveType, err
}
resp, err := c.put(leaveTypeURL+"/"+id, bytes)
if err != nil {
return leaveType, err
}
if err := json.NewDecoder(resp.Body).Decode(&leaveType); err != nil {
return leaveType, err
}
return leaveType, nil
}
// Leave contains all the leave information
type Leave struct {
ID int `json:"id"`
Description string `json:"description"`
EmployeeID int `json:"employee_id"`
FinishOn string `json:"finish_on"`
HalfDay string `json:"half_day"`
LeaveTypeID int `json:"leave_type_id"`
StartOn string `json:"start_on"`
}
// CreateLeaveRequest keeps the information needed
// for create a new leave
type CreateLeaveRequest struct {
Description string `json:"description,omitempty"`
EmployeeID int `json:"employee_id"`
FinishOn string `json:"finish_on"`
HalfDay string `json:"half_day,omitempty"`
LeaveTypeID int `json:"leave_type_id"`
StartOn string `json:"start_on"`
}
// UpdateLeaveRequest keeps the information needed
// for update a leave
type UpdateLeaveRequest struct {
Description string `json:"description,omitempty"`
EmployeeID int `json:"employee_id,omitempty"`
FinishOn string `json:"finish_on,omitempty"`
HalfDay string `json:"half_day,omitempty"`
LeaveTypeID int `json:"leave_type_id,omitempty"`
StartOn string `json:"start_on,omitempty"`
}
// CreateLeave creates a new leave.
// Admins can create leaves for all employees,
// regular users are restricted to themselves and employees they manage.
func (c Client) CreateLeave(l CreateLeaveRequest) (Leave, error) {
var leave Leave
bytes, err := json.Marshal(l)
if err != nil {
return leave, err
}
resp, err := c.post(leaveURL, bytes)
if err != nil {
return leave, err
}
if err := json.NewDecoder(resp.Body).Decode(&leave); err != nil {
return leave, err
}
return leave, nil
}
// DeleteLeave deletes an existing leave.
// Admins can delete leaves for all employees,
// regular users are restricted to themselves and employees they manage.
// Restrictions apply if the leave has already started.
func (c Client) DeleteLeave(id string) error {
_, err := c.delete(leaveURL + "/" + id)
if err != nil {
return err
}
return nil
}
// ListLeaves gets all leaves from your company.
func (c Client) ListLeaves() ([]Leave, error) {
var leaves []Leave
resp, err := c.get(leaveURL, nil)
if err != nil {
return leaves, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&leaves); err != nil {
return leaves, err
}
return leaves, nil
}
// UpdateLeave update the given leave id with the given request data.
// Admins can update leaves for all employees,
// regular users are restricted to themselves and employees they manage.
// Restrictions apply if the leave has already started.
func (c Client) UpdateLeave(id string, lt UpdateLeaveRequest) (Leave, error) {
var leave Leave
bytes, err := json.Marshal(lt)
if err != nil {
return leave, err
}
resp, err := c.put(leaveURL+"/"+id, bytes)
if err != nil {
return leave, err
}
if err := json.NewDecoder(resp.Body).Decode(&leave); err != nil {
return leave, err
}
return leave, nil
}