-
Notifications
You must be signed in to change notification settings - Fork 2
/
makeregister_event_handler.go
69 lines (56 loc) · 1.91 KB
/
makeregister_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/uid"
)
type makeregisterEventHandler struct{}
func (h *makeregisterEventHandler) Handle(ctx context.Context, factory factory, cloudWatchEvent events.CloudWatchEvent) error {
switch cloudWatchEvent.DetailType {
case "uid-requested":
uidStore, err := factory.UidStore()
if err != nil {
return err
}
uidClient := factory.UidClient()
dynamoClient := factory.DynamoClient()
eventClient := factory.EventClient()
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, 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)
}
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, v.OrganisationID, uid); err != nil {
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
}