Exemplo n.º 1
0
func (oh *orderHandler) SetState(order_id int64, source string, new_state int, order_data *OrderData) error {
	if !oh.parent.Check() {
		log.Printf("DB: can not set state for [%v] now... Will do it after.", order_id)
		u.After(oh.parent.Check, func() {
			oh.SetState(order_id, source, new_state, order_data)
		})
		return nil
	}
	to_set := bson.M{"order_state": new_state, "when": time.Now()}
	if order_data != nil {
		to_set["data"] = order_data
	}

	change := bson.M{"$set": to_set}
	log.Println("DB: change:", change["$set"])
	err := oh.Collection.Update(bson.M{"order_id": order_id, "source": source}, change)
	if err != nil && err != mgo.ErrNotFound {
		log.Printf("DB: state [%v] for order [%v] %v is not stated because %v", new_state, order_id, source, err)
		return err
	}
	if err == mgo.ErrNotFound {
		log.Printf("DB: for order %v at %v not found :(( ", order_id, source)
	}
	return err
}
Exemplo n.º 2
0
func (oh *orderHandler) SetActive(order_id int64, source string, state bool) error {
	if !oh.parent.Check() {
		u.After(oh.parent.Check, func() {
			oh.SetActive(order_id, source, state)
		})
		return nil
	}
	err := oh.Collection.Update(bson.M{"order_id": order_id, "source": source}, bson.M{"$set": bson.M{"is_active": state}})
	if err == mgo.ErrNotFound {
		log.Printf("DB: update not existed %v %v to active %v", order_id, source, state)
	}
	return err
}
Exemplo n.º 3
0
func (oh *orderHandler) SetFeedback(for_whom string, for_state int, feedback string, source string) (*int64, error) {
	if !oh.parent.Check() {
		u.After(oh.parent.Check, func() {
			oh.SetFeedback(for_whom, for_state, feedback, source)
		})
		return nil, nil
	}
	order := OrderWrapper{}
	err := oh.Collection.Find(bson.M{"whom": for_whom, "order_state": for_state, "source": source}).Sort("-when").One(&order)
	if err != nil && err != mgo.ErrNotFound {
		return nil, err
	}
	if err == mgo.ErrNotFound {
		return nil, errors.New("Заказ не найден!")
	}
	err = oh.Collection.Update(bson.M{"order_id": order.OrderId, "source": source}, bson.M{"$set": bson.M{"feedback": feedback}})
	order_id := order.OrderId
	return &order_id, err
}
func TestTaxiInfinityFail(t *testing.T) {
	conf := c.ReadTestConfigInRecursive()
	db := d.NewMainDb(conf.Main.Database.ConnString, conf.Main.Database.Name)
	taxi_conf := conf.Taxis["fake"]

	t.Logf("taxi api configuration for %+v:\n%v", taxi_conf.Name, taxi_conf.Api)
	external := i.GetTestInfAPI(taxi_conf.Api)

	external_api := external.(taxi.TaxiInterface)
	external_address_supplier := external.(taxi.AddressSupplier)

	apiMixin := taxi.ExternalApiMixin{API: external_api}

	carsCache := taxi.NewCarsCache(external_api)
	notifier := n.NewNotifier(conf.Main.CallbackAddr, taxi_conf.Chat.Key, db)

	address_handler, address_supplier := GetAddressInstruments(conf, taxi_conf.Name, external_address_supplier)
	configStore := d.NewConfigurationStorage(conf.Main.ConfigDatabase)
	botContext := taxi.FormTaxiBotContext(&apiMixin, db, configStore, taxi_conf, address_handler, carsCache)
	controller := m.FormBotController(botContext, db)

	t.Logf("Was create bot context: %+v\n", botContext)
	http.HandleFunc(fmt.Sprintf("/taxi/%v", taxi_conf.Name), controller)

	go func() {
		taxiContext := taxi.TaxiContext{API: external_api, DataBase: db, Cars: carsCache, Notifier: notifier}
		t.Logf("Will start order watcher for [%v]", botContext.Name)
		taxi.TaxiOrderWatch(&taxiContext, botContext)
	}()

	http.HandleFunc(fmt.Sprintf("/taxi/%v/streets", taxi_conf.Name), func(w http.ResponseWriter, r *http.Request) {
		geo.StreetsSearchController(w, r, address_supplier)
	})

	go func() {
		server_address := fmt.Sprintf(":%v", conf.Main.Port)
		t.Logf("\nStart listen and serving at: %v\n", server_address)
		server := &http.Server{
			Addr: server_address,
		}
		server.ListenAndServe()
	}()

	u.After(external_api.IsConnected, func() {
		inf_api := external.(*i.InfinityAPI)
		for i, _ := range inf_api.ConnStrings {
			inf_api.ConnStrings[i] += "w"
		}
	})

	_, err := u.GET(taxi_conf.DictUrl, &map[string]string{"q": "лесос"})
	if err != nil {
		t.Error("Error at getting street %v", err)
	}

	out_res, err := tu.POST(fmt.Sprintf("http://localhost:%v/taxi/%v", conf.Main.Port, taxi_conf.Name), &structs.InPkg{
		Message: &structs.InMessage{
			ID: u.GenStringId(),
			Commands: &[]structs.InCommand{
				structs.InCommand{Action: "information", Title: "information"},
			},
		},
		From:     "test user",
		UserData: &structs.InUserData{Name: "TEST"},
	})

	if err != nil {
		t.Errorf("Error at unmarshal result info %v", err)
	}

	if out_res == nil {
		t.Error("Out info result is nil!")
	} else {
		if out_res.Message.Type != "chat" {
			t.Errorf("Out message type != chat, but == %v", out_res.Message.Type)
		}
		ch, _ := configStore.GetChatConfig(taxi_conf.Chat.CompanyId)

		if out_res.Message.Body != ch.Information {
			t.Errorf("Out message body != info in config, but == %v", out_res.Message.Body)
		}
	}

}