Skip to content

Commit

Permalink
Send email to voucher when donor removes them
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx committed Nov 27, 2024
1 parent b44d4ca commit 18b5dee
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 22 deletions.

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

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package donorpage

import (
"fmt"
"net/http"
"net/url"

Expand All @@ -9,6 +10,8 @@ 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/form"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)

Expand All @@ -18,7 +21,7 @@ type areYouSureYouNoLongerNeedVoucherData struct {
Donor *donordata.Provided
}

func AreYouSureYouNoLongerNeedVoucher(tmpl template.Template, donorStore DonorStore) Handler {
func AreYouSureYouNoLongerNeedVoucher(tmpl template.Template, donorStore DonorStore, notifyClient NotifyClient) Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *donordata.Provided) error {
data := &areYouSureYouNoLongerNeedVoucherData{
App: appData,
Expand All @@ -36,6 +39,13 @@ func AreYouSureYouNoLongerNeedVoucher(tmpl template.Template, donorStore DonorSt
provided.WantVoucher = form.YesNoUnknown
nextPage := handleDoNext(doNext, provided).Format(provided.LpaID)

if err := notifyClient.SendActorEmail(r.Context(), localize.En, provided.Voucher.Email, provided.LpaUID, notify.VoucherInformedTheyAreNoLongerNeededToVouchEmail{
VoucherFullName: provided.Voucher.FullName(),
DonorFullName: provided.Donor.FullName(),
}); err != nil {
return fmt.Errorf("failed to send email: %w", err)
}

if err := donorStore.DeleteVoucher(r.Context(), provided); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor/donordata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/form"
"github.com/ministryofjustice/opg-modernising-lpa/internal/identity"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
Expand All @@ -26,7 +28,7 @@ func TestGetAreYouSureYouNoLongerNeedVoucher(t *testing.T) {
}).
Return(nil)

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

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

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

assert.Equal(t, expectedError, err)
Expand All @@ -58,31 +60,39 @@ func TestPostAreYouSureYouNoLongerNeedVoucher(t *testing.T) {
redirect: donor.PathConfirmYourIdentity,
provided: &donordata.Provided{
LpaID: "lpa-id",
Voucher: donordata.Voucher{FirstNames: "a", LastName: "b"},
LpaUID: "lpa-uid",
Donor: donordata.Donor{FirstNames: "d", LastName: "e"},
Voucher: donordata.Voucher{FirstNames: "a", LastName: "b", Email: "[email protected]"},
},
},
donordata.SelectNewVoucher: {
redirect: donor.PathEnterVoucher,
provided: &donordata.Provided{
LpaID: "lpa-id",
LpaUID: "lpa-uid",
Donor: donordata.Donor{FirstNames: "d", LastName: "e"},
WantVoucher: form.Yes,
Voucher: donordata.Voucher{FirstNames: "a", LastName: "b"},
Voucher: donordata.Voucher{FirstNames: "a", LastName: "b", Email: "[email protected]"},
IdentityUserData: identity.UserData{Status: identity.StatusConfirmed},
},
},
donordata.WithdrawLPA: {
redirect: donor.PathWithdrawThisLpa,
provided: &donordata.Provided{
LpaID: "lpa-id",
Voucher: donordata.Voucher{FirstNames: "a", LastName: "b"},
LpaUID: "lpa-uid",
Donor: donordata.Donor{FirstNames: "d", LastName: "e"},
Voucher: donordata.Voucher{FirstNames: "a", LastName: "b", Email: "[email protected]"},
IdentityUserData: identity.UserData{Status: identity.StatusConfirmed},
},
},
donordata.ApplyToCOP: {
redirect: donor.PathWhatHappensNextRegisteringWithCourtOfProtection,
provided: &donordata.Provided{
LpaID: "lpa-id",
Voucher: donordata.Voucher{FirstNames: "a", LastName: "b"},
LpaUID: "lpa-uid",
Donor: donordata.Donor{FirstNames: "d", LastName: "e"},
Voucher: donordata.Voucher{FirstNames: "a", LastName: "b", Email: "[email protected]"},
IdentityUserData: identity.UserData{Status: identity.StatusConfirmed},
RegisteringWithCourtOfProtection: true,
},
Expand All @@ -99,10 +109,20 @@ func TestPostAreYouSureYouNoLongerNeedVoucher(t *testing.T) {
DeleteVoucher(r.Context(), tc.provided).
Return(nil)

err := AreYouSureYouNoLongerNeedVoucher(nil, donorStore)(testAppData, w, r, &donordata.Provided{
notifyClient := newMockNotifyClient(t)
notifyClient.EXPECT().
SendActorEmail(r.Context(), localize.En, "[email protected]", "lpa-uid", notify.VoucherInformedTheyAreNoLongerNeededToVouchEmail{
DonorFullName: "d e",
VoucherFullName: "a b",
}).
Return(nil)

err := AreYouSureYouNoLongerNeedVoucher(nil, donorStore, notifyClient)(testAppData, w, r, &donordata.Provided{
LpaID: "lpa-id",
LpaUID: "lpa-uid",
Donor: donordata.Donor{FirstNames: "d", LastName: "e"},
WantVoucher: form.Yes,
Voucher: donordata.Voucher{FirstNames: "a", LastName: "b"},
Voucher: donordata.Voucher{FirstNames: "a", LastName: "b", Email: "[email protected]"},
IdentityUserData: identity.UserData{Status: identity.StatusConfirmed},
})
resp := w.Result()
Expand All @@ -118,6 +138,19 @@ func TestPostAreYouSureYouNoLongerNeedVoucher(t *testing.T) {
}
}

func TestPostAreYouSureYouNoLongerNeedVoucherWhenNotifyErrors(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodPost, "/?choice="+donordata.ProveOwnIdentity.String(), nil)

notifyClient := newMockNotifyClient(t)
notifyClient.EXPECT().
SendActorEmail(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(expectedError)

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

func TestPostAreYouSureYouNoLongerNeedVoucherWhenStoreErrors(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodPost, "/?choice="+donordata.ProveOwnIdentity.String(), nil)
Expand All @@ -127,7 +160,12 @@ func TestPostAreYouSureYouNoLongerNeedVoucherWhenStoreErrors(t *testing.T) {
DeleteVoucher(r.Context(), mock.Anything).
Return(expectedError)

err := AreYouSureYouNoLongerNeedVoucher(nil, donorStore)(testAppData, w, r, &donordata.Provided{})
notifyClient := newMockNotifyClient(t)
notifyClient.EXPECT().
SendActorEmail(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil)

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

assert.Equal(t, expectedError, err)
}
Expand All @@ -136,6 +174,6 @@ func TestPostAreYouSureYouNoLongerNeedVoucherWhenInvalidChoice(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodPost, "/?choice=what", nil)

err := AreYouSureYouNoLongerNeedVoucher(nil, nil)(testAppData, w, r, &donordata.Provided{})
err := AreYouSureYouNoLongerNeedVoucher(nil, nil, nil)(testAppData, w, r, &donordata.Provided{})
assert.Error(t, err)
}
50 changes: 50 additions & 0 deletions internal/donor/donorpage/mock_NotifyClient_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 @@ -100,6 +100,7 @@ type OneLoginClient interface {
type NotifyClient interface {
SendActorSMS(ctx context.Context, lang localize.Lang, to, lpaUID string, sms notify.SMS) error
SendEmail(ctx context.Context, lang localize.Lang, to string, email notify.Email) error
SendActorEmail(ctx context.Context, lang localize.Lang, to, lpaUID string, email notify.Email) error
}

type SessionStore interface {
Expand Down Expand Up @@ -427,7 +428,7 @@ func Register(
handleWithDonor(donor.PathWhatHappensNextRegisteringWithCourtOfProtection, page.None,
Guidance(tmpls.Get("what_happens_next_registering_with_court_of_protection.gohtml")))
handleWithDonor(donor.PathAreYouSureYouNoLongerNeedVoucher, page.CanGoBack,
AreYouSureYouNoLongerNeedVoucher(tmpls.Get("are_you_sure_you_no_longer_need_voucher.gohtml"), donorStore))
AreYouSureYouNoLongerNeedVoucher(tmpls.Get("are_you_sure_you_no_longer_need_voucher.gohtml"), donorStore, notifyClient))
handleWithDonor(donor.PathWeHaveInformedVoucherNoLongerNeeded, page.None,
Guidance(tmpls.Get("we_have_informed_voucher_no_longer_needed.gohtml")))

Expand Down
21 changes: 21 additions & 0 deletions internal/notify/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,24 @@ func (e VoucherHasConfirmedDonorIdentityOnSignedLpaEmail) emailID(isProduction b

return "efa0ef78-9e65-4edf-88c8-70d3da7a4b0e"
}

type VoucherInformedTheyAreNoLongerNeededToVouchEmail struct {
VoucherFullName string
DonorFullName string
}

func (e VoucherInformedTheyAreNoLongerNeededToVouchEmail) emailID(isProduction bool, lang localize.Lang) string {
if isProduction {
if lang.IsCy() {
return "TODO"
}

return "ca7c6a15-bdf3-47fe-ba01-d811ccdbc30d"
}

if lang.IsCy() {
return "TODO"
}

return "00ad14c6-f6df-4d7f-ae44-d7e27f6a9187"
}
1 change: 1 addition & 0 deletions lang/cy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,7 @@
"weWillInformVoucherTheyNoLongerNeedTo": "Welsh {{.VoucherFullName}}",
"voucherNoLongerNeeded": "Welsh {{.VoucherFullName}}",
"weHaveInformedVoucherTheyNoLongerNeedTo": "Welsh {{.VoucherFullName}}",
"weHaveInformedThePersonYouWantedToVouchForYouTheyAreNoLongerNeeded": "Welsh",
"youHaveChosen:prove-own-identity": "Welsh",
"youHaveChosen:select-new-voucher": "Welsh",
"youHaveChosen:withdraw-lpa": "Welsh",
Expand Down
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,7 @@
"weWillInformVoucherTheyNoLongerNeedTo": "We will inform {{.VoucherFullName}} they no longer need to confirm your identity. The one-time code allowing them to log into the vouching service will be cancelled.",
"voucherNoLongerNeeded": "{{.VoucherFullName}} no longer needed",
"weHaveInformedVoucherTheyNoLongerNeedTo": "We have informed {{.VoucherFullName}} they no longer need to confirm your identity.",
"weHaveInformedThePersonYouWantedToVouchForYouTheyAreNoLongerNeeded": "We have informed the person you wanted to vouch for you they are no longer needed",
"youHaveChosen:prove-own-identity": "You have chosen to find, replace or get new ID documents and confirm your identity using GOV.UK One Login.",
"youHaveChosen:select-new-voucher": "You have chosen to ask someone else to confirm your identity by vouching for you.",
"youHaveChosen:withdraw-lpa": "You have told us you no longer want to make this LPA.",
Expand Down

0 comments on commit 18b5dee

Please sign in to comment.