-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.go
122 lines (102 loc) · 4.23 KB
/
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package attorney
import (
"context"
"errors"
"fmt"
"time"
dynamodbtypes "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/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneydata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dashboard/dashboarddata"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sharecode/sharecodedata"
)
type DynamoClient interface {
One(ctx context.Context, pk dynamo.PK, sk dynamo.SK, v interface{}) error
OneByPK(ctx context.Context, pk dynamo.PK, v interface{}) error
OneByPartialSK(ctx context.Context, pk dynamo.PK, partialSK dynamo.SK, v interface{}) error
AllByPartialSK(ctx context.Context, pk dynamo.PK, partialSK dynamo.SK, v interface{}) error
LatestForActor(ctx context.Context, sk dynamo.SK, v interface{}) error
AllBySK(ctx context.Context, sk dynamo.SK, v interface{}) error
AllByKeys(ctx context.Context, keys []dynamo.Keys) ([]map[string]dynamodbtypes.AttributeValue, error)
AllKeysByPK(ctx context.Context, pk dynamo.PK) ([]dynamo.Keys, error)
Put(ctx context.Context, v interface{}) error
Create(ctx context.Context, v interface{}) error
DeleteKeys(ctx context.Context, keys []dynamo.Keys) error
DeleteOne(ctx context.Context, pk dynamo.PK, sk dynamo.SK) error
Update(ctx context.Context, pk dynamo.PK, sk dynamo.SK, values map[string]dynamodbtypes.AttributeValue, expression string) error
BatchPut(ctx context.Context, items []interface{}) error
OneBySK(ctx context.Context, sk dynamo.SK, v interface{}) error
OneByUID(ctx context.Context, uid string, v interface{}) error
WriteTransaction(ctx context.Context, transaction *dynamo.Transaction) error
}
type Store struct {
dynamoClient DynamoClient
now func() time.Time
}
func NewStore(dynamoClient DynamoClient) *Store {
return &Store{dynamoClient: dynamoClient, now: time.Now}
}
func (s *Store) Create(ctx context.Context, shareCode sharecodedata.Link, email string) (*attorneydata.Provided, error) {
data, err := appcontext.SessionFromContext(ctx)
if err != nil {
return nil, err
}
if data.LpaID == "" || data.SessionID == "" {
return nil, errors.New("attorneyStore.Create requires LpaID and SessionID")
}
attorney := &attorneydata.Provided{
PK: dynamo.LpaKey(data.LpaID),
SK: dynamo.AttorneyKey(data.SessionID),
UID: shareCode.ActorUID,
LpaID: data.LpaID,
UpdatedAt: s.now(),
IsReplacement: shareCode.IsReplacementAttorney,
IsTrustCorporation: shareCode.IsTrustCorporation,
Email: email,
}
transaction := dynamo.NewTransaction().
Create(attorney).
Create(dashboarddata.LpaLink{
PK: dynamo.LpaKey(data.LpaID),
SK: dynamo.SubKey(data.SessionID),
DonorKey: shareCode.LpaOwnerKey,
ActorType: actor.TypeAttorney,
UpdatedAt: s.now(),
}).
Delete(dynamo.Keys{PK: shareCode.PK, SK: shareCode.SK})
if err := s.dynamoClient.WriteTransaction(ctx, transaction); err != nil {
return nil, err
}
return attorney, err
}
func (s *Store) Get(ctx context.Context) (*attorneydata.Provided, error) {
data, err := appcontext.SessionFromContext(ctx)
if err != nil {
return nil, err
}
if data.LpaID == "" || data.SessionID == "" {
return nil, errors.New("attorneyStore.Get requires LpaID and SessionID")
}
var attorney attorneydata.Provided
err = s.dynamoClient.One(ctx, dynamo.LpaKey(data.LpaID), dynamo.AttorneyKey(data.SessionID), &attorney)
return &attorney, err
}
func (s *Store) Put(ctx context.Context, attorney *attorneydata.Provided) error {
attorney.UpdatedAt = s.now()
return s.dynamoClient.Put(ctx, attorney)
}
func (s *Store) Delete(ctx context.Context) error {
data, err := appcontext.SessionFromContext(ctx)
if err != nil {
return err
}
if data.LpaID == "" || data.SessionID == "" {
return errors.New("attorneyStore.Delete requires LpaID and SessionID")
}
if err := s.dynamoClient.DeleteOne(ctx, dynamo.LpaKey(data.LpaID), dynamo.AttorneyKey(data.SessionID)); err != nil {
return fmt.Errorf("error deleting attorney: %w", err)
}
return nil
}