57 lines
1.3 KiB
Go
57 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, clientID, email 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),
|
|
Email: email,
|
|
}
|
|
|
|
_, 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) {
|
|
return o.repo.OrderCompleted(ctx, order)
|
|
}
|