Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions openmeter/billing/charges/creditpurchase/adapter/charge.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func (a *adapter) CreateCharge(ctx context.Context, in creditpurchase.CreateChar
create := tx.db.ChargeCreditPurchase.Create().
SetNamespace(in.Namespace).
SetCreditAmount(in.Intent.CreditAmount).
SetNillableEffectiveAt(meta.NormalizeOptionalTimestamp(in.Intent.EffectiveAt)).
SetNillablePriority(in.Intent.Priority).
SetSettlement(in.Intent.Settlement)

create, err := chargemeta.Create(create, chargemeta.CreateInput{
Expand Down
3 changes: 3 additions & 0 deletions openmeter/billing/charges/creditpurchase/adapter/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/openmeterio/openmeter/openmeter/billing/charges/models/ledgertransaction"
"github.com/openmeterio/openmeter/openmeter/billing/charges/models/payment"
entdb "github.com/openmeterio/openmeter/openmeter/ent/db"
"github.com/openmeterio/openmeter/pkg/convert"
)

func MapCreditPurchaseChargeFromDB(dbEntity *entdb.ChargeCreditPurchase, expands meta.Expands) (creditpurchase.Charge, error) {
Expand Down Expand Up @@ -55,6 +56,8 @@ func MapCreditPurchaseChargeFromDB(dbEntity *entdb.ChargeCreditPurchase, expands
Intent: creditpurchase.Intent{
Intent: mappedMeta.Intent,
CreditAmount: dbEntity.CreditAmount,
EffectiveAt: convert.SafeToUTC(dbEntity.EffectiveAt),
Priority: dbEntity.Priority,
Settlement: dbEntity.Settlement,
},
State: creditpurchase.State{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package creditpurchase
import (
"errors"
"fmt"
"time"

"github.com/alpacahq/alpacadecimal"
"github.com/samber/lo"

"github.com/openmeterio/openmeter/openmeter/billing/charges/meta"
"github.com/openmeterio/openmeter/openmeter/billing/charges/models/ledgertransaction"
"github.com/openmeterio/openmeter/openmeter/billing/charges/models/payment"
"github.com/openmeterio/openmeter/pkg/clock"
"github.com/openmeterio/openmeter/pkg/models"
)

Expand Down Expand Up @@ -64,13 +67,19 @@ type Intent struct {
meta.Intent

CreditAmount alpacadecimal.Decimal `json:"amount"`
// EffectiveAt is the time at which the credit purchase is effective.
// Warning/TODO[later]: Currently this is not supported in credit purchase handler and the charge will be created
// with booked_at set to CreatedAt.
EffectiveAt *time.Time `json:"effectiveAt"`
Priority *int `json:"priority"`

// Settlement intent
Settlement Settlement `json:"settlement"`
}

func (i Intent) Normalized() Intent {
i.Intent = i.Intent.Normalized()
i.EffectiveAt = meta.NormalizeOptionalTimestamp(i.EffectiveAt)

calc, err := i.Currency.Calculator()
if err == nil {
Expand All @@ -80,6 +89,10 @@ func (i Intent) Normalized() Intent {
return i
}

func (i Intent) CalculateEffectiveAt() time.Time {
return lo.FromPtrOr(i.EffectiveAt, clock.Now().UTC())
}

func (i Intent) Validate() error {
var errs []error

Expand All @@ -95,7 +108,11 @@ func (i Intent) Validate() error {
errs = append(errs, fmt.Errorf("settlement: %w", err))
}

return errors.Join(errs...)
if i.EffectiveAt != nil {
return errors.New("effective at is not yet supported")
}

return models.NewNillableGenericValidationError(errors.Join(errs...))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

type State struct {
Expand Down
32 changes: 31 additions & 1 deletion openmeter/ent/db/chargecreditpurchase.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions openmeter/ent/db/chargecreditpurchase/chargecreditpurchase.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions openmeter/ent/db/chargecreditpurchase/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions openmeter/ent/db/chargecreditpurchase_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading