-
Notifications
You must be signed in to change notification settings - Fork 393
/
projectmembers.go
264 lines (227 loc) · 6.92 KB
/
projectmembers.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"strings"
"time"
"github.com/zeebo/errs"
"storj.io/common/uuid"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/dbx"
)
// ensures that projectMembers implements console.ProjectMembers.
var _ console.ProjectMembers = (*projectMembers)(nil)
// ProjectMembers exposes methods to manage ProjectMembers table in database.
type projectMembers struct {
methods dbx.Methods
db *satelliteDB
}
// GetByMemberID is a method for querying project member from the database by memberID.
func (pm *projectMembers) GetByMemberID(ctx context.Context, memberID uuid.UUID) (_ []console.ProjectMember, err error) {
defer mon.Task()(&ctx)(&err)
projectMembersDbx, err := pm.methods.All_ProjectMember_By_MemberId(ctx, dbx.ProjectMember_MemberId(memberID[:]))
if err != nil {
return nil, err
}
return projectMembersFromDbxSlice(ctx, projectMembersDbx)
}
// GetPagedWithInvitationsByProjectID is a method for querying project members and invitations from the database by projectID, offset and limit.
func (pm *projectMembers) GetPagedWithInvitationsByProjectID(ctx context.Context, projectID uuid.UUID, cursor console.ProjectMembersCursor) (_ *console.ProjectMembersPage, err error) {
defer mon.Task()(&ctx)(&err)
search := "%" + strings.ReplaceAll(cursor.Search, " ", "%") + "%"
if cursor.Limit > 50 {
cursor.Limit = 50
}
if cursor.Limit == 0 {
return nil, errs.New("limit cannot be 0")
}
if cursor.Page == 0 {
return nil, errs.New("page cannot be 0")
}
page := &console.ProjectMembersPage{
Search: cursor.Search,
Limit: cursor.Limit,
Offset: uint64((cursor.Page - 1) * cursor.Limit),
Order: cursor.Order,
OrderDirection: cursor.OrderDirection,
}
countQuery := `
SELECT (
SELECT COUNT(*)
FROM project_members pm
INNER JOIN users u ON pm.member_id = u.id
WHERE pm.project_id = $1
AND (
u.email ILIKE $2 OR
u.full_name ILIKE $2 OR
u.short_name ILIKE $2
)
) + (
SELECT COUNT(*)
FROM project_invitations
WHERE project_id = $1
AND email ILIKE $2
)`
countRow := pm.db.QueryRowContext(ctx,
countQuery,
projectID[:],
search)
err = countRow.Scan(&page.TotalCount)
if err != nil {
return nil, err
}
if page.TotalCount == 0 {
return page, nil
}
if page.Offset > page.TotalCount-1 {
return nil, errs.New("page is out of range")
}
membersQuery := `
SELECT member_id, project_id, created_at, email, inviter_id FROM (
(
SELECT pm.member_id, pm.project_id, pm.created_at, u.email, u.full_name, NULL as inviter_id
FROM project_members pm
INNER JOIN users u ON pm.member_id = u.id
WHERE pm.project_id = $1
AND (
u.email ILIKE $2 OR
u.full_name ILIKE $2 OR
u.short_name ILIKE $2
)
) UNION ALL (
SELECT NULL as member_id, project_id, created_at, LOWER(email) as email, LOWER(SPLIT_PART(email, '@', 1)) as full_name, inviter_id
FROM project_invitations pi
WHERE project_id = $1
AND email ILIKE $2
)
) results
` + projectMembersSortClause(cursor.Order, page.OrderDirection) + `
LIMIT $3 OFFSET $4`
rows, err := pm.db.QueryContext(ctx,
membersQuery,
projectID[:],
search,
page.Limit,
page.Offset,
)
if err != nil {
return nil, err
}
defer func() {
err = errs.Combine(err, rows.Close())
}()
for rows.Next() {
var (
memberID uuid.NullUUID
projectID uuid.UUID
createdAt time.Time
email string
inviterID uuid.NullUUID
)
err = rows.Scan(
&memberID,
&projectID,
&createdAt,
&email,
&inviterID,
)
if err != nil {
return nil, err
}
if memberID.Valid {
page.ProjectMembers = append(page.ProjectMembers, console.ProjectMember{
MemberID: memberID.UUID,
ProjectID: projectID,
CreatedAt: createdAt,
})
} else {
invite := console.ProjectInvitation{
ProjectID: projectID,
Email: email,
CreatedAt: createdAt,
}
if inviterID.Valid {
invite.InviterID = &inviterID.UUID
}
page.ProjectInvitations = append(page.ProjectInvitations, invite)
}
}
if err := rows.Err(); err != nil {
return nil, err
}
page.PageCount = uint(page.TotalCount / uint64(cursor.Limit))
if page.TotalCount%uint64(cursor.Limit) != 0 {
page.PageCount++
}
page.CurrentPage = cursor.Page
return page, err
}
// Insert is a method for inserting project member into the database.
func (pm *projectMembers) Insert(ctx context.Context, memberID, projectID uuid.UUID) (_ *console.ProjectMember, err error) {
defer mon.Task()(&ctx)(&err)
createdProjectMember, err := pm.methods.Create_ProjectMember(ctx,
dbx.ProjectMember_MemberId(memberID[:]),
dbx.ProjectMember_ProjectId(projectID[:]))
if err != nil {
return nil, err
}
return projectMemberFromDBX(ctx, createdProjectMember)
}
// Delete is a method for deleting project member by memberID and projectID from the database.
func (pm *projectMembers) Delete(ctx context.Context, memberID, projectID uuid.UUID) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = pm.methods.Delete_ProjectMember_By_MemberId_And_ProjectId(
ctx,
dbx.ProjectMember_MemberId(memberID[:]),
dbx.ProjectMember_ProjectId(projectID[:]),
)
return err
}
// projectMemberFromDBX is used for creating ProjectMember entity from autogenerated dbx.ProjectMember struct.
func projectMemberFromDBX(ctx context.Context, projectMember *dbx.ProjectMember) (_ *console.ProjectMember, err error) {
defer mon.Task()(&ctx)(&err)
if projectMember == nil {
return nil, errs.New("projectMember parameter is nil")
}
memberID, err := uuid.FromBytes(projectMember.MemberId)
if err != nil {
return nil, err
}
projectID, err := uuid.FromBytes(projectMember.ProjectId)
if err != nil {
return nil, err
}
return &console.ProjectMember{
MemberID: memberID,
ProjectID: projectID,
CreatedAt: projectMember.CreatedAt,
}, nil
}
// projectMembersSortClause returns what ORDER BY clause should be used when sorting project member results.
func projectMembersSortClause(order console.ProjectMemberOrder, direction console.OrderDirection) string {
dirStr := "ASC"
if direction == console.Descending {
dirStr = "DESC"
}
switch order {
case console.Email:
return "ORDER BY LOWER(email) " + dirStr
case console.Created:
return "ORDER BY created_at " + dirStr + ", LOWER(email)"
}
return "ORDER BY LOWER(full_name) " + dirStr + ", LOWER(email)"
}
// projectMembersFromDbxSlice is used for creating []ProjectMember entities from autogenerated []*dbx.ProjectMember struct.
func projectMembersFromDbxSlice(ctx context.Context, projectMembersDbx []*dbx.ProjectMember) (_ []console.ProjectMember, err error) {
defer mon.Task()(&ctx)(&err)
rs, errors := convertSliceWithErrors(projectMembersDbx,
func(v *dbx.ProjectMember) (r console.ProjectMember, _ error) {
p, err := projectMemberFromDBX(ctx, v)
if err != nil {
return r, err
}
return *p, err
})
return rs, errs.Combine(errors...)
}