more features
This commit is contained in:
107
cart-grain.go
107
cart-grain.go
@@ -35,23 +35,27 @@ const (
|
||||
)
|
||||
|
||||
type CartItem struct {
|
||||
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"`
|
||||
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"`
|
||||
ArticleType string `json:"type,omitempty"`
|
||||
Image string `json:"image,omitempty"`
|
||||
Outlet string `json:"outlet,omitempty"`
|
||||
}
|
||||
|
||||
type CartDelivery struct {
|
||||
Provider string `json:"provider"`
|
||||
Price int64 `json:"price"`
|
||||
Items []int `json:"items"`
|
||||
Id int `json:"id"`
|
||||
Provider string `json:"provider"`
|
||||
Price int64 `json:"price"`
|
||||
Items []int `json:"items"`
|
||||
PickupPoint *messages.PickupPoint `json:"pickupPoint,omitempty"`
|
||||
}
|
||||
|
||||
type CartGrain struct {
|
||||
@@ -59,10 +63,10 @@ type CartGrain struct {
|
||||
lastItemId int
|
||||
lastDeliveryId int
|
||||
storageMessages []Message
|
||||
Id CartId `json:"id"`
|
||||
Items []*CartItem `json:"items"`
|
||||
TotalPrice int64 `json:"totalPrice"`
|
||||
Deliveries []CartDelivery `json:"deliveries,omitempty"`
|
||||
Id CartId `json:"id"`
|
||||
Items []*CartItem `json:"items"`
|
||||
TotalPrice int64 `json:"totalPrice"`
|
||||
Deliveries []*CartDelivery `json:"deliveries,omitempty"`
|
||||
}
|
||||
|
||||
type Grain interface {
|
||||
@@ -108,9 +112,9 @@ func getItemData(sku string, qty int) (*messages.AddItem, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
orgPrice, _ := getInt(item.Fields[6])
|
||||
orgPrice, _ := getInt(item.Fields[5])
|
||||
|
||||
price, priceErr := getInt(item.Fields[5])
|
||||
price, priceErr := getInt(item.Fields[4])
|
||||
|
||||
if priceErr != nil {
|
||||
return nil, fmt.Errorf("invalid price")
|
||||
@@ -122,17 +126,25 @@ func getItemData(sku string, qty int) (*messages.AddItem, error) {
|
||||
} else if item.StockLevel == "5+" {
|
||||
stock = LowStock
|
||||
}
|
||||
articleType, _ := item.Fields[1].(string)
|
||||
outletGrade, ok := item.Fields[20].(string)
|
||||
var outlet *string
|
||||
if ok {
|
||||
outlet = &outletGrade
|
||||
}
|
||||
|
||||
return &messages.AddItem{
|
||||
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,
|
||||
Quantity: int32(qty),
|
||||
Price: int64(price),
|
||||
OrgPrice: int64(orgPrice),
|
||||
Sku: sku,
|
||||
Name: item.Title,
|
||||
Image: item.Img,
|
||||
Stock: int32(stock),
|
||||
Tax: 2500,
|
||||
ArticleType: articleType,
|
||||
Disclaimer: item.Disclaimer,
|
||||
Outlet: outlet,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -321,13 +333,48 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
|
||||
}
|
||||
}
|
||||
}
|
||||
c.Deliveries = append(c.Deliveries, CartDelivery{
|
||||
c.Deliveries = append(c.Deliveries, &CartDelivery{
|
||||
Id: c.lastDeliveryId,
|
||||
Provider: msg.Provider,
|
||||
Price: 49,
|
||||
Items: items,
|
||||
})
|
||||
}
|
||||
case RemoveDeliveryType:
|
||||
msg, ok := message.Content.(*messages.RemoveDelivery)
|
||||
if !ok {
|
||||
err = fmt.Errorf("expected RemoveDelivery")
|
||||
} else {
|
||||
deliveries := make([]*CartDelivery, 0, len(c.Deliveries))
|
||||
for _, delivery := range c.Deliveries {
|
||||
if delivery.Id == int(msg.Id) {
|
||||
c.TotalPrice -= delivery.Price
|
||||
} else {
|
||||
deliveries = append(deliveries, delivery)
|
||||
}
|
||||
}
|
||||
c.Deliveries = deliveries
|
||||
}
|
||||
case SetPickupPointType:
|
||||
msg, ok := message.Content.(*messages.SetPickupPoint)
|
||||
if !ok {
|
||||
err = fmt.Errorf("expected SetPickupPoint")
|
||||
} else {
|
||||
for _, delivery := range c.Deliveries {
|
||||
if delivery.Id == int(msg.DeliveryId) {
|
||||
delivery.PickupPoint = &messages.PickupPoint{
|
||||
Id: msg.Id,
|
||||
Address: msg.Address,
|
||||
City: msg.City,
|
||||
Zip: msg.Zip,
|
||||
Country: msg.Country,
|
||||
Name: msg.Name,
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("unknown message type %d", message.Type)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user