Co-authored-by: matst80 <mats.tornberg@gmail.com> Reviewed-on: #8 Co-authored-by: Mats Törnberg <mats@tornberg.me> Co-committed-by: Mats Törnberg <mats@tornberg.me>
85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package cart
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.k6n.net/go-cart-actor/pkg/actor"
|
|
"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 Create[T any]() func() *T {
|
|
return func() *T {
|
|
return new(T)
|
|
}
|
|
}
|
|
|
|
func NewCartMultationRegistry(context *CartMutationContext) actor.MutationRegistry {
|
|
|
|
reg := actor.NewMutationRegistry()
|
|
reg.RegisterMutations(
|
|
actor.NewMutation(context.AddItem),
|
|
actor.NewMutation(context.ChangeQuantity),
|
|
actor.NewMutation(context.RemoveItem),
|
|
actor.NewMutation(ClearCart),
|
|
actor.NewMutation(AddVoucher),
|
|
actor.NewMutation(RemoveVoucher),
|
|
actor.NewMutation(UpsertSubscriptionDetails),
|
|
actor.NewMutation(SetUserId),
|
|
actor.NewMutation(LineItemMarking),
|
|
actor.NewMutation(RemoveLineItemMarking),
|
|
actor.NewMutation(SubscriptionAdded),
|
|
// actor.NewMutation(SubscriptionRemoved),
|
|
)
|
|
return reg
|
|
|
|
}
|