forked from nukosuke/go-zendesk
/
user.go
180 lines (157 loc) · 5.39 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
package zendesk
import (
"context"
"encoding/json"
"fmt"
"time"
)
// User is zendesk user JSON payload format
// https://developer.zendesk.com/rest_api/docs/support/users
type User struct {
ID int64 `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
Name string `json:"name"`
Active bool `json:"active,omitempty"`
Alias string `json:"alias,omitempty"`
ChatOnly bool `json:"chat_only,omitempty"`
CustomRoleID int64 `json:"custom_role_id,omitempty"`
RoleType int64 `json:"role_type,omitempty"`
Details string `json:"details,omitempty"`
ExternalID string `json:"external_id,omitempty"`
Locale string `json:"locale,omitempty"`
LocaleID int64 `json:"locale_id,omitempty"`
Moderator bool `json:"moderator,omitempty"`
Notes string `json:"notes,omitempty"`
OnlyPrivateComments bool `json:"only_private_comments,omitempty"`
OrganizationID int64 `json:"organization_id,omitempty"`
DefaultGroupID int64 `json:"default_group_id,omitempty"`
Phone string `json:"phone,omitempty"`
SharedPhoneNumber bool `json:"shared_phone_number,omitempty"`
Photo Attachment `json:"photo,omitempty"`
RestrictedAgent bool `json:"restricted_agent,omitempty"`
Role string `json:"role,omitempty"`
Shared bool `json:"shared,omitempty"`
SharedAgent bool `json:"shared_agent,omitempty"`
Signature string `json:"signature,omitempty"`
Suspended bool `json:"suspended,omitempty"`
Tags []string `json:"tags,omitempty"`
TicketRestriction string `json:"ticket_restriction,omitempty"`
Timezone string `json:"time_zone,omitempty"`
TwoFactorAuthEnabled bool `json:"two_factor_auth_enabled,omitempty"`
//TODO: UserFields UserFields
Verified bool `json:"verified,omitempty"`
ReportCSV bool `json:"report_csv,omitempty"`
LastLoginAt time.Time `json:"last_login_at,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
const (
// UserRoleEndUser end-user
UserRoleEndUser = iota
// UserRoleAgent agent
UserRoleAgent
// UserRoleAdmin admin
UserRoleAdmin
)
var userRoleText = map[int]string{
UserRoleEndUser: "end-user",
UserRoleAgent: "agent",
UserRoleAdmin: "admin",
}
// UserListOptions is options for GetUsers
//
// ref: https://developer.zendesk.com/rest_api/docs/support/users#list-users
type UserListOptions struct {
PageOptions
Role string `url:"role,omitempty"`
Roles []string `url:"role[],omitempty"`
PermissionSet int64 `url:"permission_set,omitempty"`
}
// UserRoleText takes role type and returns role name string
func UserRoleText(role int) string {
return userRoleText[role]
}
// UserAPI an interface containing all user related methods
type UserAPI interface {
GetUsers(ctx context.Context, opts *UserListOptions) ([]User, Page, error)
GetUser(ctx context.Context, userID int64) (User, error)
CreateUser(ctx context.Context, user User) (User, error)
UpdateUser(ctx context.Context, userID int64, user User) (User, error)
}
// GetUsers fetch user list
func (z *Client) GetUsers(ctx context.Context, opts *UserListOptions) ([]User, Page, error) {
var data struct {
Users []User `json:"users"`
Page
}
tmp := opts
if tmp == nil {
tmp = &UserListOptions{}
}
u, err := addOptions("/users.json", tmp)
if err != nil {
return nil, Page{}, err
}
body, err := z.get(ctx, u)
if err != nil {
return nil, Page{}, err
}
err = json.Unmarshal(body, &data)
if err != nil {
return nil, Page{}, err
}
return data.Users, data.Page, nil
}
//TODO: GetUsersByGroupID, GetUsersByOrganizationID
// CreateUser creates new user
// ref: https://developer.zendesk.com/rest_api/docs/core/triggers#create-trigger
func (z *Client) CreateUser(ctx context.Context, user User) (User, error) {
var data, result struct {
User User `json:"user"`
}
data.User = user
body, err := z.post(ctx, "/users.json", data)
if err != nil {
return User{}, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return User{}, err
}
return result.User, nil
}
// TODO: CreateOrUpdateManyUsers(users []User)
// GetUser get an existing user
// ref: https://developer.zendesk.com/rest_api/docs/support/users#show-user
func (z *Client) GetUser(ctx context.Context, userID int64) (User, error) {
var result struct {
User User `json:"user"`
}
body, err := z.get(ctx, fmt.Sprintf("/users/%d.json", userID))
if err != nil {
return User{}, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return User{}, err
}
return result.User, nil
}
// UpdateUser update an existing user
// ref: https://developer.zendesk.com/rest_api/docs/support/users#update-user
func (z *Client) UpdateUser(ctx context.Context, userID int64, user User) (User, error) {
var data, result struct {
User User `json:"user"`
}
data.User = user
body, err := z.put(ctx, fmt.Sprintf("/users/%d.json", userID), data)
if err != nil {
return User{}, err
}
err = json.Unmarshal(body, &result)
if err != nil {
return User{}, err
}
return result.User, nil
}