Skip to content

Commit

Permalink
Merge pull request #1504 from ministryofjustice/add-missing-personali…
Browse files Browse the repository at this point in the history
…sation-to-failed-vouch-email

MLPAB-2530 Add missing personalisation to failed vouch email and remove deleted email
  • Loading branch information
hawx authored Sep 23, 2024
2 parents 6d7da1a + 70afa8f commit f01ca1e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 77 deletions.
22 changes: 5 additions & 17 deletions internal/notify/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,28 +260,16 @@ func (s VoucherInviteEmail) emailID(isProduction bool) string {
return "9af150b5-d9cd-4702-bf97-d3e6bfe81eec"
}

type VoucherFirstFailedVouchAttempt struct {
Greeting string
VoucherFullName string
type VouchingFailedAttempt struct {
Greeting string
VoucherFullName string
DonorStartPageURL string
}

func (e VoucherFirstFailedVouchAttempt) emailID(isProduction bool) string {
func (e VouchingFailedAttempt) emailID(isProduction bool) string {
if isProduction {
return "f21ee857-8c3e-43ee-adf2-2d9f1ff1a1a8"
}

return "584412e6-f235-4227-aff9-6cb56ba48e31"
}

type VoucherSecondFailedVouchAttempt struct {
Greeting string
VoucherFullName string
}

func (e VoucherSecondFailedVouchAttempt) emailID(isProduction bool) string {
if isProduction {
return "44ffb252-ce34-4164-baa5-8b21036625ac"
}

return "3af1b3c4-35ce-4a23-abd2-bd0d019985c2"
}
19 changes: 6 additions & 13 deletions internal/voucher/voucherpage/confirm_allowed_to_vouch.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/form"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore/lpadata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/task"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
"github.com/ministryofjustice/opg-modernising-lpa/internal/voucher"
Expand All @@ -24,7 +25,7 @@ type confirmAllowedToVouchData struct {
MatchIdentity bool
}

func ConfirmAllowedToVouch(tmpl template.Template, lpaStoreResolvingService LpaStoreResolvingService, voucherStore VoucherStore, notifyClient NotifyClient, donorStore DonorStore) Handler {
func ConfirmAllowedToVouch(tmpl template.Template, lpaStoreResolvingService LpaStoreResolvingService, voucherStore VoucherStore, notifyClient NotifyClient, donorStore DonorStore, appPublicURL string) Handler {
return func(appData appcontext.Data, w http.ResponseWriter, r *http.Request, provided *voucherdata.Provided) error {
lpa, err := lpaStoreResolvingService.Get(r.Context())
if err != nil {
Expand Down Expand Up @@ -52,18 +53,10 @@ func ConfirmAllowedToVouch(tmpl template.Template, lpaStoreResolvingService LpaS

donor.FailedVouchAttempts++

var email notify.Email

if donor.FailedVouchAttempts > 1 {
email = notify.VoucherSecondFailedVouchAttempt{
Greeting: notifyClient.EmailGreeting(lpa),
VoucherFullName: provided.FullName(),
}
} else {
email = notify.VoucherFirstFailedVouchAttempt{
Greeting: notifyClient.EmailGreeting(lpa),
VoucherFullName: provided.FullName(),
}
email := notify.VouchingFailedAttempt{
Greeting: notifyClient.EmailGreeting(lpa),
VoucherFullName: provided.FullName(),
DonorStartPageURL: appPublicURL + page.PathStart.Format(),
}

if err := notifyClient.SendActorEmail(r.Context(), lpa.Donor.Email, lpa.LpaUID, email); err != nil {
Expand Down
86 changes: 40 additions & 46 deletions internal/voucher/voucherpage/confirm_allowed_to_vouch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -104,7 +103,7 @@ func TestGetConfirmAllowedToVouch(t *testing.T) {
Execute(w, tc.data).
Return(nil)

err := ConfirmAllowedToVouch(template.Execute, lpaStoreResolvingService, nil, nil, nil)(testAppData, w, r, tc.provided)
err := ConfirmAllowedToVouch(template.Execute, lpaStoreResolvingService, nil, nil, nil, "app://")(testAppData, w, r, tc.provided)
resp := w.Result()

assert.Nil(t, err)
Expand All @@ -124,7 +123,7 @@ func TestGetConfirmAllowedToVouchWhenLpaStoreResolvingServiceErrors(t *testing.T
Get(r.Context()).
Return(donor, expectedError)

err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, nil, nil, nil)(testAppData, w, r, nil)
err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, nil, nil, nil, "app://")(testAppData, w, r, nil)

assert.Equal(t, expectedError, err)
}
Expand All @@ -143,7 +142,7 @@ func TestGetConfirmAllowedToVouchWhenTemplateErrors(t *testing.T) {
Execute(w, mock.Anything).
Return(expectedError)

err := ConfirmAllowedToVouch(template.Execute, lpaStoreResolvingService, nil, nil, nil)(testAppData, w, r, &voucherdata.Provided{})
err := ConfirmAllowedToVouch(template.Execute, lpaStoreResolvingService, nil, nil, nil, "app://")(testAppData, w, r, &voucherdata.Provided{})

assert.Equal(t, expectedError, err)
}
Expand Down Expand Up @@ -174,7 +173,7 @@ func TestPostConfirmAllowedToVouch(t *testing.T) {
Put(r.Context(), &voucherdata.Provided{LpaID: "lpa-id", Tasks: tasks}).
Return(nil)

err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, voucherStore, nil, nil)(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id", Tasks: voucherdata.Tasks{ConfirmYourIdentity: taskState}})
err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, voucherStore, nil, nil, "app://")(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id", Tasks: voucherdata.Tasks{ConfirmYourIdentity: taskState}})
resp := w.Result()

assert.Nil(t, err)
Expand All @@ -185,52 +184,47 @@ func TestPostConfirmAllowedToVouch(t *testing.T) {
}

func TestPostConfirmAllowedToVouchWhenNo(t *testing.T) {
testcases := map[int]notify.Email{
0: notify.VoucherFirstFailedVouchAttempt{Greeting: "Email greeting", VoucherFullName: "a b"},
1: notify.VoucherSecondFailedVouchAttempt{Greeting: "Email greeting", VoucherFullName: "a b"},
f := url.Values{
form.FieldNames.YesNo: {form.No.String()},
}

for failedVouchAttempts, email := range testcases {
t.Run(strconv.Itoa(failedVouchAttempts), func(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)

w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(f.Encode()))
r.Header.Add("Content-Type", page.FormUrlEncoded)
lpa := &lpadata.Lpa{Donor: lpadata.Donor{LastName: "Smith", Email: "[email protected]"}, LpaUID: "lpa-uid"}

lpa := &lpadata.Lpa{Donor: lpadata.Donor{LastName: "Smith", Email: "[email protected]"}, LpaUID: "lpa-uid"}
lpaStoreResolvingService := newMockLpaStoreResolvingService(t)
lpaStoreResolvingService.EXPECT().
Get(r.Context()).
Return(lpa, nil)

lpaStoreResolvingService := newMockLpaStoreResolvingService(t)
lpaStoreResolvingService.EXPECT().
Get(r.Context()).
Return(lpa, nil)

notifyClient := newMockNotifyClient(t)
notifyClient.EXPECT().
EmailGreeting(lpa).
Return("Email greeting")
notifyClient.EXPECT().
SendActorEmail(r.Context(), "[email protected]", "lpa-uid", email).
Return(nil)
notifyClient := newMockNotifyClient(t)
notifyClient.EXPECT().
EmailGreeting(lpa).
Return("Email greeting")
notifyClient.EXPECT().
SendActorEmail(r.Context(), "[email protected]", "lpa-uid", notify.VouchingFailedAttempt{
Greeting: "Email greeting",
VoucherFullName: "a b",
DonorStartPageURL: "app://" + page.PathStart.Format(),
}).
Return(nil)

donorStore := newMockDonorStore(t)
donorStore.EXPECT().
GetAny(r.Context()).
Return(&donordata.Provided{FailedVouchAttempts: failedVouchAttempts}, nil)
donorStore.EXPECT().
Put(r.Context(), &donordata.Provided{FailedVouchAttempts: failedVouchAttempts + 1}).
Return(nil)
donorStore := newMockDonorStore(t)
donorStore.EXPECT().
GetAny(r.Context()).
Return(&donordata.Provided{FailedVouchAttempts: 1}, nil)
donorStore.EXPECT().
Put(r.Context(), &donordata.Provided{FailedVouchAttempts: 2}).
Return(nil)

err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, nil, notifyClient, donorStore)(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id", FirstNames: "a", LastName: "b"})
resp := w.Result()
err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, nil, notifyClient, donorStore, "app://")(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id", FirstNames: "a", LastName: "b"})
resp := w.Result()

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

func TestPostConfirmAllowedToVouchWhenStoreErrors(t *testing.T) {
Expand All @@ -252,7 +246,7 @@ func TestPostConfirmAllowedToVouchWhenStoreErrors(t *testing.T) {
Put(r.Context(), mock.Anything).
Return(expectedError)

err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, voucherStore, nil, nil)(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id"})
err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, voucherStore, nil, nil, "app://")(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id"})
assert.Equal(t, expectedError, err)
}

Expand Down Expand Up @@ -283,7 +277,7 @@ func TestPostConfirmAllowedToVouchWhenNoWhenNotifyClientError(t *testing.T) {
GetAny(r.Context()).
Return(&donordata.Provided{}, nil)

err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, nil, notifyClient, donorStore)(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id"})
err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, nil, notifyClient, donorStore, "app://")(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id"})
resp := w.Result()

assert.Error(t, err)
Expand Down Expand Up @@ -346,7 +340,7 @@ func TestPostConfirmAllowedToVouchWhenNoWhenDonorStoreErrors(t *testing.T) {
Get(mock.Anything).
Return(&lpadata.Lpa{}, nil)

err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, nil, tc.notifyClient(t), tc.donorStore(t))(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id"})
err := ConfirmAllowedToVouch(nil, lpaStoreResolvingService, nil, tc.notifyClient(t), tc.donorStore(t), "app://")(testAppData, w, r, &voucherdata.Provided{LpaID: "lpa-id"})
resp := w.Result()

assert.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/voucher/voucherpage/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func Register(
handleVoucher(voucher.PathYourName, None,
YourName(tmpls.Get("your_name.gohtml"), lpaStoreResolvingService, voucherStore))
handleVoucher(voucher.PathConfirmAllowedToVouch, None,
ConfirmAllowedToVouch(tmpls.Get("confirm_allowed_to_vouch.gohtml"), lpaStoreResolvingService, voucherStore, notifyClient, donorStore))
ConfirmAllowedToVouch(tmpls.Get("confirm_allowed_to_vouch.gohtml"), lpaStoreResolvingService, voucherStore, notifyClient, donorStore, appPublicURL))
handleVoucher(voucher.PathYouCannotVouchForDonor, None,
Guidance(tmpls.Get("you_cannot_vouch_for_donor.gohtml"), lpaStoreResolvingService))

Expand Down

0 comments on commit f01ca1e

Please sign in to comment.