Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplestore: adding shipping address type #250

Merged
merged 4 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions client/resources/simplestore/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,40 @@ func (s *Store) handlePlaceOrder(ctx context.Context, uid clientintf.UserID,
}, nil
}

// Process form data.
formData := struct {
Addr string `json:"addr"`
}{}
if err := json.Unmarshal(request.Data, &formData); err != nil {
return &rpc.RMFetchResourceReply{
Status: rpc.ResourceStatusBadRequest,
Data: []byte("request data not valid json"),
}, nil
var shipping *ShippingAddress
// Verify the items
for _, item := range cart.Items {
prod, ok := s.products[item.Product.SKU]
if !ok {
return &rpc.RMFetchResourceReply{
Status: rpc.ResourceStatusBadRequest,
Data: []byte(fmt.Sprintf("SKU %q does not exist", item.Product.SKU)),
}, nil
}
// If a product requires shipping, ensure a shipping address
// was sent.
if shipping == nil && prod.Shipping {
// Process form data.
var formData ShippingAddress
if err := json.Unmarshal(request.Data, &formData); err != nil {
return &rpc.RMFetchResourceReply{
Status: rpc.ResourceStatusBadRequest,
Data: []byte("request data not valid json"),
}, nil
}
shipping = &formData

if shipping.Name == "" || shipping.Address1 == "" ||
shipping.City == "" || shipping.State == "" ||
shipping.PostalCode == "" {
return &rpc.RMFetchResourceReply{
Status: rpc.ResourceStatusBadRequest,
Data: []byte("incomplete shipping address"),
}, nil
}
// TODO: proper address validation, optional phone
// number validation.
}
}

// Create the order.
Expand All @@ -232,6 +257,7 @@ func (s *Store) handlePlaceOrder(ctx context.Context, uid clientintf.UserID,
if err != nil {
return nil, err
}

id := lastID.ID + 1
order := &Order{
User: uid,
Expand All @@ -240,7 +266,7 @@ func (s *Store) handlePlaceOrder(ctx context.Context, uid clientintf.UserID,
Status: StatusPlaced,
PlacedTS: time.Now(),
ShipCharge: s.cfg.ShipCharge,
ShipAddr: formData.Addr,
ShipAddr: shipping,
}

// Build the message to send to the remote user, and present it to the
Expand All @@ -256,8 +282,17 @@ func (s *Store) handlePlaceOrder(ctx context.Context, uid clientintf.UserID,
order.ID, order.User)
} else {
wpm("Thank you for placing your order #%d\n", order.ID)
if order.ShipAddr != "" {
wpm("Shipping address: %q\n", order.ShipAddr)
if order.ShipAddr != nil {
wpm("Shipping address:\n")
wpm(" Name: %s\n", order.ShipAddr.Name)
wpm(" Addr: %s\n", order.ShipAddr.Address1)
if order.ShipAddr.Address2 != "" {
wpm(" Addr: %s\n", order.ShipAddr.Address2)
}
wpm(" City: %s\n", order.ShipAddr.City)
wpm(" State: %s\n", order.ShipAddr.State)
wpm(" Zip: %s\n", order.ShipAddr.PostalCode)
wpm(" Phone: %s\n", order.ShipAddr.Phone)
}
wpm("The following were the items in your order:\n")
for _, item := range order.Cart.Items {
Expand Down
14 changes: 13 additions & 1 deletion client/resources/simplestore/products.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Product struct {
Tags []string `json:"tags"`
Price float64 `json:"price"`
Disabled bool `json:"disabled,omitempty"`
Shipping bool `json:"shipping"`
}

type productsFile struct {
Expand Down Expand Up @@ -71,6 +72,17 @@ const (
StatusCanceled OrderStatus = "canceled"
)

type ShippingAddress struct {
Name string `json:"name"`
Address1 string `json:"address1"`
Address2 string `json:"address2"`
City string `json:"city"`
State string `json:"state"`
PostalCode string `json:"postalCode"`
Phone string `json:"phone"`
CountryCode string `json:"countrycode"`
}

type Order struct {
ID OrderID `json:"id"`
User clientintf.UserID `json:"user"`
Expand All @@ -82,7 +94,7 @@ type Order struct {
ExchangeRate float64 `json:"exchange_rate"`
PayType PayType `json:"pay_type"`
Invoice string `json:"invoice"`
ShipAddr string `json:"ship_addr"`
ShipAddr *ShippingAddress `json:"ship_addr"`
}

// Total returns the total amount, with 2 decimal places accuracy.
Expand Down
10 changes: 9 additions & 1 deletion client/resources/simplestore/template/admin_order.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ Shipping : ${{ .Order.ShipCharge }}
Exchange Rate: {{ .Order.ExchangeRate }} DCR/USD
DCR Amount : {{ .Order.TotalDCR.String }}
Invoice : {{ .Order.Invoice }}
Shipping Addr: {{ .Order.ShipAddr }}
{{if .Order.ShipAddr ne nil }}
Shipping Addr:
{{ .Order.ShipAddr.Name }}
{{ .Order.ShipAddr.Address1 }}
{{ .Order.ShipAddr.Address2 }}
{{ .Order.ShipAddr.City }} {{ .Order.ShipAddr.State }} {{ .Order.ShipAddr.PostalCode }}
{{ .Order.ShipAddr.CountryCode }}
{{ .Order.ShipAddr.Phone }}
{{end}}

{{ if eq .Order.Status "placed" }}
Switch status to [shipped](/admin/orderstatusto/{{.Order.User}}/{{.Order.ID}}/shipped) [completed](/admin/orderstatusto/{{.Order.User}}/{{.Order.ID}}/completed) [canceled](/admin/orderstatusto/{{.Order.User}}/{{.Order.ID}}/canceled)
Expand Down
21 changes: 19 additions & 2 deletions client/resources/simplestore/template/cart.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,29 @@ Last Updated: {{.Updated.Format "2006-01-02 15:04:05" }}

---
## Place Order
{{$shipping := false}}
{{range .Items}}
{{if .Shipping}}
{{$shipping = true}}
{{break}}
{{end}}
{{- end}}

{{if $shipping}}
--form--
type="action" value="/placeOrder"
type="txtinput" label="Shipping Address" name="addr"
type="txtinput" label="Name" name="name"
type="txtinput" label="Address" name="address1"
type="txtinput" label="Address (optional)" name="address2"
type="txtinput" label="City" name="city"
type="txtinput" label="State" name="state"
type="txtinput" label="PostalCode" name="postalCode"
type="txtinput" label="Phone" name="phone"
type="submit" label="Place Order"
--/form--

{{else}}
[Place order](/placeOrder)
{{end}}

[Clear cart](/clearCart)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ A longish product description. Features:
"""
tags = ["first-type", "secondtag"]
price = 399.00
shipping = true