btc-pay-checker/internal/services/order.go

55 lines
1.3 KiB
Go

package services
import (
"context"
"time"
"gitea.urkob.com/urko/btc-pay-checker/internal/domain"
"gitea.urkob.com/urko/btc-pay-checker/internal/platform/mongodb/order"
"go.mongodb.org/mongo-driver/bson/primitive"
)
const defaultExpirationTime = time.Minute * 30
type Order struct {
repo *order.Repo
expiration time.Duration
}
func NewOrder(repo *order.Repo) *Order {
return &Order{
repo: repo,
expiration: defaultExpirationTime,
}
}
func (o *Order) WithExpiration(expiration time.Duration) *Order {
o.expiration = expiration
return o
}
func (o *Order) NewOrder(ctx context.Context, OrderID string, ClientID string, amount float64) (*domain.Order, error) {
order := domain.Order{
ID: primitive.NewObjectID(),
OrderID: OrderID,
ClientID: ClientID,
Amount: amount,
PaidAt: time.Time{},
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(o.expiration),
}
_, err := o.repo.Insert(ctx, &order)
if err != nil {
return nil, err
}
return &order, err
}
func (o *Order) FromAmount(ctx context.Context, amount float64, timestamp time.Time) (*domain.Order, error) {
return o.repo.FromAmount(ctx, amount, timestamp)
}
func (o *Order) OrderCompleted(ctx context.Context, order *domain.Order) error {
return o.repo.OrderCompleted(ctx, order)
}