1
0
mirror of synced 2024-11-21 20:16:02 +03:00

replace activation/deactivation to delete

This commit is contained in:
DmitryZagorulko 2018-06-28 12:17:19 +03:00
parent 76d9ab6e8f
commit f60e4795b3
12 changed files with 41 additions and 65 deletions

View File

@ -0,0 +1 @@
alter table bot add column active boolean;

View File

@ -0,0 +1 @@
alter table bot drop column active;

View File

@ -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

View File

@ -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 {

View File

@ -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"}),
},
}

View File

@ -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,

View File

@ -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('<i class="material-icons">play_arrow</i>');
but.attr("data-activity", "false")
} else {
but.find('i').replaceWith('<i class="material-icons">pause</i>');
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) {
<td>${bot.name}</td>
<td>${bot.token}</td>
<td>
<button class="activity-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
data-activity="true" data-token="${bot.token}">
<i class="material-icons">pause</i>
<button class="delete-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
data-token="${bot.token}">
<i class="material-icons">delete</i>
</button>
</td>
</tr>`;

View File

@ -16,7 +16,7 @@
margin: 0 auto;
}
#bots .activity-bot{
#bots .delete-bot{
float: right;
}

View File

@ -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"))

View File

@ -53,7 +53,7 @@
<tr>
<th>{{.Locale.TableName}}</th>
<th>{{.Locale.TableToken}}</th>
<th class="text-left">{{.Locale.TableActivity}}</th>
<th class="text-left">{{.Locale.TableDelete}}</th>
</tr>
</thead>
<tbody>
@ -62,9 +62,9 @@
<td>{{.Name}}</td>
<td>{{.Token}}</td>
<td>
<button class="activity-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
data-activity="{{.Active}}" data-token="{{.Token}}">
<i class="material-icons">{{if .Active}}pause{{else}}play_arrow{{end}}</i>
<button class="delete-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
data-token="{{.Token}}">
<i class="material-icons">delete</i>
</button>
</td>
</tr>

View File

@ -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

View File

@ -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