Skip to content

Commit

Permalink
Send event when donor is going to post office
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx committed Feb 25, 2025
1 parent 6cfb77c commit 76dc3bf
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donordata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/form"
"github.com/ministryofjustice/opg-modernising-lpa/internal/task"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
Expand All @@ -29,7 +30,7 @@ type howWillYouConfirmYourIdentityData struct {
Form *form.SelectForm[howYouWillConfirmYourIdentity, howYouWillConfirmYourIdentityOptions, *howYouWillConfirmYourIdentity]
}

func HowWillYouConfirmYourIdentity(tmpl template.Template, donorStore DonorStore) Handler {
func HowWillYouConfirmYourIdentity(tmpl template.Template, donorStore DonorStore, eventClient EventClient) Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *donordata.Provided) error {
data := &howWillYouConfirmYourIdentityData{
App: appData,
Expand All @@ -45,6 +46,12 @@ func HowWillYouConfirmYourIdentity(tmpl template.Template, donorStore DonorStore
case howYouWillConfirmYourIdentityAtPostOffice:
provided.Tasks.ConfirmYourIdentity = task.IdentityStatePending

if err := eventClient.SendConfirmAtPostOfficeSelected(r.Context(), event.ConfirmAtPostOfficeSelected{
UID: provided.LpaUID,
}); err != nil {
return fmt.Errorf("error sending event: %w", err)
}

if err := donorStore.Put(r.Context(), provided); err != nil {
return fmt.Errorf("error updating donor: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ministryofjustice/opg-modernising-lpa/internal/donor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donordata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/form"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/task"
Expand All @@ -30,7 +31,7 @@ func TestGetHowWillYouConfirmYourIdentity(t *testing.T) {
}).
Return(nil)

err := HowWillYouConfirmYourIdentity(template.Execute, nil)(testAppData, w, r, &donordata.Provided{})
err := HowWillYouConfirmYourIdentity(template.Execute, nil, nil)(testAppData, w, r, &donordata.Provided{})
resp := w.Result()

assert.Nil(t, err)
Expand All @@ -46,7 +47,7 @@ func TestGetHowWillYouConfirmYourIdentityWhenTemplateErrors(t *testing.T) {
Execute(w, mock.Anything).
Return(expectedError)

err := HowWillYouConfirmYourIdentity(template.Execute, nil)(testAppData, w, r, &donordata.Provided{})
err := HowWillYouConfirmYourIdentity(template.Execute, nil, nil)(testAppData, w, r, &donordata.Provided{})
assert.Equal(t, expectedError, err)
}

Expand Down Expand Up @@ -88,7 +89,7 @@ func TestPostHowWillYouConfirmYourIdentity(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(form.Encode()))
r.Header.Add("Content-Type", page.FormUrlEncoded)

err := HowWillYouConfirmYourIdentity(nil, nil)(testAppData, w, r, tc.provided)
err := HowWillYouConfirmYourIdentity(nil, nil, nil)(testAppData, w, r, tc.provided)
resp := w.Result()

assert.Nil(t, err)
Expand All @@ -107,22 +108,51 @@ func TestPostHowWillYouConfirmYourIdentityWhenAtPostOfficeSelected(t *testing.T)
r, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(form.Encode()))
r.Header.Add("Content-Type", page.FormUrlEncoded)

eventClient := newMockEventClient(t)
eventClient.EXPECT().
SendConfirmAtPostOfficeSelected(r.Context(), event.ConfirmAtPostOfficeSelected{
UID: "lpa-uid",
}).
Return(nil)

donorStore := newMockDonorStore(t)
donorStore.EXPECT().
Put(r.Context(), &donordata.Provided{
LpaID: "lpa-id",
Tasks: donordata.Tasks{ConfirmYourIdentity: task.IdentityStatePending},
LpaID: "lpa-id",
LpaUID: "lpa-uid",
Tasks: donordata.Tasks{ConfirmYourIdentity: task.IdentityStatePending},
}).
Return(nil)

err := HowWillYouConfirmYourIdentity(nil, donorStore)(testAppData, w, r, &donordata.Provided{LpaID: "lpa-id"})
err := HowWillYouConfirmYourIdentity(nil, donorStore, eventClient)(testAppData, w, r, &donordata.Provided{
LpaID: "lpa-id",
LpaUID: "lpa-uid",
})
resp := w.Result()

assert.Nil(t, err)
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, donor.PathTaskList.Format("lpa-id"), resp.Header.Get("Location"))
}

func TestPostHowWillYouConfirmYourIdentityWhenEventClientErrors(t *testing.T) {
form := url.Values{
form.FieldNames.Select: {howYouWillConfirmYourIdentityAtPostOffice.String()},
}

w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(form.Encode()))
r.Header.Add("Content-Type", page.FormUrlEncoded)

eventClient := newMockEventClient(t)
eventClient.EXPECT().
SendConfirmAtPostOfficeSelected(mock.Anything, mock.Anything).
Return(expectedError)

err := HowWillYouConfirmYourIdentity(nil, nil, eventClient)(testAppData, w, r, &donordata.Provided{})
assert.ErrorIs(t, err, expectedError)
}

