-
Notifications
You must be signed in to change notification settings - Fork 2
/
cloud_watch_event_handler.go
186 lines (145 loc) · 6.01 KB
/
cloud_watch_event_handler.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/app"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lambda"
"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/random"
"github.com/ministryofjustice/opg-modernising-lpa/internal/secrets"
"github.com/ministryofjustice/opg-modernising-lpa/internal/uid"
)
type cloudWatchEventHandler struct {
dynamoClient dynamodbClient
now func() time.Time
uidBaseURL string
cfg aws.Config
notifyIsProduction bool
notifyBaseURL string
appPublicURL string
eventBusName string
}
func (h *cloudWatchEventHandler) Handle(ctx context.Context, cloudWatchEvent events.CloudWatchEvent) error {
switch cloudWatchEvent.DetailType {
case "uid-requested":
uidStore := app.NewUidStore(h.dynamoClient, h.now)
uidClient := uid.New(h.uidBaseURL, lambda.New(h.cfg, v4.NewSigner(), &http.Client{Timeout: 10 * time.Second}, time.Now))
return handleUidRequested(ctx, uidStore, uidClient, cloudWatchEvent)
case "evidence-received":
return handleEvidenceReceived(ctx, h.dynamoClient, cloudWatchEvent)
case "reduced-fee-approved":
bundle, _ := localize.NewBundle("./lang/en.json", "./lang/cy.json")
//TODO do this in handleFeeApproved when/if we save lang preference in LPA
appData := page.AppData{Localizer: bundle.For(localize.En)}
secretsClient, err := secrets.NewClient(h.cfg, time.Hour)
if err != nil {
return fmt.Errorf("failed to create secrets client: %w", err)
}
notifyApiKey, err := secretsClient.Secret(ctx, secrets.GovUkNotify)
if err != nil {
return fmt.Errorf("failed to get notify API secret: %w", err)
}
notifyClient, err := notify.New(h.notifyIsProduction, h.notifyBaseURL, notifyApiKey, http.DefaultClient, event.NewClient(h.cfg, h.eventBusName))
if err != nil {
return err
}
shareCodeSender := page.NewShareCodeSender(app.NewShareCodeStore(h.dynamoClient), notifyClient, h.appPublicURL, random.String, event.NewClient(h.cfg, h.eventBusName))
return handleFeeApproved(ctx, h.dynamoClient, cloudWatchEvent, shareCodeSender, appData, h.now)
case "reduced-fee-declined":
return handleFeeDenied(ctx, h.dynamoClient, cloudWatchEvent, h.now)
case "more-evidence-required":
return handleMoreEvidenceRequired(ctx, h.dynamoClient, cloudWatchEvent, h.now)
default:
return fmt.Errorf("unknown cloudwatch event")
}
}
func handleUidRequested(ctx context.Context, uidStore UidStore, uidClient UidClient, e events.CloudWatchEvent) error {
var v event.UidRequested
if err := json.Unmarshal(e.Detail, &v); err != nil {
return fmt.Errorf("failed to unmarshal detail: %w", err)
}
uid, err := uidClient.CreateCase(ctx, &uid.CreateCaseRequestBody{Type: v.Type, Donor: v.Donor})
if err != nil {
return fmt.Errorf("failed to create case: %w", err)
}
if err := uidStore.Set(ctx, v.LpaID, v.DonorSessionID, v.OrganisationID, uid); err != nil {
return fmt.Errorf("failed to set uid: %w", err)
}
return nil
}
func handleEvidenceReceived(ctx context.Context, client dynamodbClient, event events.CloudWatchEvent) error {
var v uidEvent
if err := json.Unmarshal(event.Detail, &v); err != nil {
return fmt.Errorf("failed to unmarshal detail: %w", err)
}
var key dynamo.Key
if err := client.OneByUID(ctx, v.UID, &key); err != nil {
return fmt.Errorf("failed to resolve uid: %w", err)
}
if key.PK == "" {
return fmt.Errorf("PK missing from LPA in response")
}
if err := client.Put(ctx, map[string]string{"PK": key.PK, "SK": "#EVIDENCE_RECEIVED"}); err != nil {
return fmt.Errorf("failed to persist evidence received: %w", err)
}
return nil
}
func handleFeeApproved(ctx context.Context, client dynamodbClient, event events.CloudWatchEvent, shareCodeSender shareCodeSender, appData page.AppData, now func() time.Time) error {
var v uidEvent
if err := json.Unmarshal(event.Detail, &v); err != nil {
return fmt.Errorf("failed to unmarshal detail: %w", err)
}
donor, err := getDonorByLpaUID(ctx, client, v.UID)
if err != nil {
return err
}
donor.Tasks.PayForLpa = actor.PaymentTaskCompleted
if err := putDonor(ctx, donor, now, client); err != nil {
return fmt.Errorf("failed to update LPA task status: %w", err)
}
if err := shareCodeSender.SendCertificateProviderPrompt(ctx, appData, &donor); err != nil {
return fmt.Errorf("failed to send share code to certificate provider: %w", err)
}
return nil
}
func handleMoreEvidenceRequired(ctx context.Context, client dynamodbClient, event events.CloudWatchEvent, now func() time.Time) error {
var v uidEvent
if err := json.Unmarshal(event.Detail, &v); err != nil {
return fmt.Errorf("failed to unmarshal detail: %w", err)
}
donor, err := getDonorByLpaUID(ctx, client, v.UID)
if err != nil {
return err
}
donor.Tasks.PayForLpa = actor.PaymentTaskMoreEvidenceRequired
if err := putDonor(ctx, donor, now, client); err != nil {
return fmt.Errorf("failed to update LPA task status: %w", err)
}
return nil
}
func handleFeeDenied(ctx context.Context, client dynamodbClient, event events.CloudWatchEvent, now func() time.Time) error {
var v uidEvent
if err := json.Unmarshal(event.Detail, &v); err != nil {
return fmt.Errorf("failed to unmarshal detail: %w", err)
}
donor, err := getDonorByLpaUID(ctx, client, v.UID)
if err != nil {
return err
}
donor.Tasks.PayForLpa = actor.PaymentTaskDenied
if err := putDonor(ctx, donor, now, client); err != nil {
return fmt.Errorf("failed to update LPA task status: %w", err)
}
return nil
}