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
/
shifts.go
143 lines (116 loc) · 3.49 KB
/
shifts.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
package factorial
import (
"encoding/json"
"net/url"
)
const (
shiftURL = "/api/v1/shifts"
clockInURL = shiftURL + "/clock_in"
clockOutURL = shiftURL + "/clock_out"
)
// Shift keeps the basic information related
// with shifts in Factorial
type Shift struct {
ID int `json:"id"`
Day int `json:"day"`
Month int `json:"month"`
Year int `json:"year"`
ClockIn string `json:"clock_in"`
ClockOut string `json:"clock_out"`
EmployeeID int `json:"employee_id"`
Observations string `json:"observations"`
}
// ClockInRequest will hold the basic information
// needed for create a new shift (ClockIn) in Factorial
type ClockInRequest struct {
Now string `json:"now"`
EmployeeID int `json:"employee_id"`
}
// ClockOutRequest will hold the basic information
// needed for create a new shift (ClockOut) in Factorial
type ClockOutRequest struct {
Now string `json:"now"`
EmployeeID int `json:"employee_id"`
}
// UpdateShiftRequest will hold the basic information
// for update a given shift.
// Restricted to the user's own shifts.
type UpdateShiftRequest struct {
ClockIn string `json:"clock_in"`
ClockOut string `json:"clock_out"`
Observations string `json:"observations"`
}
// ClockIn creates a new Shift with the provided time and for the requested employee.
// If an open Shift already exists, this endpoint will return an error with code 422.
func (c Client) ClockIn(cin ClockInRequest) (Shift, error) {
var shift Shift
bytes, err := json.Marshal(cin)
if err != nil {
return shift, err
}
resp, err := c.post(clockInURL, bytes)
if err != nil {
return shift, err
}
if err := json.NewDecoder(resp.Body).Decode(&shift); err != nil {
return shift, err
}
return shift, nil
}
// ClockOut closes an employee's open shift by setting the clock-out time to the value provided.
// If no open shift exists this endpoint will return an error with status code 422.
func (c Client) ClockOut(cout ClockOutRequest) (Shift, error) {
var shift Shift
bytes, err := json.Marshal(cout)
if err != nil {
return shift, err
}
resp, err := c.post(clockOutURL, bytes)
if err != nil {
return shift, err
}
if err := json.NewDecoder(resp.Body).Decode(&shift); err != nil {
return shift, err
}
return shift, nil
}
// DeleteShift will delete the given shiftID
func (c Client) DeleteShift(id string) error {
_, err := c.delete(shiftURL + "/" + id)
if err != nil {
return err
}
return nil
}
// ListShifts gets all the shifts. Shifts are the unit to control the presence of an employee.
// A Shift has a clock-in and clock-out time (in hours and minutes).
// A shift can be opened by just setting the clock-in time, and later on, closed by updating the clock-out time.
// You can filter this list by year and month.
func (c Client) ListShifts(filter url.Values) ([]Shift, error) {
var shifts []Shift
resp, err := c.get(shiftURL, filter)
if err != nil {
return shifts, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&shifts); err != nil {
return shifts, err
}
return shifts, nil
}
// UpdateShift update the given shift id with the given data
func (c Client) UpdateShift(id string, d UpdateShiftRequest) (Shift, error) {
var shift Shift
bytes, err := json.Marshal(d)
if err != nil {
return shift, err
}
resp, err := c.put(shiftURL+"/"+id, bytes)
if err != nil {
return shift, err
}
if err := json.NewDecoder(resp.Body).Decode(&shift); err != nil {
return shift, err
}
return shift, nil
}