-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_your_lpa.go
161 lines (133 loc) · 5.21 KB
/
check_your_lpa.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package donor
import (
"context"
"errors"
"net/http"
"net/url"
"time"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type checkYourLpaData struct {
App page.AppData
Errors validation.List
Donor *actor.DonorProvidedDetails
Form *checkYourLpaForm
Completed bool
CanContinue bool
}
type checkYourLpaNotifier struct {
notifyClient NotifyClient
shareCodeSender ShareCodeSender
certificateProviderStore CertificateProviderStore
}
func (n *checkYourLpaNotifier) Notify(ctx context.Context, appData page.AppData, donor *actor.DonorProvidedDetails, wasCompleted bool) error {
if donor.CertificateProvider.CarryOutBy.IsPaper() {
return n.sendPaperNotification(ctx, appData, donor, wasCompleted)
}
return n.sendOnlineNotification(ctx, appData, donor, wasCompleted)
}
func (n *checkYourLpaNotifier) sendPaperNotification(ctx context.Context, appData page.AppData, donor *actor.DonorProvidedDetails, wasCompleted bool) error {
var sms notify.SMS
if wasCompleted {
sms = notify.CertificateProviderActingOnPaperDetailsChangedSMS{
DonorFullName: donor.Donor.FullName(),
DonorFirstNames: donor.Donor.FirstNames,
LpaUID: donor.LpaUID,
}
} else {
sms = notify.CertificateProviderActingOnPaperMeetingPromptSMS{
DonorFullName: donor.Donor.FullName(),
DonorFirstNames: donor.Donor.FirstNames,
LpaType: localize.LowerFirst(appData.Localizer.T(donor.Type.String())),
CertificateProviderStartPageURL: appData.AppPublicURL + appData.Lang.URL(page.Paths.CertificateProviderStart.Format()),
}
}
_, err := n.notifyClient.SendSMS(ctx, donor.CertificateProvider.Mobile, sms)
return err
}
func (n *checkYourLpaNotifier) sendOnlineNotification(ctx context.Context, appData page.AppData, donor *actor.DonorProvidedDetails, wasCompleted bool) error {
if !wasCompleted {
return n.shareCodeSender.SendCertificateProviderInvite(ctx, appData, donor)
}
certificateProvider, err := n.certificateProviderStore.GetAny(ctx)
if err != nil && !errors.Is(err, dynamo.NotFoundError{}) {
return err
}
var sms notify.SMS
if certificateProvider.Tasks.ConfirmYourDetails.NotStarted() {
sms = notify.CertificateProviderActingDigitallyHasNotConfirmedPersonalDetailsLPADetailsChangedPromptSMS{
LpaType: localize.LowerFirst(appData.Localizer.T(donor.Type.String())),
DonorFullName: donor.Donor.FullName(),
}
} else {
sms = notify.CertificateProviderActingDigitallyHasConfirmedPersonalDetailsLPADetailsChangedPromptSMS{
LpaType: localize.LowerFirst(appData.Localizer.T(donor.Type.String())),
DonorFullNamePossessive: appData.Localizer.Possessive(donor.Donor.FullName()),
DonorFirstNames: donor.Donor.FirstNames,
}
}
_, err = n.notifyClient.SendSMS(ctx, donor.CertificateProvider.Mobile, sms)
return err
}
func CheckYourLpa(tmpl template.Template, donorStore DonorStore, shareCodeSender ShareCodeSender, notifyClient NotifyClient, certificateProviderStore CertificateProviderStore, now func() time.Time) Handler {
notifier := &checkYourLpaNotifier{
notifyClient: notifyClient,
shareCodeSender: shareCodeSender,
certificateProviderStore: certificateProviderStore,
}
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, donor *actor.DonorProvidedDetails) error {
data := &checkYourLpaData{
App: appData,
Donor: donor,
Form: &checkYourLpaForm{
CheckedAndHappy: !donor.CheckedAt.IsZero(),
},
Completed: donor.Tasks.CheckYourLpa.Completed(),
CanContinue: donor.CheckedHash != donor.Hash,
}
if r.Method == http.MethodPost && data.CanContinue {
data.Form = readCheckYourLpaForm(r)
data.Errors = data.Form.Validate()
if data.Errors.None() {
donor.Tasks.CheckYourLpa = actor.TaskCompleted
donor.CheckedAt = now()
newHash, err := donor.GenerateHash()
if err != nil {
return err
}
donor.CheckedHash = newHash
if err := donorStore.Put(r.Context(), donor); err != nil {
return err
}
if err := notifier.Notify(r.Context(), appData, donor, data.Completed); err != nil {
return err
}
if !data.Completed {
return page.Paths.LpaDetailsSaved.RedirectQuery(w, r, appData, donor, url.Values{"firstCheck": {"1"}})
}
return page.Paths.LpaDetailsSaved.Redirect(w, r, appData, donor)
}
}
return tmpl(w, data)
}
}
type checkYourLpaForm struct {
CheckedAndHappy bool
}
func readCheckYourLpaForm(r *http.Request) *checkYourLpaForm {
return &checkYourLpaForm{
CheckedAndHappy: page.PostFormString(r, "checked-and-happy") == "1",
}
}
func (f *checkYourLpaForm) Validate() validation.List {
var errors validation.List
errors.Bool("checked-and-happy", "theBoxIfYouHaveCheckedAndHappyToShareLpa", f.CheckedAndHappy,
validation.Selected())
return errors
}