-
Notifications
You must be signed in to change notification settings - Fork 12
/
events.go
81 lines (67 loc) · 3 KB
/
events.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
package invoicing_cycle
import (
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/validator"
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/eventstore"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/logger"
"github.com/pkg/errors"
"time"
)
const (
InvoicingCycleCreateV1 = "V1_INVOICING_CYCLE_CREATE"
InvoicingCycleUpdateV1 = "V1_INVOICING_CYCLE_UPDATE"
)
type InvoicingCycleCreateEvent struct {
Tenant string `json:"tenant" validate:"required"`
Type string `json:"type"`
CreatedAt time.Time `json:"createdAt"`
SourceFields commonmodel.Source `json:"sourceFields"`
}
func NewInvoicingCycleCreateEvent(aggregate eventstore.Aggregate, invoicingCycleType string, createdAt *time.Time, sourceFields commonmodel.Source) (eventstore.Event, error) {
eventData := InvoicingCycleCreateEvent{
Tenant: aggregate.GetTenant(),
Type: invoicingCycleType,
CreatedAt: *createdAt,
SourceFields: sourceFields,
}
if err := validator.GetValidator().Struct(eventData); err != nil {
return eventstore.Event{}, errors.Wrap(err, "failed to validate InvoicingCycleCreateEvent")
}
event := eventstore.NewBaseEvent(aggregate, InvoicingCycleCreateV1)
if err := event.SetJsonData(&eventData); err != nil {
return eventstore.Event{}, errors.Wrap(err, "error setting json data for InvoicingCycleCreateEvent")
}
return event, nil
}
type InvoicingCycleUpdateEvent struct {
Tenant string `json:"tenant" validate:"required"`
Type string `json:"type,omitempty"`
UpdatedAt time.Time `json:"updatedAt"`
SourceFields commonmodel.Source `json:"sourceFields"`
}
func NewInvoicingCycleUpdateEvent(aggregate eventstore.Aggregate, invoicingCycleType string, updatedAt *time.Time, sourceFields commonmodel.Source) (eventstore.Event, error) {
eventData := InvoicingCycleUpdateEvent{
Tenant: aggregate.GetTenant(),
UpdatedAt: *updatedAt,
Type: invoicingCycleType,
SourceFields: sourceFields,
}
if err := validator.GetValidator().Struct(eventData); err != nil {
return eventstore.Event{}, errors.Wrap(err, "failed to validate InvoicingCycleUpdateEvent")
}
event := eventstore.NewBaseEvent(aggregate, InvoicingCycleUpdateV1)
if err := event.SetJsonData(&eventData); err != nil {
return eventstore.Event{}, errors.Wrap(err, "error setting json data for InvoicingCycleUpdateEvent")
}
return event, nil
}
type EventHandlers struct {
CreateInvoicingCycle CreateInvoicingCycleHandler
UpdateInvoicingCycle UpdateInvoicingCycleHandler
}
func NewEventHandlers(log logger.Logger, es eventstore.AggregateStore) *EventHandlers {
return &EventHandlers{
CreateInvoicingCycle: NewCreateInvoicingCycleHandler(log, es),
UpdateInvoicingCycle: NewUpdateInvoicingCycleHandler(log, es),
}
}