Skip to content

Commit

Permalink
feat: Support future ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
noahpistilli committed Dec 17, 2024
1 parent 0d38462 commit edb774f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 50 deletions.
6 changes: 6 additions & 0 deletions basket.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ func orderDone(r *Response) {
user.OrderId = orderId
user.Price = price

if r.request.PostForm.Get("order[TimeSelect]") != "NORMAL" {
// Split into what Dominos wants
orderTime := r.request.PostForm.Get("order[DeliveryDate]")
user.OrderTime = orderTime[:4] + "-" + orderTime[4:6] + "-" + orderTime[6:8] + " " + orderTime[8:10] + ":" + orderTime[10:12] + ":" + orderTime[12:14]
}

// If the error does fail we should alert the user and allow for the basket to be cleared.
didError := false
err = r.dominos.PlaceOrder(user)
Expand Down
26 changes: 25 additions & 1 deletion dominos/dominos.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/url"
"strings"
"time"
)

func (d *Dominos) StoreLookup(zipCode, address string) ([]Store, error) {
Expand Down Expand Up @@ -147,6 +148,25 @@ func (d *Dominos) GetStoreInfo(storeId string) (*Store, error) {
}
}

var hours []ServiceHours
if jsonData["ServiceHours"].(map[string]any)["Delivery"] == nil {
return nil, NoDeliveryHours
}

// Get current day based on local time
t, err := time.Parse(time.DateTime, jsonData["StoreAsOfTime"].(string))
if err != nil {
return nil, err
}

currDay := t.Weekday().String()[:3]
for _, times := range jsonData["ServiceHours"].(map[string]any)["Delivery"].(map[string]any)[currDay].([]any) {
hours = append(hours, ServiceHours{
OpenTime: times.(map[string]any)["OpenTime"].(string) + ":00",
CloseTime: times.(map[string]any)["CloseTime"].(string) + ":00",
})
}

return &Store{
StoreID: jsonData["StoreID"].(string),
Address: address,
Expand All @@ -155,7 +175,7 @@ func (d *Dominos) GetStoreInfo(storeId string) (*Store, error) {
IsOpen: jsonData["IsOpen"].(bool),
DetailedWait: jsonData["EstimatedWaitMinutes"].(string),
Phone: jsonData["Phone"].(string),
ServiceHours: ServiceHours{},
ServiceHours: hours,
Information: information,
}, nil
}
Expand Down Expand Up @@ -743,6 +763,10 @@ func (d *Dominos) PlaceOrder(info *User) error {
payload["Order"].(map[string]any)["Address"].(map[string]any)["AddressLine2"] = info.ApartmentNumber
}

if info.OrderTime != "" {
payload["Order"].(map[string]any)["FutureOrderTime"] = info.OrderTime
}

data, err := json.Marshal(payload)
if err != nil {
panic(err)
Expand Down
5 changes: 3 additions & 2 deletions dominos/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

var (
InvalidCountry = errors.New("invalid country code")
GenericError = errors.New("An unknown error has occurred. Please contact WiiLink support\nError Code: ")
InvalidCountry = errors.New("invalid country code")
GenericError = errors.New("An unknown error has occurred. Please contact WiiLink support\nError Code: ")
NoDeliveryHours = errors.New("no delivery hours are available")
)

func MakeError(err map[string]any) error {
Expand Down
3 changes: 2 additions & 1 deletion dominos/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Store struct {
IsOpen bool
DetailedWait string
Phone string
ServiceHours ServiceHours
ServiceHours []ServiceHours
Information string
}

Expand Down Expand Up @@ -102,4 +102,5 @@ type User struct {
PhoneNumber string
OrderId string
Price string
OrderTime string
}
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"time"
)

var pool *pgxpool.Pool
var geonameCities map[int]*models.Feature
var config *Config
var (
pool *pgxpool.Pool
geonameCities map[int]*models.Feature
config *Config
)

func checkError(err error) {
if err != nil {
Expand Down
73 changes: 30 additions & 43 deletions shop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"DemaeDominos/dominos"
"encoding/xml"
"fmt"
)

func shopOne(r *Response) {
Expand All @@ -19,9 +20,33 @@ func shopOne(r *Response) {
return
}

// Form the open times
var times []KVFieldWChildren
for i, hour := range shopData.ServiceHours {
kv := KVFieldWChildren{
XMLName: xml.Name{Local: fmt.Sprintf("values%d", i)},
Value: []any{
KVField{
XMLName: xml.Name{Local: "start"},
Value: hour.OpenTime,
},
KVField{
XMLName: xml.Name{Local: "end"},
Value: hour.CloseTime,
},
KVField{
XMLName: xml.Name{Local: "holiday"},
Value: "n",
},
},
}

times = append(times, kv)
}

shop := ShopOne{
CategoryCode: CDATA{"01"},
Address: CDATA{"Nope"},
Address: CDATA{shopData.Address},
Information: CDATA{shopData.Information},
Attention: CDATA{"why"},
Amenity: CDATA{"Domino's Pizza"},
Expand Down Expand Up @@ -60,35 +85,7 @@ func shopOne(r *Response) {
KVFieldWChildren{
XMLName: xml.Name{Local: "values"},
Value: []any{
KVField{
XMLName: xml.Name{Local: "start"},
Value: "01:00:00",
},
KVField{
XMLName: xml.Name{Local: "end"},
Value: "23:45:00",
},
KVField{
XMLName: xml.Name{Local: "holiday"},
Value: "n",
},
},
},
KVFieldWChildren{
XMLName: xml.Name{Local: "values1"},
Value: []any{
KVField{
XMLName: xml.Name{Local: "start"},
Value: "01:00:00",
},
KVField{
XMLName: xml.Name{Local: "end"},
Value: "23:45:00",
},
KVField{
XMLName: xml.Name{Local: "holiday"},
Value: "n",
},
times[:],
},
},
},
Expand All @@ -99,18 +96,7 @@ func shopOne(r *Response) {
KVFieldWChildren{
XMLName: xml.Name{Local: "values"},
Value: []any{
KVField{
XMLName: xml.Name{Local: "start"},
Value: "01:00:00",
},
KVField{
XMLName: xml.Name{Local: "end"},
Value: "23:45:00",
},
KVField{
XMLName: xml.Name{Local: "holiday"},
Value: "n",
},
times[:],
},
},
},
Expand All @@ -130,7 +116,8 @@ func shopOne(r *Response) {
},
},
},
Interval: CDATA{5},
// Dominos does 15 minute intervals
Interval: CDATA{15},
Holiday: CDATA{"No ordering on Canada Day"},
},
RecommendedItemList: KVFieldWChildren{
Expand Down

0 comments on commit edb774f

Please sign in to comment.