-
Notifications
You must be signed in to change notification settings - Fork 3
/
teams.go
197 lines (161 loc) · 5.81 KB
/
teams.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
// Copyright (c) 2022, John Moore
// All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package clickup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
type Team struct {
ID string `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
Avatar string `json:"avatar"`
Members []TeamMember `json:"members"`
}
type TeamsResponse struct {
Teams []Team `json:"teams"`
}
type TeamUser struct {
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Color string `json:"color"`
ProfilePicture string `json:"profilePicture"`
Initials string `json:"initials"`
Role int `json:"role"`
CustomRole string `json:"custom_role"`
LastActive string `json:"last_active"`
DateJoined string `json:"date_joined"`
DateInvited string `json:"date_invited"`
}
type TeamMember struct {
User TeamUser `json:"user"`
InvitedBy TeamUser `json:"invited_by"`
}
// Teams returns a listing of teams for the authenticated user (the Client).
func (c *Client) Teams(ctx context.Context) (*TeamsResponse, error) {
endpoint := fmt.Sprintf("/team")
var teamsResponse TeamsResponse
if err := c.call(ctx, http.MethodGet, endpoint, nil, &teamsResponse); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &teamsResponse, nil
}
// TeamsForWorkspace is exactly the same as Teams() and simply calls it. This
// method is to maintain consistent naming with other Teams related parts
// of the API. I would do it differently if I went back in time.
func (c *Client) TeamsForWorkspace(ctx context.Context) (*TeamsResponse, error) {
return c.Teams(ctx)
}
type Group struct {
ID string `json:"id"`
TeamID string `json:"team_id"`
Userid int `json:"userid"`
Name string `json:"name"`
Handle string `json:"handle"`
DateCreated string `json:"date_created"`
Initials string `json:"initials"`
Members []TeamUser `json:"members"`
}
type GroupsQueryResponse struct {
Groups []Group `json:"groups"`
}
// GroupsForWorkspace queries for any groups in a workspace and returns their corresponding data.
// optionalGroupIDs can be provided to narrow the data returned to the explicit groups quried.
func (c *Client) GroupsForWorkspace(ctx context.Context, workspaceID string, optionalGroupIDs ...string) (*GroupsQueryResponse, error) {
if workspaceID == "" {
return nil, fmt.Errorf("must provide a workspaceID: %w", ErrValidation)
}
urlValues := url.Values{}
urlValues.Set("team_id", workspaceID)
for _, v := range optionalGroupIDs {
urlValues.Add("group_ids%5B%5D", v)
}
endpoint := fmt.Sprintf("/group/?%s", urlValues.Encode())
var groups GroupsQueryResponse
if err := c.call(ctx, http.MethodGet, endpoint, nil, &groups); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &groups, nil
}
type CreateGroupRequest struct {
WorkspaceID string
Name string `json:"name"`
MemberIDs []int `json:"member_ids"`
}
type CreateGroupResponse struct {
ID string `json:"id"`
TeamID string `json:"team_id"`
Userid int `json:"userid"`
Name string `json:"name"`
Handle string `json:"handle"`
DateCreated string `json:"date_created"`
Initials string `json:"initials"`
Members []TeamUser `json:"members"`
}
// CreateGroup adds a new group to a workspace using group.WorkspaceID.
func (c *Client) CreateGroup(ctx context.Context, group CreateGroupRequest) (*CreateGroupResponse, error) {
if group.WorkspaceID == "" {
return nil, fmt.Errorf("must provide a workspace ID: %w", ErrValidation)
}
b, err := json.Marshal(group)
if err != nil {
return nil, fmt.Errorf("unable to serialize new group: %w", err)
}
buf := bytes.NewBuffer(b)
endpoint := fmt.Sprintf("/team/%s/group", group.WorkspaceID)
var newGroup CreateGroupResponse
if err := c.call(ctx, http.MethodPost, endpoint, buf, &newGroup); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &newGroup, nil
}
type UpdateGroupRequest struct {
ID string
Name string `json:"name,omitempty"`
Handle string `json:"handle,omitempty"`
Members []struct {
Add []int `json:"add,omitempty"`
Remove []int `json:"rem,omitempty"`
} `json:"members,omitempty"`
}
type UpdateGroupResponse struct {
ID string `json:"id"`
TeamID string `json:"team_id"`
Userid int `json:"userid"`
Name string `json:"name"`
Handle string `json:"handle"`
DateCreated string `json:"date_created"`
Initials string `json:"initials"`
Members []TeamUser `json:"members"`
}
// UpdateGroup changes an existing group using group.ID.
func (c *Client) UpdateGroup(ctx context.Context, group UpdateGroupRequest) (*UpdateGroupResponse, error) {
if group.ID == "" {
return nil, fmt.Errorf("must provide an ID: %w", ErrValidation)
}
b, err := json.Marshal(group)
if err != nil {
return nil, fmt.Errorf("unable to serialize new group: %w", err)
}
buf := bytes.NewBuffer(b)
endpoint := fmt.Sprintf("/group/%s", group.ID)
var updatedGroup UpdateGroupResponse
if err := c.call(ctx, http.MethodPut, endpoint, buf, &updatedGroup); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &updatedGroup, nil
}
// DeleteGroup removes an existing group with an id of groupID.
func (c *Client) DeleteGroup(ctx context.Context, groupID string) error {
if groupID == "" {
return fmt.Errorf("must provide a group id to delete: %w", ErrValidation)
}
return c.call(ctx, http.MethodDelete, fmt.Sprintf("/group/%s", groupID), nil, &struct{}{})
}