Skip to content

Commit

Permalink
fix(approvers): duplicated approver is not allowed (#135)
Browse files Browse the repository at this point in the history
* fix(approvers): duplicated approver is not allowed

* test: adding unit test

* chore: update error log

Co-authored-by: Rahmat Hidayat <[email protected]>

---------

Co-authored-by: Muhammad Idil Haq Amir <[email protected]>
Co-authored-by: Rahmat Hidayat <[email protected]>
  • Loading branch information
3 people authored Mar 25, 2024
1 parent 9643b56 commit a2376bb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ setup:
go get github.com/grpc-ecosystem/grpc-gateway/v2/[email protected]
go get github.com/bufbuild/buf/cmd/[email protected]
go get github.com/vektra/mockery/[email protected]

5 changes: 5 additions & 0 deletions core/appeal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,11 @@ func (s *Service) AddApprover(ctx context.Context, appealID, approvalID, email s
if appeal.Status != domain.AppealStatusPending {
return nil, fmt.Errorf("%w: can't add new approver to appeal with %q status", ErrUnableToAddApprover, appeal.Status)
}
for _, existingApprover := range approval.Approvers {
if email == existingApprover {
return nil, fmt.Errorf("%w: approver %q already exists", ErrUnableToAddApprover, email)
}
}

switch approval.Status {
case domain.ApprovalStatusPending:
Expand Down
24 changes: 24 additions & 0 deletions core/appeal/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3177,6 +3177,30 @@ func (s *ServiceTestSuite) TestAddApprover() {
s.ErrorIs(err, expectedError)
s.mockRepository.AssertExpectations(s.T())
})

s.Run("should return error if the new approver is already exist on the current approval", func() {
expectedError := appeal.ErrUnableToAddApprover
approvalID := uuid.New().String()
expectedAppeal := &domain.Appeal{
Status: domain.AppealStatusPending,
Approvals: []*domain.Approval{
{
ID: approvalID,
Status: domain.ApprovalStatusPending,
Approvers: []string{"[email protected]"},
},
},
}
s.mockRepository.EXPECT().
GetByID(mock.MatchedBy(func(ctx context.Context) bool { return true }), mock.Anything).
Return(expectedAppeal, nil).Once()

appeal, err := s.service.AddApprover(context.Background(), uuid.New().String(), approvalID, "[email protected]")

s.Nil(appeal)
s.ErrorIs(err, expectedError)
s.mockRepository.AssertExpectations(s.T())
})
}

func (s *ServiceTestSuite) TestDeleteApprover() {
Expand Down

0 comments on commit a2376bb

Please sign in to comment.