/
interaction_event_service.go
170 lines (145 loc) · 8.05 KB
/
interaction_event_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
package service
import (
"context"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
commonmodel "github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/common/model"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/interaction_event/command"
cmdhnd "github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/interaction_event/command_handler"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/interaction_event/model"
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"
interactioneventpb "github.com/openline-ai/openline-customer-os/packages/server/events-processing-proto/gen/proto/go/api/grpc/v1/interaction_event"
"strings"
)
type interactionEventService struct {
interactioneventpb.UnimplementedInteractionEventGrpcServiceServer
log logger.Logger
interactionEventsCommandHandlers *cmdhnd.CommandHandlers
}
func NewInteractionEventService(log logger.Logger, commands *cmdhnd.CommandHandlers) *interactionEventService {
return &interactionEventService{
log: log,
interactionEventsCommandHandlers: commands,
}
}
func (s *interactionEventService) UpsertInteractionEvent(ctx context.Context, request *interactioneventpb.UpsertInteractionEventGrpcRequest) (*interactioneventpb.InteractionEventIdGrpcResponse, error) {
ctx, span := tracing.StartGrpcServerTracerSpan(ctx, "InteractionEventService.UpsertInteractionEvent")
defer span.Finish()
tracing.SetServiceSpanTags(ctx, span, request.Tenant, request.LoggedInUserId)
tracing.LogObjectAsJson(span, "request", request)
interactionEventId := strings.TrimSpace(utils.NewUUIDIfEmpty(request.Id))
dataFields := model.InteractionEventDataFields{
Content: request.Content,
ContentType: request.ContentType,
Channel: request.Channel,
ChannelData: request.ChannelData,
EventType: request.EventType,
Identifier: request.Identifier,
BelongsToIssueId: request.BelongsToIssueId,
BelongsToSessionId: request.BelongsToSessionId,
Hide: request.Hide,
}
if request.Sender != nil {
dataFields.Sender = model.Sender{
Participant: commonmodel.Participant{
ID: request.Sender.Participant.Id,
ParticipantType: GetParticipantTypeFromPB(request.Sender.Participant),
},
RelationType: request.Sender.RelationType,
}
}
dataFields.Receivers = make([]model.Receiver, len(request.Receivers))
for _, receiver := range request.Receivers {
dataFields.Receivers = append(dataFields.Receivers, model.Receiver{
Participant: commonmodel.Participant{
ID: receiver.Participant.Id,
ParticipantType: GetParticipantTypeFromPB(receiver.Participant),
},
RelationType: receiver.RelationType,
})
}
source := commonmodel.Source{}
source.FromGrpc(request.SourceFields)
externalSystem := commonmodel.ExternalSystem{}
externalSystem.FromGrpc(request.ExternalSystemFields)
cmd := command.NewUpsertInteractionEventCommand(interactionEventId, request.Tenant, request.LoggedInUserId, dataFields, source, externalSystem, utils.TimestampProtoToTimePtr(request.CreatedAt), utils.TimestampProtoToTimePtr(request.UpdatedAt))
if err := s.interactionEventsCommandHandlers.UpsertInteractionEvent.Handle(ctx, cmd); err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("(NewUpsertInteractionEventCommand.Handle) tenant:{%v}, interactionEventId:{%v} , err: %v", request.Tenant, request.Id, err.Error())
return nil, s.errResponse(err)
}
return &interactioneventpb.InteractionEventIdGrpcResponse{Id: interactionEventId}, nil
}
func (s *interactionEventService) RequestGenerateSummary(ctx context.Context, request *interactioneventpb.RequestGenerateSummaryGrpcRequest) (*interactioneventpb.InteractionEventIdGrpcResponse, error) {
ctx, span := tracing.StartGrpcServerTracerSpan(ctx, "InteractionEventService.RequestGenerateSummary")
defer span.Finish()
tracing.SetServiceSpanTags(ctx, span, request.Tenant, request.LoggedInUserId)
tracing.LogObjectAsJson(span, "request", request)
cmd := command.NewRequestSummaryCommand(request.Tenant, request.InteractionEventId)
if err := s.interactionEventsCommandHandlers.RequestSummary.Handle(ctx, cmd); err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("Error handling RequestSummary command: %v", err.Error())
return nil, s.errResponse(err)
}
return &interactioneventpb.InteractionEventIdGrpcResponse{Id: request.InteractionEventId}, nil
}
func (s *interactionEventService) RequestGenerateActionItems(ctx context.Context, request *interactioneventpb.RequestGenerateActionItemsGrpcRequest) (*interactioneventpb.InteractionEventIdGrpcResponse, error) {
ctx, span := tracing.StartGrpcServerTracerSpan(ctx, "InteractionEventService.RequestGenerateActionItems")
defer span.Finish()
tracing.SetServiceSpanTags(ctx, span, request.Tenant, request.LoggedInUserId)
tracing.LogObjectAsJson(span, "request", request)
cmd := command.NewRequestActionItemsCommand(request.Tenant, request.InteractionEventId)
if err := s.interactionEventsCommandHandlers.RequestActionItems.Handle(ctx, cmd); err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("Error handling RequestActionItems command: %v", err.Error())
return nil, s.errResponse(err)
}
return &interactioneventpb.InteractionEventIdGrpcResponse{Id: request.InteractionEventId}, nil
}
func (s *interactionEventService) errResponse(err error) error {
return grpcerr.ErrResponse(err)
}
func GetParticipantTypeFromPB(participant *interactioneventpb.Participant) commonmodel.ParticipantType {
if participant == nil {
return ""
}
switch participant.ParticipantType.(type) {
case *interactioneventpb.Participant_User:
return commonmodel.UserType
case *interactioneventpb.Participant_Contact:
return commonmodel.ContactType
case *interactioneventpb.Participant_Organization:
return commonmodel.OrganizationType
case *interactioneventpb.Participant_JobRole:
return commonmodel.JobRoleType
default:
return ""
}
}
func (s *interactionEventService) ReplaceSummary(ctx context.Context, request *interactioneventpb.ReplaceSummaryGrpcRequest) (*interactioneventpb.InteractionEventIdGrpcResponse, error) {
ctx, span := tracing.StartGrpcServerTracerSpan(ctx, "InteractionEventService.ReplaceSummary")
defer span.Finish()
tracing.SetServiceSpanTags(ctx, span, request.Tenant, request.LoggedInUserId)
tracing.LogObjectAsJson(span, "request", request)
cmd := command.NewReplaceSummaryCommand(request.Tenant, request.InteractionEventId, request.Summary, request.ContentType, nil)
if err := s.interactionEventsCommandHandlers.ReplaceSummary.Handle(ctx, cmd); err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("Error handling ReplaceSummary command: %v", err.Error())
return nil, s.errResponse(err)
}
return &interactioneventpb.InteractionEventIdGrpcResponse{Id: request.InteractionEventId}, nil
}
func (s *interactionEventService) ReplaceActionItems(ctx context.Context, request *interactioneventpb.ReplaceActionItemsGrpcRequest) (*interactioneventpb.InteractionEventIdGrpcResponse, error) {
ctx, span := tracing.StartGrpcServerTracerSpan(ctx, "InteractionEventService.ReplaceActionItems")
defer span.Finish()
tracing.SetServiceSpanTags(ctx, span, request.Tenant, request.LoggedInUserId)
tracing.LogObjectAsJson(span, "request", request)
cmd := command.NewReplaceActionItemsCommand(request.Tenant, request.InteractionEventId, request.ActionItems, nil)
if err := s.interactionEventsCommandHandlers.ReplaceActionItems.Handle(ctx, cmd); err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("Error handling ReplaceActionItems command: %v", err.Error())
return nil, s.errResponse(err)
}
return &interactioneventpb.InteractionEventIdGrpcResponse{Id: request.InteractionEventId}, nil
}