-
Notifications
You must be signed in to change notification settings - Fork 2
/
factory.go
181 lines (147 loc) · 4.99 KB
/
factory.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
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"time"
"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/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lambda"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"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/random"
"github.com/ministryofjustice/opg-modernising-lpa/internal/search"
"github.com/ministryofjustice/opg-modernising-lpa/internal/secrets"
"github.com/ministryofjustice/opg-modernising-lpa/internal/uid"
)
type LambdaClient interface {
Do(*http.Request) (*http.Response, error)
}
type LpaStoreClient interface {
SendLpa(ctx context.Context, donor *actor.DonorProvidedDetails) error
Lpa(ctx context.Context, uid string) (*lpastore.Lpa, error)
}
type SecretsClient interface {
Secret(ctx context.Context, name string) (string, error)
}
type ShareCodeSender interface {
SendCertificateProviderInvite(context.Context, page.AppData, page.CertificateProviderInvite) error
SendCertificateProviderPrompt(context.Context, page.AppData, *actor.DonorProvidedDetails) error
SendAttorneys(context.Context, page.AppData, *lpastore.Lpa) error
}
type UidStore interface {
Set(ctx context.Context, lpaID, sessionID, organisationID, uid string) error
}
type UidClient interface {
CreateCase(context.Context, *uid.CreateCaseRequestBody) (string, error)
}
type Factory struct {
logger *slog.Logger
now func() time.Time
uuidString func() string
cfg aws.Config
dynamoClient dynamodbClient
appPublicURL string
lpaStoreBaseURL string
uidBaseURL string
notifyBaseURL string
notifyIsProduction bool
eventBusName string
searchEndpoint string
searchIndexName string
searchIndexingEnabled bool
// previously constructed values
appData *page.AppData
lambdaClient LambdaClient
secretsClient SecretsClient
shareCodeSender ShareCodeSender
lpaStoreClient LpaStoreClient
uidStore UidStore
uidClient UidClient
}
func (f *Factory) Now() func() time.Time {
return f.now
}
func (f *Factory) DynamoClient() dynamodbClient {
return f.dynamoClient
}
func (f *Factory) UuidString() func() string {
return f.uuidString
}
func (f *Factory) AppData() (page.AppData, error) {
if f.appData == nil {
bundle, err := localize.NewBundle("./lang/en.json", "./lang/cy.json")
if err != nil {
return page.AppData{}, err
}
//TODO do this in handleFeeApproved when/if we save lang preference in LPA
f.appData = &page.AppData{Localizer: bundle.For(localize.En)}
}
return *f.appData, nil
}
func (f *Factory) LambdaClient() LambdaClient {
if f.lambdaClient == nil {
f.lambdaClient = lambda.New(f.cfg, v4.NewSigner(), &http.Client{Timeout: 10 * time.Second}, time.Now)
}
return f.lambdaClient
}
func (f *Factory) SecretsClient() (SecretsClient, error) {
if f.secretsClient == nil {
client, err := secrets.NewClient(f.cfg, time.Hour)
if err != nil {
return nil, err
}
f.secretsClient = client
}
return f.secretsClient, nil
}
func (f *Factory) ShareCodeSender(ctx context.Context) (ShareCodeSender, error) {
if f.shareCodeSender == nil {
secretsClient, err := f.SecretsClient()
if err != nil {
return nil, err
}
notifyApiKey, err := secretsClient.Secret(ctx, secrets.GovUkNotify)
if err != nil {
return nil, fmt.Errorf("failed to get notify API secret: %w", err)
}
notifyClient, err := notify.New(f.logger, f.notifyIsProduction, f.notifyBaseURL, notifyApiKey, http.DefaultClient, event.NewClient(f.cfg, f.eventBusName))
if err != nil {
return nil, err
}
f.shareCodeSender = page.NewShareCodeSender(app.NewShareCodeStore(f.dynamoClient), notifyClient, f.appPublicURL, random.String, event.NewClient(f.cfg, f.eventBusName))
}
return f.shareCodeSender, nil
}
func (f *Factory) LpaStoreClient() (LpaStoreClient, error) {
if f.lpaStoreClient == nil {
secretsClient, err := f.SecretsClient()
if err != nil {
return nil, err
}
f.lpaStoreClient = lpastore.New(f.lpaStoreBaseURL, secretsClient, f.LambdaClient())
}
return f.lpaStoreClient, nil
}
func (f *Factory) UidStore() (UidStore, error) {
if f.uidStore == nil {
searchClient, err := search.NewClient(f.cfg, f.searchEndpoint, f.searchIndexName, f.searchIndexingEnabled)
if err != nil {
return nil, err
}
f.uidStore = app.NewUidStore(f.dynamoClient, searchClient, f.now)
}
return f.uidStore, nil
}
func (f *Factory) UidClient() UidClient {
if f.uidClient == nil {
f.uidClient = uid.New(f.uidBaseURL, f.LambdaClient())
}
return f.uidClient
}