-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemployee.go
221 lines (167 loc) · 7.43 KB
/
employee.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
220
221
package gomts
import "context"
// EmployeeClient interfaces with Employee related MyTimeStation API methods.
type EmployeeClient interface {
// Create a new employee.
Create(ctx context.Context, req *EmployeeCreateRequest) (*Employee, error)
// Get an employee by id.
Get(ctx context.Context, id string) (*Employee, error)
// List all employees.
List(ctx context.Context) ([]Employee, error)
// Update an employee by id.
Update(ctx context.Context, id string, req *EmployeeUpdateRequest) (*Employee, error)
// Delete an employee by id.
Delete(ctx context.Context, id string) (*Employee, error)
}
// EmployeeStatus represents the employee's clock-in/out state.
// Valid values *should* only be "in" or "out".
type EmployeeStatus string
const (
// EmployeeInStatus signals the employee is clocked in.
EmployeeInStatus EmployeeStatus = "in"
// EmployeeOutStatus signals the employee is clocked out.
EmployeeOutStatus EmployeeStatus = "out"
)
// Employee represents an employee working for a customer company in the
// MyTimeStation system.
type Employee struct {
// ID is the unique identifier for the employee within the MyTimeStation
// system.
ID string `json:"employee_id"`
// Name is the full name of the employee.
Name string `json:"name"`
// Title is the job title of the employee (e.g., Payroll Manager).
Title string `json:"title"`
// PrimaryDepartment is the main department where the employee works.
PrimaryDepartment string `json:"primary_department"`
// PrimaryDepartmentID is the unique identifier for the primary department.
PrimaryDepartmentID string `json:"primary_department_id"`
// CurrentDepartment is the department where the employee is currently
// working (can be different from primary).
CurrentDepartment string `json:"current_department"`
// CurrentDepartmentID is the unique identifier for the current department.
CurrentDepartmentID string `json:"current_department_id"`
// Status represents the employee's current clock-in status (in or out).
Status EmployeeStatus `json:"status"`
// CustomEmployeeID is the company-defined employee ID, which may differ
// from the system-generated ID.
CustomEmployeeID string `json:"custom_employee_id"`
// PIN is the employee's assigned personal identification number.
PIN string `json:"pin"`
// CardNumber is the employee's physical card number used for clocking
// in/out.
CardNumber string `json:"card_number"`
// CardQRCode is the QR code associated with the employee's card, used for
// clocking in/out.
CardQRCode string `json:"card_qr_code"`
// CustomFields is a map of additional employee-specific fields, such as
// phone number or start date.
CustomFields map[string]string `json:"custom_fields"`
}
// EmployeeListResponse is the response used for the List API method.
type EmployeeListResponse struct {
// Employees is the list of employees.
Employees []Employee `json:"employees"`
}
// EmployeeResponse is the response used for the Create, Get, Update and Delete
// API methods.
type EmployeeResponse struct {
// Employee is the employee of subject.
Employee Employee `json:"employee"`
}
// EmployeeCreateRequest represents the request body to create a new employee
// in the MyTimeStation system.
type EmployeeCreateRequest struct {
// Name is the full name of the employee.
// This field is required.
Name string `url:"name"`
// DepartmentID is the ID of the primary department to assign the employee.
// Either DepartmentID or DepartmentName must be supplied.
DepartmentID string `url:"department_id,omitempty"`
// DepartmentName is the name of the department to assign the employee.
// It can either create a new department or use an existing one.
// Either DepartmentID or DepartmentName must be supplied.
DepartmentName string `url:"department_name,omitempty"`
// CustomEmployeeID is an optional second ID to associate the employee with
// another system.
CustomEmployeeID string `url:"custom_employee_id,omitempty"`
// Title is the job title of the employee (e.g., Payroll Manager).
Title string `url:"title,omitempty"`
// HourlyRate is the hourly wage rate of the employee.
HourlyRate float64 `url:"hourly_rate,omitempty"`
// PIN is the 4-digit personal identification number for the employee.
PIN string `url:"pin,omitempty"`
// CustomFields allows setting one or more custom fields for the employee.
// The key is the custom field name, and the value is the field value.
CustomFields map[string]string `url:"custom_fields,omitempty"`
}
func (EmployeeCreateRequest) form() {}
// EmployeeUpdateRequest represents the request body to update an existing
// employee in the MyTimeStation system.
type EmployeeUpdateRequest struct {
// Name is the full name of the employee.
Name *string `json:"name"`
// DepartmentID is the ID of the primary department to assign the employee.
// Either DepartmentID or DepartmentName must be supplied.
DepartmentID *string `json:"department_id,omitempty"`
// DepartmentName is the name of the department to assign the employee.
// It can either create a new department or use an existing one.
// Either DepartmentID or DepartmentName must be supplied.
DepartmentName *string `json:"department_name,omitempty"`
// CustomEmployeeID is an optional second ID to associate the employee
// with another system.
CustomEmployeeID *string `json:"custom_employee_id,omitempty"`
// Title is the job title of the employee (e.g., Payroll Manager).
Title *string `json:"title,omitempty"`
// HourlyRate is the hourly wage rate of the employee.
HourlyRate *float64 `json:"hourly_rate,omitempty"`
// PIN is the 4-digit personal identification number for the employee.
PIN *string `json:"pin,omitempty"`
// CustomFields allows setting one or more custom fields for the employee.
// The key is the custom field name, and the value is the field value.
CustomFields map[string]string `json:"custom_fields,omitempty"`
// ConvertPrimaryDepartment indicates if the previous primary department
// should be retained as a secondary department when the primary department
// is changed. This parameter applies only to the current API request.
ConvertPrimaryDepartment *bool `json:"convert_primary_department,omitempty"`
}
// employeeService implements EmployeeClient
type employeeClient = client
func (c *employeeClient) Create(ctx context.Context, req *EmployeeCreateRequest) (*Employee, error) {
resp, err := httpPost[EmployeeResponse](ctx, c, "/employees", req)
if err != nil {
return nil, err
}
return &resp.Employee, nil
}
func (c *employeeClient) Get(ctx context.Context, id string) (*Employee, error) {
resp, err := httpGet[EmployeeResponse](ctx, c, "/employees/"+id)
if err != nil {
return nil, err
}
return &resp.Employee, nil
}
func (c *employeeClient) Update(ctx context.Context, id string, req *EmployeeUpdateRequest) (*Employee, error) {
resp, err := httpPut[EmployeeResponse](ctx, c, "/employees/"+id, req)
if err != nil {
return nil, err
}
return &resp.Employee, nil
}
func (c *employeeClient) Delete(ctx context.Context, id string) (*Employee, error) {
resp, err := httpDelete[EmployeeResponse](ctx, c, "/employees/"+id)
if err != nil {
return nil, err
}
return &resp.Employee, nil
}
func (c *employeeClient) List(ctx context.Context) ([]Employee, error) {
resp, err := httpGet[EmployeeListResponse](ctx, c, "/employees")
if err != nil {
return nil, err
}
return resp.Employees, nil
}
// compile-time assertion that employeeClient implementation fulfils
// EmployeeClient interface.
var _ EmployeeClient = (*employeeClient)(nil)