Skip to content

Commit

Permalink
fix: http code if user already member in invite
Browse files Browse the repository at this point in the history
Service won't return 5xx if a user is already a member of
organization while creating the invite.

Signed-off-by: Kush Sharma <thekushsharma@gmail.com>
  • Loading branch information
kushsharma committed Sep 23, 2024
1 parent f0d6eea commit cc8c57c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions core/invitation/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

var (
ErrNotFound = errors.New("invitation not found")
InviteExpired = errors.New("invitation expired")
ErrNotFound = errors.New("invitation not found")
ErrInviteExpired = errors.New("invitation expired")
ErrAlreadyMember = errors.New("user already exists in organization")
)

const (
Expand Down
4 changes: 2 additions & 2 deletions core/invitation/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (s Service) Create(ctx context.Context, inviteToCreate Invitation) (Invitat
return Invitation{}, err
}
if userOrgMember {
return Invitation{}, fmt.Errorf("user %s is already a member of organization %s", inviteToCreate.UserEmailID, inviteToCreate.OrgID)
return Invitation{}, fmt.Errorf("%w: user: %s, organization: %s", ErrAlreadyMember, inviteToCreate.UserEmailID, inviteToCreate.OrgID)
}

// before creating a new invite check if user has already an active invite
Expand Down Expand Up @@ -282,7 +282,7 @@ func (s Service) Accept(ctx context.Context, id uuid.UUID) error {
return err
}
if invite.ExpiresAt.Before(time.Now()) {
return InviteExpired
return ErrInviteExpired
}

// check if user is already a member of the organization
Expand Down
5 changes: 4 additions & 1 deletion internal/api/v1beta1/invitations.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func (h Handler) CreateOrganizationInvitation(ctx context.Context, request *fron
GroupIDs: request.GetGroupIds(),
})
if err != nil {
if errors.Is(err, invitation.ErrAlreadyMember) {
return nil, status.Errorf(codes.AlreadyExists, err.Error())
}
return nil, status.Errorf(codes.Internal, err.Error())
}
createdInvitations = append(createdInvitations, inv)
Expand Down Expand Up @@ -222,7 +225,7 @@ func (h Handler) AcceptOrganizationInvitation(ctx context.Context, request *fron

if err := h.invitationService.Accept(ctx, inviteID); err != nil {
switch {
case errors.Is(err, invitation.InviteExpired):
case errors.Is(err, invitation.ErrInviteExpired):
return nil, grpcInvitationExpiredError
case errors.Is(err, invitation.ErrNotFound):
return nil, grpcInvitationNotFoundError
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v1beta1/invitations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ func TestHandler_AcceptOrganizationInvitation(t *testing.T) {
name: "should return error if invitation is expired",
setup: func(is *mocks.InvitationService, us *mocks.UserService, gs *mocks.GroupService, os *mocks.OrganizationService) {
os.EXPECT().Get(mock.AnythingOfType("context.backgroundCtx"), testOrgID).Return(testOrgMap[testOrgID], nil)
is.EXPECT().Accept(mock.AnythingOfType("context.backgroundCtx"), testInvitation3ID).Return(invitation.InviteExpired)
is.EXPECT().Accept(mock.AnythingOfType("context.backgroundCtx"), testInvitation3ID).Return(invitation.ErrInviteExpired)
},
request: &frontierv1beta1.AcceptOrganizationInvitationRequest{
Id: testInvitation3ID.String(),
Expand Down

0 comments on commit cc8c57c

Please sign in to comment.