Skip to content

Commit 56bf525

Browse files
committed
add delete function
Signed-off-by: Christian Richter <c.richter@opencloud.eu>
1 parent 55420ab commit 56bf525

5 files changed

Lines changed: 155 additions & 3 deletions

File tree

services/invitations/pkg/service/v0/instrument.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ type instrument struct {
2121
metrics *metrics.Metrics
2222
}
2323

24+
func (i instrument) DeleteById(ctx context.Context, id string) error {
25+
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
26+
us := v * 1000000
27+
i.metrics.Latency.WithLabelValues().Observe(us)
28+
i.metrics.Duration.WithLabelValues().Observe(v)
29+
}))
30+
31+
defer timer.ObserveDuration()
32+
i.metrics.Counter.WithLabelValues().Inc()
33+
34+
return i.next.DeleteById(ctx, id)
35+
}
36+
37+
func (i instrument) DeleteByInvitedEmail(ctx context.Context, email string) error {
38+
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
39+
us := v * 1000000
40+
i.metrics.Latency.WithLabelValues().Observe(us)
41+
i.metrics.Duration.WithLabelValues().Observe(v)
42+
}))
43+
44+
defer timer.ObserveDuration()
45+
i.metrics.Counter.WithLabelValues().Inc()
46+
47+
return i.next.DeleteByInvitedEmail(ctx, email)
48+
}
49+
2450
func (i instrument) List(ctx context.Context, userId string) ([]*invitations.Invitation, error) {
2551
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
2652
us := v * 1000000

services/invitations/pkg/service/v0/logging.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ type logging struct {
2020
logger log.Logger
2121
}
2222

23+
func (l logging) DeleteById(ctx context.Context, id string) error {
24+
l.logger.Debug().
25+
Interface("invitation", id).
26+
Msg("DeleteById")
27+
28+
return l.next.DeleteById(ctx, id)
29+
}
30+
31+
func (l logging) DeleteByInvitedEmail(ctx context.Context, email string) error {
32+
l.logger.Debug().
33+
Interface("invitation", email).
34+
Msg("DeleteByInvitedEmail")
35+
36+
return l.next.DeleteByInvitedEmail(ctx, email)
37+
}
38+
2339
func (l logging) List(ctx context.Context, userId string) ([]*invitations.Invitation, error) {
2440
l.logger.Debug().
2541
Interface("invitation", "list").

services/invitations/pkg/service/v0/service.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type Service interface {
3434
List(ctx context.Context, userId string) ([]*invitations.Invitation, error)
3535
GetByInvitedEmail(ctx context.Context, email string) (*invitations.Invitation, error)
3636
GetByInviteId(ctx context.Context, id string) (*invitations.Invitation, error)
37+
DeleteById(ctx context.Context, id string) error
38+
DeleteByInvitedEmail(ctx context.Context, email string) error
3739
}
3840

3941
// Backend defines the behaviour of a user backend.
@@ -153,7 +155,6 @@ func (s svc) Invite(ctx context.Context, invitation *invitations.Invitation) (*i
153155

154156
// List implements the service interface
155157
func (s svc) List(ctx context.Context, userId string) ([]*invitations.Invitation, error) {
156-
fmt.Println("list invitations for user", userId)
157158
// get logged in user
158159
u, ok := revactx.ContextGetUser(ctx)
159160
if !ok {
@@ -190,7 +191,51 @@ func (s svc) List(ctx context.Context, userId string) ([]*invitations.Invitation
190191
return invSlice, nil
191192
}
192193

193-
// GetByInviteId implements the service interface
194+
// DeleteById deletes invitation by id
195+
func (s svc) DeleteById(ctx context.Context, id string) error {
196+
u, ok := revactx.ContextGetUser(ctx)
197+
if !ok {
198+
return ErrUnauthorized
199+
}
200+
201+
toDelete, err := s.Persistance.Read(id)
202+
if err != nil {
203+
return fmt.Errorf("%w: %s", ErrPersistence, err)
204+
}
205+
206+
if toDelete[0].Metadata["inviterUserId"] != u.GetId().GetOpaqueId() {
207+
return ErrUnauthorized
208+
}
209+
210+
return s.Persistance.Delete(id)
211+
}
212+
213+
// DeleteByEmail deletes invitation by email of the invited person
214+
func (s svc) DeleteByInvitedEmail(ctx context.Context, email string) error {
215+
u, ok := revactx.ContextGetUser(ctx)
216+
if !ok {
217+
return ErrUnauthorized
218+
}
219+
invKeyList, err := s.Persistance.List()
220+
if err != nil {
221+
return fmt.Errorf("%w: %s", ErrPersistence, err)
222+
}
223+
for _, key := range invKeyList {
224+
inv, err := s.Persistance.Read(key)
225+
if err != nil {
226+
return fmt.Errorf("%w: %s", ErrPersistence, err)
227+
}
228+
for _, value := range inv {
229+
if value.Metadata["invitedUserEmailAddress"] == email &&
230+
u.GetId().GetOpaqueId() == value.Metadata["inviterUserId"].(string) {
231+
return s.Persistance.Delete(key)
232+
}
233+
}
234+
}
235+
return nil
236+
}
237+
238+
// GetByInviteId gets invitation by id
194239
func (s svc) GetByInviteId(ctx context.Context, inviteId string) (*invitations.Invitation, error) {
195240
u, ok := revactx.ContextGetUser(ctx)
196241
if !ok {
@@ -215,7 +260,7 @@ func (s svc) GetByInviteId(ctx context.Context, inviteId string) (*invitations.I
215260
return invite, nil
216261
}
217262

218-
// GetByInvitedEmail implements the service interface
263+
// GetByInvitedEmail gets invitation by email
219264
func (s svc) GetByInvitedEmail(ctx context.Context, email string) (*invitations.Invitation, error) {
220265
u, ok := revactx.ContextGetUser(ctx)
221266
if !ok {

services/invitations/pkg/service/v0/service_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,41 @@ var _ = Describe("Service", func() {
149149
Expect(inv.InvitedUserEmailAddress).To(Equal(invite.InvitedUserEmailAddress))
150150
})
151151
})
152+
153+
Describe("DeleteById", func() {
154+
It("should delete an invitation by ID", func() {
155+
mockUUID := "test-uuid"
156+
testSvc, _ = service.New(
157+
service.Logger(log.NewLogger()),
158+
service.Config(&config.Config{
159+
Persistance: config.Persistance{
160+
Store: "memory",
161+
},
162+
}),
163+
service.WithUUIDGenerator(func() string {
164+
return mockUUID
165+
}),
166+
)
167+
168+
ctx = revactx.ContextSetUser(ctx, inviter)
169+
_, err := testSvc.Invite(ctx, invite)
170+
Expect(err).ToNot(HaveOccurred())
171+
172+
err = testSvc.DeleteById(ctx, mockUUID)
173+
Expect(err).ToNot(HaveOccurred())
174+
})
175+
})
176+
177+
Describe("DeleteByInvitedEmail", func() {
178+
It("should delete an invitation by email", func() {
179+
ctx = revactx.ContextSetUser(ctx, inviter)
180+
_, err := testSvc.Invite(ctx, invite)
181+
Expect(err).ToNot(HaveOccurred())
182+
183+
inv, err := testSvc.GetByInvitedEmail(ctx, invite.InvitedUserEmailAddress)
184+
Expect(err).ToNot(HaveOccurred())
185+
Expect(inv).ToNot(BeNil())
186+
Expect(inv.InvitedUserEmailAddress).To(Equal(invite.InvitedUserEmailAddress))
187+
})
188+
})
152189
})

services/invitations/pkg/service/v0/tracing.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,34 @@ type tracing struct {
2121
tp trace.TracerProvider
2222
}
2323

24+
func (t tracing) DeleteById(ctx context.Context, id string) error {
25+
spanOpts := []trace.SpanStartOption{
26+
trace.WithSpanKind(trace.SpanKindServer),
27+
trace.WithAttributes(
28+
attribute.KeyValue{
29+
Key: "invitation", Value: attribute.StringValue(id),
30+
}),
31+
}
32+
ctx, span := t.tp.Tracer("invitations").Start(ctx, "Delete", spanOpts...)
33+
defer span.End()
34+
35+
return t.next.DeleteById(ctx, id)
36+
}
37+
38+
func (t tracing) DeleteByInvitedEmail(ctx context.Context, email string) error {
39+
spanOpts := []trace.SpanStartOption{
40+
trace.WithSpanKind(trace.SpanKindServer),
41+
trace.WithAttributes(
42+
attribute.KeyValue{
43+
Key: "invitation", Value: attribute.StringValue(email),
44+
}),
45+
}
46+
ctx, span := t.tp.Tracer("invitations").Start(ctx, "Delete", spanOpts...)
47+
defer span.End()
48+
49+
return t.next.DeleteByInvitedEmail(ctx, email)
50+
}
51+
2452
func (t tracing) List(ctx context.Context, userId string) ([]*invitations.Invitation, error) {
2553
spanOpts := []trace.SpanStartOption{
2654
trace.WithSpanKind(trace.SpanKindServer),

0 commit comments

Comments
 (0)