-
Notifications
You must be signed in to change notification settings - Fork 20
/
organizations.go
319 lines (271 loc) · 10.7 KB
/
organizations.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
package clerk
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)
type OrganizationsService service
type Organization struct {
Object string `json:"object"`
ID string `json:"id"`
Name string `json:"name"`
Slug *string `json:"slug"`
LogoURL *string `json:"logo_url"`
MembersCount *int `json:"members_count,omitempty"`
MaxAllowedMemberships int `json:"max_allowed_memberships"`
PublicMetadata json.RawMessage `json:"public_metadata"`
PrivateMetadata json.RawMessage `json:"private_metadata,omitempty"`
CreatedBy string `json:"created_by"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
type CreateOrganizationParams struct {
Name string `json:"name"`
Slug *string `json:"slug,omitempty"`
CreatedBy string `json:"created_by"`
MaxAllowedMemberships *int `json:"max_allowed_memberships,omitempty"`
PublicMetadata json.RawMessage `json:"public_metadata,omitempty"`
PrivateMetadata json.RawMessage `json:"private_metadata,omitempty"`
}
func (s *OrganizationsService) Create(params CreateOrganizationParams) (*Organization, error) {
req, _ := s.client.NewRequest(http.MethodPost, OrganizationsUrl, ¶ms)
var organization Organization
_, err := s.client.Do(req, &organization)
if err != nil {
return nil, err
}
return &organization, nil
}
type UpdateOrganizationParams struct {
Name *string `json:"name,omitempty"`
MaxAllowedMemberships *int `json:"max_allowed_memberships,omitempty"`
PublicMetadata json.RawMessage `json:"public_metadata,omitempty"`
PrivateMetadata json.RawMessage `json:"private_metadata,omitempty"`
}
func (s *OrganizationsService) Update(organizationID string, params UpdateOrganizationParams) (*Organization, error) {
req, _ := s.client.NewRequest(http.MethodPatch, fmt.Sprintf("%s/%s", OrganizationsUrl, organizationID), ¶ms)
var organization Organization
_, err := s.client.Do(req, &organization)
if err != nil {
return nil, err
}
return &organization, nil
}
type UpdateOrganizationMetadataParams struct {
PublicMetadata json.RawMessage `json:"public_metadata,omitempty"`
PrivateMetadata json.RawMessage `json:"private_metadata,omitempty"`
}
func (s *OrganizationsService) UpdateMetadata(organizationID string, params UpdateOrganizationMetadataParams) (*Organization, error) {
req, _ := s.client.NewRequest(http.MethodPatch, fmt.Sprintf("%s/%s/metadata", OrganizationsUrl, organizationID), ¶ms)
var organization Organization
_, err := s.client.Do(req, &organization)
return &organization, err
}
func (s *OrganizationsService) Delete(organizationID string) (*DeleteResponse, error) {
req, _ := s.client.NewRequest(http.MethodDelete, fmt.Sprintf("%s/%s", OrganizationsUrl, organizationID))
var deleteResponse DeleteResponse
_, err := s.client.Do(req, &deleteResponse)
if err != nil {
return nil, err
}
return &deleteResponse, nil
}
func (s *OrganizationsService) Read(organizationIDOrSlug string) (*Organization, error) {
req, err := s.client.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", OrganizationsUrl, organizationIDOrSlug))
if err != nil {
return nil, err
}
var organization Organization
_, err = s.client.Do(req, &organization)
if err != nil {
return nil, err
}
return &organization, nil
}
type OrganizationsResponse struct {
Data []Organization `json:"data"`
TotalCount int64 `json:"total_count"`
}
type ListAllOrganizationsParams struct {
Limit *int
Offset *int
IncludeMembersCount bool
Query string
UserIDs []string
}
func (s *OrganizationsService) ListAll(params ListAllOrganizationsParams) (*OrganizationsResponse, error) {
req, _ := s.client.NewRequest(http.MethodGet, OrganizationsUrl)
query := req.URL.Query()
if params.Limit != nil {
query.Set("limit", strconv.Itoa(*params.Limit))
}
if params.Offset != nil {
query.Set("offset", strconv.Itoa(*params.Offset))
}
if params.IncludeMembersCount {
query.Set("include_members_count", strconv.FormatBool(params.IncludeMembersCount))
}
if params.Query != "" {
query.Add("query", params.Query)
}
for _, userID := range params.UserIDs {
query.Add("user_id", userID)
}
req.URL.RawQuery = query.Encode()
var organizationsResponse *OrganizationsResponse
_, err := s.client.Do(req, &organizationsResponse)
if err != nil {
return nil, err
}
return organizationsResponse, nil
}
type OrganizationInvitation struct {
Object string `json:"object"`
ID string `json:"id"`
EmailAddress string `json:"email_address"`
OrganizationID string `json:"organization_id"`
PublicMetadata json.RawMessage `json:"public_metadata"`
Role string `json:"role"`
Status string `json:"status"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
type CreateOrganizationInvitationParams struct {
EmailAddress string `json:"email_address"`
InviterUserID string `json:"inviter_user_id"`
OrganizationID string `json:"organization_id"`
PublicMetadata json.RawMessage `json:"public_metadata,omitempty"`
RedirectURL string `json:"redirect_url,omitempty"`
Role string `json:"role"`
}
func (s *OrganizationsService) CreateInvitation(params CreateOrganizationInvitationParams) (*OrganizationInvitation, error) {
endpoint := fmt.Sprintf("%s/%s/%s", OrganizationsUrl, params.OrganizationID, InvitationsURL)
req, _ := s.client.NewRequest(http.MethodPost, endpoint, ¶ms)
var organizationInvitation OrganizationInvitation
_, err := s.client.Do(req, &organizationInvitation)
return &organizationInvitation, err
}
type ListOrganizationMembershipsParams struct {
OrganizationID string
Limit *int
Offset *int
Roles []string `json:"role"`
UserIDs []string `json:"user_id"`
EmailAddresses []string `json:"email_address"`
PhoneNumbers []string `json:"phone_number"`
Usernames []string `json:"username"`
Web3Wallets []string `json:"web3_wallet"`
OrderBy *string `json:"order_by"`
Query *string `json:"query"`
}
type ΟrganizationMembershipPublicUserData struct {
FirstName *string `json:"first_name"`
LastName *string `json:"last_name"`
ProfileImageURL string `json:"profile_image_url"`
Identifier string `json:"identifier"`
UserID string `json:"user_id"`
}
type OrganizationMembership struct {
Object string `json:"object"`
ID string `json:"id"`
PublicMetadata json.RawMessage `json:"public_metadata"`
PrivateMetadata json.RawMessage `json:"private_metadata"`
Role string `json:"role"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
Organization *Organization `json:"organization"`
PublicUserData *ΟrganizationMembershipPublicUserData `json:"public_user_data"`
}
type ListOrganizationMembershipsResponse struct {
Data []OrganizationMembership `json:"data"`
TotalCount int64 `json:"total_count"`
}
func (s *OrganizationsService) addMembersSearchParamsToRequest(r *http.Request, params ListOrganizationMembershipsParams) {
query := r.URL.Query()
for _, email := range params.EmailAddresses {
query.Add("email_address", email)
}
for _, phone := range params.PhoneNumbers {
query.Add("phone_number", phone)
}
for _, web3Wallet := range params.Web3Wallets {
query.Add("web3_wallet", web3Wallet)
}
for _, username := range params.Usernames {
query.Add("username", username)
}
for _, userID := range params.UserIDs {
query.Add("user_id", userID)
}
for _, role := range params.Roles {
query.Add("role", role)
}
if params.Query != nil {
query.Add("query", *params.Query)
}
r.URL.RawQuery = query.Encode()
}
func (s *OrganizationsService) ListMemberships(params ListOrganizationMembershipsParams) (*ListOrganizationMembershipsResponse, error) {
endpoint := fmt.Sprintf("%s/%s/memberships", OrganizationsUrl, params.OrganizationID)
req, _ := s.client.NewRequest(http.MethodGet, endpoint)
s.addMembersSearchParamsToRequest(req, ListOrganizationMembershipsParams{
EmailAddresses: params.EmailAddresses,
PhoneNumbers: params.PhoneNumbers,
Web3Wallets: params.Web3Wallets,
Usernames: params.Usernames,
UserIDs: params.UserIDs,
Roles: params.Roles,
Query: params.Query,
OrderBy: params.OrderBy,
})
query := req.URL.Query()
if params.Limit != nil {
query.Set("limit", strconv.Itoa(*params.Limit))
}
if params.Offset != nil {
query.Set("offset", strconv.Itoa(*params.Offset))
}
req.URL.RawQuery = query.Encode()
var membershipsResponse *ListOrganizationMembershipsResponse
_, err := s.client.Do(req, &membershipsResponse)
if err != nil {
return nil, err
}
return membershipsResponse, nil
}
type CreateOrganizationMembershipParams struct {
UserID string `json:"user_id"`
Role string `json:"role"`
}
func (s *OrganizationsService) CreateMembership(organizationID string, params CreateOrganizationMembershipParams) (*OrganizationMembership, error) {
req, _ := s.client.NewRequest(http.MethodPost, fmt.Sprintf("%s/%s/memberships", OrganizationsUrl, organizationID), ¶ms)
var organizationMembership OrganizationMembership
_, err := s.client.Do(req, &organizationMembership)
if err != nil {
return nil, err
}
return &organizationMembership, nil
}
type UpdateOrganizationMembershipParams struct {
UserID string `json:"user_id"`
Role string `json:"role"`
}
func (s *OrganizationsService) UpdateMembership(organizationID string, params UpdateOrganizationMembershipParams) (*OrganizationMembership, error) {
req, _ := s.client.NewRequest(http.MethodPatch, fmt.Sprintf("%s/%s/memberships/%s", OrganizationsUrl, organizationID, params.UserID), ¶ms)
var organizationMembership OrganizationMembership
_, err := s.client.Do(req, &organizationMembership)
if err != nil {
return nil, err
}
return &organizationMembership, nil
}
func (s *OrganizationsService) DeleteMembership(organizationID, userID string) (*OrganizationMembership, error) {
req, _ := s.client.NewRequest(http.MethodDelete, fmt.Sprintf("%s/%s/memberships/%s", OrganizationsUrl, organizationID, userID))
var organizationMembership OrganizationMembership
_, err := s.client.Do(req, &organizationMembership)
if err != nil {
return nil, err
}
return &organizationMembership, nil
}