146 lines
4.6 KiB
Go
146 lines
4.6 KiB
Go
package cart
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.k6n.net/go-cart-actor/pkg/actor"
|
|
messages "git.k6n.net/go-cart-actor/pkg/messages"
|
|
"github.com/matst80/go-redis-inventory/pkg/inventory"
|
|
)
|
|
|
|
type CartMutationContext struct {
|
|
reservationService inventory.CartReservationService
|
|
}
|
|
|
|
func NewCartMutationContext(reservationService inventory.CartReservationService) *CartMutationContext {
|
|
return &CartMutationContext{
|
|
reservationService: reservationService,
|
|
}
|
|
}
|
|
|
|
func (c *CartMutationContext) ReserveItem(ctx context.Context, cartId CartId, sku string, locationId *string, quantity uint16) (*time.Time, error) {
|
|
if quantity <= 0 || c.reservationService == nil {
|
|
return nil, nil
|
|
}
|
|
l := inventory.LocationID("se")
|
|
if locationId != nil {
|
|
l = inventory.LocationID(*locationId)
|
|
}
|
|
ttl := time.Minute * 15
|
|
endTime := time.Now().Add(ttl)
|
|
err := c.reservationService.ReserveForCart(ctx, inventory.CartReserveRequest{
|
|
CartID: inventory.CartID(cartId.String()),
|
|
InventoryReference: &inventory.InventoryReference{
|
|
SKU: inventory.SKU(sku),
|
|
LocationID: l,
|
|
},
|
|
TTL: ttl,
|
|
Quantity: uint32(quantity),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &endTime, nil
|
|
|
|
}
|
|
|
|
func (c *CartMutationContext) ReleaseItem(ctx context.Context, cartId CartId, sku string, locationId *string) error {
|
|
if c.reservationService == nil {
|
|
return nil
|
|
}
|
|
l := inventory.LocationID("se")
|
|
if locationId != nil {
|
|
l = inventory.LocationID(*locationId)
|
|
}
|
|
return c.reservationService.ReleaseForCart(ctx, inventory.SKU(sku), l, inventory.CartID(cartId.String()))
|
|
}
|
|
|
|
func NewCartMultationRegistry(context *CartMutationContext) actor.MutationRegistry {
|
|
|
|
reg := actor.NewMutationRegistry()
|
|
reg.RegisterMutations(
|
|
actor.NewMutation(context.AddItem, func() *messages.AddItem {
|
|
return &messages.AddItem{}
|
|
}),
|
|
actor.NewMutation(context.ChangeQuantity, func() *messages.ChangeQuantity {
|
|
return &messages.ChangeQuantity{}
|
|
}),
|
|
actor.NewMutation(context.RemoveItem, func() *messages.RemoveItem {
|
|
return &messages.RemoveItem{}
|
|
}),
|
|
actor.NewMutation(context.InitializeCheckout, func() *messages.InitializeCheckout {
|
|
return &messages.InitializeCheckout{}
|
|
}),
|
|
actor.NewMutation(OrderCreated, func() *messages.OrderCreated {
|
|
return &messages.OrderCreated{}
|
|
}),
|
|
actor.NewMutation(RemoveDelivery, func() *messages.RemoveDelivery {
|
|
return &messages.RemoveDelivery{}
|
|
}),
|
|
actor.NewMutation(SetDelivery, func() *messages.SetDelivery {
|
|
return &messages.SetDelivery{}
|
|
}),
|
|
actor.NewMutation(SetPickupPoint, func() *messages.SetPickupPoint {
|
|
return &messages.SetPickupPoint{}
|
|
}),
|
|
actor.NewMutation(ClearCart, func() *messages.ClearCartRequest {
|
|
return &messages.ClearCartRequest{}
|
|
}),
|
|
actor.NewMutation(AddVoucher, func() *messages.AddVoucher {
|
|
return &messages.AddVoucher{}
|
|
}),
|
|
actor.NewMutation(RemoveVoucher, func() *messages.RemoveVoucher {
|
|
return &messages.RemoveVoucher{}
|
|
}),
|
|
actor.NewMutation(UpsertSubscriptionDetails, func() *messages.UpsertSubscriptionDetails {
|
|
return &messages.UpsertSubscriptionDetails{}
|
|
}),
|
|
actor.NewMutation(context.InventoryReserved, func() *messages.InventoryReserved {
|
|
return &messages.InventoryReserved{}
|
|
}),
|
|
actor.NewMutation(PreConditionFailed, func() *messages.PreConditionFailed {
|
|
return &messages.PreConditionFailed{}
|
|
}),
|
|
actor.NewMutation(SetUserId, func() *messages.SetUserId {
|
|
return &messages.SetUserId{}
|
|
}),
|
|
actor.NewMutation(LineItemMarking, func() *messages.LineItemMarking {
|
|
return &messages.LineItemMarking{}
|
|
}),
|
|
actor.NewMutation(RemoveLineItemMarking, func() *messages.RemoveLineItemMarking {
|
|
return &messages.RemoveLineItemMarking{}
|
|
}),
|
|
actor.NewMutation(SubscriptionAdded, func() *messages.SubscriptionAdded {
|
|
return &messages.SubscriptionAdded{}
|
|
}),
|
|
actor.NewMutation(PaymentStarted, func() *messages.PaymentStarted {
|
|
return &messages.PaymentStarted{}
|
|
}),
|
|
actor.NewMutation(PaymentCompleted, func() *messages.PaymentCompleted {
|
|
return &messages.PaymentCompleted{}
|
|
}),
|
|
actor.NewMutation(PaymentDeclined, func() *messages.PaymentDeclined {
|
|
return &messages.PaymentDeclined{}
|
|
}),
|
|
actor.NewMutation(PaymentEventHandler, func() *messages.PaymentEvent {
|
|
return &messages.PaymentEvent{}
|
|
}),
|
|
actor.NewMutation(ConfirmationViewed, func() *messages.ConfirmationViewed {
|
|
return &messages.ConfirmationViewed{}
|
|
}),
|
|
actor.NewMutation(CreateCheckoutOrder, func() *messages.CreateCheckoutOrder {
|
|
return &messages.CreateCheckoutOrder{}
|
|
}),
|
|
actor.NewMutation(AddGiftcard, func() *messages.AddGiftcard {
|
|
return &messages.AddGiftcard{}
|
|
}),
|
|
actor.NewMutation(RemoveGiftcard, func() *messages.RemoveGiftcard {
|
|
return &messages.RemoveGiftcard{}
|
|
}),
|
|
)
|
|
return reg
|
|
|
|
}
|