-
Notifications
You must be signed in to change notification settings - Fork 12
/
user.go
185 lines (159 loc) · 4.21 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
package opslevel
import (
"fmt"
)
type MemberInput struct {
Email string `json:"email"`
}
type UserId struct {
Id ID
Email string
}
type User struct {
UserId
HTMLUrl string
Name string
Role UserRole
// We cannot have this here because its breaks a TON of queries
// Teams *TeamIdConnection
}
type UserConnection struct {
Nodes []User
PageInfo PageInfo
TotalCount int
}
type UserIdentifierInput struct {
Id ID `graphql:"id" json:"id,omitempty"`
Email string `graphql:"email" json:"email,omitempty"`
}
type UserInput struct {
Name string `json:"name,omitempty"`
Role UserRole `json:"role,omitempty"`
SkipWelcomeEmail bool `json:"skipWelcomeEmail"`
}
//#region Helpers
func NewUserIdentifier(value string) UserIdentifierInput {
if IsID(value) {
return UserIdentifierInput{
Id: ID(value),
}
}
return UserIdentifierInput{
Email: value,
}
}
func (u *User) Teams(client *Client, variables *PayloadVariables) (*TeamIdConnection, error) {
var q struct {
Account struct {
User struct {
Teams TeamIdConnection `graphql:"teams(after: $after, first: $first)"`
} `graphql:"user(id: $user)"`
}
}
if u.Id == "" {
return nil, fmt.Errorf("unable to get teams, nil user id")
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["user"] = u.Id
if err := client.Query(&q, *variables, WithName("UserTeamsList")); err != nil { // what goes in "" here and how is it derived?
return nil, err
}
for q.Account.User.Teams.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.User.Teams.PageInfo.End
conn, err := u.Teams(client, variables)
if err != nil {
return nil, err
}
q.Account.User.Teams.Nodes = append(q.Account.User.Teams.Nodes, conn.Nodes...)
q.Account.User.Teams.PageInfo = conn.PageInfo
q.Account.User.Teams.TotalCount += conn.TotalCount
}
return &q.Account.User.Teams, nil
}
//#endregion
//#region Create
func (client *Client) InviteUser(email string, input UserInput) (*User, error) {
var m struct {
Payload struct {
User User
Errors []OpsLevelErrors
} `graphql:"userInvite(email: $email input: $input)"`
}
v := PayloadVariables{
"email": email,
"input": input,
}
err := client.Mutate(&m, v, WithName("UserInvite"))
return &m.Payload.User, HandleErrors(err, m.Payload.Errors)
}
//#endregion
//#region Retrieve
func (client *Client) GetUser(value string) (*User, error) {
var q struct {
Account struct {
User User `graphql:"user(input: $input)"`
}
}
v := PayloadVariables{
"input": NewUserIdentifier(value),
}
err := client.Query(&q, v, WithName("UserGet"))
return &q.Account.User, HandleErrors(err, nil)
}
func (client *Client) ListUsers(variables *PayloadVariables) (UserConnection, error) {
var q struct {
Account struct {
Users UserConnection `graphql:"users(after: $after, first: $first)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("UserList")); err != nil {
return UserConnection{}, err
}
for q.Account.Users.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Users.PageInfo.End
resp, err := client.ListUsers(variables)
if err != nil {
return UserConnection{}, err
}
q.Account.Users.Nodes = append(q.Account.Users.Nodes, resp.Nodes...)
q.Account.Users.PageInfo = resp.PageInfo
q.Account.Users.TotalCount += resp.TotalCount
}
return q.Account.Users, nil
}
//#endregion
//#region Update
func (client *Client) UpdateUser(user string, input UserInput) (*User, error) {
var m struct {
Payload struct {
User User
Errors []OpsLevelErrors
} `graphql:"userUpdate(user: $user input: $input)"`
}
v := PayloadVariables{
"user": NewUserIdentifier(user),
"input": input,
}
err := client.Mutate(&m, v, WithName("UserUpdate"))
return &m.Payload.User, HandleErrors(err, m.Payload.Errors)
}
//#endregion
//#region Delete
func (client *Client) DeleteUser(user string) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors
} `graphql:"userDelete(user: $user)"`
}
v := PayloadVariables{
"user": NewUserIdentifier(user),
}
err := client.Mutate(&m, v, WithName("UserDelete"))
return HandleErrors(err, m.Payload.Errors)
}
//#endregion