-
Notifications
You must be signed in to change notification settings - Fork 2
/
confirm_dont_want_to_be_certificate_provider_logged_out.go
100 lines (82 loc) · 3.4 KB
/
confirm_dont_want_to_be_certificate_provider_logged_out.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
package certificateprovider
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/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore"
"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 confirmDontWantToBeCertificateProviderDataLoggedOut struct {
App page.AppData
Errors validation.List
Lpa *lpastore.Lpa
}
func ConfirmDontWantToBeCertificateProviderLoggedOut(tmpl template.Template, shareCodeStore ShareCodeStore, lpaStoreResolvingService LpaStoreResolvingService, lpaStoreClient LpaStoreClient, donorStore DonorStore, sessionStore SessionStore, notifyClient NotifyClient, appPublicURL string) page.Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request) error {
session, err := sessionStore.LpaData(r)
if err != nil {
return err
}
ctx := page.ContextWithSessionData(r.Context(), &page.SessionData{LpaID: session.LpaID})
lpa, err := lpaStoreResolvingService.Get(ctx)
if err != nil {
return err
}
data := &confirmDontWantToBeCertificateProviderDataLoggedOut{
App: appData,
Lpa: lpa,
}
if r.Method == http.MethodPost {
var email notify.Email
if !lpa.SignedAt.IsZero() {
email = notify.CertificateProviderOptedOutPostWitnessingEmail{
CertificateProviderFirstNames: lpa.CertificateProvider.FirstNames,
CertificateProviderFullName: lpa.CertificateProvider.FullName(),
DonorFullName: lpa.Donor.FullName(),
LpaType: appData.Localizer.T(lpa.Type.String()),
LpaUID: lpa.LpaUID,
DonorStartPageURL: appPublicURL + page.Paths.Start.Format(),
}
if !lpa.CannotRegister {
if err := lpaStoreClient.SendCertificateProviderOptOut(ctx, lpa.LpaUID, actoruid.Service); err != nil {
return err
}
}
} else {
donor, err := donorStore.GetAny(ctx)
if err != nil {
return err
}
email = notify.CertificateProviderOptedOutPreWitnessingEmail{
CertificateProviderFullName: donor.CertificateProvider.FullName(),
DonorFullName: donor.Donor.FullName(),
LpaType: appData.Localizer.T(donor.Type.String()),
LpaUID: donor.LpaUID,
DonorStartPageURL: appPublicURL + page.Paths.Start.Format(),
}
donor.CertificateProvider = actor.CertificateProvider{}
donor.Tasks.CertificateProvider = actor.TaskNotStarted
donor.Tasks.CheckYourLpa = actor.TaskNotStarted
if err = donorStore.Put(ctx, donor); err != nil {
return err
}
}
shareCode, err := shareCodeStore.Get(r.Context(), actor.TypeCertificateProvider, r.URL.Query().Get("referenceNumber"))
if err != nil {
return err
}
if err := shareCodeStore.Delete(r.Context(), shareCode); err != nil {
return err
}
if err := notifyClient.SendActorEmail(ctx, lpa.Donor.Email, lpa.LpaUID, email); err != nil {
return err
}
return page.Paths.CertificateProvider.YouHaveDecidedNotToBeACertificateProvider.RedirectQuery(w, r, appData, url.Values{"donorFullName": {lpa.Donor.FullName()}})
}
return tmpl(w, data)
}
}