Skip to content

Commit

Permalink
Merge pull request #1659 from ministryofjustice/MLPAB-2673-correspond…
Browse files Browse the repository at this point in the history
…ent-event

MLPAB-2673 Send correspondent-updated event
  • Loading branch information
hawx authored Dec 4, 2024
2 parents e864a8b + 9a6b233 commit 1979d09
Show file tree
Hide file tree
Showing 15 changed files with 433 additions and 100 deletions.
126 changes: 71 additions & 55 deletions cypress/e2e/donor/add-correspondent.cy.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,75 @@
import { AddressFormAssertions } from "../../support/e2e";

describe('Add correspondent', () => {
beforeEach(() => {
cy.visit('/fixtures?progress=provideYourDetails&redirect=');
});

it('allows none', () => {
cy.contains('M-FAKE-').click();
cy.contains('Go to task list').click();
cy.contains('li', 'Add a correspondent').should('contain', 'Not started').click();

cy.checkA11yApp();
cy.contains('label', 'No').click();
cy.contains('button', 'Save and continue').click();
cy.contains('li', 'Add a correspondent').should('contain', 'Completed');
});

it('allows without address', () => {
cy.contains('M-FAKE-').click();
cy.contains('Go to task list').click();
cy.contains('li', 'Add a correspondent').should('contain', 'Not started').click();

cy.checkA11yApp();
cy.contains('label', 'Yes').click();
cy.contains('button', 'Save and continue').click();

cy.checkA11yApp();
cy.get('#f-first-names').type('John');
cy.get('#f-last-name').type('Smith');
cy.get('#f-email').type('[email protected]');
cy.contains('label', 'No').click();
cy.contains('button', 'Save and continue').click();
cy.contains('li', 'Add a correspondent').should('contain', 'Completed');
});

it('allows with address', () => {
cy.contains('M-FAKE-').click();
cy.contains('Go to task list').click();
cy.contains('li', 'Add a correspondent').should('contain', 'Not started').click();

cy.checkA11yApp();
cy.contains('label', 'Yes').click();
cy.contains('button', 'Save and continue').click();

cy.checkA11yApp();
cy.get('#f-first-names').type('John');
cy.get('#f-last-name').type('Smith');
cy.get('#f-email').type('[email protected]');
cy.contains('label', 'Yes').click();
cy.contains('button', 'Save and continue').click();

cy.contains('label', 'Enter a new address').click();
cy.contains('button', 'Continue').click();
AddressFormAssertions.assertCanAddAddressFromSelect()

cy.contains('li', 'Add a correspondent').should('contain', 'Completed');
});
beforeEach(() => {
cy.visit('/fixtures?progress=provideYourDetails&redirect=');
});

it('allows none', () => {
cy.contains('M-FAKE-').click();
cy.contains('Go to task list').click();
cy.contains('li', 'Add a correspondent').should('contain', 'Not started').click();

cy.checkA11yApp();
cy.contains('label', 'No').click();
cy.contains('button', 'Save and continue').click();
cy.contains('li', 'Add a correspondent').should('contain', 'Completed');
});

it('allows without address', () => {
cy.contains('M-FAKE-').click();
cy.contains('Go to task list').click();
cy.contains('li', 'Add a correspondent').should('contain', 'Not started').click();

cy.checkA11yApp();
cy.contains('label', 'Yes').click();
cy.contains('button', 'Save and continue').click();

cy.checkA11yApp();
cy.get('#f-first-names').type('John');
cy.get('#f-last-name').type('Smith');
cy.get('#f-email').type('[email protected]');
cy.contains('label', 'No').click();
cy.contains('button', 'Save and continue').click();
cy.contains('li', 'Add a correspondent').should('contain', 'Completed');

cy.contains('.govuk-summary-list__row', 'Reference number').find('.govuk-summary-list__value')
.invoke('text')
.then((uid) => {
cy.visit(`http://localhost:9001/?detail-type=correspondent-updated&detail=${uid}`);

cy.contains(`{"uid":"${uid}","firstNames":"John","lastName":"Smith","email":"[email protected]"}`);
});
});

it('allows with address', () => {
cy.contains('M-FAKE-').click();
cy.contains('Go to task list').click();
cy.contains('li', 'Add a correspondent').should('contain', 'Not started').click();

cy.checkA11yApp();
cy.contains('label', 'Yes').click();
cy.contains('button', 'Save and continue').click();

cy.checkA11yApp();
cy.get('#f-first-names').type('John');
cy.get('#f-last-name').type('Smith');
cy.get('#f-email').type('[email protected]');
cy.contains('label', 'Yes').click();
cy.contains('button', 'Save and continue').click();

cy.contains('label', 'Enter a new address').click();
cy.contains('button', 'Continue').click();
AddressFormAssertions.assertCanAddAddressFromSelect()

cy.contains('li', 'Add a correspondent').should('contain', 'Completed');

cy.contains('.govuk-summary-list__row', 'Reference number').find('.govuk-summary-list__value')
.invoke('text')
.then((uid) => {
cy.visit(`http://localhost:9001/?detail-type=correspondent-updated&detail=${uid}`);

cy.contains(`{"uid":"${uid}","firstNames":"John","lastName":"Smith","email":"[email protected]","address":{"line1":"2 RICHMOND PLACE","line2":"","line3":"","town":"BIRMINGHAM","postcode":"B14 7ED","country":"GB"}}`);
});
});
});
12 changes: 11 additions & 1 deletion internal/donor/donorpage/add_correspondent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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 @@ -19,7 +20,7 @@ type addCorrespondentData struct {
Donor *donordata.Provided
}

