-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
340 lines (294 loc) · 8.61 KB
/
user.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package ilert
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
// User definition https://api.ilert.com/api-docs/#tag/Users
type User struct {
ID int64 `json:"id,omitempty"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
Username string `json:"username,omitempty"`
Email string `json:"email,omitempty"`
Timezone string `json:"timezone,omitempty"`
Position string `json:"position,omitempty"`
Department string `json:"department,omitempty"`
Language string `json:"language,omitempty"`
Role string `json:"role,omitempty"`
Region string `json:"region,omitempty"`
ShiftColor string `json:"shiftColor,omitempty"`
}
// Phone definition
type Phone struct {
RegionCode string `json:"regionCode"`
Number string `json:"number"`
}
// UserLanguage defines user language
var UserLanguage = struct {
English string
German string
}{
English: "en",
German: "de",
}
// UserLanguageAll defines user language list
var UserLanguageAll = []string{
UserLanguage.English,
UserLanguage.German,
}
// UserRole defines user roles
var UserRole = struct {
User string
Admin string
Stakeholder string
}{
User: "USER",
Admin: "ADMIN",
Stakeholder: "STAKEHOLDER",
}
// UserRoleAll defines user roles list
var UserRoleAll = []string{
UserRole.User,
UserRole.Admin,
UserRole.Stakeholder,
}
// CreateUserInput represents the input of a CreateUser operation.
type CreateUserInput struct {
_ struct{}
User *User
SendNoInvitation *bool
}
// CreateUserOutput represents the output of a CreateUser operation.
type CreateUserOutput struct {
_ struct{}
User *User
}
func (u *User) GetUsername() string {
return u.Username
}
// CreateUser creates a new user. Requires ADMIN privileges. https://api.ilert.com/api-docs/#tag/Users/paths/~1users/post
func (c *Client) CreateUser(input *CreateUserInput) (*CreateUserOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.User == nil {
return nil, errors.New("user input is required")
}
requestURL := apiRoutes.users
if input.SendNoInvitation != nil {
requestURL = fmt.Sprintf("%s?send-no-invitation=%t", apiRoutes.users, *input.SendNoInvitation)
}
resp, err := c.httpClient.R().SetBody(input.User).Post(requestURL)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 201); apiErr != nil {
return nil, apiErr
}
user := &User{}
err = json.Unmarshal(resp.Body(), user)
if err != nil {
return nil, err
}
return &CreateUserOutput{User: user}, nil
}
// GetUserInput represents the input of a GetUser operation.
type GetUserInput struct {
_ struct{}
UserID *int64
Username *string
}
// GetUserOutput represents the output of a GetUser operation.
type GetUserOutput struct {
_ struct{}
User *User
}
// GetCurrentUser gets the currently authenticated user. https://api.ilert.com/api-docs/#tag/Users/paths/~1users~1current/get
func (c *Client) GetCurrentUser() (*GetUserOutput, error) {
input := &GetUserInput{Username: String("current")}
return c.GetUser(input)
}
// GetUser gets the user with specified id or username. https://api.ilert.com/api-docs/#tag/Users/paths/~1users~1{user-id}/get
func (c *Client) GetUser(input *GetUserInput) (*GetUserOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UserID == nil && input.Username == nil {
return nil, errors.New("user id or username is required")
}
var url string
if input.UserID != nil {
url = fmt.Sprintf("%s/%d", apiRoutes.users, *input.UserID)
} else {
url = fmt.Sprintf("%s/%s", apiRoutes.users, *input.Username)
}
resp, err := c.httpClient.R().Get(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
user := &User{}
err = json.Unmarshal(resp.Body(), user)
if err != nil {
return nil, err
}
return &GetUserOutput{User: user}, nil
}
// GetUsersInput represents the input of a GetUsers operation.
type GetUsersInput struct {
_ struct{}
// an integer specifying the starting point (beginning with 0) when paging through a list of entities
StartIndex *int
// the maximum number of results when paging through a list of entities.
// Maximum: 100
MaxResults *int
}
// GetUsersOutput represents the output of a GetUsers operation.
type GetUsersOutput struct {
_ struct{}
Users []*User
}
// GetUsers lists existing users. https://api.ilert.com/api-docs/#tag/Users/paths/~1users/get
func (c *Client) GetUsers(input *GetUsersInput) (*GetUsersOutput, error) {
q := url.Values{}
if input.StartIndex != nil {
q.Add("start-index", strconv.Itoa(*input.StartIndex))
}
if input.MaxResults != nil {
q.Add("max-results", strconv.Itoa(*input.MaxResults))
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s?%s", apiRoutes.users, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
users := make([]*User, 0)
err = json.Unmarshal(resp.Body(), &users)
if err != nil {
return nil, err
}
return &GetUsersOutput{Users: users}, nil
}
// SearchUserInput represents the input of a SearchUser operation.
type SearchUserInput struct {
_ struct{}
UserEmail *string
}
// SearchUserOutput represents the output of a SearchUser operation.
type SearchUserOutput struct {
_ struct{}
User *User
}
// SearchUser gets the user with specified name.
func (c *Client) SearchUser(input *SearchUserInput) (*SearchUserOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UserEmail == nil {
return nil, errors.New("user email is required")
}
resp, err := c.httpClient.R().SetBody(User{Email: *input.UserEmail}).Post(fmt.Sprintf("%s/search-email", apiRoutes.users))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
user := &User{}
err = json.Unmarshal(resp.Body(), user)
if err != nil {
return nil, err
}
return &SearchUserOutput{User: user}, nil
}
// UpdateUserInput represents the input of a UpdateUser operation.
type UpdateUserInput struct {
_ struct{}
UserID *int64
Username *string
User *User
}
// UpdateUserOutput represents the output of a UpdateUser operation.
type UpdateUserOutput struct {
_ struct{}
User *User
}
// UpdateCurrentUser updates the currently authenticated user. https://api.ilert.com/api-docs/#tag/Users/paths/~1users~1current/put
func (c *Client) UpdateCurrentUser(input *UpdateUserInput) (*UpdateUserOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
input.Username = String("current")
return c.UpdateUser(input)
}
// UpdateUser updates an existing user. https://api.ilert.com/api-docs/#tag/Users/paths/~1users~1{user-id}/put
func (c *Client) UpdateUser(input *UpdateUserInput) (*UpdateUserOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.User == nil {
return nil, errors.New("user input is required")
}
if input.UserID == nil && input.Username == nil {
return nil, errors.New("user id or username is required")
}
var url string
if input.UserID != nil {
url = fmt.Sprintf("%s/%d", apiRoutes.users, *input.UserID)
} else {
url = fmt.Sprintf("%s/%s", apiRoutes.users, *input.Username)
}
resp, err := c.httpClient.R().SetBody(input.User).Put(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
user := &User{}
err = json.Unmarshal(resp.Body(), user)
if err != nil {
return nil, err
}
return &UpdateUserOutput{User: user}, nil
}
// DeleteUserInput represents the input of a DeleteUser operation.
type DeleteUserInput struct {
_ struct{}
UserID *int64
Username *string
}
// DeleteUserOutput represents the output of a DeleteUser operation.
type DeleteUserOutput struct {
_ struct{}
}
// DeleteUser deletes the specified user. https://api.ilert.com/api-docs/#tag/Users/paths/~1users~1{user-id}/delete
func (c *Client) DeleteUser(input *DeleteUserInput) (*DeleteUserOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.UserID == nil && input.Username == nil {
return nil, errors.New("user id or username is required")
}
var url string
if input.UserID != nil {
url = fmt.Sprintf("%s/%d", apiRoutes.users, *input.UserID)
} else {
url = fmt.Sprintf("%s/%s", apiRoutes.users, *input.Username)
}
resp, err := c.httpClient.R().Delete(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 204); apiErr != nil {
return nil, apiErr
}
return &DeleteUserOutput{}, nil
}