-
Notifications
You must be signed in to change notification settings - Fork 2
/
your_name.go
104 lines (83 loc) · 2.83 KB
/
your_name.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package donor
import (
"fmt"
"net/http"
"net/url"
"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"
)
type yourNameData struct {
App page.AppData
Errors validation.List
Form *yourNameForm
NameWarning *actor.SameNameWarning
}
func YourName(tmpl template.Template, donorStore DonorStore) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &yourNameData{
App: appData,
Form: &yourNameForm{
FirstNames: donor.Donor.FirstNames,
LastName: donor.Donor.LastName,
OtherNames: donor.Donor.OtherNames,
},
}
if r.Method == http.MethodPost {
data.Form = readYourNameForm(r)
data.Errors = data.Form.Validate()
nameWarning := actor.NewSameNameWarning(
actor.TypeDonor,
donorMatches(donor, data.Form.FirstNames, data.Form.LastName),
data.Form.FirstNames,
data.Form.LastName,
)
if data.Errors.Any() ||
data.Form.IgnoreNameWarning != nameWarning.String() &&
donor.Donor.FullName() != fmt.Sprintf("%s %s", data.Form.FirstNames, data.Form.LastName) {
data.NameWarning = nameWarning
}
if !data.Errors.Any() && data.NameWarning == nil {
if !donor.NamesChanged(data.Form.FirstNames, data.Form.LastName, data.Form.OtherNames) {
return page.Paths.MakeANewLPA.Redirect(w, r, appData, donor)
}
donor.Donor.FirstNames = data.Form.FirstNames
donor.Donor.LastName = data.Form.LastName
donor.Donor.OtherNames = data.Form.OtherNames
donor.HasSentApplicationUpdatedEvent = false
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
return page.Paths.WeHaveUpdatedYourDetails.RedirectQuery(w, r, appData, donor, url.Values{"detail": {"name"}})
}
}
return tmpl(w, data)
}
}
type yourNameForm struct {
FirstNames string
LastName string
OtherNames string
IgnoreNameWarning string
}
func readYourNameForm(r *http.Request) *yourNameForm {
d := &yourNameForm{}
d.FirstNames = page.PostFormString(r, "first-names")
d.LastName = page.PostFormString(r, "last-name")
d.OtherNames = page.PostFormString(r, "other-names")
d.IgnoreNameWarning = page.PostFormString(r, "ignore-name-warning")
return d
}
func (f *yourNameForm) Validate() validation.List {
var errors validation.List
errors.String("first-names", "firstNames", f.FirstNames,
validation.Empty(),
validation.StringTooLong(53))
errors.String("last-name", "lastName", f.LastName,
validation.Empty(),
validation.StringTooLong(61))
errors.String("other-names", "otherNamesYouAreKnownBy", f.OtherNames,
validation.StringTooLong(50))
return errors
}