-
Notifications
You must be signed in to change notification settings - Fork 2
/
resend_witness_code.go
47 lines (37 loc) · 1.31 KB
/
resend_witness_code.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
package donor
import (
"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 resendWitnessCodeData struct {
App page.AppData
Errors validation.List
}
func ResendWitnessCode(tmpl template.Template, witnessCodeSender WitnessCodeSender, actorType actor.Type) Handler {
send := witnessCodeSender.SendToCertificateProvider
redirect := page.Paths.WitnessingAsCertificateProvider
if actorType == actor.TypeIndependentWitness {
send = witnessCodeSender.SendToIndependentWitness
redirect = page.Paths.WitnessingAsIndependentWitness
}
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &resendWitnessCodeData{
App: appData,
}
if r.Method == http.MethodPost {
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)
}
}