-
Notifications
You must be signed in to change notification settings - Fork 302
/
invitation_store.go
130 lines (114 loc) · 4.38 KB
/
invitation_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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"context"
"runtime/trace"
"time"
"github.com/jinzhu/gorm"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
)
// GetInvitationStore returns an InvitationStore on the given db (or transaction).
func GetInvitationStore(db *gorm.DB) InvitationStore {
return &invitationStore{store: newStore(db)}
}
type invitationStore struct {
*store
}
var errInvitationAlreadySent = errors.DefineAlreadyExists("invitation_already_sent", "invitation already sent")
func (s *invitationStore) CreateInvitation(ctx context.Context, invitation *ttnpb.Invitation) (*ttnpb.Invitation, error) {
defer trace.StartRegion(ctx, "create invitation").End()
model := Invitation{
Email: invitation.Email,
Token: invitation.Token,
ExpiresAt: invitation.ExpiresAt,
}
if err := s.createEntity(ctx, &model); err != nil {
err = convertError(err)
if errors.IsAlreadyExists(err) {
return nil, errInvitationAlreadySent.New()
}
return nil, err
}
return model.toPB(), nil
}
func (s *invitationStore) FindInvitations(ctx context.Context) ([]*ttnpb.Invitation, error) {
defer trace.StartRegion(ctx, "find invitations").End()
var invitationModels []Invitation
query := s.query(ctx, Invitation{})
query = query.Order(orderFromContext(ctx, "invitations", "id", "ASC"))
if limit, offset := limitAndOffsetFromContext(ctx); limit != 0 {
countTotal(ctx, query.Model(&Invitation{}))
query = query.Limit(limit).Offset(offset)
}
if err := query.Find(&invitationModels).Error; err != nil {
return nil, err
}
invitationProtos := make([]*ttnpb.Invitation, len(invitationModels))
for i, invitationModel := range invitationModels {
invitationProtos[i] = invitationModel.toPB()
}
return invitationProtos, nil
}
var errInvitationNotFound = errors.DefineNotFound("invitation_not_found", "invitation not found")
func (s *invitationStore) GetInvitation(ctx context.Context, token string) (*ttnpb.Invitation, error) {
defer trace.StartRegion(ctx, "get invitation").End()
var invitationModel Invitation
if err := s.query(ctx, Invitation{}).Where(Invitation{Token: token}).First(&invitationModel).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
return nil, errInvitationNotFound.New()
}
return nil, err
}
return invitationModel.toPB(), nil
}
var errInvitationAlreadyAccepted = errors.DefineAlreadyExists("invitation_already_accepted", "invitation already accepted")
func (s *invitationStore) SetInvitationAcceptedBy(ctx context.Context, token string, acceptedByID *ttnpb.UserIdentifiers) error {
defer trace.StartRegion(ctx, "update invitation").End()
var invitationModel Invitation
if err := s.query(ctx, Invitation{}).Where(Invitation{Token: token}).First(&invitationModel).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
return errInvitationNotFound.New()
}
return err
}
user, err := s.findEntity(ctx, acceptedByID, "id")
if err != nil {
return err
}
if invitationModel.AcceptedByID == nil {
id := user.PrimaryKey()
invitationModel.AcceptedByID = &id
} else {
return errInvitationAlreadyAccepted.New()
}
acceptedAt := cleanTime(time.Now())
invitationModel.AcceptedAt = &acceptedAt
return s.updateEntity(ctx, &invitationModel, "accepted_by_id", "accepted_at")
}
func (s *invitationStore) DeleteInvitation(ctx context.Context, email string) error {
defer trace.StartRegion(ctx, "delete invitation").End()
var invitationModel Invitation
if err := s.query(ctx, Invitation{}).Where(Invitation{Email: email}).First(&invitationModel).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
return errInvitationNotFound.New()
}
return err
}
if invitationModel.AcceptedByID != nil {
return errInvitationAlreadyAccepted.New()
}
return s.query(ctx, Invitation{}).Delete(&invitationModel).Error
}