-
Notifications
You must be signed in to change notification settings - Fork 2
/
choose_attorneys.go
208 lines (167 loc) · 6.39 KB
/
choose_attorneys.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
package donor
import (
"net/http"
"net/url"
"strings"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/date"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type chooseAttorneysData struct {
App page.AppData
Errors validation.List
Donor *actor.DonorProvidedDetails
Form *chooseAttorneysForm
ShowDetails bool
DobWarning string
NameWarning *actor.SameNameWarning
}
func ChooseAttorneys(tmpl template.Template, donorStore DonorStore, newUID func() actoruid.UID) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
addAnother := r.FormValue("addAnother") == "1"
attorney, attorneyFound := donor.Attorneys.Get(actoruid.FromRequest(r))
if r.Method == http.MethodGet && len(donor.Attorneys.Attorneys) > 0 && !attorneyFound && !addAnother {
return page.Paths.ChooseAttorneysSummary.Redirect(w, r, appData, donor)
}
data := &chooseAttorneysData{
App: appData,
Donor: donor,
Form: &chooseAttorneysForm{
FirstNames: attorney.FirstNames,
LastName: attorney.LastName,
Email: attorney.Email,
Dob: attorney.DateOfBirth,
},
ShowDetails: attorneyFound == false && addAnother == false,
}
if r.Method == http.MethodPost {
data.Form = readChooseAttorneysForm(r)
data.Errors = data.Form.Validate()
dobWarning := data.Form.DobWarning()
nameWarning := actor.NewSameNameWarning(
actor.TypeAttorney,
attorneyMatches(donor, attorney.UID, data.Form.FirstNames, data.Form.LastName),
data.Form.FirstNames,
data.Form.LastName,
)
if data.Form.Dob != attorney.DateOfBirth && (data.Errors.Any() || data.Form.IgnoreDobWarning != dobWarning) {
data.DobWarning = dobWarning
}
if data.Form.NameHasChanged(attorney) && (data.Errors.Any() || data.Form.IgnoreNameWarning != nameWarning.String()) {
data.NameWarning = nameWarning
}
if data.Errors.None() && data.DobWarning == "" && data.NameWarning == nil {
if attorneyFound == false {
attorney = actor.Attorney{UID: newUID()}
}
attorney.FirstNames = data.Form.FirstNames
attorney.LastName = data.Form.LastName
attorney.Email = data.Form.Email
attorney.DateOfBirth = data.Form.Dob
donor.Attorneys.Put(attorney)
donor.Tasks.ChooseAttorneys = page.ChooseAttorneysState(donor.Attorneys, donor.AttorneyDecisions)
donor.Tasks.ChooseReplacementAttorneys = page.ChooseReplacementAttorneysState(donor)
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
return page.Paths.ChooseAttorneysAddress.RedirectQuery(w, r, appData, donor, url.Values{"id": {attorney.UID.String()}})
}
}
if !attorney.DateOfBirth.IsZero() {
data.DobWarning = data.Form.DobWarning()
}
return tmpl(w, data)
}
}
type chooseAttorneysForm struct {
FirstNames string
LastName string
Email string
Dob date.Date
IgnoreDobWarning string
IgnoreNameWarning string
}
func readChooseAttorneysForm(r *http.Request) *chooseAttorneysForm {
d := &chooseAttorneysForm{}
d.FirstNames = page.PostFormString(r, "first-names")
d.LastName = page.PostFormString(r, "last-name")
d.Email = page.PostFormString(r, "email")
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.IgnoreDobWarning = page.PostFormString(r, "ignore-dob-warning")
d.IgnoreNameWarning = page.PostFormString(r, "ignore-name-warning")
return d
}
func (f *chooseAttorneysForm) 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("email", "email", f.Email,
validation.Email())
errors.Date("date-of-birth", "dateOfBirth", f.Dob,
validation.DateMissing(),
validation.DateMustBeReal(),
validation.DateMustBePast())
return errors
}
func (f *chooseAttorneysForm) 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 "attorneyDateOfBirthIsUnder18"
}
}
return ""
}
func attorneyMatches(donor *actor.DonorProvidedDetails, uid actoruid.UID, firstNames, lastName string) actor.Type {
if firstNames == "" && lastName == "" {
return actor.TypeNone
}
if strings.EqualFold(donor.Donor.FirstNames, firstNames) && strings.EqualFold(donor.Donor.LastName, lastName) {
return actor.TypeDonor
}
for _, attorney := range donor.Attorneys.Attorneys {
if attorney.UID != uid && 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
}
func (f *chooseAttorneysForm) NameHasChanged(attorney actor.Attorney) bool {
return attorney.FirstNames != f.FirstNames || attorney.LastName != f.LastName
}