Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MLPAB-1767 Send notification when lpa created in lpa-store #1695

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions cmd/event-received/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@

// previously constructed values
appData *appcontext.Data
bundle *localize.Bundle
bundle Bundle
lambdaClient LambdaClient
secretsClient SecretsClient
shareCodeSender ShareCodeSender
lpaStoreClient LpaStoreClient
uidStore UidStore
uidClient UidClient
scheduledStore ScheduledStore
notifyClient NotifyClient
}

func (f *Factory) Now() func() time.Time {
Expand All @@ -96,7 +97,7 @@
return f.uuidString
}

func (f *Factory) Bundle() (*localize.Bundle, error) {
func (f *Factory) Bundle() (Bundle, error) {
if f.bundle == nil {
bundle, err := localize.NewBundle("./lang/en.json", "./lang/cy.json")
if err != nil {
Expand Down Expand Up @@ -146,22 +147,7 @@

func (f *Factory) ShareCodeSender(ctx context.Context) (ShareCodeSender, error) {
if f.shareCodeSender == nil {
bundle, err := f.Bundle()
if err != nil {
return nil, err
}

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, f.httpClient, event.NewClient(f.cfg, f.eventBusName), bundle)
notifyClient, err := f.NotifyClient(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -221,3 +207,31 @@

return f.scheduledStore
}

func (f *Factory) NotifyClient(ctx context.Context) (NotifyClient, error) {
if f.notifyClient == nil {
bundle, err := f.Bundle()
if err != nil {
return nil, err
}

secretsClient, err := f.SecretsClient()
if err != nil {
return nil, err
}

Check warning on line 221 in cmd/event-received/factory.go

View check run for this annotation

Codecov / codecov/patch

cmd/event-received/factory.go#L220-L221

Added lines #L220 - L221 were not covered by tests

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, f.httpClient, event.NewClient(f.cfg, f.eventBusName), bundle)
if err != nil {
return nil, err
}

f.notifyClient = notifyClient
}

return f.notifyClient, nil
}
65 changes: 41 additions & 24 deletions cmd/event-received/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ import (
"github.com/stretchr/testify/assert"
)

func TestNow(t *testing.T) {
func TestFactoryNow(t *testing.T) {
factory := &Factory{now: testNowFn}

assert.Equal(t, testNow, factory.Now()())
}

func TestDynamoClient(t *testing.T) {
func TestFactoryDynamoClient(t *testing.T) {
dynamoClient := newMockDynamodbClient(t)
factory := &Factory{dynamoClient: dynamoClient}

assert.Equal(t, dynamoClient, factory.DynamoClient())
}

func TestUuidString(t *testing.T) {
func TestFactoryUuidString(t *testing.T) {
factory := &Factory{uuidString: testUuidStringFn}

assert.Equal(t, testUuidString, factory.UuidString()())
}

func TestAppData(t *testing.T) {
func TestFactoryAppData(t *testing.T) {
factory := &Factory{}

appData, err := factory.AppData()
assert.Error(t, err)
assert.Equal(t, appcontext.Data{}, appData)
}

func TestAppDataWhenSet(t *testing.T) {
func TestFactoryAppDataWhenSet(t *testing.T) {
expected := appcontext.Data{Page: "hi"}
factory := &Factory{appData: &expected}

Expand All @@ -47,14 +47,14 @@ func TestAppDataWhenSet(t *testing.T) {
assert.Equal(t, expected, appData)
}

func TestLambdaClient(t *testing.T) {
func TestFactoryLambdaClient(t *testing.T) {
factory := &Factory{}

client := factory.LambdaClient()
assert.NotNil(t, client)
}

func TestLambdaClientWhenSet(t *testing.T) {
func TestFactoryLambdaClientWhenSet(t *testing.T) {
expected := newMockLambdaClient(t)

factory := &Factory{lambdaClient: expected}
Expand All @@ -63,15 +63,15 @@ func TestLambdaClientWhenSet(t *testing.T) {
assert.Equal(t, expected, client)
}

func TestSecretsClient(t *testing.T) {
func TestFactorySecretsClient(t *testing.T) {
factory := &Factory{}

client, err := factory.SecretsClient()
assert.Nil(t, err)
assert.NotNil(t, client)
}

func TestSecretsClientWhenSet(t *testing.T) {
func TestFactorySecretsClientWhenSet(t *testing.T) {
expected := newMockSecretsClient(t)

factory := &Factory{secretsClient: expected}
Expand All @@ -81,7 +81,7 @@ func TestSecretsClientWhenSet(t *testing.T) {
assert.Equal(t, expected, client)
}

func TestShareCodeSender(t *testing.T) {
func TestFactoryShareCodeSender(t *testing.T) {
ctx := context.Background()

secretsClient := newMockSecretsClient(t)
Expand All @@ -96,7 +96,7 @@ func TestShareCodeSender(t *testing.T) {
assert.NotNil(t, sender)
}

func TestShareCodeSenderWhenSet(t *testing.T) {
func TestFactoryShareCodeSenderWhenSet(t *testing.T) {
ctx := context.Background()

expected := newMockShareCodeSender(t)
Expand All @@ -108,7 +108,7 @@ func TestShareCodeSenderWhenSet(t *testing.T) {
assert.Equal(t, expected, sender)
}

func TestShareCodeSenderWhenBundleError(t *testing.T) {
func TestFactoryShareCodeSenderWhenBundleError(t *testing.T) {
ctx := context.Background()

factory := &Factory{}
Expand All @@ -117,7 +117,7 @@ func TestShareCodeSenderWhenBundleError(t *testing.T) {
assert.ErrorIs(t, err, os.ErrNotExist)
}

func TestShareCodeSenderWhenSecretsClientError(t *testing.T) {
func TestFactoryShareCodeSenderWhenSecretsClientError(t *testing.T) {
ctx := context.Background()

secretsClient := newMockSecretsClient(t)
Expand All @@ -131,7 +131,7 @@ func TestShareCodeSenderWhenSecretsClientError(t *testing.T) {
assert.ErrorIs(t, err, expectedError)
}

func TestShareCodeSenderWhenNotifyClientError(t *testing.T) {
func TestFactoryShareCodeSenderWhenNotifyClientError(t *testing.T) {
ctx := context.Background()

secretsClient := newMockSecretsClient(t)
Expand All @@ -145,7 +145,7 @@ func TestShareCodeSenderWhenNotifyClientError(t *testing.T) {
assert.NotNil(t, err)
}

func TestLpaStoreClient(t *testing.T) {
func TestFactoryLpaStoreClient(t *testing.T) {
secretsClient := newMockSecretsClient(t)

factory := &Factory{secretsClient: secretsClient}
Expand All @@ -155,7 +155,7 @@ func TestLpaStoreClient(t *testing.T) {
assert.NotNil(t, client)
}

func TestLpaStoreClientWhenSet(t *testing.T) {
func TestFactoryLpaStoreClientWhenSet(t *testing.T) {
expected := newMockLpaStoreClient(t)

factory := &Factory{lpaStoreClient: expected}
Expand All @@ -165,15 +165,15 @@ func TestLpaStoreClientWhenSet(t *testing.T) {
assert.Equal(t, expected, client)
}

func TestUidStore(t *testing.T) {
func TestFactoryUidStore(t *testing.T) {
factory := &Factory{}

store, err := factory.UidStore()
assert.Nil(t, err)
assert.NotNil(t, store)
}

func TestUidStoreWhenSet(t *testing.T) {
func TestFactoryUidStoreWhenSet(t *testing.T) {
expected := newMockUidStore(t)

factory := &Factory{uidStore: expected}
Expand All @@ -183,47 +183,64 @@ func TestUidStoreWhenSet(t *testing.T) {
assert.Equal(t, expected, store)
}

func TestUidClient(t *testing.T) {
func TestFactoryUidClient(t *testing.T) {
factory := &Factory{}

client := factory.UidClient()
assert.NotNil(t, client)
}

func TestUidClientWhenSet(t *testing.T) {
func TestFactoryUidClientWhenSet(t *testing.T) {
expected := newMockUidClient(t)
factory := &Factory{uidClient: expected}

client := factory.UidClient()
assert.Equal(t, expected, client)
}

func TestEventClient(t *testing.T) {
func TestFactoryEventClient(t *testing.T) {
factory := &Factory{}

client := factory.EventClient()
assert.NotNil(t, client)
}

func TestEventClientWhenSet(t *testing.T) {
func TestFactoryEventClientWhenSet(t *testing.T) {
expected := newMockEventClient(t)
factory := &Factory{eventClient: expected}

client := factory.EventClient()
assert.Equal(t, expected, client)
}

func TestScheduledStore(t *testing.T) {
func TestFactoryScheduledStore(t *testing.T) {
factory := &Factory{}

client := factory.ScheduledStore()
assert.NotNil(t, client)
}

func TestScheduledStoreWhenSet(t *testing.T) {
func TestFactoryScheduledStoreWhenSet(t *testing.T) {
expected := newMockScheduledStore(t)
factory := &Factory{scheduledStore: expected}

client := factory.ScheduledStore()
assert.Equal(t, expected, client)
}

func TestFactoryBundle(t *testing.T) {
factory := &Factory{}

bundle, err := factory.Bundle()
assert.Nil(t, bundle)
assert.Error(t, err)
}

func TestFactoryBundleWhenSet(t *testing.T) {
expected := newMockBundle(t)
factory := &Factory{bundle: expected}

bundle, err := factory.Bundle()
assert.Equal(t, expected, bundle)
assert.Nil(t, err)
}
52 changes: 52 additions & 0 deletions cmd/event-received/lpastore_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ministryofjustice/opg-modernising-lpa/internal/dashboard/dashboarddata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
)

type lpastoreEventHandler struct{}
Expand All @@ -28,6 +29,24 @@ func (h *lpastoreEventHandler) Handle(ctx context.Context, factory factory, clou
}

switch v.ChangeType {
case "CREATE":
lpaStoreClient, err := factory.LpaStoreClient()
if err != nil {
return fmt.Errorf("could not create LpaStoreClient: %w", err)
}

bundle, err := factory.Bundle()
if err != nil {
return fmt.Errorf("could not load Bundle: %w", err)
}

notifyClient, err := factory.NotifyClient(ctx)
if err != nil {
return fmt.Errorf("could not create NotifyClient: %w", err)
}

return handleCreate(ctx, factory.DynamoClient(), lpaStoreClient, notifyClient, bundle, v)

case "REGISTER":
lpaStoreClient, err := factory.LpaStoreClient()
if err != nil {
Expand All @@ -50,6 +69,39 @@ func (h *lpastoreEventHandler) Handle(ctx context.Context, factory factory, clou
return fmt.Errorf("unknown lpastore event")
}

func handleCreate(ctx context.Context, client dynamodbClient, lpaStoreClient LpaStoreClient, notifyClient NotifyClient, bundle Bundle, v lpaUpdatedEvent) error {
lpa, err := lpaStoreClient.Lpa(ctx, v.UID)
if err != nil {
return fmt.Errorf("error getting lpa: %w", err)
}

localizer := bundle.For(lpa.Donor.ContactLanguagePreference)

if lpa.Donor.Channel.IsPaper() {
if err := notifyClient.SendActorSMS(ctx, notify.ToLpaDonor(lpa), v.UID, notify.PaperDonorLpaSubmittedSMS{
LpaType: localizer.T(lpa.Type.String()),
}); err != nil {
return fmt.Errorf("error sending sms: %w", err)
}

return nil
}

donor, err := getDonorByLpaUID(ctx, client, v.UID)
if err != nil {
return fmt.Errorf("error getting donor: %w", err)
}

if err := notifyClient.SendActorEmail(ctx, notify.ToDonor(donor), v.UID, notify.DigitalDonorLpaSubmittedEmail{
Greeting: notifyClient.EmailGreeting(lpa),
LpaType: localizer.T(lpa.Type.String()),
}); err != nil {
return fmt.Errorf("error sending email: %w", err)
}

return nil
}

func handleRegister(ctx context.Context, client dynamodbClient, lpaStoreClient LpaStoreClient, eventClient EventClient, v lpaUpdatedEvent) error {
lpa, err := lpaStoreClient.Lpa(ctx, v.UID)
if err != nil {
Expand Down
Loading
Loading