Skip to content
Snippets Groups Projects
Commit 3e8f0d81 authored by meskio's avatar meskio :tent:
Browse files

Fix get order

parent 406404a8
Branches
No related tags found
No related merge requests found
...@@ -36,6 +36,11 @@ type OrderPurchase struct { ...@@ -36,6 +36,11 @@ type OrderPurchase struct {
Ammount int `json:"ammount"` Ammount int `json:"ammount"`
} }
type OrderGetResponse struct {
Order Order `json:"order"`
Transaction *Transaction `json:"transaction"`
}
func (a *api) refundOrders() { func (a *api) refundOrders() {
const refundSleeptime = 10 * time.Minute const refundSleeptime = 10 * time.Minute
for { for {
...@@ -133,23 +138,21 @@ func (a *api) GetOrder(num int, w http.ResponseWriter, req *http.Request) { ...@@ -133,23 +138,21 @@ func (a *api) GetOrder(num int, w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
var body struct { var body OrderGetResponse
Order Order `json:"order"`
Transaction *Transaction `json:"transaction"`
}
body.Order = order body.Order = order
var transaction Transaction var transaction Transaction
err = a.db.Joins("OrderPurchase"). err = a.db.Where("member = ? AND type = 'order' AND id IN (?)", num,
Where("member = ? AND type = 'order' AND order_purchases.order_id = ?", num, order.ID). a.db.Table("order_purchases").
Where("order_id = ?", order.ID).
Select("transaction_id")).
Find(&transaction).Error Find(&transaction).Error
if err != nil { if err != nil {
if err.Error() != "record not found" {
log.Printf("Can't get order transaction %s: %v", vars["id"], err) log.Printf("Can't get order transaction %s: %v", vars["id"], err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
} else { if transaction.ID != 0 {
body.Transaction = &transaction body.Transaction = &transaction
} }
......
...@@ -3,6 +3,7 @@ package api ...@@ -3,6 +3,7 @@ package api
import ( import (
"net/http" "net/http"
"path" "path"
"strconv"
"testing" "testing"
"time" "time"
) )
...@@ -224,6 +225,58 @@ func TestOrderDeactivation(t *testing.T) { ...@@ -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() { func (tapi *testAPI) addTestOrder() {
resp := tapi.do("POST", "/order", testOrder, nil) resp := tapi.do("POST", "/order", testOrder, nil)
if resp.StatusCode != http.StatusCreated { if resp.StatusCode != http.StatusCreated {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment