Skip to content

Commit

Permalink
Merge 8e26b9d into 35ab83c
Browse files Browse the repository at this point in the history
  • Loading branch information
acsauk authored Jan 25, 2024
2 parents 35ab83c + 8e26b9d commit e0878b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
17 changes: 7 additions & 10 deletions cmd/event-received/cloud_watch_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,24 @@ func handleEvidenceReceived(ctx context.Context, client dynamodbClient, event ev
return nil
}

func handleFeeApproved(ctx context.Context, dynamoClient dynamodbClient, event events.CloudWatchEvent, shareCodeSender shareCodeSender, appData page.AppData, now func() time.Time) error {
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)
}

lpa, err := getDonorByLpaUID(ctx, dynamoClient, v.UID)
donor, err := getDonorByLpaUID(ctx, client, v.UID)
if err != nil {
return err
}

lpa.Tasks.PayForLpa = actor.PaymentTaskCompleted
lpa.UpdatedAt = now()
donor.Tasks.PayForLpa = actor.PaymentTaskCompleted

if err := dynamoClient.Put(ctx, lpa); err != nil {
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, &lpa); err != nil {
if err := shareCodeSender.SendCertificateProviderPrompt(ctx, appData, &donor); err != nil {
return fmt.Errorf("failed to send share code to certificate provider: %w", err)
}

Expand All @@ -157,9 +156,8 @@ func handleMoreEvidenceRequired(ctx context.Context, client dynamodbClient, even
}

donor.Tasks.PayForLpa = actor.PaymentTaskMoreEvidenceRequired
donor.UpdatedAt = now()

if err := client.Put(ctx, donor); err != nil {
if err := putDonor(ctx, donor, now, client); err != nil {
return fmt.Errorf("failed to update LPA task status: %w", err)
}

Expand All @@ -178,9 +176,8 @@ func handleFeeDenied(ctx context.Context, client dynamodbClient, event events.Cl
}

donor.Tasks.PayForLpa = actor.PaymentTaskDenied
donor.UpdatedAt = now()

if err := client.Put(ctx, donor); err != nil {
if err := putDonor(ctx, donor, now, client); err != nil {
return fmt.Errorf("failed to update LPA task status: %w", err)
}

Expand Down
18 changes: 9 additions & 9 deletions cmd/event-received/cloud_watch_event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ func TestHandleFeeApproved(t *testing.T) {
return nil
})
client.EXPECT().
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskCompleted}, UpdatedAt: now}).
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskCompleted}, UpdatedAt: now, Hash: 13171477638077665116}).
Return(nil)

shareCodeSender := newMockShareCodeSender(t)
shareCodeSender.EXPECT().
SendCertificateProviderPrompt(ctx, page.AppData{}, &actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskCompleted}, UpdatedAt: now}).
SendCertificateProviderPrompt(ctx, page.AppData{}, &actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskCompleted}}).
Return(nil)

err := handleFeeApproved(ctx, client, event, shareCodeSender, page.AppData{}, func() time.Time { return now })
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestHandleFeeApprovedWhenDynamoClientPutError(t *testing.T) {
return nil
})
client.EXPECT().
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskCompleted}, UpdatedAt: now}).
Put(ctx, mock.Anything).
Return(expectedError)

err := handleFeeApproved(ctx, client, event, nil, page.AppData{}, func() time.Time { return now })
Expand Down Expand Up @@ -262,12 +262,12 @@ func TestHandleFeeApprovedWhenShareCodeSenderError(t *testing.T) {
return nil
})
client.EXPECT().
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskCompleted}, UpdatedAt: now}).
Put(ctx, mock.Anything).
Return(nil)

shareCodeSender := newMockShareCodeSender(t)
shareCodeSender.EXPECT().
SendCertificateProviderPrompt(ctx, page.AppData{}, &actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskCompleted}, UpdatedAt: now}).
SendCertificateProviderPrompt(ctx, page.AppData{}, mock.Anything).
Return(expectedError)

err := handleFeeApproved(ctx, client, event, shareCodeSender, page.AppData{}, func() time.Time { return now })
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestHandleMoreEvidenceRequired(t *testing.T) {
return nil
})
client.EXPECT().
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskMoreEvidenceRequired}, UpdatedAt: now}).
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskMoreEvidenceRequired}, UpdatedAt: now, Hash: 2420797377841961994}).
Return(nil)

err := handleMoreEvidenceRequired(ctx, client, event, func() time.Time { return now })
Expand Down Expand Up @@ -329,7 +329,7 @@ func TestHandleMoreEvidenceRequiredWhenPutError(t *testing.T) {
return nil
})
client.EXPECT().
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskMoreEvidenceRequired}, UpdatedAt: now}).
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskMoreEvidenceRequired}, UpdatedAt: now, Hash: 2420797377841961994}).
Return(expectedError)

err := handleMoreEvidenceRequired(ctx, client, event, func() time.Time { return now })
Expand Down Expand Up @@ -360,7 +360,7 @@ func TestHandleFeeDenied(t *testing.T) {
return nil
})
client.EXPECT().
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskDenied}, UpdatedAt: now}).
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskDenied}, UpdatedAt: now, Hash: 3695518532056580250}).
Return(nil)

err := handleFeeDenied(ctx, client, event, func() time.Time { return now })
Expand Down Expand Up @@ -391,7 +391,7 @@ func TestHandleFeeDeniedWhenPutError(t *testing.T) {
return nil
})
client.EXPECT().
Put(ctx, actor.DonorProvidedDetails{PK: "LPA#123", SK: "#DONOR#456", Tasks: actor.DonorTasks{PayForLpa: actor.PaymentTaskDenied}, UpdatedAt: now}).
Put(ctx, mock.Anything).
Return(expectedError)

err := handleFeeDenied(ctx, client, event, func() time.Time { return now })
Expand Down
14 changes: 14 additions & 0 deletions cmd/event-received/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/aws/aws-lambda-go/events"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
Expand Down Expand Up @@ -51,6 +52,19 @@ func handleObjectTagsAdded(ctx context.Context, dynamodbClient dynamodbClient, e
return nil
}

func putDonor(ctx context.Context, donor actor.DonorProvidedDetails, now func() time.Time, client dynamodbClient) error {
donor.UpdatedAt = now()

hash, err := donor.GenerateHash()
if err != nil {
return err
}

donor.Hash = hash

return client.Put(ctx, donor)
}

func getDonorByLpaUID(ctx context.Context, client dynamodbClient, uid string) (actor.DonorProvidedDetails, error) {
var key dynamo.Key
if err := client.OneByUID(ctx, uid, &key); err != nil {
Expand Down

0 comments on commit e0878b2

Please sign in to comment.