-
Notifications
You must be signed in to change notification settings - Fork 2
/
certificate_provider_details.go
152 lines (125 loc) · 5.01 KB
/
certificate_provider_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
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 certificateProviderDetailsData struct {
App page.AppData
Errors validation.List
Form *certificateProviderDetailsForm
NameWarning *actor.SameNameWarning
}
func CertificateProviderDetails(tmpl template.Template, donorStore DonorStore) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &certificateProviderDetailsData{
App: appData,
Form: &certificateProviderDetailsForm{
FirstNames: donor.CertificateProvider.FirstNames,
LastName: donor.CertificateProvider.LastName,
HasNonUKMobile: donor.CertificateProvider.HasNonUKMobile,
},
}
if donor.CertificateProvider.HasNonUKMobile {
data.Form.NonUKMobile = donor.CertificateProvider.Mobile
} else {
data.Form.Mobile = donor.CertificateProvider.Mobile
}
if r.Method == http.MethodPost {
data.Form = readCertificateProviderDetailsForm(r)
data.Errors = data.Form.Validate()
sameNameWarning := actor.NewSameNameWarning(
actor.TypeCertificateProvider,
certificateProviderMatches(donor, data.Form.FirstNames, data.Form.LastName),
data.Form.FirstNames,
data.Form.LastName,
)
if data.Errors.Any() || data.Form.IgnoreNameWarning != sameNameWarning.String() {
data.NameWarning = sameNameWarning
}
if data.Errors.None() && data.NameWarning == nil {
donor.CertificateProvider.FirstNames = data.Form.FirstNames
donor.CertificateProvider.LastName = data.Form.LastName
donor.CertificateProvider.HasNonUKMobile = data.Form.HasNonUKMobile
if data.Form.HasNonUKMobile {
donor.CertificateProvider.Mobile = data.Form.NonUKMobile
} else {
donor.CertificateProvider.Mobile = data.Form.Mobile
}
if !donor.Tasks.CertificateProvider.Completed() {
donor.Tasks.CertificateProvider = actor.TaskInProgress
}
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
return page.Paths.HowDoYouKnowYourCertificateProvider.Redirect(w, r, appData, donor)
}
}
return tmpl(w, data)
}
}
type certificateProviderDetailsForm struct {
FirstNames string
LastName string
Mobile string
HasNonUKMobile bool
NonUKMobile string
IgnoreNameWarning string
IgnoreSimilarNameWarning bool
}
func readCertificateProviderDetailsForm(r *http.Request) *certificateProviderDetailsForm {
return &certificateProviderDetailsForm{
FirstNames: page.PostFormString(r, "first-names"),
LastName: page.PostFormString(r, "last-name"),
Mobile: page.PostFormString(r, "mobile"),
HasNonUKMobile: page.PostFormString(r, "has-non-uk-mobile") == "1",
NonUKMobile: page.PostFormString(r, "non-uk-mobile"),
IgnoreNameWarning: page.PostFormString(r, "ignore-name-warning"),
IgnoreSimilarNameWarning: page.PostFormString(r, "ignore-similar-name-warning") == "yes",
}
}
func (d *certificateProviderDetailsForm) Validate() validation.List {
var errors validation.List
errors.String("first-names", "firstNames", d.FirstNames,
validation.Empty())
errors.String("last-name", "lastName", d.LastName,
validation.Empty())
if d.HasNonUKMobile {
errors.String("non-uk-mobile", "yourCertificateProvidersMobileNumber", d.NonUKMobile,
validation.Empty(),
validation.NonUKMobile().ErrorLabel("enterAMobileNumberInTheCorrectFormat"))
} else {
errors.String("mobile", "yourCertificateProvidersUkMobileNumber", d.Mobile,
validation.Empty(),
validation.Mobile().ErrorLabel("enterAMobileNumberInTheCorrectFormat"))
}
return errors
}
func certificateProviderMatches(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.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
}