func TestPostHowWillYouConfirmYourIdentityWhenStoreErrors(t *testing.T) {
form := url.Values{
form.FieldNames.Select: {howYouWillConfirmYourIdentityAtPostOffice.String()},
Expand All @@ -132,12 +162,17 @@ func TestPostHowWillYouConfirmYourIdentityWhenStoreErrors(t *testing.T) {
r, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(form.Encode()))
r.Header.Add("Content-Type", page.FormUrlEncoded)

eventClient := newMockEventClient(t)
eventClient.EXPECT().
SendConfirmAtPostOfficeSelected(mock.Anything, mock.Anything).
Return(nil)

donorStore := newMockDonorStore(t)
donorStore.EXPECT().
Put(r.Context(), mock.Anything).
Return(expectedError)

err := HowWillYouConfirmYourIdentity(nil, donorStore)(testAppData, w, r, &donordata.Provided{})
err := HowWillYouConfirmYourIdentity(nil, donorStore, eventClient)(testAppData, w, r, &donordata.Provided{})
assert.ErrorIs(t, err, expectedError)
}

Expand All @@ -153,7 +188,7 @@ func TestPostHowWillYouConfirmYourIdentityWhenValidationErrors(t *testing.T) {
})).
Return(nil)

err := HowWillYouConfirmYourIdentity(template.Execute, nil)(testAppData, w, r, &donordata.Provided{})
err := HowWillYouConfirmYourIdentity(template.Execute, nil, nil)(testAppData, w, r, &donordata.Provided{})
resp := w.Result()

assert.Nil(t, err)
Expand Down
47 changes: 47 additions & 0 deletions internal/donor/donorpage/mock_EventClient_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/donor/donorpage/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ type EventClient interface {
SendCertificateProviderStarted(ctx context.Context, e event.CertificateProviderStarted) error
SendIdentityCheckMismatched(ctx context.Context, e event.IdentityCheckMismatched) error
SendCorrespondentUpdated(ctx context.Context, e event.CorrespondentUpdated) error
SendConfirmAtPostOfficeSelected(ctx context.Context, e event.ConfirmAtPostOfficeSelected) error
}

