-
Notifications
You must be signed in to change notification settings - Fork 2
/
your_details.go
235 lines (190 loc) · 6.91 KB
/
your_details.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package donor
import (
"fmt"
"net/http"
"strings"
"github.com/gorilla/sessions"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/date"
"github.com/ministryofjustice/opg-modernising-lpa/internal/form"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type yourDetailsData struct {
App page.AppData
Errors validation.List
Form *yourDetailsForm
YesNoMaybeOptions actor.YesNoMaybeOptions
DobWarning string
NameWarning *actor.SameNameWarning
}
func YourDetails(tmpl template.Template, donorStore DonorStore, sessionStore sessions.Store) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &yourDetailsData{
App: appData,
Form: &yourDetailsForm{
FirstNames: donor.Donor.FirstNames,
LastName: donor.Donor.LastName,
OtherNames: donor.Donor.OtherNames,
Dob: donor.Donor.DateOfBirth,
CanSign: donor.Donor.ThinksCanSign,
},
YesNoMaybeOptions: actor.YesNoMaybeValues,
}
if r.Method == http.MethodGet && data.Form.FirstNames == "" {
if latestDonor, _ := donorStore.Latest(r.Context()); latestDonor != nil {
data.Form = &yourDetailsForm{
FirstNames: latestDonor.Donor.FirstNames,
LastName: latestDonor.Donor.LastName,
OtherNames: latestDonor.Donor.OtherNames,
Dob: latestDonor.Donor.DateOfBirth,
CanSign: latestDonor.Donor.ThinksCanSign,
}
}
}
if r.Method == http.MethodPost {
loginSession, err := sesh.Login(sessionStore, r)
if err != nil {
return err
}
if loginSession.Email == "" {
return fmt.Errorf("no email in login session")
}
data.Form = readYourDetailsForm(r)
data.Errors = data.Form.Validate()
dobWarning := data.Form.DobWarning()
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.IgnoreDobWarning != dobWarning {
data.DobWarning = dobWarning
}
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.DobWarning == "" && data.NameWarning == nil {
redirect := page.Paths.YourAddress
if donor.Donor.FirstNames != data.Form.FirstNames || donor.Donor.LastName != data.Form.LastName || donor.Donor.DateOfBirth != data.Form.Dob {
donor.Donor.FirstNames = data.Form.FirstNames
donor.Donor.LastName = data.Form.LastName
donor.Donor.DateOfBirth = data.Form.Dob
donor.HasSentApplicationUpdatedEvent = false
}
donor.Donor.OtherNames = data.Form.OtherNames
donor.Donor.ThinksCanSign = data.Form.CanSign
donor.Donor.Email = loginSession.Email
if donor.Donor.ThinksCanSign.IsYes() {
donor.Donor.CanSign = form.Yes
} else {
redirect = page.Paths.CheckYouCanSign
}
if !donor.Tasks.YourDetails.Completed() {
donor.Tasks.YourDetails = actor.TaskInProgress
}
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
return redirect.Redirect(w, r, appData, donor)
}
}
if !donor.Donor.DateOfBirth.IsZero() {
data.DobWarning = data.Form.DobWarning()
}
return tmpl(w, data)
}
}
type yourDetailsForm struct {
FirstNames string
LastName string
OtherNames string
Dob date.Date
CanSign actor.YesNoMaybe
CanSignError error
IgnoreDobWarning string
IgnoreNameWarning string
}
func readYourDetailsForm(r *http.Request) *yourDetailsForm {
d := &yourDetailsForm{}
d.FirstNames = page.PostFormString(r, "first-names")
d.LastName = page.PostFormString(r, "last-name")
d.OtherNames = page.PostFormString(r, "other-names")
d.Dob = date.New(
page.PostFormString(r, "date-of-birth-year"),
page.PostFormString(r, "date-of-birth-month"),
page.PostFormString(r, "date-of-birth-day"))
d.CanSign, d.CanSignError = actor.ParseYesNoMaybe(page.PostFormString(r, "can-sign"))
d.IgnoreDobWarning = page.PostFormString(r, "ignore-dob-warning")
d.IgnoreNameWarning = page.PostFormString(r, "ignore-name-warning")
return d
}
func (f *yourDetailsForm) 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))
errors.Date("date-of-birth", "dateOfBirth", f.Dob,
validation.DateMissing(),
validation.DateMustBeReal(),
validation.DateMustBePast())
errors.Error("can-sign", "yesIfCanSign", f.CanSignError,
validation.Selected())
return errors
}
func (f *yourDetailsForm) DobWarning() string {
var (
today = date.Today()
hundredYearsEarlier = today.AddDate(-100, 0, 0)
eighteenYearsEarlier = today.AddDate(-18, 0, 0)
)
if !f.Dob.IsZero() {
if f.Dob.Before(hundredYearsEarlier) {
return "dateOfBirthIsOver100"
}
if f.Dob.Before(today) && f.Dob.After(eighteenYearsEarlier) {
return "dateOfBirthIsUnder18"
}
}
return ""
}
func donorMatches(donor *actor.DonorProvidedDetails, firstNames, lastName string) actor.Type {
if firstNames == "" && lastName == "" {
return actor.TypeNone
}
for _, attorney := range donor.Attorneys.Attorneys {
if strings.EqualFold(attorney.FirstNames, firstNames) && strings.EqualFold(attorney.LastName, lastName) {
return actor.TypeAttorney
}
}
for _, attorney := range donor.ReplacementAttorneys.Attorneys {
if strings.EqualFold(attorney.FirstNames, firstNames) && strings.EqualFold(attorney.LastName, lastName) {
return actor.TypeReplacementAttorney
}
}
if strings.EqualFold(donor.CertificateProvider.FirstNames, firstNames) && strings.EqualFold(donor.CertificateProvider.LastName, lastName) {
return actor.TypeCertificateProvider
}
for _, person := range donor.PeopleToNotify {
if strings.EqualFold(person.FirstNames, firstNames) && strings.EqualFold(person.LastName, lastName) {
return actor.TypePersonToNotify
}
}
if strings.EqualFold(donor.AuthorisedSignatory.FirstNames, firstNames) && strings.EqualFold(donor.AuthorisedSignatory.LastName, lastName) {
return actor.TypeAuthorisedSignatory
}
if strings.EqualFold(donor.IndependentWitness.FirstNames, firstNames) && strings.EqualFold(donor.IndependentWitness.LastName, lastName) {
return actor.TypeIndependentWitness
}
return actor.TypeNone
}