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

57 lines
1.3 KiB
Go
Raw Normal View History

2023-07-19 11:47:46 +02:00
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, clientID, email string, amount float64) (*domain.Order, error) {
2023-07-19 11:47:46 +02:00
order := domain.Order{
ID: primitive.NewObjectID(),
OrderID: orderID,
ClientID: clientID,
2023-07-19 11:47:46 +02:00
Amount: amount,
PaidAt: time.Time{},
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(o.expiration),
Email: email,
2023-07-19 11:47:46 +02:00
}
2023-07-19 11:47:46 +02:00
_, 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) (*domain.Order, error) {
2023-07-19 11:47:46 +02:00
return o.repo.OrderCompleted(ctx, order)
}