type DashboardStore interface {
Expand Down Expand Up @@ -411,7 +412,7 @@ func Register(
handleWithDonor(donor.PathConfirmYourIdentity, page.CanGoBack,
ConfirmYourIdentity(tmpls.Get("prove_your_identity.gohtml"), donorStore))
handleWithDonor(donor.PathHowWillYouConfirmYourIdentity, page.None,
HowWillYouConfirmYourIdentity(tmpls.Get("how_will_you_confirm_your_identity.gohtml"), donorStore))
HowWillYouConfirmYourIdentity(tmpls.Get("how_will_you_confirm_your_identity.gohtml"), donorStore, eventClient))
handleWithDonor(donor.PathCompletingYourIdentityConfirmation, page.None,
CompletingYourIdentityConfirmation(tmpls.Get("completing_your_identity_confirmation.gohtml")))
handleWithDonor(donor.PathIdentityWithOneLogin, page.CanGoBack,
Expand Down
31 changes: 18 additions & 13 deletions internal/event/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ import (
const source = "opg.poas.makeregister"

var events = map[any]string{
(*UidRequested)(nil): "uid-requested",
(*ApplicationDeleted)(nil): "application-deleted",
(*ApplicationUpdated)(nil): "application-updated",
(*ReducedFeeRequested)(nil): "reduced-fee-requested",
(*NotificationSent)(nil): "notification-sent",
(*PaperFormRequested)(nil): "paper-form-requested",
(*PaymentReceived)(nil): "payment-received",
(*CertificateProviderStarted)(nil): "certificate-provider-started",
(*AttorneyStarted)(nil): "attorney-started",
(*IdentityCheckMismatched)(nil): "identity-check-mismatched",
(*CorrespondentUpdated)(nil): "correspondent-updated",
(*LpaAccessGranted)(nil): "lpa-access-granted",
(*LetterRequested)(nil): "letter-requested",
(*UidRequested)(nil): "uid-requested",
(*ApplicationDeleted)(nil): "application-deleted",
(*ApplicationUpdated)(nil): "application-updated",
(*ReducedFeeRequested)(nil): "reduced-fee-requested",
(*NotificationSent)(nil): "notification-sent",
(*PaperFormRequested)(nil): "paper-form-requested",
(*PaymentReceived)(nil): "payment-received",
(*CertificateProviderStarted)(nil): "certificate-provider-started",
(*AttorneyStarted)(nil): "attorney-started",
(*IdentityCheckMismatched)(nil): "identity-check-mismatched",
(*CorrespondentUpdated)(nil): "correspondent-updated",
(*LpaAccessGranted)(nil): "lpa-access-granted",
(*LetterRequested)(nil): "letter-requested",
(*ConfirmAtPostOfficeSelected)(nil): "confirm-at-post-office-selected",
}

type eventbridgeClient interface {
Expand Down Expand Up @@ -97,6 +98,10 @@ func (c *Client) SendLetterRequested(ctx context.Context, event LetterRequested)
return send[LetterRequested](ctx, c, event)
}

func (c *Client) SendConfirmAtPostOfficeSelected(ctx context.Context, event ConfirmAtPostOfficeSelected) error {
return send[ConfirmAtPostOfficeSelected](ctx, c, event)
}

func send[T any](ctx context.Context, c *Client, detail any) error {
detailType, ok := events[(*T)(nil)]
if !ok {
Expand Down
5 changes: 5 additions & 0 deletions internal/event/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func TestClientSendEvents(t *testing.T) {

return func(client *Client) error { return client.SendLetterRequested(ctx, event) }, event
},
"confirm-at-post-office-selected": func() (func(*Client) error, any) {
event := ConfirmAtPostOfficeSelected{UID: "a"}

return func(client *Client) error { return client.SendConfirmAtPostOfficeSelected(ctx, event) }, event
},
}

for eventName, setup := range testcases {
Expand Down
4 changes: 4 additions & 0 deletions internal/event/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ type LetterRequested struct {
ActorType actor.Type `json:"actorType"`
ActorUID actoruid.UID `json:"actorUID"`
}

type ConfirmAtPostOfficeSelected struct {
UID string `json:"uid"`
}
5 changes: 5 additions & 0 deletions internal/event/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ var eventTests = map[string]map[string]any{
ActorUID: actoruid.New(),
},
},
"confirm-at-post-office-selected": {
"valid": ConfirmAtPostOfficeSelected{
UID: "M-1111-2222-3333",
},
},
}

func TestEventSchema(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions internal/event/testdata/confirm-at-post-office-selected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$id": "https://opg.service.justice.gov.uk/opg.poas.sirius/confirm-at-post-office-selected.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "opg.poas.sirius/confirm-at-post-office-selected",
"type": "object",
"properties": {
"uid": {
"type": "string",
"description": "The UID of the LPA",
"pattern": "^M(-[A-Z0-9]{4}){3}$"
}
},
"required": [
"uid"
]
}
3 changes: 2 additions & 1 deletion scripts/get_event_schemas.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ for v in uid-requested \
identity-check-mismatched \
correspondent-updated \
lpa-access-granted \
letter-requested
letter-requested \
confirm-at-post-office-selected
do
echo $v
curl -o internal/event/testdata/$v.json "https://raw.githubusercontent.com/ministryofjustice/opg-event-store/main/domains/POAS/events/$v/schema.json"
Expand Down

0 comments on commit 76dc3bf

Please sign in to comment.