Skip to content

Commit

Permalink
[NOREF] Revert "remove unused action option from BE (#2764)" (#2802)
Browse files Browse the repository at this point in the history
Revert "remove unused action option from BE (#2764)"

This reverts commit 9f0b918.
  • Loading branch information
ClayBenson94 committed Sep 9, 2024
1 parent e7914a4 commit c98010f
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 24 deletions.
55 changes: 42 additions & 13 deletions pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func (s *Server) routes() {
businessCaseHandler := handlers.NewBusinessCaseHandler(
base,
services.NewFetchBusinessCaseByID(
serviceConfig,
store.FetchBusinessCaseByID,
services.AuthorizeHasEASiRole,
),
Expand All @@ -263,20 +264,48 @@ func (s *Server) routes() {

actionHandler := handlers.NewActionHandler(
base,
services.NewBusinessCaseTakeAction(
services.NewTakeAction(
store.FetchSystemIntakeByID,
services.NewSubmitBusinessCase(
serviceConfig,
services.AuthorizeUserIsIntakeRequester,
store.FetchOpenBusinessCaseByIntakeID,
appvalidation.BusinessCaseForSubmit,
saveAction,
store.UpdateSystemIntake,
store.UpdateBusinessCase,
emailClient.SystemIntake.SendSubmitBizCaseRequesterNotification,
emailClient.SystemIntake.SendSubmitBizCaseReviewerNotification,
publisher.PublishBusinessCase,
),
map[models.ActionType]services.ActionExecuter{
models.ActionTypeSUBMITBIZCASE: services.NewSubmitBusinessCase(
serviceConfig,
services.AuthorizeUserIsIntakeRequester,
store.FetchOpenBusinessCaseByIntakeID,
appvalidation.BusinessCaseForSubmit,
saveAction,
store.UpdateSystemIntake,
store.UpdateBusinessCase,
emailClient.SystemIntake.SendSubmitBizCaseRequesterNotification,
emailClient.SystemIntake.SendSubmitBizCaseReviewerNotification,
publisher.PublishBusinessCase,
),
models.ActionTypeSUBMITFINALBIZCASE: services.NewSubmitBusinessCase(
serviceConfig,
services.AuthorizeUserIsIntakeRequester,
store.FetchOpenBusinessCaseByIntakeID,
appvalidation.BusinessCaseForSubmit,
saveAction,
store.UpdateSystemIntake,
store.UpdateBusinessCase,
emailClient.SystemIntake.SendSubmitBizCaseRequesterNotification,
emailClient.SystemIntake.SendSubmitBizCaseReviewerNotification,
publisher.PublishBusinessCase,
),
models.ActionTypeSUBMITINTAKE: services.NewSubmitSystemIntake(
serviceConfig,
services.AuthorizeUserIsIntakeRequester,
store.UpdateSystemIntake,
// quick adapter to retrofit the new interface to take the place
// of the old interface
func(ctx context.Context, si *models.SystemIntake) (string, error) {
err := publisher.PublishSystemIntake(ctx, *si)
return "", err
},
saveAction,
emailClient.SystemIntake.SendSubmitInitialFormRequesterNotification,
emailClient.SystemIntake.SendSubmitInitialFormReviewerNotification,
),
},
),
)
api.Handle("/system_intake/{intake_id}/actions", actionHandler.Handle())
Expand Down
15 changes: 11 additions & 4 deletions pkg/services/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
// ActionExecuter is a function that can execute an action
type ActionExecuter func(context.Context, *models.SystemIntake, *models.Action) error

// NewBusinessCaseTakeAction is a service to create and execute an action
func NewBusinessCaseTakeAction(
// NewTakeAction is a service to create and execute an action
func NewTakeAction(
fetch func(context.Context, uuid.UUID) (*models.SystemIntake, error),
submitBusinessCase ActionExecuter,
actionTypeMap map[models.ActionType]ActionExecuter,
) func(context.Context, *models.Action) error {
return func(ctx context.Context, action *models.Action) error {
intake, fetchErr := fetch(ctx, *action.IntakeID)
Expand All @@ -31,7 +31,14 @@ func NewBusinessCaseTakeAction(
}
}

return submitBusinessCase(ctx, intake, action)
if executeAction, ok := actionTypeMap[action.ActionType]; ok {
return executeAction(ctx, intake, action)
}
return &apperrors.ResourceConflictError{
Err: errors.New("invalid action type"),
Resource: intake,
ResourceID: intake.ID.String(),
}
}
}

Expand Down
29 changes: 27 additions & 2 deletions pkg/services/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,45 @@ func (s *ServicesTestSuite) TestNewTakeAction() {
return &models.SystemIntake{ID: id}, nil
}

s.Run("returns QueryError if fetch fails", func() {
failFetch := func(ctx context.Context, id uuid.UUID) (*models.SystemIntake, error) {
return nil, errors.New("error")
}
createAction := NewTakeAction(failFetch, map[models.ActionType]ActionExecuter{})
id := uuid.New()
action := models.Action{
IntakeID: &id,
ActionType: models.ActionTypeSUBMITINTAKE,
}
err := createAction(ctx, &action)
s.IsType(&apperrors.QueryError{}, err)
})

s.Run("returns error from an action service", func() {
submitError := errors.New("test")
failSubmit := func(ctx context.Context, intake *models.SystemIntake, action *models.Action) error {
return submitError
}
createAction := NewBusinessCaseTakeAction(fetch, failSubmit)
createAction := NewTakeAction(fetch, map[models.ActionType]ActionExecuter{models.ActionTypeSUBMITINTAKE: failSubmit})
id := uuid.New()
action := models.Action{
IntakeID: &id,
ActionType: models.ActionTypeSUBMITBIZCASE,
ActionType: models.ActionTypeSUBMITINTAKE,
}
err := createAction(ctx, &action)
s.Equal(submitError, err)
})

s.Run("returns ResourceConflictError if invalid action type", func() {
createAction := NewTakeAction(fetch, map[models.ActionType]ActionExecuter{})
id := uuid.New()
action := models.Action{
IntakeID: &id,
ActionType: "INVALID",
}
err := createAction(ctx, &action)
s.IsType(&apperrors.ResourceConflictError{}, err)
})
}

func (s *ServicesTestSuite) TestNewSubmitSystemIntake() {
Expand Down
1 change: 1 addition & 0 deletions pkg/services/business_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

// NewFetchBusinessCaseByID is a service to fetch the business case by id
func NewFetchBusinessCaseByID(
config Config,
fetch func(c context.Context, id uuid.UUID) (*models.BusinessCaseWithCosts, error),
authorized func(context.Context) bool,
) func(c context.Context, id uuid.UUID) (*models.BusinessCaseWithCosts, error) {
Expand Down
7 changes: 5 additions & 2 deletions pkg/services/business_case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (
)

func (s *ServicesTestSuite) TestBusinessCaseByIDFetcher() {
logger := zap.NewNop()
fakeID := uuid.New()
serviceConfig := NewConfig(logger, nil)
serviceConfig.clock = clock.NewMock()
authorized := func(context context.Context) bool { return true }

s.Run("successfully fetches Business Case by ID without an error", func() {
Expand All @@ -26,7 +29,7 @@ func (s *ServicesTestSuite) TestBusinessCaseByIDFetcher() {
},
}, nil
}
fetchBusinessCaseByID := NewFetchBusinessCaseByID(fetch, authorized)
fetchBusinessCaseByID := NewFetchBusinessCaseByID(serviceConfig, fetch, authorized)
businessCase, err := fetchBusinessCaseByID(context.Background(), fakeID)
s.NoError(err)

Expand All @@ -37,7 +40,7 @@ func (s *ServicesTestSuite) TestBusinessCaseByIDFetcher() {
fetch := func(ctx context.Context, id uuid.UUID) (*models.BusinessCaseWithCosts, error) {
return &models.BusinessCaseWithCosts{}, errors.New("fetch failed")
}
fetchBusinessCaseByID := NewFetchBusinessCaseByID(fetch, authorized)
fetchBusinessCaseByID := NewFetchBusinessCaseByID(serviceConfig, fetch, authorized)

businessCase, err := fetchBusinessCaseByID(context.Background(), uuid.New())

Expand Down
1 change: 1 addition & 0 deletions src/types/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type ActionType =
*/
export type CreateActionPayload = {
intakeId: string;
actionType: ActionType;
feedback?: string;
};

Expand Down
12 changes: 10 additions & 2 deletions src/views/BusinessCase/Review/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import './index.scss';

type ReviewProps = {
businessCase: BusinessCaseModel;
isFinal: boolean;
};

const Review = ({ businessCase }: ReviewProps) => {
const Review = ({ businessCase, isFinal }: ReviewProps) => {
const { t } = useTranslation('businessCase');

const history = useHistory();
Expand Down Expand Up @@ -55,7 +56,14 @@ const Review = ({ businessCase }: ReviewProps) => {
type="submit"
disabled={isSubmitting}
onClick={() => {
dispatch(postAction({ intakeId: businessCase.systemIntakeId }));
dispatch(
postAction({
intakeId: businessCase.systemIntakeId,
actionType: isFinal
? 'SUBMIT_FINAL_BIZ_CASE'
: 'SUBMIT_BIZ_CASE'
})
);
}}
>
{t('sendBusinessCase')}
Expand Down
4 changes: 3 additions & 1 deletion src/views/BusinessCase/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ export const BusinessCase = () => {
/>
<Route
path="/business/:businessCaseId/review"
render={() => <Review businessCase={businessCase} />}
render={() => (
<Review businessCase={businessCase} isFinal={isFinal} />
)}
/>
<Route
path="/business/:businessCaseId/view"
Expand Down

0 comments on commit c98010f

Please sign in to comment.