2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-20 14:50:47 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-31 14:11:53 +00:00
|
|
|
"strings"
|
2023-06-08 00:16:53 +01:00
|
|
|
"time"
|
2018-11-20 14:50:47 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2020-01-15 02:29:51 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
2018-11-20 14:50:47 +00:00
|
|
|
)
|
|
|
|
|
2019-11-04 14:37:39 +00:00
|
|
|
// ensures that projectMembers implements console.ProjectMembers.
|
|
|
|
var _ console.ProjectMembers = (*projectMembers)(nil)
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// ProjectMembers exposes methods to manage ProjectMembers table in database.
|
|
|
|
type projectMembers struct {
|
2018-12-28 12:07:35 +00:00
|
|
|
methods dbx.Methods
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetByMemberID is a method for querying project member from the database by memberID.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (pm *projectMembers) GetByMemberID(ctx context.Context, memberID uuid.UUID) (_ []console.ProjectMember, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-28 12:07:35 +00:00
|
|
|
projectMembersDbx, err := pm.methods.All_ProjectMember_By_MemberId(ctx, dbx.ProjectMember_MemberId(memberID[:]))
|
2018-11-20 14:50:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectMembersFromDbxSlice(ctx, projectMembersDbx)
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-08 00:16:53 +01:00
|
|
|
// 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 (
|
2023-06-13 13:34:41 +01:00
|
|
|
memberID uuid.NullUUID
|
2023-06-08 00:16:53 +01:00
|
|
|
projectID uuid.UUID
|
|
|
|
createdAt time.Time
|
|
|
|
email string
|
2023-06-13 13:34:41 +01:00
|
|
|
inviterID uuid.NullUUID
|
2023-06-08 00:16:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// Insert is a method for inserting project member into the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (pm *projectMembers) Insert(ctx context.Context, memberID, projectID uuid.UUID) (_ *console.ProjectMember, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-28 12:07:35 +00:00
|
|
|
createdProjectMember, err := pm.methods.Create_ProjectMember(ctx,
|
2018-11-20 14:50:47 +00:00
|
|
|
dbx.ProjectMember_MemberId(memberID[:]),
|
|
|
|
dbx.ProjectMember_ProjectId(projectID[:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectMemberFromDBX(ctx, createdProjectMember)
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 12:29:01 +00:00
|
|
|
// Delete is a method for deleting project member by memberID and projectID from the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
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(
|
2018-12-10 12:29:01 +00:00
|
|
|
ctx,
|
|
|
|
dbx.ProjectMember_MemberId(memberID[:]),
|
|
|
|
dbx.ProjectMember_ProjectId(projectID[:]),
|
|
|
|
)
|
2018-11-20 14:50:47 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// projectMemberFromDBX is used for creating ProjectMember entity from autogenerated dbx.ProjectMember struct.
|
2019-06-04 12:55:38 +01:00
|
|
|
func projectMemberFromDBX(ctx context.Context, projectMember *dbx.ProjectMember) (_ *console.ProjectMember, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-20 14:50:47 +00:00
|
|
|
if projectMember == nil {
|
|
|
|
return nil, errs.New("projectMember parameter is nil")
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:49:16 +01:00
|
|
|
memberID, err := uuid.FromBytes(projectMember.MemberId)
|
2018-11-20 14:50:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:49:16 +01:00
|
|
|
projectID, err := uuid.FromBytes(projectMember.ProjectId)
|
2018-11-20 14:50:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
return &console.ProjectMember{
|
2018-11-20 14:50:47 +00:00
|
|
|
MemberID: memberID,
|
|
|
|
ProjectID: projectID,
|
|
|
|
CreatedAt: projectMember.CreatedAt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-06-08 00:16:53 +01:00
|
|
|
// 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)"
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// projectMembersFromDbxSlice is used for creating []ProjectMember entities from autogenerated []*dbx.ProjectMember struct.
|
2019-06-04 12:55:38 +01:00
|
|
|
func projectMembersFromDbxSlice(ctx context.Context, projectMembersDbx []*dbx.ProjectMember) (_ []console.ProjectMember, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2023-06-01 13:32:11 +01:00
|
|
|
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...)
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|