Skip to content

Commit

Permalink
Send to correct page from previous application
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx committed Oct 30, 2023
1 parent 83146dd commit 73cc358
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 53 deletions.
12 changes: 7 additions & 5 deletions internal/page/donor/previous_application_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
Expand All @@ -30,16 +29,18 @@ func PreviousApplicationNumber(tmpl template.Template, donorStore DonorStore) Ha

if data.Errors.None() {
if lpa.PreviousApplicationNumber != data.Form.PreviousApplicationNumber {
lpa.HasSentApplicationUpdatedEvent = false
lpa.PreviousApplicationNumber = data.Form.PreviousApplicationNumber
lpa.Tasks.YourDetails = actor.TaskCompleted

if err := donorStore.Put(r.Context(), lpa); err != nil {
return err
}
}

return appData.Redirect(w, r, lpa, page.Paths.TaskList.Format(lpa.ID))
if lpa.PreviousApplicationNumber[0] == '7' {
return appData.Redirect(w, r, lpa, page.Paths.WhatWasYourOriginalFee.Format(lpa.ID))
} else {
return appData.Redirect(w, r, lpa, page.Paths.WhatHappensAfterNoFee.Format(lpa.ID))
}
}
}

Expand All @@ -61,7 +62,8 @@ func (f *previousApplicationNumberForm) Validate() validation.List {
var errors validation.List

errors.String("previous-application-number", "previousApplicationNumber", f.PreviousApplicationNumber,
validation.Empty())
validation.Empty(),
validation.ReferenceNumber())

return errors
}
83 changes: 50 additions & 33 deletions internal/page/donor/previous_application_number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"testing"

"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -71,39 +70,46 @@ func TestGetPreviousApplicationNumberWhenTemplateErrors(t *testing.T) {
}

