-
Notifications
You must be signed in to change notification settings - Fork 2
/
your_independent_witness.go
123 lines (98 loc) · 3.67 KB
/
your_independent_witness.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
package donor
import (
"net/http"
"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 yourIndependentWitnessData struct {
App page.AppData
Errors validation.List
Form *yourIndependentWitnessForm
NameWarning *actor.SameNameWarning
}
func YourIndependentWitness(tmpl template.Template, donorStore DonorStore) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &yourIndependentWitnessData{
App: appData,
Form: &yourIndependentWitnessForm{
FirstNames: donor.IndependentWitness.FirstNames,
LastName: donor.IndependentWitness.LastName,
},
}
if r.Method == http.MethodPost {
data.Form = readYourIndependentWitnessForm(r)
data.Errors = data.Form.Validate()
nameWarning := actor.NewSameNameWarning(
actor.TypeIndependentWitness,
independentWitnessMatches(donor, data.Form.FirstNames, data.Form.LastName),
data.Form.FirstNames,
data.Form.LastName,
)
if data.Errors.Any() || data.Form.IgnoreNameWarning != nameWarning.String() {
data.NameWarning = nameWarning
}
if !data.Errors.Any() && data.NameWarning == nil {
donor.IndependentWitness.FirstNames = data.Form.FirstNames
donor.IndependentWitness.LastName = data.Form.LastName
if !donor.Tasks.ChooseYourSignatory.Completed() {
donor.Tasks.ChooseYourSignatory = actor.TaskInProgress
}
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
return page.Paths.YourIndependentWitnessMobile.Redirect(w, r, appData, donor)
}
}
return tmpl(w, data)
}
}
type yourIndependentWitnessForm struct {
FirstNames string
LastName string
IgnoreNameWarning string
}
func readYourIndependentWitnessForm(r *http.Request) *yourIndependentWitnessForm {
return &yourIndependentWitnessForm{
FirstNames: page.PostFormString(r, "first-names"),
LastName: page.PostFormString(r, "last-name"),
IgnoreNameWarning: page.PostFormString(r, "ignore-name-warning"),
}
}
func (f *yourIndependentWitnessForm) 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))
return errors
}
func independentWitnessMatches(donor *actor.DonorProvidedDetails, 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 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
}
if strings.EqualFold(donor.AuthorisedSignatory.FirstNames, firstNames) && strings.EqualFold(donor.AuthorisedSignatory.LastName, lastName) {
return actor.TypeAuthorisedSignatory
}
return actor.TypeNone
}