/
event_create_invoice_for_contract.go
55 lines (49 loc) · 2.58 KB
/
event_create_invoice_for_contract.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
package invoice
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/pkg/errors"
"time"
)
type InvoiceForContractCreateEvent struct {
Tenant string `json:"tenant" validate:"required"`
ContractId string `json:"contractId" validate:"required"`
CreatedAt time.Time `json:"createdAt"`
SourceFields commonmodel.Source `json:"sourceFields"`
DryRun bool `json:"dryRun"`
Currency string `json:"currency"`
PeriodStartDate time.Time `json:"periodStartDate"`
PeriodEndDate time.Time `json:"periodEndDate"`
BillingCycle string `json:"billingCycle"` // Deprecated: Use BillingCycleInMonths instead
BillingCycleInMonths int64 `json:"billingCycleInMonths" validate:"required_if=OffCycle false"`
Note string `json:"note"`
OffCycle bool `json:"offCycle,omitempty"`
Postpaid bool `json:"postpaid"`
Preview bool `json:"preview"`
}
func NewInvoiceForContractCreateEvent(aggregate eventstore.Aggregate, sourceFields commonmodel.Source, contractId, currency, note string, billingCycleInMonths int64, dryRun, offCycle, postpaid, preview bool, createdAt, periodStartDate, periodEndDate time.Time) (eventstore.Event, error) {
eventData := InvoiceForContractCreateEvent{
Tenant: aggregate.GetTenant(),
ContractId: contractId,
CreatedAt: createdAt,
SourceFields: sourceFields,
Currency: currency,
DryRun: dryRun,
PeriodStartDate: periodStartDate,
PeriodEndDate: periodEndDate,
BillingCycleInMonths: billingCycleInMonths,
Note: note,
OffCycle: offCycle,
Postpaid: postpaid,
Preview: preview,
}
if err := validator.GetValidator().Struct(eventData); err != nil {
return eventstore.Event{}, errors.Wrap(err, "failed to validate InvoiceCreateEvent")
}
event := eventstore.NewBaseEvent(aggregate, InvoiceCreateForContractV1)
if err := event.SetJsonData(&eventData); err != nil {
return eventstore.Event{}, errors.Wrap(err, "error setting json data for InvoiceCreateEvent")
}
return event, nil
}