-
Notifications
You must be signed in to change notification settings - Fork 387
/
projectinvitations.go
134 lines (107 loc) · 4.18 KB
/
projectinvitations.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
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"storj.io/common/uuid"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/dbx"
)
// Ensure that projectInvitations implements console.ProjectInvitations.
var _ console.ProjectInvitations = (*projectInvitations)(nil)
// projectInvitations is an implementation of console.ProjectInvitations.
type projectInvitations struct {
db dbx.Methods
}
// Upsert updates a project member invitation if it exists and inserts it otherwise.
func (invites *projectInvitations) Upsert(ctx context.Context, invite *console.ProjectInvitation) (_ *console.ProjectInvitation, err error) {
defer mon.Task()(&ctx)(&err)
if invite == nil {
return nil, Error.New("invitation is nil")
}
createFields := dbx.ProjectInvitation_Create_Fields{}
if invite.InviterID != nil {
id := invite.InviterID[:]
createFields.InviterId = dbx.ProjectInvitation_InviterId(id)
}
dbxInvite, err := invites.db.Replace_ProjectInvitation(ctx,
dbx.ProjectInvitation_ProjectId(invite.ProjectID[:]),
dbx.ProjectInvitation_Email(normalizeEmail(invite.Email)),
createFields,
)
if err != nil {
return nil, err
}
return projectInvitationFromDBX(dbxInvite)
}
// Get returns a project member invitation from the database.
func (invites *projectInvitations) Get(ctx context.Context, projectID uuid.UUID, email string) (_ *console.ProjectInvitation, err error) {
defer mon.Task()(&ctx)(&err)
dbxInvite, err := invites.db.Get_ProjectInvitation_By_ProjectId_And_Email(ctx,
dbx.ProjectInvitation_ProjectId(projectID[:]),
dbx.ProjectInvitation_Email(normalizeEmail(email)),
)
if err != nil {
return nil, err
}
return projectInvitationFromDBX(dbxInvite)
}
// GetByProjectID returns all of the project member invitations for the project specified by the given ID.
func (invites *projectInvitations) GetByProjectID(ctx context.Context, projectID uuid.UUID) (_ []console.ProjectInvitation, err error) {
defer mon.Task()(&ctx)(&err)
dbxInvites, err := invites.db.All_ProjectInvitation_By_ProjectId(ctx, dbx.ProjectInvitation_ProjectId(projectID[:]))
if err != nil {
return nil, err
}
return projectInvitationSliceFromDBX(dbxInvites)
}
// GetByEmail returns all of the project member invitations for the specified email address.
func (invites *projectInvitations) GetByEmail(ctx context.Context, email string) (_ []console.ProjectInvitation, err error) {
defer mon.Task()(&ctx)(&err)
dbxInvites, err := invites.db.All_ProjectInvitation_By_Email(ctx, dbx.ProjectInvitation_Email(normalizeEmail(email)))
if err != nil {
return nil, err
}
return projectInvitationSliceFromDBX(dbxInvites)
}
// Delete removes a project member invitation from the database.
func (invites *projectInvitations) Delete(ctx context.Context, projectID uuid.UUID, email string) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = invites.db.Delete_ProjectInvitation_By_ProjectId_And_Email(ctx,
dbx.ProjectInvitation_ProjectId(projectID[:]),
dbx.ProjectInvitation_Email(normalizeEmail(email)),
)
return err
}
// projectInvitationFromDBX converts a project member invitation from the database to a *console.ProjectInvitation.
func projectInvitationFromDBX(dbxInvite *dbx.ProjectInvitation) (_ *console.ProjectInvitation, err error) {
if dbxInvite == nil {
return nil, Error.New("dbx invitation is nil")
}
invite := &console.ProjectInvitation{
Email: dbxInvite.Email,
CreatedAt: dbxInvite.CreatedAt,
}
projectID, err := uuid.FromBytes(dbxInvite.ProjectId)
if err != nil {
return nil, err
}
invite.ProjectID = projectID
if dbxInvite.InviterId != nil {
inviterID, err := uuid.FromBytes(dbxInvite.InviterId)
if err != nil {
return nil, err
}
invite.InviterID = &inviterID
}
return invite, nil
}
// projectInvitationSliceFromDBX converts a project member invitation slice from the database to a
// slice of console.ProjectInvitation.
func projectInvitationSliceFromDBX(dbxInvites []*dbx.ProjectInvitation) (invites []console.ProjectInvitation, err error) {
return convertSlice(dbxInvites,
func(i *dbx.ProjectInvitation) (console.ProjectInvitation, error) {
r, err := projectInvitationFromDBX(i)
return *r, err
})
}