From c98010f8e4a5460f360f571a7a3bbe088ac34066 Mon Sep 17 00:00:00 2001 From: Clay Benson Date: Mon, 9 Sep 2024 12:20:54 -0400 Subject: [PATCH] [NOREF] Revert "remove unused action option from BE (#2764)" (#2802) Revert "remove unused action option from BE (#2764)" This reverts commit 9f0b9189fd15cbce3f2d8c5b4833ebabc9b419f3. --- pkg/server/routes.go | 55 +++++++++++++++++++------ pkg/services/action.go | 15 +++++-- pkg/services/action_test.go | 29 ++++++++++++- pkg/services/business_case.go | 1 + pkg/services/business_case_test.go | 7 +++- src/types/action.ts | 1 + src/views/BusinessCase/Review/index.tsx | 12 +++++- src/views/BusinessCase/index.tsx | 4 +- 8 files changed, 100 insertions(+), 24 deletions(-) diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 5c413a9163..813fcf8b17 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -237,6 +237,7 @@ func (s *Server) routes() { businessCaseHandler := handlers.NewBusinessCaseHandler( base, services.NewFetchBusinessCaseByID( + serviceConfig, store.FetchBusinessCaseByID, services.AuthorizeHasEASiRole, ), @@ -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()) diff --git a/pkg/services/action.go b/pkg/services/action.go index 74a4ecf209..9255fc0770 100644 --- a/pkg/services/action.go +++ b/pkg/services/action.go @@ -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) @@ -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(), + } } } diff --git a/pkg/services/action_test.go b/pkg/services/action_test.go index 2b3088ae65..eefd161d15 100644 --- a/pkg/services/action_test.go +++ b/pkg/services/action_test.go @@ -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() { diff --git a/pkg/services/business_case.go b/pkg/services/business_case.go index 65df301e64..f94a6a935e 100644 --- a/pkg/services/business_case.go +++ b/pkg/services/business_case.go @@ -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) { diff --git a/pkg/services/business_case_test.go b/pkg/services/business_case_test.go index d7bc16fa6a..a5298ece83 100644 --- a/pkg/services/business_case_test.go +++ b/pkg/services/business_case_test.go @@ -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() { @@ -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) @@ -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()) diff --git a/src/types/action.ts b/src/types/action.ts index 9e9c346084..6c0a0ce5ae 100644 --- a/src/types/action.ts +++ b/src/types/action.ts @@ -23,6 +23,7 @@ export type ActionType = */ export type CreateActionPayload = { intakeId: string; + actionType: ActionType; feedback?: string; }; diff --git a/src/views/BusinessCase/Review/index.tsx b/src/views/BusinessCase/Review/index.tsx index 8b0da4d0ae..b943836e4b 100644 --- a/src/views/BusinessCase/Review/index.tsx +++ b/src/views/BusinessCase/Review/index.tsx @@ -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(); @@ -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')} diff --git a/src/views/BusinessCase/index.tsx b/src/views/BusinessCase/index.tsx index 684d80da51..e01b8dac1c 100644 --- a/src/views/BusinessCase/index.tsx +++ b/src/views/BusinessCase/index.tsx @@ -213,7 +213,9 @@ export const BusinessCase = () => { /> } + render={() => ( + + )} />