-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathteam_members.go
169 lines (140 loc) · 4.28 KB
/
team_members.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
package latitude
import "path"
const memberBasePath = "/team/members"
// MemberService interface defines available member methods
type MemberService interface {
List(listOpt *ListOptions) ([]Member, *Response, error)
Create(request *MemberCreateRequest) (*Member, *Response, error)
Delete(UserID string) (*Response, error)
}
// MemberServiceOp implements MemberService
type MemberServiceOp struct {
client requestDoer
}
type MemberListResponse struct {
Data []MemberListData `json:"data"`
Meta meta `json:"meta"`
}
type MemberListData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes MemberListAttributes `json:"attributes"`
}
type MemberListAttributes struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
MfaEnabled bool `json:"mfa_enabled"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Role Role `json:"role"`
}
type MemberResponse struct {
Data MemberData `json:"data"`
Meta meta `json:"meta"`
}
type MemberData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes MemberAttributes `json:"attributes"`
}
type MemberAttributes struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
MfaEnabled bool `json:"mfa_enabled"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
RoleName string `json:"role"`
}
type MemberCreateRequest struct {
Data MemberCreateData `json:"data"`
}
type MemberCreateData struct {
Type string `json:"type"`
Attributes MemberCreateAttributes `json:"attributes"`
}
type MemberCreateAttributes struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Role AvailableRole `json:"role"`
}
type Member struct {
ID string `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
MfaEnabled bool `json:"mfa_enabled"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
RoleName string `json:"role"`
}
// Flatten latitude API data structure
func NewFlatMember(md MemberData) Member {
return Member{
md.ID,
md.Attributes.FirstName,
md.Attributes.LastName,
md.Attributes.Email,
md.Attributes.MfaEnabled,
md.Attributes.CreatedAt,
md.Attributes.UpdatedAt,
md.Attributes.RoleName,
}
}
func NewFlatMemberList(md []MemberData) []Member {
var members []Member
for _, member := range md {
members = append(members, NewFlatMember(member))
}
return members
}
// List returns a list of team members
func (s *MemberServiceOp) List(listOpts *ListOptions) (members []Member, resp *Response, err error) {
apiPathQuery := listOpts.WithQuery(memberBasePath)
for {
res := new(MemberListResponse)
membersData := []MemberData{}
resp, err = s.client.DoRequest("GET", apiPathQuery, nil, res)
if err != nil {
return nil, resp, err
}
for _, data := range res.Data {
mD := MemberData{
ID: data.ID,
Type: data.Type,
Attributes: MemberAttributes{
FirstName: data.Attributes.FirstName,
LastName: data.Attributes.LastName,
Email: data.Attributes.Email,
MfaEnabled: data.Attributes.MfaEnabled,
CreatedAt: data.Attributes.CreatedAt,
UpdatedAt: data.Attributes.UpdatedAt,
RoleName: data.Attributes.Role.Name,
},
}
membersData = append(membersData, mD)
}
members = append(members, NewFlatMemberList(membersData)...)
if apiPathQuery = nextPage(res.Meta, listOpts); apiPathQuery != "" {
continue
}
return
}
}
// Create creates a new team member
func (s *MemberServiceOp) Create(request *MemberCreateRequest) (*Member, *Response, error) {
member := new(MemberResponse)
resp, err := s.client.DoRequest("POST", memberBasePath, request, member)
if err != nil {
return nil, resp, err
}
flatMember := NewFlatMember(member.Data)
return &flatMember, resp, err
}
// Delete deletes a team member
func (s *MemberServiceOp) Delete(MemberID string) (*Response, error) {
apiPath := path.Join(memberBasePath, MemberID)
return s.client.DoRequest("DELETE", apiPath, nil, nil)
}