-
Notifications
You must be signed in to change notification settings - Fork 2
/
your_date_of_birth.go
107 lines (84 loc) · 2.7 KB
/
your_date_of_birth.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
package donor
import (
"net/http"
"net/url"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/date"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type yourDateOfBirthData struct {
App page.AppData
Errors validation.List
Form *yourDateOfBirthForm
DobWarning string
}
func YourDateOfBirth(tmpl template.Template, donorStore DonorStore) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &yourDateOfBirthData{
App: appData,
Form: &yourDateOfBirthForm{
Dob: donor.Donor.DateOfBirth,
},
}
if r.Method == http.MethodPost {
data.Form = readYourDateOfBirthForm(r)
data.Errors = data.Form.Validate()
dobWarning := data.Form.DobWarning()
if data.Errors.Any() || data.Form.IgnoreDobWarning != dobWarning {
data.DobWarning = dobWarning
}
if data.Errors.None() && data.DobWarning == "" {
if donor.Donor.DateOfBirth == data.Form.Dob {
return page.Paths.MakeANewLPA.Redirect(w, r, appData, donor)
}
donor.Donor.DateOfBirth = data.Form.Dob
donor.HasSentApplicationUpdatedEvent = false
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
return page.Paths.WeHaveUpdatedYourDetails.RedirectQuery(w, r, appData, donor, url.Values{"detail": {"dateOfBirth"}})
}
}
data.DobWarning = data.Form.DobWarning()
return tmpl(w, data)
}
}
type yourDateOfBirthForm struct {
Dob date.Date
IgnoreDobWarning string
}
func readYourDateOfBirthForm(r *http.Request) *yourDateOfBirthForm {
d := &yourDateOfBirthForm{}
d.Dob = date.New(
page.PostFormString(r, "date-of-birth-year"),
page.PostFormString(r, "date-of-birth-month"),
page.PostFormString(r, "date-of-birth-day"))
d.IgnoreDobWarning = page.PostFormString(r, "ignore-dob-warning")
return d
}
func (f *yourDateOfBirthForm) Validate() validation.List {
var errors validation.List
errors.Date("date-of-birth", "dateOfBirth", f.Dob,
validation.DateMissing(),
validation.DateMustBeReal(),
validation.DateMustBePast())
return errors
}
func (f *yourDateOfBirthForm) DobWarning() string {
var (
today = date.Today()
hundredYearsEarlier = today.AddDate(-100, 0, 0)
eighteenYearsEarlier = today.AddDate(-18, 0, 0)
)
if !f.Dob.IsZero() {
if f.Dob.Before(hundredYearsEarlier) {
return "dateOfBirthIsOver100"
}
if f.Dob.Before(today) && f.Dob.After(eighteenYearsEarlier) {
return "dateOfBirthIsUnder18"
}
}
return ""
}