Skip to content

Commit

Permalink
Merge 7d71ef4 into c0de457
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx authored Nov 7, 2023
2 parents c0de457 + 7d71ef4 commit 428e508
Show file tree
Hide file tree
Showing 46 changed files with 1,019 additions and 611 deletions.
1 change: 1 addition & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ coverage:
- "./internal/identity/yoti*"
- "./internal/telemetry"
- "./internal/page/fixtures"
- "./cmd/event-received/main.go"
- "./cmd/mlpa/main.go"
- "./cmd/mock-notify/main.go"
- "./cmd/mock-onelogin/main.go"
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ get-lpa: ##@app dumps all entries in the lpas dynamodb table that are related t
docker compose -f docker/docker-compose.yml exec localstack awslocal dynamodb \
query --table-name lpas --key-condition-expression 'PK = :pk' --expression-attribute-values '{":pk": {"S": "LPA#$(id)"}}'

get-donor-session-id:
docker compose -f docker/docker-compose.yml exec localstack awslocal dynamodb \
query --table-name lpas --key-condition-expression 'PK = :pk and begins_with(SK, :sk)' --expression-attribute-values '{":pk": {"S": "LPA#$(id)"}, ":sk": {"S": "#DONOR#"}}' | jq -r .Items[0].SK.S | sed 's/#DONOR#//g'

get-documents: ##@app dumps all documents in the lpas dynamodb table that are related to the LPA id supplied e.g. get-documents lpaId=abc-123
docker compose -f docker/docker-compose.yml exec localstack awslocal dynamodb \
query --table-name lpas --key-condition-expression 'PK = :pk and begins_with(SK, :sk)' --expression-attribute-values '{":pk": {"S": "LPA#$(lpaId)"}, ":sk": {"S": "#DOCUMENT#"}}'
Expand Down Expand Up @@ -116,5 +120,8 @@ emit-object-tags-added-without-virus: ##@app emits a ObjectTagging:Put event wit

curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"Records":[{"eventSource":"aws:s3","eventTime":"2023-10-23T15:58:33.081Z","eventName":"ObjectTagging:Put","s3":{"bucket":{"name":"uploads-opg-modernising-lpa-eu-west-1"},"object":{"key":"$(key)"}}}]}'

emit-uid-requested: ##@app emits a uid-requested event with the given detail e.g. emit-uid-requested lpaID=abc sessionID=xyz
curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"version":"0","id":"63eb7e5f-1f10-4744-bba9-e16d327c3b98","detail-type":"uid-requested","source":"opg.poas.makeregister","account":"653761790766","time":"2023-08-30T13:40:30Z","region":"eu-west-1","resources":[],"detail":{"LpaID":"$(lpaID)","DonorSessionID":"$(sessionID)","Type":"pfa","Donor":{"Name":"abc","Dob":"2000-01-01","Postcode":"F1 1FF"}}}'

logs: ##@app tails logs for all containers running
docker compose -f docker/docker-compose.yml -f docker/docker-compose.dev.yml logs -f
183 changes: 183 additions & 0 deletions cmd/event-received/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package main

import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/aws/aws-lambda-go/events"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/uid"
)

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, 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, dynamoClient 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 := getLpaByUID(ctx, dynamoClient, v.UID)
if err != nil {
return err
}

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

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

if err := shareCodeSender.SendCertificateProvider(ctx, notify.CertificateProviderInviteEmail, appData, false, &lpa); 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)
}

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

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

if err := client.Put(ctx, lpa); 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)
}

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

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

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

return nil
}

func handleObjectTagsAdded(ctx context.Context, dynamodbClient dynamodbClient, event events.S3Event, s3Client s3Client, documentStore DocumentStore) error {
objectKey := event.Records[0].S3.Object.Key
if objectKey == "" {
return fmt.Errorf("object key missing")
}

tags, err := s3Client.GetObjectTags(ctx, objectKey)
if err != nil {
return fmt.Errorf("failed to get tags for object: %w", err)
}

hasScannedTag := false
hasVirus := false

for _, tag := range tags {
if *tag.Key == "virus-scan-status" {
hasScannedTag = true
hasVirus = *tag.Value == virusFound
break
}
}

if !hasScannedTag {
return nil
}

parts := strings.Split(objectKey, "/")

lpa, err := getLpaByUID(ctx, dynamodbClient, parts[0])
if err != nil {
return err
}

err = documentStore.UpdateScanResults(ctx, lpa.ID, objectKey, hasVirus)
if err != nil {
return fmt.Errorf("failed to update scan results: %w", err)
}

return nil
}

func getLpaByUID(ctx context.Context, client dynamodbClient, uid string) (page.Lpa, error) {
var key dynamo.Key
if err := client.OneByUID(ctx, uid, &key); err != nil {
return page.Lpa{}, fmt.Errorf("failed to resolve uid: %w", err)
}

if key.PK == "" {
return page.Lpa{}, fmt.Errorf("PK missing from LPA in response")
}

var lpa page.Lpa
if err := client.One(ctx, key.PK, key.SK, &lpa); err != nil {
return page.Lpa{}, fmt.Errorf("failed to get LPA: %w", err)
}

return lpa, nil
}
Loading

0 comments on commit 428e508

Please sign in to comment.