Esempio n. 1
0
func (s *SediAPI) toInternalOrders(sor *SediOrdersResponse) []t.Order {
	result := []t.Order{}
	for _, order := range sor.Orders {
		order_state, ok := STATES_MAPPING[order.Status.Id]
		if !ok {
			continue
		}
		time_delivery := time.Unix(int64(order.Date), 0)
		int_order := t.Order{
			ID:           order.OrderId,
			State:        order_state,
			Cost:         int(order.Cost),
			TimeDelivery: &time_delivery,
		}

		if order.Driver != nil && order.Driver.Car != nil {
			car := order.Driver.Car
			id_car := car.Id
			int_order.IDCar = id_car

			var car_color string
			if car.Props != nil {
				for _, prop := range car.Props.Properties {
					if prop.Key == "color" {
						car_color = prop.Value.Name
						break
					}
				}
			}
			s.setCar(id_car, car.Name, car.Number, car_color)
		}
		result = append(result, int_order)
	}
	return result
}
func (m *TaxiMasterAPI) Orders() []t.Order {
	result := []t.Order{}
	//here we get persisted orders with our source and not ended state
	orders, err := m.Storage.Orders.GetOrders(bson.M{"source": m.Source, "order_state": bson.M{"$nin": []int64{t.ORDER_PAYED, t.ORDER_CANCELED}}})
	if err != nil {
		log.Printf("Error at retrieving current order ids: %v", err)
		return result
	}
	//and for each persisted order retrieve his info from external source
	for _, order := range orders {
		res, err := m._get_request("get_order_state", map[string]string{"order_id": order.OrderId}, true)
		if err != nil {
			log.Printf("Error at getting info from API by order id [%v]: %v", order.OrderId, err)
			continue
		}
		res_container := TMAPIOrderResponseWrapper{}
		err = json.Unmarshal(res, &res_container)
		if err != nil {
			log.Printf("Error at unmarshalling order response for order id [%v]: %v", order.OrderId, err)
			continue
		}
		if ok, message := res_container.Check(); !ok {
			log.Printf("Error at response result: %s", message)
			continue
		}
		response_order := res_container.Data
		result_order := t.Order{ID: response_order.OrderId}
		if state, ok := OrderStateMap[response_order.StateKind]; ok {
			result_order.State = state
		} else {
			log.Printf("Not imply state of response order [%v] state = %v", order.OrderId, res_container.Data.StateKind)
			continue
		}
		result_order.IDCar = response_order.CarId
		arrival_time, err := time.Parse("20060102150405", response_order.SourceTime)
		if err != nil {
			result_order.TimeArrival = &arrival_time
		}
		result = append(result, result_order)
	}
	return result
}