예제 #1
0
func GetMessageAdditionalFunctionsHandler(start_addr string, notifier *ntf.Notifier, db *d.MainDb, config c.ChatConfig, chc *CoffeeHouseConfiguration) http.Handler {
	m := GetMartini(config.Name, config.CompanyId, start_addr, db)
	m.Post(start_addr,
		web.LoginRequired,
		web.AutHandler.CheckReadRights(config.CompanyId),
		web.AutHandler.CheckWriteRights(config.CompanyId),
		func(render render.Render, req *http.Request) {
			cfd := CoffeeFunctionData{}
			request_body, err := ioutil.ReadAll(req.Body)
			if err != nil {
				log.Printf("Coffee serv: error at read data: %v", err)
				render.JSON(500, map[string]interface{}{"ok": false, "detail": "can not read request body"})
				return
			}
			err = json.Unmarshal(request_body, &cfd)
			if err != nil {
				log.Printf("Coffee serv: error at unmarshal json: %v", err)
				render.JSON(500, map[string]interface{}{"ok": false, "detail": fmt.Sprintf("can not unmarshal request body %v \n %s", err, request_body)})
				return
			}
			log.Printf("Coffee serv: message function: %+v", cfd)
			orderId, err := strconv.ParseInt(cfd.Context.OrderId, 10, 64)
			if err != nil {
				log.Printf("Coffee serv: error at parse int ")
			}
			var result string
			switch cfd.Action {
			case "cancel":
				commands := getCommands(chc, false, false)
				notifier.NotifyTextWithCommands(cfd.Context.UserName, "Ваш заказ отменили!", commands)
				db.Orders.SetActive(orderId, cfd.Context.CompanyName, false)
				result = "Отменено"

			case "start":
				notifier.NotifyText(cfd.Context.UserName, "Ваш заказ в процессе приготовления!")
				result = "В процессе"

			case "end":
				notifier.NotifyText(cfd.Context.UserName, "Ваш заказ готов!")
				db.Orders.SetActive(orderId, cfd.Context.CompanyName, false)
				result = "Окончено"

			case "confirm":
				notifier.NotifyText(cfd.Context.UserName, "Вы уверены что хотите сделать заказ?")
				result = "Подтверждение отправлено"
			}

			db.Messages.SetMessageFunctionUsed(cfd.MessageId, cfd.Action)
			db.Messages.UpdateMessageRelatedOrderState(cfd.MessageId, result)

			found, err := db.Orders.GetByOwner(cfd.Context.UserName, cfd.Context.CompanyName, true)
			render.JSON(200, map[string]interface{}{"ok": true, "result": result, "user_have_active_orders": found != nil})
		})
	return m
}