Skip to content

Commit

Permalink
feat(appeal): use accountID filter to fetch pendingAppeals (#53)
Browse files Browse the repository at this point in the history
- use accountID filter to fetch pendingAppeals
  • Loading branch information
bsushmith authored Jun 13, 2023
1 parent 35c97d2 commit 5b691c7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ plugins:
out: api/proto
opt:
- paths=source_relative
- lang=go
- lang=go
15 changes: 9 additions & 6 deletions core/appeal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ func (s *Service) Create(ctx context.Context, appeals []*domain.Appeal, opts ...
isAdditionalAppealCreation := createAppealOpts.IsAdditionalAppeal

resourceIDs := []string{}
accountIDs := []string{}
for _, a := range appeals {
resourceIDs = append(resourceIDs, a.ResourceID)
accountIDs = append(accountIDs, a.AccountID)
}
resources, err := s.getResourcesMap(ctx, resourceIDs)
if err != nil {
Expand All @@ -196,7 +198,10 @@ func (s *Service) Create(ctx context.Context, appeals []*domain.Appeal, opts ...
return err
}

pendingAppeals, err := s.getPendingAppealsMap(ctx)
pendingAppeals, err := s.getAppealsMap(ctx, &domain.ListAppealsFilter{
Statuses: []string{domain.AppealStatusPending},
AccountIDs: accountIDs,
})
if err != nil {
return fmt.Errorf("listing pending appeals: %w", err)
}
Expand Down Expand Up @@ -777,11 +782,9 @@ func (s *Service) getApproval(ctx context.Context, appealID, approvalID string)
return appeal, approval, nil
}

// getPendingAppealsMap returns map[account_id]map[resource_id]map[role]*domain.Appeal, error
func (s *Service) getPendingAppealsMap(ctx context.Context) (map[string]map[string]map[string]*domain.Appeal, error) {
appeals, err := s.repo.Find(ctx, &domain.ListAppealsFilter{
Statuses: []string{domain.AppealStatusPending},
})
// getAppealsMap returns map[account_id]map[resource_id]map[role]*domain.Appeal, error
func (s *Service) getAppealsMap(ctx context.Context, filters *domain.ListAppealsFilter) (map[string]map[string]map[string]*domain.Appeal, error) {
appeals, err := s.repo.Find(ctx, filters)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions core/appeal/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,8 @@ func (s *ServiceTestSuite) TestCreate() {
s.mockProviderService.On("Find", mock.Anything).Return(providers, nil).Once()
s.mockPolicyService.On("Find", mock.Anything).Return(policies, nil).Once()
expectedExistingAppealsFilters := &domain.ListAppealsFilter{
Statuses: []string{domain.AppealStatusPending},
Statuses: []string{domain.AppealStatusPending},
AccountIDs: []string{"[email protected]", "addOnBehalfApprovedNotification-user"},
}
s.mockRepository.EXPECT().
Find(mock.AnythingOfType("*context.emptyCtx"), expectedExistingAppealsFilters).
Expand Down Expand Up @@ -1255,7 +1256,8 @@ func (s *ServiceTestSuite) TestCreateAppeal__WithExistingAppealAndWithAutoApprov
s.mockProviderService.On("Find", mock.Anything).Return(providers, nil).Once()
s.mockPolicyService.On("Find", mock.Anything).Return(policies, nil).Once()
expectedExistingAppealsFilters := &domain.ListAppealsFilter{
Statuses: []string{domain.AppealStatusPending},
Statuses: []string{domain.AppealStatusPending},
AccountIDs: []string{accountID},
}
s.mockRepository.EXPECT().
Find(mock.AnythingOfType("*context.emptyCtx"), expectedExistingAppealsFilters).
Expand Down

0 comments on commit 5b691c7

Please sign in to comment.