-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.go
84 lines (65 loc) · 1.94 KB
/
handlers.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
package main
import (
"context"
"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"
)
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, "/")
donor, err := getDonorByLpaUID(ctx, dynamodbClient, parts[0])
if err != nil {
return err
}
err = documentStore.UpdateScanResults(ctx, donor.LpaID, objectKey, hasVirus)
if err != nil {
return fmt.Errorf("failed to update scan results: %w", err)
}
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.Keys
if err := client.OneByUID(ctx, uid, &key); err != nil {
return nil, fmt.Errorf("failed to resolve uid: %w", err)
}
if key.PK == nil {
return nil, fmt.Errorf("PK missing from LPA in response")
}
var donor actor.DonorProvidedDetails
if err := client.One(ctx, key.PK, key.SK, &donor); err != nil {
return nil, fmt.Errorf("failed to get LPA: %w", err)
}
return &donor, nil
}