-
Notifications
You must be signed in to change notification settings - Fork 2
/
uid_store.go
70 lines (58 loc) · 1.95 KB
/
uid_store.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
package app
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/search"
)
type DynamoUpdateClient interface {
UpdateReturn(ctx context.Context, pk dynamo.PK, sk dynamo.SK, values map[string]types.AttributeValue, expression string) (map[string]types.AttributeValue, error)
}
type SearchClient interface {
Index(ctx context.Context, lpa search.Lpa) error
}
type uidStore struct {
dynamoClient DynamoUpdateClient
now func() time.Time
searchClient SearchClient
}
func NewUidStore(dynamoClient DynamoUpdateClient, searchClient SearchClient, now func() time.Time) *uidStore {
return &uidStore{dynamoClient: dynamoClient, searchClient: searchClient, now: now}
}
func (s *uidStore) Set(ctx context.Context, lpaID, sessionID, organisationID, uid string) error {
values, err := attributevalue.MarshalMap(map[string]any{
":uid": uid,
":now": s.now(),
})
if err != nil {
return err
}
var sk dynamo.SK = dynamo.DonorKey(sessionID)
if organisationID != "" {
sk = dynamo.OrganisationKey(organisationID)
}
newAttrs, err := s.dynamoClient.UpdateReturn(ctx, dynamo.LpaKey(lpaID), sk, values,
"set LpaUID = :uid, UpdatedAt = :now")
if err != nil {
return fmt.Errorf("uidStore update failed: %w", err)
}
var donor *actor.DonorProvidedDetails
if err := attributevalue.UnmarshalMap(newAttrs, &donor); err != nil {
return fmt.Errorf("uidStore unmarshal failed: %w", err)
}
if err := s.searchClient.Index(ctx, search.Lpa{
PK: dynamo.LpaKey(lpaID).PK(),
SK: sk.SK(),
Donor: search.LpaDonor{
FirstNames: donor.Donor.FirstNames,
LastName: donor.Donor.LastName,
},
}); err != nil {
return fmt.Errorf("uidStore index failed: %w", err)
}
return nil
}