-
Notifications
You must be signed in to change notification settings - Fork 0
/
roster_service.go
210 lines (174 loc) · 6.19 KB
/
roster_service.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
package service
import (
"context"
"fmt"
"github.com/haleyrc/pkg/errors"
"github.com/haleyrc/pkg/hash"
"github.com/haleyrc/pkg/logger"
"github.com/haleyrc/pkg/pg"
"github.com/haleyrc/pkg/random"
"github.com/haleyrc/pkg/time"
"github.com/pborman/uuid"
"github.com/haleyrc/cheevos/domain"
)
var _ domain.RosterService = &rosterService{}
type Emailer interface {
SendInvitation(ctx context.Context, email, code string) error
}
type RosterRepository interface {
DeleteInvitationByCode(ctx context.Context, tx pg.Tx, hashedCode string) error
GetInvitation(ctx context.Context, tx pg.Tx, i *domain.Invitation, id string) error
GetInvitationByCode(ctx context.Context, tx pg.Tx, i *domain.Invitation, hashedCode string) error
InsertInvitation(ctx context.Context, tx pg.Tx, i *domain.Invitation, hashedCode string) error
UpdateInvitation(ctx context.Context, tx pg.Tx, i *domain.Invitation, hashedCode string) error
GetMembership(ctx context.Context, tx pg.Tx, m *domain.Membership, orgID, userID string) error
InsertMembership(ctx context.Context, tx pg.Tx, m *domain.Membership) error
InsertOrganization(ctx context.Context, tx pg.Tx, org *domain.Organization) error
}
func NewRosterService(db Database, email Emailer, logger logger.Logger, repo RosterRepository) domain.RosterService {
return &rosterLogger{
Logger: logger,
Service: &rosterService{
DB: db,
Email: email,
Repo: repo,
},
}
}
type rosterService struct {
DB Database
Email Emailer
Repo RosterRepository
}
func (svc *rosterService) AcceptInvitation(ctx context.Context, userID, code string) error {
err := svc.DB.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
var invitation domain.Invitation
hashedCode := hash.Generate(code)
if err := svc.Repo.GetInvitationByCode(ctx, tx, &invitation, hashedCode); err != nil {
return errors.WrapError(err)
}
if invitation.Expired() {
return domain.InvitationExpiredError{}
}
membership := &domain.Membership{
OrganizationID: invitation.OrganizationID,
UserID: userID,
Joined: time.Now(),
}
if err := membership.Validate(); err != nil {
return errors.WrapError(err)
}
if err := svc.Repo.InsertMembership(ctx, tx, membership); err != nil {
return errors.WrapError(err)
}
return svc.Repo.DeleteInvitationByCode(ctx, tx, hashedCode)
})
if err != nil {
return fmt.Errorf("accept invitation failed: %w", err)
}
return nil
}
// CreateOrganization creates a new organization and persists it to the
// database. It returns a response containing the new organization if
// successful.
func (svc *rosterService) CreateOrganization(ctx context.Context, name, ownerID string) (*domain.Organization, error) {
var org domain.Organization
err := svc.DB.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
org = domain.Organization{
ID: uuid.New(),
Name: name,
OwnerID: ownerID,
}
if err := org.Validate(); err != nil {
return errors.WrapError(err)
}
membership := &domain.Membership{
OrganizationID: org.ID,
UserID: ownerID,
Joined: time.Now(),
}
if err := membership.Validate(); err != nil {
return errors.WrapError(err)
}
if err := svc.Repo.InsertOrganization(ctx, tx, &org); err != nil {
return errors.WrapError(err)
}
return svc.Repo.InsertMembership(ctx, tx, membership)
})
if err != nil {
return nil, fmt.Errorf("create organization failed: %w", err)
}
return &org, nil
}
func (svc *rosterService) DeclineInvitation(ctx context.Context, code string) error {
err := svc.DB.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
return svc.Repo.DeleteInvitationByCode(ctx, tx, hash.Generate(code))
})
if err != nil {
return fmt.Errorf("decline invitation failed: %w", err)
}
return nil
}
func (svc *rosterService) GetInvitation(ctx context.Context, id string) (*domain.Invitation, error) {
var invitation domain.Invitation
err := svc.DB.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
return svc.Repo.GetInvitation(ctx, tx, &invitation, id)
})
if err != nil {
return nil, fmt.Errorf("get invitation failed: %w", err)
}
return &invitation, nil
}
func (svc *rosterService) InviteUserToOrganization(ctx context.Context, email, orgID string) (*domain.Invitation, error) {
var invitation domain.Invitation
err := svc.DB.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
invitation = domain.Invitation{
ID: uuid.New(),
Email: email,
OrganizationID: orgID,
Expires: time.Now().Add(domain.InvitationValidFor),
}
if err := invitation.Validate(); err != nil {
return errors.WrapError(err)
}
code := random.String(domain.InvitationCodeLength)
if err := svc.Repo.InsertInvitation(ctx, tx, &invitation, hash.Generate(code)); err != nil {
return errors.WrapError(err)
}
return svc.Email.SendInvitation(ctx, invitation.Email, code)
})
if err != nil {
return nil, fmt.Errorf("invite user to organization failed: %w", err)
}
return &invitation, nil
}
func (svc *rosterService) IsMember(ctx context.Context, orgID, userID string) error {
err := svc.DB.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
return svc.Repo.GetMembership(ctx, tx, &domain.Membership{}, orgID, userID)
})
if err != nil {
return fmt.Errorf("is member failed: %w", err)
}
return nil
}
// RefreshInvitation updates an invitation with a new expiration time and code.
// Refreshing an invitation will invalidate the initial invitation email (as
// well as any other refresh emails).
func (svc *rosterService) RefreshInvitation(ctx context.Context, id string) (*domain.Invitation, error) {
var invitation domain.Invitation
err := svc.DB.WithTx(ctx, func(ctx context.Context, tx pg.Tx) error {
if err := svc.Repo.GetInvitation(ctx, tx, &invitation, id); err != nil {
return errors.WrapError(err)
}
invitation.Expires = time.Now().Add(domain.InvitationValidFor)
newCode := random.String(domain.InvitationCodeLength)
if err := svc.Repo.UpdateInvitation(ctx, tx, &invitation, hash.Generate(newCode)); err != nil {
return errors.WrapError(err)
}
return svc.Email.SendInvitation(ctx, invitation.Email, newCode)
})
if err != nil {
return nil, fmt.Errorf("refresh invitation failed: %w", err)
}
return &invitation, nil
}