-
Notifications
You must be signed in to change notification settings - Fork 2
/
lpastore_event_handler.go
51 lines (39 loc) · 1.13 KB
/
lpastore_event_handler.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
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/aws/aws-lambda-go/events"
)
type lpastoreEventHandler struct{}
func (h *lpastoreEventHandler) Handle(ctx context.Context, factory factory, cloudWatchEvent events.CloudWatchEvent) error {
switch cloudWatchEvent.DetailType {
case "lpa-updated":
return handleLpaUpdated(ctx, factory.DynamoClient(), cloudWatchEvent, factory.Now())
default:
return fmt.Errorf("unknown lpastore event")
}
}
type lpaUpdatedEvent struct {
UID string `json:"uid"`
ChangeType string `json:"changeType"`
}
func handleLpaUpdated(ctx context.Context, client dynamodbClient, event events.CloudWatchEvent, now func() time.Time) error {
var v lpaUpdatedEvent
if err := json.Unmarshal(event.Detail, &v); err != nil {
return fmt.Errorf("failed to unmarshal detail: %w", err)
}
if v.ChangeType != "PERFECT" {
return nil
}
donor, err := getDonorByLpaUID(ctx, client, v.UID)
if err != nil {
return err
}
donor.PerfectAt = now()
if err := putDonor(ctx, donor, now, client); err != nil {
return fmt.Errorf("failed to update donor details: %w", err)
}
return nil
}