/
comment_service.go
53 lines (45 loc) · 2.3 KB
/
comment_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
package service
import (
"context"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/config"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/comment"
"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"
)
type commentService struct {
commentpb.UnimplementedCommentGrpcServiceServer
services *Services
log logger.Logger
aggregateStore eventstore.AggregateStore
}
func NewCommentService(services *Services, log logger.Logger, aggregateStore eventstore.AggregateStore, cfg *config.Config) *commentService {
return &commentService{
services: services,
log: log,
aggregateStore: aggregateStore,
}
}
func (s *commentService) UpsertComment(ctx context.Context, request *commentpb.UpsertCommentGrpcRequest) (*commentpb.CommentIdGrpcResponse, error) {
ctx, span := tracing.StartGrpcServerTracerSpan(ctx, "CommentService.UpsertComment")
defer span.Finish()
tracing.SetServiceSpanTags(ctx, span, request.Tenant, request.UserId)
tracing.LogObjectAsJson(span, "request", request)
commentId := utils.NewUUIDIfEmpty(request.Id)
initAggregateFunc := func() eventstore.Aggregate {
return comment.NewCommentAggregateWithTenantAndID(request.Tenant, commentId)
}
_, err := s.services.RequestHandler.HandleGRPCRequest(ctx, initAggregateFunc, *eventstore.NewLoadAggregateOptions(), request)
if err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("(UpsertComment) tenant:{%v}, commentId:{%s} ,err: %v", request.Tenant, commentId, err.Error())
return nil, grpcerr.ErrResponse(err)
}
return &commentpb.CommentIdGrpcResponse{Id: commentId}, nil
}
func (s *commentService) errResponse(err error) error {
return grpcerr.ErrResponse(err)
}