flamingo.me/flamingo-commerce/v3@v3.11.0/test/integrationtest/projecttest/modules/cart/fake_voucher_handler.go (about)

     1  package cart
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	domainCart "flamingo.me/flamingo-commerce/v3/cart/domain/cart"
     8  	"flamingo.me/flamingo-commerce/v3/cart/infrastructure"
     9  	"flamingo.me/flamingo-commerce/v3/price/domain"
    10  )
    11  
    12  type (
    13  	// FakeVoucherHandler used vouchers in integration tests
    14  	FakeVoucherHandler struct{}
    15  )
    16  
    17  var (
    18  	_ infrastructure.VoucherHandler = &FakeVoucherHandler{}
    19  )
    20  
    21  // ApplyVoucher fake implementation
    22  func (f FakeVoucherHandler) ApplyVoucher(ctx context.Context, cart *domainCart.Cart, couponCode string) (*domainCart.Cart, error) {
    23  	if couponCode != "100-percent-off" {
    24  		err := errors.New("voucher code invalid")
    25  		return nil, err
    26  	}
    27  
    28  	coupon := domainCart.CouponCode{
    29  		Code: couponCode,
    30  	}
    31  
    32  	if !f.voucherAlreadyApplied(cart, couponCode) {
    33  		cart.AppliedCouponCodes = append(cart.AppliedCouponCodes, coupon)
    34  	}
    35  
    36  	if couponCode == "100-percent-off" {
    37  		for delKey, delivery := range cart.Deliveries {
    38  			for itemKey, item := range delivery.Cartitems {
    39  				cart.Deliveries[delKey].Cartitems[itemKey].AppliedDiscounts = []domainCart.AppliedDiscount{{
    40  					CampaignCode:  "100-percent-off",
    41  					CouponCode:    "100-percent-off",
    42  					Label:         "100% Off",
    43  					Applied:       item.RowPriceGross.Inverse(),
    44  					Type:          "coupon",
    45  					IsItemRelated: false,
    46  					SortOrder:     0,
    47  				}}
    48  				cart.Deliveries[delKey].Cartitems[itemKey].RowPriceGrossWithDiscount = domain.NewZero(item.RowPriceGross.Currency())
    49  				cart.Deliveries[delKey].Cartitems[itemKey].RowPriceNetWithDiscount = domain.NewZero(item.RowPriceGross.Currency())
    50  				cart.Deliveries[delKey].Cartitems[itemKey].NonItemRelatedDiscountAmount = item.RowPriceGross.Inverse()
    51  				cart.Deliveries[delKey].Cartitems[itemKey].TotalDiscountAmount = item.RowPriceGross.Inverse()
    52  			}
    53  		}
    54  	}
    55  
    56  	return cart, nil
    57  }
    58  
    59  // RemoveVoucher fake implementation
    60  func (f FakeVoucherHandler) RemoveVoucher(ctx context.Context, cart *domainCart.Cart, couponCode string) (*domainCart.Cart, error) {
    61  	for i, coupon := range cart.AppliedCouponCodes {
    62  		if coupon.Code == couponCode {
    63  			cart.AppliedCouponCodes[i] = cart.AppliedCouponCodes[len(cart.AppliedCouponCodes)-1]
    64  			cart.AppliedCouponCodes[len(cart.AppliedCouponCodes)-1] = domainCart.CouponCode{}
    65  			cart.AppliedCouponCodes = cart.AppliedCouponCodes[:len(cart.AppliedCouponCodes)-1]
    66  			return cart, nil
    67  		}
    68  	}
    69  
    70  	return nil, errors.New("couldn't remove supplied voucher since it wasn't applied before")
    71  }
    72  
    73  func (FakeVoucherHandler) voucherAlreadyApplied(cart *domainCart.Cart, code string) bool {
    74  	for _, coupon := range cart.AppliedCouponCodes {
    75  		if coupon.Code == code {
    76  			return true
    77  		}
    78  	}
    79  	return false
    80  }