From edb774fcce6301f1f84a211d48bc1428e9c7cf0c Mon Sep 17 00:00:00 2001 From: Sketch <75850871+SketchMaster2001@users.noreply.github.com> Date: Tue, 17 Dec 2024 14:32:08 -0500 Subject: [PATCH] feat: Support future ordering --- basket.go | 6 ++++ dominos/dominos.go | 26 +++++++++++++++- dominos/errors.go | 5 +-- dominos/structure.go | 3 +- main.go | 8 +++-- shop.go | 73 ++++++++++++++++++-------------------------- 6 files changed, 71 insertions(+), 50 deletions(-) diff --git a/basket.go b/basket.go index 3cbc962..24c823c 100644 --- a/basket.go +++ b/basket.go @@ -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) diff --git a/dominos/dominos.go b/dominos/dominos.go index 1c908e5..cedfb63 100644 --- a/dominos/dominos.go +++ b/dominos/dominos.go @@ -10,6 +10,7 @@ import ( "net/http" "net/url" "strings" + "time" ) func (d *Dominos) StoreLookup(zipCode, address string) ([]Store, error) { @@ -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, @@ -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 } @@ -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) diff --git a/dominos/errors.go b/dominos/errors.go index 57c6112..edf69fb 100644 --- a/dominos/errors.go +++ b/dominos/errors.go @@ -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 { diff --git a/dominos/structure.go b/dominos/structure.go index 963b71b..452819a 100644 --- a/dominos/structure.go +++ b/dominos/structure.go @@ -33,7 +33,7 @@ type Store struct { IsOpen bool DetailedWait string Phone string - ServiceHours ServiceHours + ServiceHours []ServiceHours Information string } @@ -102,4 +102,5 @@ type User struct { PhoneNumber string OrderId string Price string + OrderTime string } diff --git a/main.go b/main.go index 3fb6e6e..7800277 100644 --- a/main.go +++ b/main.go @@ -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 { diff --git a/shop.go b/shop.go index bed9211..b25a166 100644 --- a/shop.go +++ b/shop.go @@ -3,6 +3,7 @@ package main import ( "DemaeDominos/dominos" "encoding/xml" + "fmt" ) func shopOne(r *Response) { @@ -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"}, @@ -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[:], }, }, }, @@ -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[:], }, }, }, @@ -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{