-
Notifications
You must be signed in to change notification settings - Fork 2
/
choose_replacement_attorneys.go
129 lines (103 loc) · 4.16 KB
/
choose_replacement_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
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/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type chooseReplacementAttorneysData struct {
App page.AppData
Errors validation.List
Donor *actor.DonorProvidedDetails
Form *chooseAttorneysForm
DobWarning string
NameWarning *actor.SameNameWarning
}
func ChooseReplacementAttorneys(tmpl template.Template, donorStore DonorStore, uuidString func() string) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
addAnother := r.FormValue("addAnother") == "1"
attorney, attorneyFound := donor.ReplacementAttorneys.Get(r.URL.Query().Get("id"))
if r.Method == http.MethodGet && donor.ReplacementAttorneys.Len() > 0 && !attorneyFound && !addAnother {
return page.Paths.ChooseReplacementAttorneysSummary.Redirect(w, r, appData, donor)
}
data := &chooseReplacementAttorneysData{
App: appData,
Donor: donor,
Form: &chooseAttorneysForm{
FirstNames: attorney.FirstNames,
LastName: attorney.LastName,
Email: attorney.Email,
Dob: attorney.DateOfBirth,
},
}
if r.Method == http.MethodPost {
data.Form = readChooseAttorneysForm(r)
data.Errors = data.Form.Validate()
dobWarning := data.Form.DobWarning()
nameWarning := actor.NewSameNameWarning(
actor.TypeReplacementAttorney,
replacementAttorneyMatches(donor, attorney.ID, 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() {
data.NameWarning = nameWarning
}
if data.Errors.None() && data.DobWarning == "" && data.NameWarning == nil {
if attorneyFound == false {
attorney = actor.Attorney{ID: uuidString()}
}
attorney.FirstNames = data.Form.FirstNames
attorney.LastName = data.Form.LastName
attorney.Email = data.Form.Email
attorney.DateOfBirth = data.Form.Dob
donor.ReplacementAttorneys.Put(attorney)
donor.Tasks.ChooseReplacementAttorneys = page.ChooseReplacementAttorneysState(donor)
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
return appData.Paths.ChooseReplacementAttorneysAddress.RedirectQuery(w, r, appData, donor, url.Values{"id": {attorney.ID}})
}
}
return tmpl(w, data)
}
}
func replacementAttorneyMatches(donor *actor.DonorProvidedDetails, id, 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 strings.EqualFold(attorney.FirstNames, firstNames) && strings.EqualFold(attorney.LastName, lastName) {
return actor.TypeAttorney
}
}
for _, attorney := range donor.ReplacementAttorneys.Attorneys {
if attorney.ID != id && 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
}