Skip to content

Commit 908a8e6

Browse files
authored
chore(controlplane): continue invite system org scope (#555)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent ef06a7c commit 908a8e6

File tree

7 files changed

+29
-38
lines changed

7 files changed

+29
-38
lines changed

app/controlplane/api/controlplane/v1/org_invitation.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ service OrgInvitationService {
2828
rpc Create(OrgInvitationServiceCreateRequest) returns (OrgInvitationServiceCreateResponse);
2929
// Revoke an invitation.
3030
rpc Revoke(OrgInvitationServiceRevokeRequest) returns (OrgInvitationServiceRevokeResponse);
31-
// List all invitations sent by the current user.
31+
// List all invitations in the current org
3232
rpc ListSent(OrgInvitationServiceListSentRequest) returns (OrgInvitationServiceListSentResponse);
3333
}
3434

app/controlplane/api/controlplane/v1/org_invitation_grpc.pb.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/api/gen/frontend/controlplane/v1/org_invitation.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/internal/biz/orginvitation.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type OrgInvitationRepo interface {
4848
PendingInvitation(ctx context.Context, orgID uuid.UUID, receiverEmail string) (*OrgInvitation, error)
4949
PendingInvitations(ctx context.Context, receiverEmail string) ([]*OrgInvitation, error)
5050
SoftDelete(ctx context.Context, id uuid.UUID) error
51-
ListBySenderAndOrg(ctx context.Context, sender, org uuid.UUID) ([]*OrgInvitation, error)
51+
ListByOrg(ctx context.Context, org uuid.UUID) ([]*OrgInvitation, error)
5252
ChangeStatus(ctx context.Context, ID uuid.UUID, status OrgInvitationStatus) error
5353
}
5454

@@ -126,32 +126,27 @@ func (uc *OrgInvitationUseCase) Create(ctx context.Context, orgID, senderID, rec
126126
return invitation, nil
127127
}
128128

129-
func (uc *OrgInvitationUseCase) ListBySenderAndOrg(ctx context.Context, senderID, orgID string) ([]*OrgInvitation, error) {
130-
senderUUID, err := uuid.Parse(senderID)
131-
if err != nil {
132-
return nil, NewErrInvalidUUID(err)
133-
}
134-
129+
func (uc *OrgInvitationUseCase) ListByOrg(ctx context.Context, orgID string) ([]*OrgInvitation, error) {
135130
orgUUID, err := uuid.Parse(orgID)
136131
if err != nil {
137132
return nil, NewErrInvalidUUID(err)
138133
}
139134

140-
return uc.repo.ListBySenderAndOrg(ctx, senderUUID, orgUUID)
135+
return uc.repo.ListByOrg(ctx, orgUUID)
141136
}
142137

143138
// Revoke an invitation by ID only if the user is the one who created it
144-
func (uc *OrgInvitationUseCase) Revoke(ctx context.Context, senderID, invitationID string) error {
139+
func (uc *OrgInvitationUseCase) Revoke(ctx context.Context, orgID, invitationID string) error {
145140
invitationUUID, err := uuid.Parse(invitationID)
146141
if err != nil {
147142
return NewErrInvalidUUID(err)
148143
}
149144

150-
// We care only about invitations that are pending and sent by the user
145+
// We care only about pending invitations in the given org
151146
m, err := uc.repo.FindByID(ctx, invitationUUID)
152147
if err != nil {
153148
return fmt.Errorf("error finding invitation %s: %w", invitationID, err)
154-
} else if m == nil || m.Sender.ID != senderID {
149+
} else if m == nil || m.Org.ID != orgID {
155150
return NewErrNotFound("invitation")
156151
}
157152

app/controlplane/internal/biz/orginvitation_integration_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ func (s *OrgInvitationIntegrationTestSuite) TestList() {
3434
ctx := context.Background()
3535
inviteOrg1A, err := s.OrgInvitation.Create(ctx, s.org1.ID, s.user.ID, receiverEmail)
3636
s.NoError(err)
37-
inviteOrg1B, err := s.OrgInvitation.Create(ctx, s.org1.ID, s.user.ID, "[email protected]")
37+
// same org but another user
38+
inviteOrg1B, err := s.OrgInvitation.Create(ctx, s.org1.ID, s.user2.ID, "[email protected]")
3839
s.NoError(err)
3940
inviteOrg2A, err := s.OrgInvitation.Create(ctx, s.org2.ID, s.user.ID, receiverEmail)
4041
s.NoError(err)
@@ -63,7 +64,7 @@ func (s *OrgInvitationIntegrationTestSuite) TestList() {
6364

6465
for _, tc := range testCases {
6566
s.T().Run(tc.name, func(t *testing.T) {
66-
invites, err := s.OrgInvitation.ListBySenderAndOrg(ctx, s.user.ID, tc.orgID)
67+
invites, err := s.OrgInvitation.ListByOrg(ctx, tc.orgID)
6768
s.NoError(err)
6869
s.Equal(tc.expected, invites)
6970
})
@@ -198,21 +199,21 @@ func (s *OrgInvitationIntegrationTestSuite) TestAcceptPendingInvitations() {
198199

199200
func (s *OrgInvitationIntegrationTestSuite) TestRevoke() {
200201
s.T().Run("invalid ID", func(t *testing.T) {
201-
err := s.OrgInvitation.Revoke(context.Background(), s.user.ID, "deadbeef")
202+
err := s.OrgInvitation.Revoke(context.Background(), s.org1.ID, "deadbeef")
202203
s.Error(err)
203204
s.True(biz.IsErrInvalidUUID(err))
204205
})
205206

206207
s.T().Run("invitation not found", func(t *testing.T) {
207-
err := s.OrgInvitation.Revoke(context.Background(), s.user.ID, uuid.NewString())
208+
err := s.OrgInvitation.Revoke(context.Background(), s.org1.ID, uuid.NewString())
208209
s.Error(err)
209210
s.True(biz.IsNotFound(err))
210211
})
211212

212-
s.T().Run("invitation not created by this user", func(t *testing.T) {
213-
invite, err := s.OrgInvitation.Create(context.Background(), s.org1.ID, s.user2.ID, "[email protected]")
214-
require.NoError(s.T(), err)
215-
err = s.OrgInvitation.Revoke(context.Background(), s.user.ID, invite.ID.String())
213+
s.T().Run("invitation in another org", func(t *testing.T) {
214+
_, err := s.OrgInvitation.Create(context.Background(), s.org2.ID, s.user.ID, receiverEmail)
215+
s.NoError(err)
216+
err = s.OrgInvitation.Revoke(context.Background(), s.org1.ID, uuid.NewString())
216217
s.Error(err)
217218
s.True(biz.IsNotFound(err))
218219
})
@@ -223,8 +224,8 @@ func (s *OrgInvitationIntegrationTestSuite) TestRevoke() {
223224
err = s.OrgInvitation.AcceptInvitation(context.Background(), invite.ID.String())
224225
require.NoError(s.T(), err)
225226

226-
// It's in accepted state now
227-
err = s.OrgInvitation.Revoke(context.Background(), s.user.ID, invite.ID.String())
227+
// It's in accepted state now so it can not be revoked
228+
err = s.OrgInvitation.Revoke(context.Background(), s.org1.ID, invite.ID.String())
228229
s.Error(err)
229230
s.ErrorContains(err, "not in pending state")
230231
s.True(biz.IsErrValidation(err))
@@ -233,9 +234,9 @@ func (s *OrgInvitationIntegrationTestSuite) TestRevoke() {
233234
s.T().Run("happy path", func(t *testing.T) {
234235
invite, err := s.OrgInvitation.Create(context.Background(), s.org1.ID, s.user.ID, receiverEmail)
235236
require.NoError(s.T(), err)
236-
err = s.OrgInvitation.Revoke(context.Background(), s.user.ID, invite.ID.String())
237+
err = s.OrgInvitation.Revoke(context.Background(), s.org1.ID, invite.ID.String())
237238
s.NoError(err)
238-
err = s.OrgInvitation.Revoke(context.Background(), s.user.ID, invite.ID.String())
239+
err = s.OrgInvitation.Revoke(context.Background(), s.org1.ID, invite.ID.String())
239240
s.Error(err)
240241
s.True(biz.IsNotFound(err))
241242
})

app/controlplane/internal/data/orginvitation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ func (r *OrgInvitation) SoftDelete(ctx context.Context, id uuid.UUID) error {
110110
return r.data.db.OrgInvitation.UpdateOneID(id).SetDeletedAt(time.Now()).Exec(ctx)
111111
}
112112

113-
func (r *OrgInvitation) ListBySenderAndOrg(ctx context.Context, userID, orgID uuid.UUID) ([]*biz.OrgInvitation, error) {
114-
invite, err := r.query().Where(orginvitation.SenderID(userID), orginvitation.OrganizationID(orgID)).All(ctx)
113+
func (r *OrgInvitation) ListByOrg(ctx context.Context, orgID uuid.UUID) ([]*biz.OrgInvitation, error) {
114+
invite, err := r.query().Where(orginvitation.OrganizationID(orgID)).All(ctx)
115115
if err != nil {
116-
return nil, fmt.Errorf("error finding invites for user %s: %w", userID.String(), err)
116+
return nil, fmt.Errorf("error finding invites for org %s: %w", orgID.String(), err)
117117
}
118118

119119
res := make([]*biz.OrgInvitation, len(invite))

app/controlplane/internal/service/orginvitation.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,25 @@ func (s *OrgInvitationService) Create(ctx context.Context, req *pb.OrgInvitation
5858
}
5959

6060
func (s *OrgInvitationService) Revoke(ctx context.Context, req *pb.OrgInvitationServiceRevokeRequest) (*pb.OrgInvitationServiceRevokeResponse, error) {
61-
user, err := requireCurrentUser(ctx)
61+
org, err := requireCurrentOrg(ctx)
6262
if err != nil {
6363
return nil, err
6464
}
6565

66-
if err := s.useCase.Revoke(ctx, user.ID, req.Id); err != nil {
66+
if err := s.useCase.Revoke(ctx, org.ID, req.Id); err != nil {
6767
return nil, handleUseCaseErr("invitation", err, s.log)
6868
}
6969

7070
return &pb.OrgInvitationServiceRevokeResponse{}, nil
7171
}
7272

7373
func (s *OrgInvitationService) ListSent(ctx context.Context, _ *pb.OrgInvitationServiceListSentRequest) (*pb.OrgInvitationServiceListSentResponse, error) {
74-
user, err := requireCurrentUser(ctx)
75-
if err != nil {
76-
return nil, err
77-
}
78-
7974
org, err := requireCurrentOrg(ctx)
8075
if err != nil {
8176
return nil, err
8277
}
8378

84-
invitations, err := s.useCase.ListBySenderAndOrg(ctx, user.ID, org.ID)
79+
invitations, err := s.useCase.ListByOrg(ctx, org.ID)
8580
if err != nil {
8681
return nil, handleUseCaseErr("invitation", err, s.log)
8782
}

0 commit comments

Comments
 (0)