-
Notifications
You must be signed in to change notification settings - Fork 2
/
what_is_your_home_address.go
101 lines (83 loc) · 2.98 KB
/
what_is_your_home_address.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
package certificateproviderpage
import (
"context"
"errors"
"log/slog"
"net/http"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/certificateprovider/certificateproviderdata"
"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 whatIsYourHomeAddressData struct {
App page.AppData
Addresses []place.Address
Form *form.AddressForm
Errors validation.List
}
func WhatIsYourHomeAddress(logger Logger, tmpl template.Template, addressClient AddressClient, certificateProviderStore CertificateProviderStore) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, certificateProvider *certificateproviderdata.Provided) error {
f := form.NewAddressForm()
f.Action = "postcode"
data := &whatIsYourHomeAddressData{
App: appData,
Form: f,
}
if certificateProvider.HomeAddress.Line1 != "" {
data.Form.Action = "manual"
data.Form.Address = &certificateProvider.HomeAddress
}
if r.Method == http.MethodPost {
data.Form = form.ReadAddressForm(r)
data.Errors = data.Form.Validate(true)
switch data.Form.Action {
case "manual":
if data.Errors.None() {
certificateProvider.HomeAddress = *data.Form.Address
if err := certificateProviderStore.Put(r.Context(), certificateProvider); err != nil {
return err
}
return page.Paths.CertificateProvider.YourPreferredLanguage.Redirect(w, r, appData, certificateProvider.LpaID)
}
case "postcode-select":
if data.Errors.None() {
data.Form.Action = "manual"
} else {
lookupAddress(r.Context(), logger, addressClient, data)
}
case "postcode-lookup":
if data.Errors.None() {
lookupAddress(r.Context(), logger, addressClient, data)
} else {
data.Form.Action = "postcode"
}
}
}
if r.Method == http.MethodGet {
action := r.FormValue(data.Form.FieldNames.Action)
if action == "manual" {
data.Form.Action = "manual"
data.Form.Address = &place.Address{}
}
}
return tmpl(w, data)
}
}
func lookupAddress(ctx context.Context, logger Logger, addressClient AddressClient, data *whatIsYourHomeAddressData) {
addresses, err := addressClient.LookupPostcode(ctx, data.Form.LookupPostcode)
if err != nil {
logger.InfoContext(ctx, "postcode lookup", slog.Any("err", err))
if errors.As(err, &place.BadRequestError{}) {
data.Errors.Add("lookup-postcode", validation.EnterError{Label: "invalidPostcode"})
} else {
data.Errors.Add("lookup-postcode", validation.CustomError{Label: "couldNotLookupPostcode"})
}
data.Form.Action = "postcode"
} else if len(addresses) == 0 {
data.Errors.Add("lookup-postcode", validation.CustomError{Label: "noYourAddressesFound"})
data.Form.Action = "postcode"
}
data.Addresses = addresses
}