func AddCorrespondent(tmpl template.Template, donorStore DonorStore) Handler {
func AddCorrespondent(tmpl template.Template, donorStore DonorStore, eventClient EventClient) Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *donordata.Provided) error {
data := &addCorrespondentData{
App: appData,
Expand All @@ -36,8 +37,17 @@ func AddCorrespondent(tmpl template.Template, donorStore DonorStore) Handler {

var redirectUrl donor.Path
if provided.AddCorrespondent.IsNo() {
if provided.Correspondent.FirstNames != "" {
if err := eventClient.SendCorrespondentUpdated(r.Context(), event.CorrespondentUpdated{
UID: provided.LpaUID,
}); err != nil {
return err
}
}

provided.Correspondent = donordata.Correspondent{}
provided.Tasks.AddCorrespondent = task.StateCompleted

redirectUrl = donor.PathTaskList
} else {
if provided.Correspondent.FirstNames == "" {
Expand Down
51 changes: 45 additions & 6 deletions internal/donor/donorpage/add_correspondent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 TestGetAddCorrespondent(t *testing.T) {
}).
Return(nil)

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

assert.Nil(t, err)
Expand All @@ -50,7 +51,7 @@ func TestGetAddCorrespondentFromStore(t *testing.T) {
}).
Return(nil)

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

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

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

assert.Equal(t, expectedError, err)
Expand All @@ -81,6 +82,7 @@ func TestPostAddCorrespondent(t *testing.T) {
expectedCorrespondent donordata.Correspondent
expectedTaskState task.State
redirect donor.Path
setupEventClient func(*mockEventClient)
}{
"yes was yes": {
yesNo: form.Yes,
Expand All @@ -107,6 +109,13 @@ func TestPostAddCorrespondent(t *testing.T) {
existingTaskState: task.StateCompleted,
expectedTaskState: task.StateCompleted,
redirect: donor.PathTaskList,
setupEventClient: func(eventClient *mockEventClient) {
eventClient.EXPECT().
SendCorrespondentUpdated(mock.Anything, event.CorrespondentUpdated{
UID: "lpa-uid",
}).
Return(nil)
},
},
"no": {
yesNo: form.No,
Expand All @@ -129,14 +138,21 @@ func TestPostAddCorrespondent(t *testing.T) {
donorStore.EXPECT().
Put(r.Context(), &donordata.Provided{
LpaID: "lpa-id",
LpaUID: "lpa-uid",
AddCorrespondent: tc.yesNo,
Correspondent: tc.expectedCorrespondent,
Tasks: donordata.Tasks{AddCorrespondent: tc.expectedTaskState},
}).
Return(nil)

err := AddCorrespondent(nil, donorStore)(testAppData, w, r, &donordata.Provided{
eventClient := newMockEventClient(t)
if tc.setupEventClient != nil {
tc.setupEventClient(eventClient)
}

err := AddCorrespondent(nil, donorStore, eventClient)(testAppData, w, r, &donordata.Provided{
LpaID: "lpa-id",
LpaUID: "lpa-uid",
Correspondent: tc.existingCorrespondent,
Tasks: donordata.Tasks{AddCorrespondent: tc.existingTaskState},
})
Expand All @@ -149,6 +165,29 @@ func TestPostAddCorrespondent(t *testing.T) {
}
}

func TestPostAddCorrespondentWhenEventClientErrors(t *testing.T) {
f := url.Values{
form.FieldNames.YesNo: {form.No.String()},
}

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

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

err := AddCorrespondent(nil, nil, eventClient)(testAppData, w, r, &donordata.Provided{
LpaID: "lpa-id",
LpaUID: "lpa-uid",
Correspondent: donordata.Correspondent{FirstNames: "John"},
Tasks: donordata.Tasks{AddCorrespondent: task.StateCompleted},
})
assert.Equal(t, expectedError, err)
}

func TestPostAddCorrespondentWhenStoreErrors(t *testing.T) {
f := url.Values{
form.FieldNames.YesNo: {form.Yes.String()},
Expand All @@ -163,7 +202,7 @@ func TestPostAddCorrespondentWhenStoreErrors(t *testing.T) {
Put(r.Context(), mock.Anything).
Return(expectedError)

err := AddCorrespondent(nil, donorStore)(testAppData, w, r, &donordata.Provided{})
err := AddCorrespondent(nil, donorStore, nil)(testAppData, w, r, &donordata.Provided{})

assert.Equal(t, expectedError, err)
}
Expand All @@ -180,7 +219,7 @@ func TestPostAddCorrespondentWhenValidationErrors(t *testing.T) {
})).
Return(nil)

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

assert.Nil(t, err)
Expand Down
14 changes: 13 additions & 1 deletion internal/donor/donorpage/enter_correspondent_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ 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/place"
"github.com/ministryofjustice/opg-modernising-lpa/internal/task"
)

func EnterCorrespondentAddress(logger Logger, tmpl template.Template, addressClient AddressClient, donorStore DonorStore) Handler {
func EnterCorrespondentAddress(logger Logger, tmpl template.Template, addressClient AddressClient, donorStore DonorStore, eventClient EventClient) Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *donordata.Provided) error {
data := newChooseAddressData(
appData,
Expand All @@ -35,6 +36,17 @@ func EnterCorrespondentAddress(logger Logger, tmpl template.Template, addressCli
provided.Tasks.AddCorrespondent = task.StateCompleted
provided.Correspondent.Address = address

if err := eventClient.SendCorrespondentUpdated(r.Context(), event.CorrespondentUpdated{
UID: provided.LpaUID,
FirstNames: provided.Correspondent.FirstNames,
LastName: provided.Correspondent.LastName,
Email: provided.Correspondent.Email,
Phone: provided.Correspondent.Phone,
Address: &provided.Correspondent.Address,
}); err != nil {
return err
}

if err := donorStore.Put(r.Context(), provided); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 1979d09

Please sign in to comment.