-
Notifications
You must be signed in to change notification settings - Fork 12
/
aggregate.go
71 lines (64 loc) · 2.84 KB
/
aggregate.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
package aggregate
import (
"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"
cmnmod "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_session/event"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/interaction_session/model"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/eventstore"
"github.com/pkg/errors"
"strings"
)
const (
InteractionSessionAggregateType eventstore.AggregateType = "interaction_session"
)
type InteractionSessionAggregate struct {
*aggregate.CommonTenantIdAggregate
InteractionSession *model.InteractionSession
}
func NewInteractionSessionAggregateWithTenantAndID(tenant, id string) *InteractionSessionAggregate {
interactionEventAggregate := InteractionSessionAggregate{}
interactionEventAggregate.CommonTenantIdAggregate = aggregate.NewCommonAggregateWithTenantAndId(InteractionSessionAggregateType, tenant, id)
interactionEventAggregate.SetWhen(interactionEventAggregate.When)
interactionEventAggregate.InteractionSession = &model.InteractionSession{}
interactionEventAggregate.Tenant = tenant
return &interactionEventAggregate
}
func (a *InteractionSessionAggregate) When(evt eventstore.Event) error {
switch evt.GetEventType() {
case event.InteractionSessionCreateV1:
return a.onInteractionSessionCreate(evt)
default:
if strings.HasPrefix(evt.GetEventType(), constants.EsInternalStreamPrefix) {
return nil
}
err := eventstore.ErrInvalidEventType
err.EventType = evt.GetEventType()
return err
}
}
func (a *InteractionSessionAggregate) onInteractionSessionCreate(evt eventstore.Event) error {
var eventData event.InteractionSessionCreateEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
a.InteractionSession.ID = a.ID
a.InteractionSession.Tenant = a.Tenant
a.InteractionSession.Channel = eventData.Channel
a.InteractionSession.ChannelData = eventData.ChannelData
a.InteractionSession.Status = eventData.Status
a.InteractionSession.Type = eventData.Type
a.InteractionSession.Name = eventData.Name
a.InteractionSession.Identifier = eventData.Identifier
a.InteractionSession.Source = cmnmod.Source{
Source: eventData.Source,
SourceOfTruth: eventData.Source,
AppSource: eventData.AppSource,
}
a.InteractionSession.CreatedAt = eventData.CreatedAt
a.InteractionSession.UpdatedAt = eventData.UpdatedAt
if eventData.ExternalSystem.Available() {
a.InteractionSession.ExternalSystems = []cmnmod.ExternalSystem{eventData.ExternalSystem}
}
return nil
}