-
Notifications
You must be signed in to change notification settings - Fork 2
/
enter_correspondent_details.go
110 lines (92 loc) · 3.5 KB
/
enter_correspondent_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
package donor
import (
"net/http"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/form"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/place"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type enterCorrespondentDetailsData struct {
App page.AppData
Errors validation.List
Form *enterCorrespondentDetailsForm
NameWarning *actor.SameNameWarning
}
func EnterCorrespondentDetails(tmpl template.Template, donorStore DonorStore) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &enterCorrespondentDetailsData{
App: appData,
Form: &enterCorrespondentDetailsForm{
FirstNames: donor.Correspondent.FirstNames,
LastName: donor.Correspondent.LastName,
Email: donor.Correspondent.Email,
Organisation: donor.Correspondent.Organisation,
Telephone: donor.Correspondent.Telephone,
WantAddress: form.NewYesNoForm(donor.Correspondent.WantAddress),
},
}
if r.Method == http.MethodPost {
data.Form = readEnterCorrespondentDetailsForm(r)
data.Errors = data.Form.Validate()
if data.Errors.None() {
donor.Correspondent.FirstNames = data.Form.FirstNames
donor.Correspondent.LastName = data.Form.LastName
donor.Correspondent.Email = data.Form.Email
donor.Correspondent.Organisation = data.Form.Organisation
donor.Correspondent.Telephone = data.Form.Telephone
donor.Correspondent.WantAddress = data.Form.WantAddress.YesNo
var redirect page.LpaPath
if donor.Correspondent.WantAddress.IsNo() {
donor.Correspondent.Address = place.Address{}
donor.Tasks.AddCorrespondent = actor.TaskCompleted
redirect = page.Paths.WhoCanCorrespondentsDetailsBeSharedWith
} else {
if !donor.Tasks.AddCorrespondent.Completed() && donor.Correspondent.Address.Line1 == "" {
donor.Tasks.AddCorrespondent = actor.TaskInProgress
}
redirect = page.Paths.EnterCorrespondentAddress
}
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
return redirect.Redirect(w, r, appData, donor)
}
}
return tmpl(w, data)
}
}
type enterCorrespondentDetailsForm struct {
FirstNames string
LastName string
Email string
Organisation string
Telephone string
WantAddress *form.YesNoForm
}
func readEnterCorrespondentDetailsForm(r *http.Request) *enterCorrespondentDetailsForm {
return &enterCorrespondentDetailsForm{
FirstNames: page.PostFormString(r, "first-names"),
LastName: page.PostFormString(r, "last-name"),
Email: page.PostFormString(r, "email"),
Organisation: page.PostFormString(r, "organisation"),
Telephone: page.PostFormString(r, "telephone"),
WantAddress: form.ReadYesNoForm(r, "yesToAddAnAddress"),
}
}
func (f *enterCorrespondentDetailsForm) 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.Empty(),
validation.Email())
errors.String("telephone", "phoneNumber", f.Telephone,
validation.Telephone())
return errors.Append(f.WantAddress.Validate())
}