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

Fix get order

parent 406404a8
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment