-
Notifications
You must be signed in to change notification settings - Fork 12
/
aggregate.go
120 lines (106 loc) · 4.22 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
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
package aggregate
import (
"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/constants"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/common/aggregate"
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/log_entry/event"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/log_entry/model"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/eventstore"
"github.com/pkg/errors"
"strings"
)
const (
LogEntryAggregateType eventstore.AggregateType = "log_entry"
)
type LogEntryAggregate struct {
*aggregate.CommonTenantIdAggregate
LogEntry *model.LogEntry
}
func NewLogEntryAggregateWithTenantAndID(tenant, id string) *LogEntryAggregate {
logEntryAggregate := LogEntryAggregate{}
logEntryAggregate.CommonTenantIdAggregate = aggregate.NewCommonAggregateWithTenantAndId(LogEntryAggregateType, tenant, id)
logEntryAggregate.SetWhen(logEntryAggregate.When)
logEntryAggregate.LogEntry = &model.LogEntry{}
logEntryAggregate.Tenant = tenant
return &logEntryAggregate
}
func (a *LogEntryAggregate) When(evt eventstore.Event) error {
switch evt.GetEventType() {
case event.LogEntryCreateV1:
return a.onLogEntryCreate(evt)
case event.LogEntryUpdateV1:
return a.onLogEntryUpdate(evt)
case event.LogEntryAddTagV1:
return a.onLogEntryAddTag(evt)
case event.LogEntryRemoveTagV1:
return a.onLogEntryRemoveTag(evt)
default:
if strings.HasPrefix(evt.GetEventType(), constants.EsInternalStreamPrefix) {
return nil
}
err := eventstore.ErrInvalidEventType
err.EventType = evt.GetEventType()
return err
}
}
func (a *LogEntryAggregate) onLogEntryCreate(evt eventstore.Event) error {
var eventData event.LogEntryCreateEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
a.LogEntry.ID = a.ID
a.LogEntry.Tenant = a.Tenant
a.LogEntry.Content = eventData.Content
a.LogEntry.ContentType = eventData.ContentType
a.LogEntry.AuthorUserId = eventData.AuthorUserId
if eventData.LoggedOrganizationId != "" {
a.LogEntry.LoggedOrganizationIds = utils.AddToListIfNotExists(a.LogEntry.LoggedOrganizationIds, eventData.LoggedOrganizationId)
}
a.LogEntry.StartedAt = eventData.StartedAt
a.LogEntry.Source = commonmodel.Source{
Source: eventData.Source,
SourceOfTruth: eventData.SourceOfTruth,
AppSource: eventData.AppSource,
}
a.LogEntry.CreatedAt = eventData.CreatedAt
a.LogEntry.UpdatedAt = eventData.UpdatedAt
if eventData.ExternalSystem.Available() {
a.LogEntry.ExternalSystems = []commonmodel.ExternalSystem{eventData.ExternalSystem}
}
return nil
}
func (a *LogEntryAggregate) onLogEntryUpdate(evt eventstore.Event) error {
var eventData event.LogEntryUpdateEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
a.LogEntry.Content = eventData.Content
a.LogEntry.ContentType = eventData.ContentType
a.LogEntry.StartedAt = eventData.StartedAt
a.LogEntry.UpdatedAt = eventData.UpdatedAt
if eventData.LoggedOrganizationId != "" {
a.LogEntry.LoggedOrganizationIds = utils.AddToListIfNotExists(a.LogEntry.LoggedOrganizationIds, eventData.LoggedOrganizationId)
}
if eventData.SourceOfTruth != "" {
a.LogEntry.Source.SourceOfTruth = eventData.SourceOfTruth
}
return nil
}
func (a *LogEntryAggregate) onLogEntryAddTag(evt eventstore.Event) error {
var eventData event.LogEntryAddTagEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
a.LogEntry.TagIds = append(a.LogEntry.TagIds, eventData.TagId)
a.LogEntry.TagIds = utils.RemoveDuplicates(a.LogEntry.TagIds)
return nil
}
func (a *LogEntryAggregate) onLogEntryRemoveTag(evt eventstore.Event) error {
var eventData event.LogEntryRemoveTagEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
a.LogEntry.TagIds = utils.RemoveFromList(a.LogEntry.TagIds, eventData.TagId)
return nil
}