func TestPostPreviousApplicationNumber(t *testing.T) {
form := url.Values{
"previous-application-number": {"ABC"},
testcases := map[string]page.LpaPath{
"7": page.Paths.WhatWasYourOriginalFee,
"M": page.Paths.WhatHappensAfterNoFee,
}

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

donorStore := newMockDonorStore(t)
donorStore.
On("Put", r.Context(), &page.Lpa{
ID: "lpa-id",
UID: "lpa-uid",
PreviousApplicationNumber: "ABC",
Tasks: page.Tasks{YourDetails: actor.TaskCompleted},
}).
Return(nil)

err := PreviousApplicationNumber(nil, donorStore)(testAppData, w, r, &page.Lpa{
ID: "lpa-id",
UID: "lpa-uid",
HasSentApplicationUpdatedEvent: true,
})
resp := w.Result()

assert.Nil(t, err)
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, page.Paths.TaskList.Format("lpa-id"), resp.Header.Get("Location"))
for start, redirect := range testcases {
t.Run(start, func(t *testing.T) {
form := url.Values{
"previous-application-number": {start},
}

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

donorStore := newMockDonorStore(t)
donorStore.
On("Put", r.Context(), &page.Lpa{
ID: "lpa-id",
UID: "lpa-uid",
PreviousApplicationNumber: start,
}).
Return(nil)

err := PreviousApplicationNumber(nil, donorStore)(testAppData, w, r, &page.Lpa{
ID: "lpa-id",
UID: "lpa-uid",
})
resp := w.Result()

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

func TestPostPreviousApplicationNumberWhenNotChanged(t *testing.T) {
form := url.Values{
"previous-application-number": {"ABC"},
"previous-application-number": {"M-0000"},
}

w := httptest.NewRecorder()
Expand All @@ -113,18 +119,18 @@ func TestPostPreviousApplicationNumberWhenNotChanged(t *testing.T) {
err := PreviousApplicationNumber(nil, nil)(testAppData, w, r, &page.Lpa{
ID: "lpa-id",
UID: "lpa-uid",
PreviousApplicationNumber: "ABC",
PreviousApplicationNumber: "M-0000",
})
resp := w.Result()

assert.Nil(t, err)
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, page.Paths.TaskList.Format("lpa-id"), resp.Header.Get("Location"))
assert.Equal(t, page.Paths.WhatHappensAfterNoFee.Format("lpa-id"), resp.Header.Get("Location"))
}

func TestPostPreviousApplicationNumberWhenStoreErrors(t *testing.T) {
form := url.Values{
"previous-application-number": {"ABC"},
"previous-application-number": {"MABC"},
}

w := httptest.NewRecorder()
Expand Down Expand Up @@ -177,10 +183,21 @@ func TestPreviousApplicationNumberFormValidate(t *testing.T) {
form *previousApplicationNumberForm
errors validation.List
}{
"valid": {
"valid modernised": {
form: &previousApplicationNumberForm{
PreviousApplicationNumber: "M",
},
},
"valid old": {
form: &previousApplicationNumberForm{
PreviousApplicationNumber: "7",
},
},
"invalid": {
form: &previousApplicationNumberForm{
PreviousApplicationNumber: "A",
PreviousApplicationNumber: "x",
},
errors: validation.With("previous-application-number", validation.ReferenceNumberError{Label: "previousApplicationNumber"}),
},
"empty": {
form: &previousApplicationNumberForm{},
Expand Down
6 changes: 5 additions & 1 deletion internal/page/donor/which_fee_type_are_you_applying_for.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ func WhichFeeTypeAreYouApplyingFor(tmpl template.Template, donorStore DonorStore
return err
}

return appData.Redirect(w, r, lpa, page.Paths.EvidenceRequired.Format(lpa.ID))
if lpa.FeeType.IsRepeatApplicationFee() {
return appData.Redirect(w, r, lpa, page.Paths.PreviousApplicationNumber.Format(lpa.ID))

Check warning on line 39 in internal/page/donor/which_fee_type_are_you_applying_for.go

View check run for this annotation

Codecov / codecov/patch

internal/page/donor/which_fee_type_are_you_applying_for.go#L39

Added line #L39 was not covered by tests
} else {
return appData.Redirect(w, r, lpa, page.Paths.EvidenceRequired.Format(lpa.ID))
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions internal/page/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ type AppPaths struct {
UseExistingAddress LpaPath
WhatACertificateProviderDoes LpaPath
WhatHappensAfterNoFee LpaPath
WhatWasYourOriginalFee LpaPath
WhenCanTheLpaBeUsed LpaPath
WhichFeeTypeAreYouApplyingFor LpaPath
WithdrawThisLpa LpaPath
Expand Down Expand Up @@ -313,6 +314,7 @@ var Paths = AppPaths{
UseExistingAddress: "/use-existing-address",
WhatACertificateProviderDoes: "/what-a-certificate-provider-does",
WhatHappensAfterNoFee: "/what-happens-after-no-fee",
WhatWasYourOriginalFee: "/what-was-your-original-fee",
WhenCanTheLpaBeUsed: "/when-can-the-lpa-be-used",
WhichFeeTypeAreYouApplyingFor: "/which-fee-type-are-you-applying-for",
WithdrawThisLpa: "/withdraw-this-lpa",
Expand Down
14 changes: 14 additions & 0 deletions internal/validation/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,17 @@ func (c DateMustBePastCheck) CheckDate(label string, value date.Date) Formattabl
func DateMustBePast() DateMustBePastCheck {
return DateMustBePastCheck{}
}

type ReferenceNumberCheck struct{}

func (c ReferenceNumberCheck) CheckString(label, value string) FormattableError {
if value[0] != 'M' && value[0] != '7' {
return ReferenceNumberError{Label: label}
}

Check warning on line 369 in internal/validation/check.go

View check run for this annotation

Codecov / codecov/patch

internal/validation/check.go#L366-L369

Added lines #L366 - L369 were not covered by tests

return nil

Check warning on line 371 in internal/validation/check.go

View check run for this annotation

Codecov / codecov/patch

internal/validation/check.go#L371

Added line #L371 was not covered by tests
}

func ReferenceNumber() ReferenceNumberCheck {
return ReferenceNumberCheck{}

Check warning on line 375 in internal/validation/check.go

View check run for this annotation

Codecov / codecov/patch

internal/validation/check.go#L374-L375

Added lines #L374 - L375 were not covered by tests
}
10 changes: 10 additions & 0 deletions internal/validation/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ func (e DateMustBePastError) Format(l Localizer) string {
})
}

type ReferenceNumberError struct {
Label string
}

func (e ReferenceNumberError) Format(l Localizer) string {
return l.Format("errorReferenceNumber", map[string]any{
"Label": l.T(e.Label),
})

Check warning on line 159 in internal/validation/error.go

View check run for this annotation

Codecov / codecov/patch

internal/validation/error.go#L156-L159

Added lines #L156 - L159 were not covered by tests
}

func lowerFirst(s string) string {
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToLower(r)) + s[n:]
Expand Down
8 changes: 7 additions & 1 deletion lang/cy.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"errorSelect": "Dewiswch {{.Label}}",
"errorEmail": "{{.Label}} Welsh",
"errorPostcode": "{{.Label}} Welsh",
"errorReferenceNumber": "{{.Label}} Welsh",
"donorMatchesActorWarning": "Welsh {{.FirstNames}} {{.LastName}} {{.Type}}",
"attorneyMatchesActorWarning": "Welsh {{.FirstNames}} {{.LastName}} {{.Type}}",
"attorneyMatchesAttorneyWarning": "Welsh {{.FirstNames}} {{.LastName}}",
Expand Down Expand Up @@ -897,5 +898,10 @@
"youHaveWithdrawnLpaNumber": "Welsh <span class=\"govuk-!-font-weight-bold\">{{.UID}}</span>",
"opgWillNowContactAnyoneWhoHasAlreadyBeenContacted": "Welsh",
"withdrawn": "Welsh",
"dateLpaSigned": "Welsh"
"dateLpaSigned": "Welsh",
"whatIsYourPreviousReferenceNumber": "Welsh",
"whatIsYourPreviousReferenceNumberContent": "<p class=\"govuk-body\">Welsh</p>",
"whereCanFindReferenceNumber": "Welsh",
"whereCanFindReferenceNumberDetails": "<p class=\"govuk-body\">Welsh</p>",
"previousApplicationNumber": "Welsh"
}
8 changes: 7 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"errorSelect": "Select {{.Label}}",
"errorEmail": "{{.Label}} must be in the correct format, like [email protected]",
"errorPostcode": "{{.Label}} must be a UK postcode",
"errorReferenceNumber": "{{.Label}} must begin with 7 or M",
"donorMatchesActorWarning": "The donor’s name is also {{.FirstNames}} {{.LastName}}. The donor cannot be {{.Type}}.",
"attorneyMatchesActorWarning": "There is also an attorney called {{.FirstNames}} {{.LastName}}. An attorney cannot be {{.Type}}.",
"attorneyMatchesAttorneyWarning": "There is already an attorney called {{.FirstNames}} {{.LastName}}.",
Expand Down Expand Up @@ -841,5 +842,10 @@
"youHaveWithdrawnLpaNumber": "You have withdrawn LPA number <span class=\"govuk-!-font-weight-bold\">{{.UID}}</span>.",
"opgWillNowContactAnyoneWhoHasAlreadyBeenContacted": "OPG will now contact anyone who has already been contacted in relation to your LPA and notify them of your decision to withdraw.",
"withdrawn": "Withdrawn",
"dateLpaSigned": "Date LPA signed"
"dateLpaSigned": "Date LPA signed",
"whatIsYourPreviousReferenceNumber": "What is your previous reference number?",
"whatIsYourPreviousReferenceNumberContent": "<p class=\"govuk-body\">You’ve told us that you recently made an LPA application.</p><p class=\"govuk-body\">We need your previous reference number so that we can match your old application to your new one.</p>",
"whereCanFindReferenceNumber": "Where can I find my reference number?",
"whereCanFindReferenceNumberDetails": "<p class=\"govuk-body\">Your reference number (also called ‘OPG reference number’ or ‘Our ref’) is on any letter you have from the Office of the Public Guardian.</p><p class=\"govuk-body\">It should also be on the cover letter we sent you when we sent your original application back to you.</p>",
"previousApplicationNumber": "Previous reference number"
}
21 changes: 9 additions & 12 deletions web/template/previous_application_number.gohtml
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
{{ template "page" . }}

{{ define "pageTitle" }}{{ tr .App "previousApplicationNumber" }}{{ end }}
{{ define "pageTitle" }}{{ tr .App "whatIsYourPreviousReferenceNumber" }}{{ end }}

{{ define "main" }}
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<form novalidate method="post">
<div class="govuk-form-group {{ if .Errors.Has "previous-application-number" }}govuk-form-group--error{{ end }}">
<fieldset class="govuk-fieldset">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--xl">
<h1 class="govuk-fieldset__heading">{{ tr .App "previousApplicationNumber" }}</h1>
</legend>
{{ template "error-message" (errorMessage . "previous-application-number") }}
<h1 class="govuk-heading-xl">{{ tr .App "whatIsYourPreviousReferenceNumber" }}</h1>

{{ trHtml .App "whatIsYourPreviousReferenceNumberContent" }}

{{ template "input" (input . "previous-application-number" "previousApplicationNumber" .Form.PreviousApplicationNumber "classes" "govuk-input--width-20") }}
</fieldset>
</div>
{{ template "details" (details . "whereCanFindReferenceNumber" "whereCanFindReferenceNumberDetails" false) }}

<form novalidate method="post">
{{ template "input" (input . "previous-application-number" "previousApplicationNumber" .Form.PreviousApplicationNumber "labelClasses" "govuk-label--s" "classes" "govuk-input--width-20") }}

{{ template "continue-button" . }}
{{ template "save-and-return-buttons" . }}
{{ template "csrf-field" . }}
</form>
</div>
Expand Down

0 comments on commit 73cc358

Please sign in to comment.