From f60e4795b366b1ed7550625b71f02471d22a24b7 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Thu, 28 Jun 2018 12:17:19 +0300 Subject: [PATCH] replace activation/deactivation to delete --- migrations/1530177124_app.down.sql | 1 + migrations/1530177124_app.up.sql | 1 + models.go | 1 - repository.go | 4 +-- routing.go | 55 +++++++++--------------------- routing_test.go | 6 ++-- static/script.js | 20 +++++------ static/style.css | 2 +- telegram.go | 4 +-- templates/form.html | 8 ++--- translate/translate.en.yml | 2 +- translate/translate.ru.yml | 2 +- 12 files changed, 41 insertions(+), 65 deletions(-) create mode 100644 migrations/1530177124_app.down.sql create mode 100644 migrations/1530177124_app.up.sql diff --git a/migrations/1530177124_app.down.sql b/migrations/1530177124_app.down.sql new file mode 100644 index 0000000..013d097 --- /dev/null +++ b/migrations/1530177124_app.down.sql @@ -0,0 +1 @@ +alter table bot add column active boolean; \ No newline at end of file diff --git a/migrations/1530177124_app.up.sql b/migrations/1530177124_app.up.sql new file mode 100644 index 0000000..9ba8c8c --- /dev/null +++ b/migrations/1530177124_app.up.sql @@ -0,0 +1 @@ +alter table bot drop column active; \ No newline at end of file diff --git a/models.go b/models.go index d0845a1..90497be 100644 --- a/models.go +++ b/models.go @@ -25,7 +25,6 @@ type Bot struct { Name string `gorm:"name type:varchar(40)" json:"name,omitempty"` CreatedAt time.Time UpdatedAt time.Time - Active bool `json:"active,omitempty"` } // User model diff --git a/repository.go b/repository.go index 8cb1dae..1c62eed 100644 --- a/repository.go +++ b/repository.go @@ -48,8 +48,8 @@ func getBotByToken(token string) (*Bot, error) { return &bot, nil } -func (b *Bot) setBotActivity() error { - return orm.DB.Model(b).Where("token = ?", b.Token).Update("Active", !b.Active).Error +func (b *Bot) deleteBot() error { + return orm.DB.Delete(b, "token = ?", b.Token).Error } func getBotChannelByToken(token string) uint64 { diff --git a/routing.go b/routing.go index 6bf01e1..6df7272 100644 --- a/routing.go +++ b/routing.go @@ -61,7 +61,7 @@ func setWrapperRoutes() { http.HandleFunc("/create/", createHandler) http.HandleFunc("/actions/activity", activityHandler) http.HandleFunc("/add-bot/", addBotHandler) - http.HandleFunc("/activity-bot/", activityBotHandler) + http.HandleFunc("/delete-bot/", deleteBotHandler) } func renderTemplate(w http.ResponseWriter, tmpl string, c interface{}) { @@ -200,7 +200,6 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) { } b.Channel = data.ChannelID - b.Active = true err = c.createBot(b) if err != nil { @@ -222,7 +221,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) { w.Write(jsonString) } -func activityBotHandler(w http.ResponseWriter, r *http.Request) { +func deleteBotHandler(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() setLocale(r.Header.Get("Accept-Language")) body, err := ioutil.ReadAll(r.Body) @@ -250,36 +249,16 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) { return } - ch := v1.Channel{ - ID: getBotChannelByToken(b.Token), - Type: "telegram", - Events: []string{ - "message_sent", - "message_updated", - "message_deleted", - "message_read", - }, - } - var client = v1.New(c.MGURL, c.MGToken) - if b.Active { - data, status, err := client.DeactivateTransportChannel(ch.ID) - if status > http.StatusOK { - http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_deactivating_channel"}), http.StatusBadRequest) - logger.Error(b.ID, status, err.Error(), data) - return - } - } else { - data, status, err := client.ActivateTransportChannel(ch) - if status > http.StatusCreated { - http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest) - logger.Error(b.ID, status, err.Error(), data) - return - } + data, status, err := client.DeactivateTransportChannel(getBotChannelByToken(b.Token)) + if status > http.StatusOK { + http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_deactivating_channel"}), http.StatusBadRequest) + logger.Error(b.ID, status, err.Error(), data) + return } - err = b.setBotActivity() + err = b.deleteBot() if err != nil { raven.CaptureErrorAndWait(err, nil) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError) @@ -309,15 +288,15 @@ func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) { p, bots, map[string]interface{}{ - "ButtonSave": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "button_save"}), - "ApiKey": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "api_key"}), - "TabSettings": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_settings"}), - "TabBots": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_bots"}), - "TableName": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_name"}), - "TableToken": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_token"}), - "AddBot": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "add_bot"}), - "TableActivity": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_activity"}), - "Title": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "title"}), + "ButtonSave": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "button_save"}), + "ApiKey": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "api_key"}), + "TabSettings": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_settings"}), + "TabBots": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_bots"}), + "TableName": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_name"}), + "TableToken": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_token"}), + "AddBot": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "add_bot"}), + "TableDelete": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_delete"}), + "Title": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "title"}), }, } diff --git a/routing_test.go b/routing_test.go index 7959441..e9076aa 100644 --- a/routing_test.go +++ b/routing_test.go @@ -103,7 +103,7 @@ func TestRouting_addBotHandler(t *testing.T) { assert.Equal(t, "123123:Qwerty", res["token"]) } -func TestRouting_activityBotHandler(t *testing.T) { +func TestRouting_deleteBotHandler(t *testing.T) { defer gock.Off() gock.New("https://test.retailcrm.pro"). @@ -114,13 +114,13 @@ func TestRouting_activityBotHandler(t *testing.T) { Reply(200). BodyString(`{"id": 1}`) - req, err := http.NewRequest("POST", "/activity-bot/", strings.NewReader(`{"token": "123123:Qwerty", "active": false, "connectionId": 1}`)) + req, err := http.NewRequest("POST", "/delete-bot/", strings.NewReader(`{"token": "123123:Qwerty", "active": false, "connectionId": 1}`)) if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() - handler := http.HandlerFunc(activityBotHandler) + handler := http.HandlerFunc(deleteBotHandler) handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusOK, rr.Code, diff --git a/static/script.js b/static/script.js index bde8d25..7519274 100644 --- a/static/script.js +++ b/static/script.js @@ -39,21 +39,17 @@ $("#add-bot").on("submit", function(e) { ) }); -$(document).on("click", ".activity-bot", function(e) { +$(document).on("click", ".delete-bot", function(e) { let but = $(this); - send("/activity-bot/", + send("/delete-bot/", { token: but.attr("data-token"), - active: (but.attr("data-activity") === 'true'), connectionId: parseInt($('input[name=connectionId]').val()), }, function () { - if (but.attr("data-activity") === 'true') { - but.find('i').replaceWith('play_arrow'); - but.attr("data-activity", "false") - } else { - but.find('i').replaceWith('pause'); - but.attr("data-activity", "true") + but.parents("tr").remove(); + if ($("#bots tbody tr").length === 0) { + $("#bots").addClass("hide"); } } ) @@ -89,9 +85,9 @@ function getBotTemplate(data) { ${bot.name} ${bot.token} - `; diff --git a/static/style.css b/static/style.css index 69bda59..ce9aa32 100644 --- a/static/style.css +++ b/static/style.css @@ -16,7 +16,7 @@ margin: 0 auto; } -#bots .activity-bot{ +#bots .delete-bot{ float: right; } diff --git a/telegram.go b/telegram.go index dfec7ea..b8cdc08 100644 --- a/telegram.go +++ b/telegram.go @@ -39,7 +39,7 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string return } - if b.ID == 0 || !b.Active { + if b.ID == 0 { logger.Error(token, "telegramWebhookHandler: missing or deactivated") w.WriteHeader(http.StatusOK) return @@ -237,7 +237,7 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { cid, _ := strconv.ParseInt(msg.Data.ExternalChatID, 10, 64) b := getBot(c.ID, msg.Data.ChannelID) - if b.ID == 0 || !b.Active { + if b.ID == 0 { logger.Error(msg.Data.ChannelID, "mgWebhookHandler: missing or deactivated") w.WriteHeader(http.StatusBadRequest) w.Write([]byte("missing or deactivated")) diff --git a/templates/form.html b/templates/form.html index 058b6a3..ae66278 100644 --- a/templates/form.html +++ b/templates/form.html @@ -53,7 +53,7 @@ {{.Locale.TableName}} {{.Locale.TableToken}} - {{.Locale.TableActivity}} + {{.Locale.TableDelete}} @@ -62,9 +62,9 @@ {{.Name}} {{.Token}} - diff --git a/translate/translate.en.yml b/translate/translate.en.yml index 7dc6013..ac499d5 100644 --- a/translate/translate.en.yml +++ b/translate/translate.en.yml @@ -3,7 +3,7 @@ tab_settings: CRM settings tab_bots: Bots table_name: Name table_token: Token -table_activity: Activity mark +table_delete: Delete api_key: API key add_bot: Add a bot title: Module of connecting Telegram to retailCRM diff --git a/translate/translate.ru.yml b/translate/translate.ru.yml index 7697778..cd44a72 100644 --- a/translate/translate.ru.yml +++ b/translate/translate.ru.yml @@ -3,7 +3,7 @@ tab_settings: Настройки CRM tab_bots: Боты table_name: Имя table_token: Токен -table_activity: Активность +table_delete: Удалить api_key: API Ключ add_bot: Добавить бота title: Модуль подключения Telegram к retailCRM