-
Notifications
You must be signed in to change notification settings - Fork 2
/
organisation_store.go
128 lines (103 loc) · 3.22 KB
/
organisation_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
package app
import (
"context"
"errors"
"fmt"
"time"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
)
type organisationStore struct {
dynamoClient DynamoClient
uuidString func() string
newUID func() actoruid.UID
randomString func(int) string
now func() time.Time
}
// An organisationLink is used to join a Member to an Organisation to be accessed by MemberID.
type organisationLink struct {
// PK is the same as the PK for the Member
PK string
// SK is the Member ID for the Member
SK string
MemberSK string
}
func (s *organisationStore) Create(ctx context.Context, member *actor.Member, name string) (*actor.Organisation, error) {
data, err := page.SessionDataFromContext(ctx)
if err != nil {
return nil, err
}
if data.SessionID == "" {
return nil, errors.New("organisationStore.Create requires SessionID")
}
organisation := &actor.Organisation{
PK: dynamo.OrganisationKey(member.OrganisationID),
SK: dynamo.OrganisationKey(member.OrganisationID),
ID: member.OrganisationID,
Name: name,
CreatedAt: s.now(),
}
if err := s.dynamoClient.Create(ctx, organisation); err != nil {
return nil, fmt.Errorf("error creating organisation: %w", err)
}
return organisation, nil
}
func (s *organisationStore) Get(ctx context.Context) (*actor.Organisation, error) {
data, err := page.SessionDataFromContext(ctx)
if err != nil {
return nil, err
}
if data.SessionID == "" {
return nil, errors.New("organisationStore.Get requires SessionID")
}
var member actor.Member
if err := s.dynamoClient.OneBySK(ctx, dynamo.MemberKey(data.SessionID), &member); err != nil {
return nil, err
}
var organisation actor.Organisation
if err := s.dynamoClient.One(ctx, member.PK, member.PK, &organisation); err != nil {
return nil, err
}
if !organisation.DeletedAt.IsZero() {
return nil, dynamo.NotFoundError{}
}
return &organisation, err
}
func (s *organisationStore) Put(ctx context.Context, organisation *actor.Organisation) error {
organisation.UpdatedAt = s.now()
return s.dynamoClient.Put(ctx, organisation)
}
func (s *organisationStore) CreateLPA(ctx context.Context) (*actor.DonorProvidedDetails, error) {
data, err := page.SessionDataFromContext(ctx)
if err != nil {
return nil, err
}
if data.OrganisationID == "" {
return nil, errors.New("organisationStore.CreateLPA requires OrganisationID")
}
lpaID := s.uuidString()
donorUID := s.newUID()
donor := &actor.DonorProvidedDetails{
PK: dynamo.LpaKey(lpaID),
SK: dynamo.OrganisationKey(data.OrganisationID),
LpaID: lpaID,
CreatedAt: s.now(),
Version: 1,
Donor: actor.Donor{
UID: donorUID,
},
}
if donor.Hash, err = donor.GenerateHash(); err != nil {
return nil, err
}
if err := s.dynamoClient.Create(ctx, donor); err != nil {
return nil, err
}
return donor, err
}
func (s *organisationStore) SoftDelete(ctx context.Context, organisation *actor.Organisation) error {
organisation.DeletedAt = s.now()
return s.dynamoClient.Put(ctx, organisation)
}