Skip to content

Commit

Permalink
Merge 01c0bde into ce83ff6
Browse files Browse the repository at this point in the history
  • Loading branch information
acsauk authored Jul 16, 2024
2 parents ce83ff6 + 01c0bde commit 59b2c2c
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 60 deletions.
9 changes: 9 additions & 0 deletions cmd/event-received/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Factory struct {
searchEndpoint string
searchIndexName string
searchIndexingEnabled bool
eventClient EventClient

// previously constructed values
appData *page.AppData
Expand Down Expand Up @@ -179,3 +180,11 @@ func (f *Factory) UidClient() UidClient {

return f.uidClient
}

func (f *Factory) EventClient() EventClient {
if f.eventClient == nil {
f.eventClient = event.NewClient(f.cfg, f.eventBusName)
}

return f.eventClient
}
15 changes: 15 additions & 0 deletions cmd/event-received/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,18 @@ func TestUidClientWhenSet(t *testing.T) {
client := factory.UidClient()
assert.Equal(t, expected, client)
}

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

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

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

client := factory.EventClient()
assert.Equal(t, expected, client)
}
6 changes: 6 additions & 0 deletions cmd/event-received/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"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/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/random"
"github.com/ministryofjustice/opg-modernising-lpa/internal/s3"
Expand All @@ -36,6 +37,7 @@ type factory interface {
LpaStoreClient() (LpaStoreClient, error)
UidStore() (UidStore, error)
UidClient() UidClient
EventClient() EventClient
}

type Handler interface {
Expand Down Expand Up @@ -64,6 +66,10 @@ type DocumentStore interface {
UpdateScanResults(ctx context.Context, lpaID, objectKey string, virusDetected bool) error
}

type EventClient interface {
SendApplicationUpdated(ctx context.Context, event event.ApplicationUpdated) error
}

type Event struct {
events.S3Event
events.CloudWatchEvent
Expand Down
25 changes: 23 additions & 2 deletions cmd/event-received/makeregister_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ func (h *makeregisterEventHandler) Handle(ctx context.Context, factory factory,
}

uidClient := factory.UidClient()
dynamoClient := factory.DynamoClient()
eventClient := factory.EventClient()

return handleUidRequested(ctx, uidStore, uidClient, cloudWatchEvent)
return handleUidRequested(ctx, uidStore, uidClient, cloudWatchEvent, dynamoClient, eventClient)

default:
return fmt.Errorf("unknown makeregister event")
}
}

