-
Notifications
You must be signed in to change notification settings - Fork 25
/
orginvitation.go
148 lines (122 loc) · 4.2 KB
/
orginvitation.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
//
// Copyright 2023 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package data
import (
"context"
"fmt"
"time"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/biz"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent/orginvitation"
"github.com/go-kratos/kratos/v2/log"
"github.com/google/uuid"
)
type OrgInvitation struct {
data *Data
log *log.Helper
}
func NewOrgInvitation(data *Data, logger log.Logger) biz.OrgInvitationRepo {
return &OrgInvitation{
data: data,
log: log.NewHelper(logger),
}
}
func (r *OrgInvitation) Create(ctx context.Context, orgID, senderID uuid.UUID, receiverEmail string) (*biz.OrgInvitation, error) {
invite, err := r.data.db.OrgInvitation.Create().
SetOrganizationID(orgID).
SetSenderID(senderID).
SetReceiverEmail(receiverEmail).
Save(ctx)
if err != nil {
return nil, err
}
return r.FindByID(ctx, invite.ID)
}
func (r *OrgInvitation) PendingInvitation(ctx context.Context, orgID uuid.UUID, receiverEmail string) (*biz.OrgInvitation, error) {
invite, err := r.query().
Where(
orginvitation.OrganizationID(orgID),
orginvitation.ReceiverEmail(receiverEmail),
orginvitation.StatusEQ(biz.OrgInvitationStatusPending),
).Only(ctx)
if err != nil && !ent.IsNotFound(err) {
return nil, err
} else if invite == nil {
return nil, nil
}
return entInviteToBiz(invite), nil
}
func (r *OrgInvitation) PendingInvitations(ctx context.Context, receiverEmail string) ([]*biz.OrgInvitation, error) {
invites, err := r.query().Where(
orginvitation.ReceiverEmail(receiverEmail),
orginvitation.StatusEQ(biz.OrgInvitationStatusPending),
).All(ctx)
if err != nil {
return nil, fmt.Errorf("error finding invites for user %s: %w", receiverEmail, err)
}
res := make([]*biz.OrgInvitation, len(invites))
for i, v := range invites {
res[i] = entInviteToBiz(v)
}
return res, nil
}
func (r *OrgInvitation) ChangeStatus(ctx context.Context, id uuid.UUID, status biz.OrgInvitationStatus) error {
return r.data.db.OrgInvitation.UpdateOneID(id).SetStatus(status).Exec(ctx)
}
// Full query of non-deleted invites with all edges loaded
func (r *OrgInvitation) query() *ent.OrgInvitationQuery {
return r.data.db.OrgInvitation.Query().WithOrganization().WithSender().Where(orginvitation.DeletedAtIsNil())
}
func (r *OrgInvitation) FindByID(ctx context.Context, id uuid.UUID) (*biz.OrgInvitation, error) {
invite, err := r.query().Where(orginvitation.ID(id)).Only(ctx)
if err != nil && !ent.IsNotFound(err) {
return nil, fmt.Errorf("error finding invite %s: %w", id.String(), err)
} else if invite == nil {
return nil, nil
}
return entInviteToBiz(invite), nil
}
func (r *OrgInvitation) SoftDelete(ctx context.Context, id uuid.UUID) error {
return r.data.db.OrgInvitation.UpdateOneID(id).SetDeletedAt(time.Now()).Exec(ctx)
}
func (r *OrgInvitation) ListBySender(ctx context.Context, userID uuid.UUID) ([]*biz.OrgInvitation, error) {
invite, err := r.query().Where(orginvitation.SenderID(userID)).All(ctx)
if err != nil {
return nil, fmt.Errorf("error finding invites for user %s: %w", userID.String(), err)
}
res := make([]*biz.OrgInvitation, len(invite))
for i, v := range invite {
res[i] = entInviteToBiz(v)
}
return res, nil
}
func entInviteToBiz(i *ent.OrgInvitation) *biz.OrgInvitation {
if i == nil {
return nil
}
res := &biz.OrgInvitation{
ID: i.ID,
ReceiverEmail: i.ReceiverEmail,
CreatedAt: toTimePtr(i.CreatedAt),
Status: i.Status,
}
if i.Edges.Organization != nil {
res.Org = entOrgToBizOrg(i.Edges.Organization)
}
if i.Edges.Sender != nil {
res.Sender = entUserToBizUser(i.Edges.Sender)
}
return res
}