-
Notifications
You must be signed in to change notification settings - Fork 2
/
choose_people_to_notify.go
145 lines (116 loc) · 4.31 KB
/
choose_people_to_notify.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
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 choosePeopleToNotifyData struct {
App page.AppData
Errors validation.List
Form *choosePeopleToNotifyForm
NameWarning *actor.SameNameWarning
}
func ChoosePeopleToNotify(tmpl template.Template, donorStore DonorStore, uuidString func() string) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
if len(donor.PeopleToNotify) > 4 {
return page.Paths.ChoosePeopleToNotifySummary.Redirect(w, r, appData, donor)
}
addAnother := r.FormValue("addAnother") == "1"
personToNotify, personFound := donor.PeopleToNotify.Get(r.URL.Query().Get("id"))
if r.Method == http.MethodGet && len(donor.PeopleToNotify) > 0 && personFound == false && addAnother == false {
return page.Paths.ChoosePeopleToNotifySummary.Redirect(w, r, appData, donor)
}
data := &choosePeopleToNotifyData{
App: appData,
Form: &choosePeopleToNotifyForm{
FirstNames: personToNotify.FirstNames,
LastName: personToNotify.LastName,
},
}
if r.Method == http.MethodPost {
data.Form = readChoosePeopleToNotifyForm(r)
data.Errors = data.Form.Validate()
nameWarning := actor.NewSameNameWarning(
actor.TypePersonToNotify,
personToNotifyMatches(donor, personToNotify.ID, 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.None() && data.NameWarning == nil {
if personFound == false {
personToNotify = actor.PersonToNotify{
FirstNames: data.Form.FirstNames,
LastName: data.Form.LastName,
ID: uuidString(),
}
donor.PeopleToNotify = append(donor.PeopleToNotify, personToNotify)
} else {
personToNotify.FirstNames = data.Form.FirstNames
personToNotify.LastName = data.Form.LastName
donor.PeopleToNotify.Put(personToNotify)
}
if !donor.Tasks.PeopleToNotify.Completed() {
donor.Tasks.PeopleToNotify = actor.TaskInProgress
}
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
return appData.Paths.ChoosePeopleToNotifyAddress.RedirectQuery(w, r, appData, donor, url.Values{"id": {personToNotify.ID}})
}
}
return tmpl(w, data)
}
}
type choosePeopleToNotifyForm struct {
FirstNames string
LastName string
IgnoreNameWarning string
}
func readChoosePeopleToNotifyForm(r *http.Request) *choosePeopleToNotifyForm {
return &choosePeopleToNotifyForm{
FirstNames: page.PostFormString(r, "first-names"),
LastName: page.PostFormString(r, "last-name"),
IgnoreNameWarning: page.PostFormString(r, "ignore-name-warning"),
}
}
func (f *choosePeopleToNotifyForm) 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 personToNotifyMatches(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 strings.EqualFold(attorney.FirstNames, firstNames) && strings.EqualFold(attorney.LastName, lastName) {
return actor.TypeReplacementAttorney
}
}
for _, person := range donor.PeopleToNotify {
if person.ID != id && strings.EqualFold(person.FirstNames, firstNames) && strings.EqualFold(person.LastName, lastName) {
return actor.TypePersonToNotify
}
}
return actor.TypeNone
}