From 3e8f0d81d480f7c4610654931cca9010931c735b Mon Sep 17 00:00:00 2001 From: meskio <meskio@sindominio.net> Date: Tue, 6 Oct 2020 14:15:15 +0200 Subject: [PATCH] Fix get order --- api/order.go | 27 +++++++++++++----------- api/order_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/api/order.go b/api/order.go index 59b82e6..4a65572 100644 --- a/api/order.go +++ b/api/order.go @@ -36,6 +36,11 @@ type OrderPurchase struct { Ammount int `json:"ammount"` } +type OrderGetResponse struct { + Order Order `json:"order"` + Transaction *Transaction `json:"transaction"` +} + func (a *api) refundOrders() { const refundSleeptime = 10 * time.Minute for { @@ -133,23 +138,21 @@ func (a *api) GetOrder(num int, w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusInternalServerError) return } - var body struct { - Order Order `json:"order"` - Transaction *Transaction `json:"transaction"` - } + var body OrderGetResponse body.Order = order var transaction Transaction - err = a.db.Joins("OrderPurchase"). - Where("member = ? AND type = 'order' AND order_purchases.order_id = ?", num, order.ID). + err = a.db.Where("member = ? AND type = 'order' AND id IN (?)", num, + a.db.Table("order_purchases"). + Where("order_id = ?", order.ID). + Select("transaction_id")). Find(&transaction).Error if err != nil { - if err.Error() != "record not found" { - log.Printf("Can't get order transaction %s: %v", vars["id"], err) - w.WriteHeader(http.StatusInternalServerError) - return - } - } else { + log.Printf("Can't get order transaction %s: %v", vars["id"], err) + w.WriteHeader(http.StatusInternalServerError) + return + } + if transaction.ID != 0 { body.Transaction = &transaction } diff --git a/api/order_test.go b/api/order_test.go index da66d14..b11144a 100644 --- a/api/order_test.go +++ b/api/order_test.go @@ -3,6 +3,7 @@ package api import ( "net/http" "path" + "strconv" "testing" "time" ) @@ -224,6 +225,58 @@ func TestOrderDeactivation(t *testing.T) { } } +func TestGetOrder(t *testing.T) { + tapi := newTestAPI(t) + defer tapi.close() + tapi.addTestMember() + tapi.addTestOrder() + + var orders []Order + resp := tapi.do("GET", "/order/active", nil, &orders) + if resp.StatusCode != http.StatusOK { + t.Fatal("Can't get orders:", resp.Status) + } + if len(orders) != 1 { + t.Fatal("Didn't find my new order") + } + id := strconv.Itoa(int(orders[0].ID)) + + var body OrderGetResponse + resp = tapi.do("GET", "/order/"+id, nil, &body) + if resp.StatusCode != http.StatusOK { + t.Fatal("Can't get order:", resp.Status) + } + if body.Transaction != nil { + t.Error("Unexpected transaction", body.Transaction) + } + if body.Order.Name != testOrder.Name { + t.Error("Wrong name:", body.Order.Name) + } + + purchase := []OrderPurchase{ + { + ProductCode: testProduct.Code, + Ammount: 3, + OrderID: orders[0].ID, + }, + } + resp = tapi.do("POST", "/order/purchase", purchase, nil) + if resp.StatusCode != http.StatusCreated { + t.Fatal("Can't create order:", resp.Status) + } + + resp = tapi.do("GET", "/order/"+id, nil, &body) + if resp.StatusCode != http.StatusOK { + t.Fatal("Can't get order:", resp.Status) + } + if body.Transaction == nil { + t.Fatal("There is no transaction") + } + if body.Transaction.Type != "order" { + t.Error("Wrong transaction type", body.Transaction.Type) + } +} + func (tapi *testAPI) addTestOrder() { resp := tapi.do("POST", "/order", testOrder, nil) if resp.StatusCode != http.StatusCreated { -- GitLab