diff --git a/pkg/cart/cart-grain.go b/pkg/cart/cart-grain.go index a4fb4b3..5c5816b 100644 --- a/pkg/cart/cart-grain.go +++ b/pkg/cart/cart-grain.go @@ -92,10 +92,12 @@ type CartGrain struct { } type Voucher struct { - Code string `json:"code"` - Rules []string `json:"rules"` - Id uint32 `json:"id"` - Value int64 `json:"value"` + Code string `json:"code"` + Applied bool `json:"applied"` + Rules []string `json:"rules"` + Description string `json:"description,omitempty"` + Id uint32 `json:"id"` + Value int64 `json:"value"` } func (v *Voucher) AppliesTo(cart *CartGrain) ([]*CartItem, bool) { @@ -270,13 +272,15 @@ func (c *CartGrain) UpdateTotals() { c.TotalPrice.Add(delivery.Price) } for _, voucher := range c.Vouchers { - if _, ok := voucher.AppliesTo(c); ok { + _, ok := voucher.AppliesTo(c) + voucher.Applied = false + if ok { value := NewPriceFromIncVat(voucher.Value, 25) if c.TotalPrice.IncVat <= value.IncVat { // don't apply discounts to more than the total price continue } - + voucher.Applied = true c.TotalDiscount.Add(*value) c.TotalPrice.Subtract(*value) } diff --git a/pkg/messages/messages.pb.go b/pkg/messages/messages.pb.go index 9f4f8a5..86ccfed 100644 --- a/pkg/messages/messages.pb.go +++ b/pkg/messages/messages.pb.go @@ -931,6 +931,7 @@ type AddVoucher struct { Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` Value int64 `protobuf:"varint,2,opt,name=value,proto3" json:"value,omitempty"` VoucherRules []string `protobuf:"bytes,3,rep,name=voucherRules,proto3" json:"voucherRules,omitempty"` + Description string `protobuf:"bytes,4,opt,name=description,proto3" json:"description,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -986,6 +987,13 @@ func (x *AddVoucher) GetVoucherRules() []string { return nil } +func (x *AddVoucher) GetDescription() string { + if x != nil { + return x.Description + } + return "" +} + type RemoveVoucher struct { state protoimpl.MessageState `protogen:"open.v1"` Id uint32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` @@ -1267,12 +1275,13 @@ const file_messages_proto_rawDesc = "" + "\x12InitializeCheckout\x12\x18\n" + "\aorderId\x18\x01 \x01(\tR\aorderId\x12\x16\n" + "\x06status\x18\x02 \x01(\tR\x06status\x12,\n" + - "\x11paymentInProgress\x18\x03 \x01(\bR\x11paymentInProgress\"Z\n" + + "\x11paymentInProgress\x18\x03 \x01(\bR\x11paymentInProgress\"|\n" + "\n" + "AddVoucher\x12\x12\n" + "\x04code\x18\x01 \x01(\tR\x04code\x12\x14\n" + "\x05value\x18\x02 \x01(\x03R\x05value\x12\"\n" + - "\fvoucherRules\x18\x03 \x03(\tR\fvoucherRules\"\x1f\n" + + "\fvoucherRules\x18\x03 \x03(\tR\fvoucherRules\x12 \n" + + "\vdescription\x18\x04 \x01(\tR\vdescription\"\x1f\n" + "\rRemoveVoucher\x12\x0e\n" + "\x02id\x18\x01 \x01(\rR\x02id\"\xa7\x01\n" + "\x19UpsertSubscriptionDetails\x12\x13\n" + diff --git a/pkg/voucher/service.go b/pkg/voucher/service.go index 01a9677..f65cd7c 100644 --- a/pkg/voucher/service.go +++ b/pkg/voucher/service.go @@ -15,9 +15,10 @@ type Rule struct { } type Voucher struct { - Code string `json:"code"` - Value int64 `json:"value"` - Rules string `json:"rules"` + Code string `json:"code"` + Value int64 `json:"value"` + Rules string `json:"rules"` + Description string `json:"description,omitempty"` } type Service struct { @@ -40,8 +41,9 @@ func (s *Service) GetVoucher(code string) (*messages.AddVoucher, error) { } return &messages.AddVoucher{ - Code: code, - Value: v.Value, + Code: code, + Value: v.Value, + Description: v.Description, VoucherRules: []string{ v.Rules, }, diff --git a/proto/messages.proto b/proto/messages.proto index 4ec43f5..772bfdd 100644 --- a/proto/messages.proto +++ b/proto/messages.proto @@ -99,6 +99,7 @@ message AddVoucher { string code = 1; int64 value = 2; repeated string voucherRules = 3; + string description = 4; } message RemoveVoucher { uint32 id = 1; }