From 3f6f78c83912d8aaf801c9017b0897647aebd41e Mon Sep 17 00:00:00 2001 From: matst80 Date: Thu, 14 Nov 2024 19:33:04 +0100 Subject: [PATCH] more features --- cart-grain.go | 96 ++++++++++++++++++++++++++++-------------- cart-grain_test.go | 19 +++++++++ discarded-host_test.go | 5 ++- proto/messages.pb.go | 92 ++++++++++++++++++++++++++++------------ proto/messages.proto | 6 ++- 5 files changed, 156 insertions(+), 62 deletions(-) diff --git a/cart-grain.go b/cart-grain.go index 0b772fa..eb6fdcb 100644 --- a/cart-grain.go +++ b/cart-grain.go @@ -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() diff --git a/cart-grain_test.go b/cart-grain_test.go index 162c25d..ddafc40 100644 --- a/cart-grain_test.go +++ b/cart-grain_test.go @@ -67,6 +67,25 @@ func TestAddToCartShortCut(t *testing.T) { } } +func TestAddRequestToGrain(t *testing.T) { + grain, err := spawn(ToCartId("kalle")) + if err != nil { + t.Errorf("Error spawning: %v\n", err) + } + msg := GetMessage(AddRequestType, &messages.AddRequest{ + Quantity: 2, + Sku: "763281", + }) + result, err := grain.HandleMessage(msg, false) + if err != nil { + t.Errorf("Error handling message: %v\n", err) + } + if result.StatusCode != 200 { + t.Errorf("Call failed\n") + } + t.Log(result) +} + func TestAddToCart(t *testing.T) { grain, err := spawn(ToCartId("kalle")) if err != nil { diff --git a/discarded-host_test.go b/discarded-host_test.go index a4303ff..e272bf7 100644 --- a/discarded-host_test.go +++ b/discarded-host_test.go @@ -6,9 +6,10 @@ import ( ) func TestDiscardedHost(t *testing.T) { - dh := NewDiscardedHostHandler(func(host string) { + dh := NewDiscardedHostHandler(8080) + dh.SetReconnectHandler(func(host string) { t.Log(host) - }, 8080) + }) dh.AppendHost("localhost") time.Sleep(2 * time.Second) if dh.hosts[0].Tries == 0 { diff --git a/proto/messages.pb.go b/proto/messages.pb.go index 49ff5a8..9ac3e5d 100644 --- a/proto/messages.pb.go +++ b/proto/messages.pb.go @@ -78,11 +78,15 @@ type AddItem struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Quantity int32 `protobuf:"varint,2,opt,name=Quantity,proto3" json:"Quantity,omitempty"` - Price int64 `protobuf:"varint,3,opt,name=Price,proto3" json:"Price,omitempty"` - Sku string `protobuf:"bytes,4,opt,name=Sku,proto3" json:"Sku,omitempty"` - Name string `protobuf:"bytes,5,opt,name=Name,proto3" json:"Name,omitempty"` - Image string `protobuf:"bytes,6,opt,name=Image,proto3" json:"Image,omitempty"` + Quantity int32 `protobuf:"varint,2,opt,name=Quantity,proto3" json:"Quantity,omitempty"` + Price int64 `protobuf:"varint,3,opt,name=Price,proto3" json:"Price,omitempty"` + OrgPrice int64 `protobuf:"varint,9,opt,name=OrgPrice,proto3" json:"OrgPrice,omitempty"` + Sku string `protobuf:"bytes,4,opt,name=Sku,proto3" json:"Sku,omitempty"` + Name string `protobuf:"bytes,5,opt,name=Name,proto3" json:"Name,omitempty"` + Image string `protobuf:"bytes,6,opt,name=Image,proto3" json:"Image,omitempty"` + Stock int32 `protobuf:"varint,7,opt,name=Stock,proto3" json:"Stock,omitempty"` + Tax int32 `protobuf:"varint,8,opt,name=Tax,proto3" json:"Tax,omitempty"` + Disclaimer string `protobuf:"bytes,10,opt,name=Disclaimer,proto3" json:"Disclaimer,omitempty"` } func (x *AddItem) Reset() { @@ -129,6 +133,13 @@ func (x *AddItem) GetPrice() int64 { return 0 } +func (x *AddItem) GetOrgPrice() int64 { + if x != nil { + return x.OrgPrice + } + return 0 +} + func (x *AddItem) GetSku() string { if x != nil { return x.Sku @@ -150,6 +161,27 @@ func (x *AddItem) GetImage() string { return "" } +func (x *AddItem) GetStock() int32 { + if x != nil { + return x.Stock + } + return 0 +} + +func (x *AddItem) GetTax() int32 { + if x != nil { + return x.Tax + } + return 0 +} + +func (x *AddItem) GetDisclaimer() string { + if x != nil { + return x.Disclaimer + } + return "" +} + type RemoveItem struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -354,28 +386,34 @@ var file_messages_proto_rawDesc = []byte{ 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x03, 0x53, 0x6b, 0x75, 0x22, 0x77, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x49, 0x74, 0x65, - 0x6d, 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x14, 0x0a, - 0x05, 0x50, 0x72, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x50, 0x72, - 0x69, 0x63, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x03, 0x53, 0x6b, 0x75, 0x12, 0x12, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x6d, 0x61, - 0x67, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x22, - 0x1c, 0x0a, 0x0a, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x12, 0x0e, 0x0a, - 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x49, 0x64, 0x22, 0x3c, 0x0a, - 0x0e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, - 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x49, 0x64, 0x12, - 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x22, 0x3f, 0x0a, 0x0b, 0x53, - 0x65, 0x74, 0x44, 0x65, 0x6c, 0x69, 0x76, 0x65, 0x72, 0x79, 0x12, 0x1a, 0x0a, 0x08, 0x50, 0x72, - 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x50, 0x72, - 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x18, - 0x02, 0x20, 0x03, 0x28, 0x03, 0x52, 0x05, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x22, 0x20, 0x0a, 0x0e, - 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x44, 0x65, 0x6c, 0x69, 0x76, 0x65, 0x72, 0x79, 0x12, 0x0e, - 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x49, 0x64, 0x42, 0x0c, - 0x5a, 0x0a, 0x2e, 0x3b, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x09, 0x52, 0x03, 0x53, 0x6b, 0x75, 0x22, 0xdb, 0x01, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x49, 0x74, + 0x65, 0x6d, 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x14, + 0x0a, 0x05, 0x50, 0x72, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x50, + 0x72, 0x69, 0x63, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x4f, 0x72, 0x67, 0x50, 0x72, 0x69, 0x63, 0x65, + 0x18, 0x09, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x4f, 0x72, 0x67, 0x50, 0x72, 0x69, 0x63, 0x65, + 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x53, + 0x6b, 0x75, 0x12, 0x12, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x18, + 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x05, + 0x53, 0x74, 0x6f, 0x63, 0x6b, 0x18, 0x07, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x53, 0x74, 0x6f, + 0x63, 0x6b, 0x12, 0x10, 0x0a, 0x03, 0x54, 0x61, 0x78, 0x18, 0x08, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x03, 0x54, 0x61, 0x78, 0x12, 0x1e, 0x0a, 0x0a, 0x44, 0x69, 0x73, 0x63, 0x6c, 0x61, 0x69, 0x6d, + 0x65, 0x72, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x44, 0x69, 0x73, 0x63, 0x6c, 0x61, + 0x69, 0x6d, 0x65, 0x72, 0x22, 0x1c, 0x0a, 0x0a, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x49, 0x74, + 0x65, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, + 0x49, 0x64, 0x22, 0x3c, 0x0a, 0x0e, 0x43, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x51, 0x75, 0x61, 0x6e, + 0x74, 0x69, 0x74, 0x79, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, + 0x52, 0x02, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, + 0x22, 0x3f, 0x0a, 0x0b, 0x53, 0x65, 0x74, 0x44, 0x65, 0x6c, 0x69, 0x76, 0x65, 0x72, 0x79, 0x12, + 0x1a, 0x0a, 0x08, 0x50, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x08, 0x50, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x49, + 0x74, 0x65, 0x6d, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x03, 0x52, 0x05, 0x49, 0x74, 0x65, 0x6d, + 0x73, 0x22, 0x20, 0x0a, 0x0e, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x44, 0x65, 0x6c, 0x69, 0x76, + 0x65, 0x72, 0x79, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x02, 0x49, 0x64, 0x42, 0x0c, 0x5a, 0x0a, 0x2e, 0x3b, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, + 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/proto/messages.proto b/proto/messages.proto index 76ca6ca..30555e3 100644 --- a/proto/messages.proto +++ b/proto/messages.proto @@ -10,9 +10,13 @@ message AddRequest { message AddItem { int32 Quantity = 2; int64 Price = 3; + int64 OrgPrice = 9; string Sku = 4; string Name = 5; string Image = 6; + int32 Stock = 7; + int32 Tax = 8; + string Disclaimer = 10; } message RemoveItem { @@ -32,5 +36,3 @@ message SetDelivery { message RemoveDelivery { int64 Id = 1; } - -