Exemplo n.º 1
0
func (engine *Engine) CreateOrder(order *data.Order) error {

	if order.ID != 0 {
		return ErrNonEmptyID
	}

	if order.State == "" {
		order.State = data.OrderStateDraft
	} else if order.State != data.OrderStateDraft {
		return ErrNotDraft
	}

	if order.Time == 0 {
		order.Time = uint64(time.Now().UTC().Unix())
	} else if order.Time < uint64(time.Now().UTC().Add(-PastTimeTreshold).Unix()) {
		return ErrIsPast
	}

	tx, err := engine.storage.Begin()
	if err != nil {
		return err
	}

	if err := tx.CreateOrder(order); err != nil {
		return tx.Rollback(err)
	}

	populate.Order(order)

	if err := tx.Commit(); err != nil {
		return err
	}

	return nil
}
Exemplo n.º 2
0
func TestForestOrderItemCreateOrderPlaced(t *testing.T) {

	productInOrder := data.Product{
		Name: generateName("Product"),
		Price: data.Price{
			Vat: 20,
			Net: 100,
		},
	}

	createProduct(&productInOrder)

	product := data.Product{
		Name: generateName("Product"),
		Price: data.Price{
			Vat: 20,
			Net: 100,
		},
	}

	createProduct(&product)

	order := data.Order{
		Items: []data.OrderItem{
			data.OrderItem{
				Product:  productInOrder,
				Quantity: 1,
			},
		},
	}

	createOrder(&order)

	order.State = data.OrderStatePlaced

	updateOrder(&order)

	payload := data.OrderItem{
		Product: data.Product{
			ID: product.ID,
		},
		Quantity: 0,
	}

	got := Error{}

	r := getForestClient().POST(t, forest.Path("/orders/{orderID}/items/{}", order.ID, "").Content(payload, MimeJSON))

	// TODO:
	// forest.ExpectStatus(t, r, http.StatusForbidden)
	forest.ExpectStatus(t, r, http.StatusInternalServerError)

	forest.ExpectJSONDocument(t, r, &got)

	if got.Message == "" {
		t.Error("Got empty error message")
	}

}
Exemplo n.º 3
0
func (engine *Engine) UpdateOrder(order *data.Order) error {

	if order.ID == 0 {
		return ErrEmptyID
	}

	tx, err := engine.storage.Begin()
	if err != nil {
		return err
	}

	existingOrder := &data.Order{
		ID: order.ID,
	}

	if err := tx.LoadOrder(existingOrder); err != nil {
		return tx.Rollback(err)
	}

	if order.Time == 0 {
		order.Time = existingOrder.Time
	} else if order.Time < uint64(time.Now().UTC().Add(-PastTimeTreshold).Unix()) {
		return tx.Rollback(ErrIsPast)
	}

	if order.State == "" {
		order.State = existingOrder.State
	} else if order.State != existingOrder.State {
		if existingOrder.State != data.OrderStateDraft {
			return tx.Rollback(ErrNotDraft)
		}
		switch order.State {
		case data.OrderStatePlaced:
			if len(existingOrder.Items) < 1 {
				return tx.Rollback(ErrOrderEmpty)
			}
		case data.OrderStateCancelled:
		default:
			return tx.Rollback(ErrUnknownState)
		}
	}

	if err := tx.UpdateOrder(order); err != nil {
		return tx.Rollback(err)
	}

	populate.Order(order)

	if err := tx.Commit(); err != nil {
		return err
	}

	return nil
}
Exemplo n.º 4
0
func TestForestRemoveOrderItemOrderPlaced(t *testing.T) {

	productInOrder := data.Product{
		Name: generateName("Product"),
		Price: data.Price{
			Vat: 20,
			Net: 100,
		},
	}

	createProduct(&productInOrder)

	product := data.Product{
		Name: generateName("Product"),
		Price: data.Price{
			Vat: 20,
			Net: 100,
		},
	}

	createProduct(&product)

	order := data.Order{
		Items: []data.OrderItem{
			data.OrderItem{
				Product:  productInOrder,
				Quantity: 1,
			},
		},
	}

	createOrder(&order)

	order.State = data.OrderStatePlaced

	updateOrder(&order)

	got := Error{}

	r := getForestClient().DELETE(t, forest.Path("/orders/{orderID}/items/{productID}", order.ID, product.ID))

	// TODO:
	// forest.ExpectStatus(t, r, http.StatusNotFound)
	forest.ExpectStatus(t, r, http.StatusInternalServerError)

	forest.ExpectJSONDocument(t, r, &got)

	if got.Message == "" {
		t.Error("Got empty error message")
	}

}
Exemplo n.º 5
0
func TestForestUpdateOrderInvalidStateTransition(t *testing.T) {

	invalidTransitions := [][2]string{
		// [2]string{data.OrderStateDraft, data.OrderStatePlaced},    // valid
		// [2]string{data.OrderStateDraft, data.OrderStateCancelled}, // valid
		[2]string{data.OrderStatePlaced, data.OrderStateDraft},
		[2]string{data.OrderStatePlaced, data.OrderStateCancelled},
		[2]string{data.OrderStateCancelled, data.OrderStateDraft},
		[2]string{data.OrderStateCancelled, data.OrderStatePlaced},
		[2]string{data.OrderStateDraft, "UNKNOWN"},
		[2]string{data.OrderStatePlaced, "UNKNOWN"},
		[2]string{data.OrderStateCancelled, "UNKNOWN"},
	}

	product := data.Product{
		Name: generateName("Product"),
		Price: data.Price{
			Vat: 20,
			Net: 100,
		},
	}

	createProduct(&product)

	for _, invalidTransition := range invalidTransitions {

		fromState, toState := invalidTransition[0], invalidTransition[1]

		order := data.Order{
			Items: []data.OrderItem{
				data.OrderItem{
					Product:  product,
					Quantity: 1,
				},
			},
		}

		createOrder(&order)

		if fromState != order.State {
			order.State = fromState
			updateOrder(&order)
		}

		payload := data.Order{
			State: toState,
		}

		got := Error{}

		r := getForestClient().PUT(t, forest.Path("/orders/{id}", order.ID).Content(payload, MimeJSON))

		// TODO:
		// forest.ExpectStatus(t, r, http.StatusBadRequest)
		forest.ExpectStatus(t, r, http.StatusInternalServerError)

		forest.ExpectJSONDocument(t, r, &got)

		if got.Message == "" {
			t.Error("Got empty error message")
		}

	}

}