more features
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 29s
Build and Publish / BuildAndDeploy (push) Successful in 2m23s

This commit is contained in:
matst80
2024-11-14 19:33:04 +01:00
parent d617fd9657
commit 3f6f78c839
5 changed files with 156 additions and 62 deletions

View File

@@ -26,13 +26,26 @@ func (id *CartId) UnmarshalJSON(data []byte) error {
return nil
}
type StockStatus int
const (
OutOfStock StockStatus = 0
LowStock StockStatus = 1
InStock StockStatus = 2
)
type CartItem struct {
Id int `json:"id"`
Sku string `json:"sku"`
Name string `json:"name"`
Price int64 `json:"price"`
Quantity int `json:"qty"`
Image string `json:"image"`
Id int `json:"id"`
ParentId int `json:"parentId,omitempty"`
Sku string `json:"sku"`
Name string `json:"name"`
Price int64 `json:"price"`
OrgPrice int64 `json:"orgPrice"`
Stock StockStatus `json:"stock"`
Quantity int `json:"qty"`
Tax int `json:"tax"`
Disclaimer string `json:"disclaimer,omitempty"`
Image string `json:"image,omitempty"`
}
type CartDelivery struct {
@@ -79,34 +92,47 @@ func (c *CartGrain) GetCurrentState() (*FrameWithPayload, error) {
return &ret, nil
}
func getInt(data interface{}) (int, error) {
switch v := data.(type) {
case float64:
return int(v), nil
case int:
return v, nil
default:
return 0, fmt.Errorf("invalid type")
}
}
func getItemData(sku string, qty int) (*messages.AddItem, error) {
item, err := FetchItem(sku)
if err != nil {
return nil, err
}
price := 0
priceField, ok := item.Fields[4]
if ok {
priceFloat, ok := priceField.(float64)
if !ok {
price, ok = priceField.(int)
if !ok {
return nil, fmt.Errorf("invalid price type")
}
} else {
price = int(priceFloat)
}
}
if price == 0 {
orgPrice, _ := getInt(item.Fields[6])
price, priceErr := getInt(item.Fields[5])
if priceErr != nil {
return nil, fmt.Errorf("invalid price")
}
stock := InStock
if item.StockLevel == "0" || item.StockLevel == "" {
stock = OutOfStock
} else if item.StockLevel == "5+" {
stock = LowStock
}
return &messages.AddItem{
Quantity: int32(qty),
Price: int64(price),
Sku: sku,
Name: item.Title,
Image: item.Img,
Quantity: int32(qty),
Price: int64(price),
OrgPrice: int64(orgPrice),
Sku: sku,
Name: item.Title,
Image: item.Img,
Stock: int32(stock),
Tax: 2500,
Disclaimer: item.Disclaimer,
}, nil
}
@@ -219,13 +245,21 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
} else {
c.mu.Lock()
c.lastItemId++
tax := 2500
if msg.Tax > 0 {
tax = int(msg.Tax)
}
c.Items = append(c.Items, &CartItem{
Id: c.lastItemId,
Quantity: int(msg.Quantity),
Sku: msg.Sku,
Name: msg.Name,
Price: msg.Price,
Image: msg.Image,
Id: c.lastItemId,
Quantity: int(msg.Quantity),
Sku: msg.Sku,
Name: msg.Name,
Price: msg.Price,
Image: msg.Image,
Stock: StockStatus(msg.Stock),
Disclaimer: msg.Disclaimer,
OrgPrice: msg.OrgPrice,
Tax: tax,
})
c.TotalPrice += msg.Price * int64(msg.Quantity)
c.mu.Unlock()