-
Notifications
You must be signed in to change notification settings - Fork 12
/
aggregate.go
94 lines (77 loc) · 3.15 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
package invoicing_cycle
import (
"context"
"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"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
"strings"
)
const (
InvoicingCycleAggregateType eventstore.AggregateType = "invoicing_cycle"
)
type InvoicingCycleAggregate struct {
*aggregate.CommonTenantIdAggregate
InvoicingCycle *InvoicingCycle
}
func GetInvoicingCycleObjectID(aggregateID string, tenant string) string {
return aggregate.GetAggregateObjectID(aggregateID, tenant, InvoicingCycleAggregateType)
}
func LoadInvoicingCycleAggregate(ctx context.Context, eventStore eventstore.AggregateStore, tenant, objectID string) (*InvoicingCycleAggregate, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "LoadInvoicingCycleAggregate")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, tenant)
span.LogFields(log.String("ObjectID", objectID))
invoicingCycleAggregate := NewInvoicingCycleAggregateWithTenantAndID(tenant, objectID)
err := aggregate.LoadAggregate(ctx, eventStore, invoicingCycleAggregate, *eventstore.NewLoadAggregateOptions())
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
return invoicingCycleAggregate, nil
}
func NewInvoicingCycleAggregateWithTenantAndID(tenant, id string) *InvoicingCycleAggregate {
invoicingCycleAggregate := InvoicingCycleAggregate{}
invoicingCycleAggregate.CommonTenantIdAggregate = aggregate.NewCommonAggregateWithTenantAndId(InvoicingCycleAggregateType, tenant, id)
invoicingCycleAggregate.SetWhen(invoicingCycleAggregate.When)
invoicingCycleAggregate.InvoicingCycle = &InvoicingCycle{}
invoicingCycleAggregate.Tenant = tenant
return &invoicingCycleAggregate
}
func (a *InvoicingCycleAggregate) When(evt eventstore.Event) error {
switch evt.GetEventType() {
case InvoicingCycleCreateV1:
return a.onInvoicingCycleCreate(evt)
case InvoicingCycleUpdateV1:
return a.onInvoicingCycleUpdate(evt)
default:
if strings.HasPrefix(evt.GetEventType(), constants.EsInternalStreamPrefix) {
return nil
}
err := eventstore.ErrInvalidEventType
err.EventType = evt.GetEventType()
return err
}
}
func (a *InvoicingCycleAggregate) onInvoicingCycleCreate(evt eventstore.Event) error {
var eventData InvoicingCycleCreateEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
a.InvoicingCycle.ID = a.ID
a.InvoicingCycle.Type = eventData.Type
a.InvoicingCycle.CreatedAt = eventData.CreatedAt
a.InvoicingCycle.SourceFields = eventData.SourceFields
return nil
}
func (a *InvoicingCycleAggregate) onInvoicingCycleUpdate(evt eventstore.Event) error {
var eventData InvoicingCycleUpdateEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
a.InvoicingCycle.Type = eventData.Type
return nil
}