-
Notifications
You must be signed in to change notification settings - Fork 2
/
dashboard_store.go
250 lines (204 loc) · 6.66 KB
/
dashboard_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package app
import (
"context"
"errors"
"slices"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
)
type LpaStoreResolvingService interface {
ResolveList(ctx context.Context, donors []*actor.DonorProvidedDetails) ([]*lpastore.Lpa, error)
}
// An lpaLink is used to join an actor to an LPA.
type lpaLink struct {
// PK is the same as the PK for the LPA
PK dynamo.LpaKeyType
// SK is the subKey for the current user
SK dynamo.SubKeyType
// DonorKey is the donorKey for the donor
DonorKey dynamo.LpaOwnerKeyType
// ActorType is the type for the current user
ActorType actor.Type
// UpdatedAt is set to allow this data to be queried from SKUpdatedAtIndex
UpdatedAt time.Time
}
func (l lpaLink) UserSub() string {
if l.SK == "" {
return ""
}
return strings.Split(l.SK.SK(), dynamo.SubKey("").SK())[1]
}
type dashboardStore struct {
dynamoClient DynamoClient
lpaStoreResolvingService LpaStoreResolvingService
}
func isLpaKey(k dynamo.Keys) bool {
_, donorOK := k.SK.(dynamo.DonorKeyType)
_, orgOK := k.SK.(dynamo.OrganisationKeyType)
return donorOK || orgOK
}
func isCertificateProviderKey(k dynamo.Keys) bool {
_, ok := k.SK.(dynamo.CertificateProviderKeyType)
return ok
}
func isAttorneyKey(k dynamo.Keys) bool {
_, ok := k.SK.(dynamo.AttorneyKeyType)
return ok
}
func (s *dashboardStore) SubExistsForActorType(ctx context.Context, sub string, actorType actor.Type) (bool, error) {
var links []lpaLink
if err := s.dynamoClient.AllBySK(ctx, dynamo.SubKey(sub), &links); err != nil {
return false, err
}
for _, link := range links {
if link.ActorType == actorType {
return true, nil
}
}
return false, nil
}
func (s *dashboardStore) GetAll(ctx context.Context) (donor, attorney, certificateProvider []page.LpaAndActorTasks, err error) {
data, err := page.SessionDataFromContext(ctx)
if err != nil {
return nil, nil, nil, err
}
if data.SessionID == "" {
return nil, nil, nil, errors.New("donorStore.GetAll requires SessionID")
}
var links []lpaLink
if err := s.dynamoClient.AllBySK(ctx, dynamo.SubKey(data.SessionID), &links); err != nil {
return nil, nil, nil, err
}
var searchKeys []dynamo.Keys
keyMap := map[string]actor.Type{}
for _, key := range links {
searchKeys = append(searchKeys, dynamo.Keys{PK: key.PK, SK: key.DonorKey})
if key.ActorType == actor.TypeAttorney {
searchKeys = append(searchKeys, dynamo.Keys{PK: key.PK, SK: dynamo.AttorneyKey(data.SessionID)})
}
if key.ActorType == actor.TypeCertificateProvider {
searchKeys = append(searchKeys, dynamo.Keys{PK: key.PK, SK: dynamo.CertificateProviderKey(data.SessionID)})
}
_, id, _ := strings.Cut(key.PK.PK(), "#")
keyMap[id] = key.ActorType
}
if len(searchKeys) == 0 {
return nil, nil, nil, nil
}
lpasOrProvidedDetails, err := s.dynamoClient.AllByKeys(ctx, searchKeys)
if err != nil {
return nil, nil, nil, err
}
var (
referencedKeys []dynamo.Keys
donorsDetails []*actor.DonorProvidedDetails
)
for _, item := range lpasOrProvidedDetails {
var ks dynamo.Keys
if err = attributevalue.UnmarshalMap(item, &ks); err != nil {
return nil, nil, nil, err
}
if isLpaKey(ks) {
var donorDetails struct {
actor.DonorProvidedDetails
ReferencedSK dynamo.OrganisationKeyType
}
if err := attributevalue.UnmarshalMap(item, &donorDetails); err != nil {
return nil, nil, nil, err
}
if donorDetails.ReferencedSK != "" {
referencedKeys = append(referencedKeys, dynamo.Keys{PK: ks.PK, SK: donorDetails.ReferencedSK})
} else if donorDetails.LpaUID != "" {
donorsDetails = append(donorsDetails, &donorDetails.DonorProvidedDetails)
}
}
}
if len(referencedKeys) > 0 {
referencedLpas, err := s.dynamoClient.AllByKeys(ctx, referencedKeys)
if err != nil {
return nil, nil, nil, err
}
for _, item := range referencedLpas {
donorDetails := &actor.DonorProvidedDetails{}
if err := attributevalue.UnmarshalMap(item, donorDetails); err != nil {
return nil, nil, nil, err
}
if donorDetails.LpaUID != "" {
donorsDetails = append(donorsDetails, donorDetails)
}
}
}
resolvedLpas, err := s.lpaStoreResolvingService.ResolveList(ctx, donorsDetails)
if err != nil {
return nil, nil, nil, err
}
certificateProviderMap := map[string]page.LpaAndActorTasks{}
attorneyMap := map[string]page.LpaAndActorTasks{}
for _, lpa := range resolvedLpas {
switch keyMap[lpa.LpaID] {
case actor.TypeDonor:
donor = append(donor, page.LpaAndActorTasks{Lpa: lpa})
case actor.TypeAttorney:
attorneyMap[lpa.LpaID] = page.LpaAndActorTasks{Lpa: lpa}
case actor.TypeCertificateProvider:
certificateProviderMap[lpa.LpaID] = page.LpaAndActorTasks{Lpa: lpa}
}
}
for _, item := range lpasOrProvidedDetails {
var ks dynamo.Keys
if err = attributevalue.UnmarshalMap(item, &ks); err != nil {
return nil, nil, nil, err
}
if isAttorneyKey(ks) {
attorneyProvidedDetails := &actor.AttorneyProvidedDetails{}
if err := attributevalue.UnmarshalMap(item, attorneyProvidedDetails); err != nil {
return nil, nil, nil, err
}
lpaID := attorneyProvidedDetails.LpaID
if entry, ok := attorneyMap[lpaID]; ok {
if attorneyProvidedDetails.IsReplacement && entry.Lpa.Submitted {
delete(attorneyMap, lpaID)
continue
}
entry.Attorney = attorneyProvidedDetails
attorneyMap[lpaID] = entry
continue
}
}
if isCertificateProviderKey(ks) {
certificateProviderProvidedDetails := &actor.CertificateProviderProvidedDetails{}
if err := attributevalue.UnmarshalMap(item, certificateProviderProvidedDetails); err != nil {
return nil, nil, nil, err
}
lpaID := certificateProviderProvidedDetails.LpaID
if !certificateProviderProvidedDetails.SignedAt.IsZero() {
delete(certificateProviderMap, lpaID)
}
if entry, ok := certificateProviderMap[lpaID]; ok {
entry.CertificateProvider = certificateProviderProvidedDetails
certificateProviderMap[lpaID] = entry
}
}
}
certificateProvider = mapValues(certificateProviderMap)
attorney = mapValues(attorneyMap)
byUpdatedAt := func(a, b page.LpaAndActorTasks) int {
return b.Lpa.UpdatedAt.Compare(a.Lpa.UpdatedAt)
}
slices.SortFunc(donor, byUpdatedAt)
slices.SortFunc(attorney, byUpdatedAt)
slices.SortFunc(certificateProvider, byUpdatedAt)
return donor, attorney, certificateProvider, nil
}
func mapValues[M ~map[K]V, K comparable, V any](m M) []V {
r := make([]V, 0, len(m))
for _, v := range m {
r = append(r, v)
}
return r
}