func handleUidRequested(ctx context.Context, uidStore UidStore, uidClient UidClient, e events.CloudWatchEvent) error {
func handleUidRequested(ctx context.Context, uidStore UidStore, uidClient UidClient, e events.CloudWatchEvent, dynamoClient dynamodbClient, eventClient EventClient) error {
var v event.UidRequested
if err := json.Unmarshal(e.Detail, &v); err != nil {
return fmt.Errorf("failed to unmarshal detail: %w", err)
Expand All @@ -44,5 +46,24 @@ func handleUidRequested(ctx context.Context, uidStore UidStore, uidClient UidCli
return fmt.Errorf("failed to set uid: %w", err)
}

donor, err := getDonorByLpaUID(ctx, dynamoClient, uid)
if err != nil {
return err
}

if err := eventClient.SendApplicationUpdated(ctx, event.ApplicationUpdated{
UID: donor.LpaUID,
Type: donor.Type.String(),
CreatedAt: donor.CreatedAt,
Donor: event.ApplicationUpdatedDonor{
FirstNames: donor.Donor.FirstNames,
LastName: donor.Donor.LastName,
DateOfBirth: donor.Donor.DateOfBirth,
Address: donor.Donor.Address,
},
}); err != nil {
return err
}

return nil
}
103 changes: 98 additions & 5 deletions cmd/event-received/makeregister_event_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package main

import (
"context"
"encoding/json"
"fmt"
"testing"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/date"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/place"
"github.com/ministryofjustice/opg-modernising-lpa/internal/uid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -20,18 +26,20 @@ func TestMakeRegisterHandlerHandleUnknownEvent(t *testing.T) {
}

func TestHandleUidRequested(t *testing.T) {
event := events.CloudWatchEvent{
e := events.CloudWatchEvent{
DetailType: "uid-requested",
Detail: json.RawMessage(`{"lpaID":"an-id","donorSessionID":"donor-id","organisationID":"org-id","type":"personal-welfare","donor":{"name":"a donor","dob":"2000-01-02","postcode":"F1 1FF"}}`),
}

dob := date.New("2000", "01", "02")

uidClient := newMockUidClient(t)
uidClient.EXPECT().
CreateCase(ctx, &uid.CreateCaseRequestBody{
Type: "personal-welfare",
Donor: uid.DonorDetails{
Name: "a donor",
Dob: date.New("2000", "01", "02"),
Dob: dob,
Postcode: "F1 1FF",
},
}).
Expand All @@ -42,16 +50,60 @@ func TestHandleUidRequested(t *testing.T) {
Set(ctx, "an-id", "donor-id", "org-id", "M-1111-2222-3333").
Return(nil)

dynamoClient := newMockDynamodbClient(t)
dynamoClient.
On("OneByUID", ctx, "M-1111-2222-3333", mock.Anything).
Return(func(ctx context.Context, uid string, v interface{}) error {
b, _ := attributevalue.Marshal(dynamo.Keys{PK: dynamo.LpaKey("123"), SK: dynamo.LpaOwnerKey(dynamo.DonorKey("456"))})
attributevalue.Unmarshal(b, v)
return nil
})
dynamoClient.
On("One", ctx, dynamo.LpaKey("123"), dynamo.DonorKey("456"), mock.Anything).
Return(func(ctx context.Context, pk dynamo.PK, sk dynamo.SK, v interface{}) error {
b, _ := attributevalue.Marshal(&actor.DonorProvidedDetails{
Donor: actor.Donor{FirstNames: "a", LastName: "b", Address: place.Address{Line1: "a"}, DateOfBirth: dob},
Type: actor.LpaTypePersonalWelfare,
CreatedAt: testNow,
LpaUID: "M-1111-2222-3333",
PK: dynamo.LpaKey("123"),
SK: dynamo.LpaOwnerKey(dynamo.DonorKey("456")),
})
attributevalue.Unmarshal(b, v)
return nil
})

eventClient := newMockEventClient(t)
eventClient.EXPECT().
SendApplicationUpdated(ctx, event.ApplicationUpdated{
UID: "M-1111-2222-3333",
Type: actor.LpaTypePersonalWelfare.String(),
CreatedAt: testNow,
Donor: event.ApplicationUpdatedDonor{
FirstNames: "a",
LastName: "b",
DateOfBirth: date.New("2000", "1", "2"),
Address: place.Address{Line1: "a"},
},
}).
Return(nil)

factory := newMockFactory(t)
factory.EXPECT().
UidStore().
Return(uidStore, nil)
factory.EXPECT().
UidClient().
Return(uidClient)
factory.EXPECT().
DynamoClient().
Return(dynamoClient)
factory.EXPECT().
EventClient().
Return(eventClient)

handler := makeregisterEventHandler{}
err := handler.Handle(ctx, factory, event)
err := handler.Handle(ctx, factory, e)

assert.Nil(t, err)
}
Expand All @@ -67,7 +119,7 @@ func TestHandleUidRequestedWhenUidClientErrors(t *testing.T) {
CreateCase(ctx, mock.Anything).
Return("", expectedError)

err := handleUidRequested(ctx, nil, uidClient, event)
err := handleUidRequested(ctx, nil, uidClient, event, nil, nil)
assert.Equal(t, fmt.Errorf("failed to create case: %w", expectedError), err)
}

Expand All @@ -87,6 +139,47 @@ func TestHandleUidRequestedWhenUidStoreErrors(t *testing.T) {
Set(ctx, "an-id", "donor-id", "", "M-1111-2222-3333").
Return(expectedError)

err := handleUidRequested(ctx, uidStore, uidClient, event)
err := handleUidRequested(ctx, uidStore, uidClient, event, nil, nil)
assert.Equal(t, fmt.Errorf("failed to set uid: %w", expectedError), err)
}

func TestHandleUidRequestedWhenEventClientErrors(t *testing.T) {
e := events.CloudWatchEvent{
DetailType: "uid-requested",
Detail: json.RawMessage(`{"lpaID":"an-id","donorSessionID":"donor-id","type":"personal-welfare","donor":{"name":"a donor","dob":"2000-01-02","postcode":"F1 1FF"}}`),
}

uidClient := newMockUidClient(t)
uidClient.EXPECT().
CreateCase(ctx, mock.Anything).
Return("M-1111-2222-3333", nil)

uidStore := newMockUidStore(t)
uidStore.EXPECT().
Set(ctx, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil)

dynamoClient := newMockDynamodbClient(t)
dynamoClient.
On("OneByUID", mock.Anything, mock.Anything, mock.Anything).
Return(func(ctx context.Context, uid string, v interface{}) error {
b, _ := attributevalue.Marshal(dynamo.Keys{PK: dynamo.LpaKey("123"), SK: dynamo.LpaOwnerKey(dynamo.DonorKey("456"))})
attributevalue.Unmarshal(b, v)
return nil
})
dynamoClient.
On("One", ctx, dynamo.LpaKey("123"), dynamo.DonorKey("456"), mock.Anything).
Return(func(ctx context.Context, pk dynamo.PK, sk dynamo.SK, v interface{}) error {
b, _ := attributevalue.Marshal(&actor.DonorProvidedDetails{})
attributevalue.Unmarshal(b, v)
return nil
})

eventClient := newMockEventClient(t)
eventClient.EXPECT().
SendApplicationUpdated(ctx, mock.Anything).
Return(expectedError)

err := handleUidRequested(ctx, uidStore, uidClient, e, dynamoClient, eventClient)
assert.Equal(t, expectedError, err)
}
84 changes: 84 additions & 0 deletions cmd/event-received/mock_EventClient_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 59b2c2c

Please sign in to comment.