-
Notifications
You must be signed in to change notification settings - Fork 2
/
change_mobile_number.go
114 lines (97 loc) · 3.42 KB
/
change_mobile_number.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
package donor
import (
"context"
"errors"
"net/http"
"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 changeMobileNumberData struct {
App page.AppData
Errors validation.List
Form *changeMobileNumberForm
ActorType actor.Type
FirstNames string
}
func ChangeMobileNumber(tmpl template.Template, witnessCodeSender WitnessCodeSender, actorType actor.Type) Handler {
var send func(context.Context, *actor.DonorProvidedDetails, page.Localizer) error
var redirect page.LpaPath
switch actorType {
case actor.TypeIndependentWitness:
send = witnessCodeSender.SendToIndependentWitness
redirect = page.Paths.WitnessingAsIndependentWitness
case actor.TypeCertificateProvider:
send = witnessCodeSender.SendToCertificateProvider
redirect = page.Paths.WitnessingAsCertificateProvider
default:
panic("ChangeMobileNumber only supports IndependentWitness or CertificateProvider actors")
}
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &changeMobileNumberData{
App: appData,
Form: &changeMobileNumberForm{},
ActorType: actorType,
FirstNames: donor.CertificateProvider.FirstNames,
}
if actorType == actor.TypeIndependentWitness {
data.FirstNames = donor.IndependentWitness.FirstNames
}
if r.Method == http.MethodPost {
data.Form = readChangeMobileNumberForm(r)
data.Errors = data.Form.Validate()
if data.Errors.None() {
if actorType == actor.TypeIndependentWitness {
donor.IndependentWitness.HasNonUKMobile = data.Form.HasNonUKMobile
if data.Form.HasNonUKMobile {
donor.IndependentWitness.Mobile = data.Form.NonUKMobile
} else {
donor.IndependentWitness.Mobile = data.Form.Mobile
}
} else {
donor.CertificateProvider.HasNonUKMobile = data.Form.HasNonUKMobile
if data.Form.HasNonUKMobile {
donor.CertificateProvider.Mobile = data.Form.NonUKMobile
} else {
donor.CertificateProvider.Mobile = data.Form.Mobile
}
}
if err := send(r.Context(), donor, appData.Localizer); err != nil {
if errors.Is(err, page.ErrTooManyWitnessCodeRequests) {
data.Errors.Add("request", validation.CustomError{Label: "pleaseWaitOneMinute"})
return tmpl(w, data)
}
return err
}
return redirect.Redirect(w, r, appData, donor)
}
}
return tmpl(w, data)
}
}
type changeMobileNumberForm struct {
Mobile string
HasNonUKMobile bool
NonUKMobile string
}
func readChangeMobileNumberForm(r *http.Request) *changeMobileNumberForm {
return &changeMobileNumberForm{
Mobile: page.PostFormString(r, "mobile"),
HasNonUKMobile: page.PostFormString(r, "has-non-uk-mobile") == "1",
NonUKMobile: page.PostFormString(r, "non-uk-mobile"),
}
}
func (f *changeMobileNumberForm) Validate() validation.List {
var errors validation.List
if f.HasNonUKMobile {
errors.String("non-uk-mobile", "aMobilePhoneNumber", f.NonUKMobile,
validation.Empty(),
validation.NonUKMobile().ErrorLabel("enterAMobileNumberInTheCorrectFormat"))
} else {
errors.String("mobile", "aUKMobileNumber", f.Mobile,
validation.Empty(),
validation.Mobile().ErrorLabel("enterAMobileNumberInTheCorrectFormat"))
}
return errors
}