-
Notifications
You must be signed in to change notification settings - Fork 12
/
event_store_service.go
86 lines (75 loc) · 3.26 KB
/
event_store_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
package service
import (
"context"
"github.com/EventStore/EventStore-Client-Go/v3/esdb"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/constants"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/common/aggregate"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/eventstore"
grpcerr "github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/grpc_errors"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/logger"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/tracing"
commentpb "github.com/openline-ai/openline-customer-os/packages/server/events-processing-proto/gen/proto/go/api/grpc/v1/comment"
eventstorepb "github.com/openline-ai/openline-customer-os/packages/server/events-processing-proto/gen/proto/go/api/grpc/v1/event_store"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/emptypb"
"time"
)
type eventStoreService struct {
commentpb.UnimplementedCommentGrpcServiceServer
services *Services
log logger.Logger
aggregateStore eventstore.AggregateStore
}
func NewEventStoreService(services *Services, log logger.Logger, aggregateStore eventstore.AggregateStore) *eventStoreService {
return &eventStoreService{
services: services,
log: log,
aggregateStore: aggregateStore,
}
}
func (s *eventStoreService) DeleteEventStoreStream(ctx context.Context, request *eventstorepb.DeleteEventStoreStreamRequest) (*emptypb.Empty, error) {
ctx, span := tracing.StartGrpcServerTracerSpan(ctx, "EventStoreService.DeleteEventStoreStream")
defer span.Finish()
tracing.SetServiceSpanTags(ctx, span, request.Tenant, "")
tracing.LogObjectAsJson(span, "request", request)
if request.Tenant == "" {
err := grpcerr.ErrMissingField("tenant")
tracing.TraceErr(span, err)
return nil, grpcerr.ErrResponse(err)
} else if request.Type == "" {
err := grpcerr.ErrMissingField("type")
tracing.TraceErr(span, err)
return nil, grpcerr.ErrResponse(err)
} else if request.Id == "" {
err := grpcerr.ErrMissingField("id")
tracing.TraceErr(span, err)
return nil, grpcerr.ErrResponse(err)
}
aggr := aggregate.NewCommonAggregateWithTenantAndId(eventstore.AggregateType(request.Type), request.Tenant, request.Id)
// Check if aggregate exists
err := s.aggregateStore.Exists(ctx, aggr.GetID())
if err != nil {
if !errors.Is(err, eventstore.ErrAggregateNotFound) {
return &emptypb.Empty{}, grpcerr.ErrResponse(err)
} else {
return &emptypb.Empty{}, nil
}
}
// 1 day in seconds
maxAgeSeconds := constants.StreamMetadataMaxAgeSeconds
if request.MinutesUntilDeletion > 0 {
maxAgeSeconds = int(request.MinutesUntilDeletion * 60)
}
streamMetadata := esdb.StreamMetadata{}
streamMetadata.SetMaxAge(time.Duration(maxAgeSeconds) * time.Second)
err = s.aggregateStore.UpdateStreamMetadata(ctx, aggr.GetID(), streamMetadata)
if err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("Error while updating stream metadata: %s", err)
return &emptypb.Empty{}, grpcerr.ErrResponse(err)
}
return &emptypb.Empty{}, nil
}
func (s *eventStoreService) errResponse(err error) error {
return grpcerr.ErrResponse(err)
}