1
0
mirror of synced 2024-11-22 12:26:02 +03:00

fix activation/deactivation of crm, add rename handling crm

This commit is contained in:
DmitryZagorulko 2018-08-31 16:09:44 +03:00
parent b75c725b3e
commit 6c545c2477
5 changed files with 39 additions and 14 deletions

1
go.mod
View File

@ -31,6 +31,7 @@ require (
github.com/jinzhu/now v0.0.0-20180511015916-ed742868f2ae // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/json-iterator/go v0.0.0-20180806060727-1624edc4454b // indirect
github.com/jstemmer/go-junit-report v0.0.0-20180614143834-385fac0ced9a // indirect
github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.0.0 // indirect

2
go.sum
View File

@ -64,6 +64,8 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5i
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/json-iterator/go v0.0.0-20180806060727-1624edc4454b h1:X61dhFTE1Au92SvyF8HyAwdjWqiSdfBgFR7wTxC0+uU=
github.com/json-iterator/go v0.0.0-20180806060727-1624edc4454b/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jstemmer/go-junit-report v0.0.0-20180614143834-385fac0ced9a h1:2qq552JOlVHGYvqPc9ynBnGPDHeA7p0/QRn2NkrO8vk=
github.com/jstemmer/go-junit-report v0.0.0-20180614143834-385fac0ced9a/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=

View File

@ -29,6 +29,10 @@ func (c *Connection) createConnection() error {
}
func (c *Connection) saveConnection() error {
return orm.DB.Save(c).Error
}
func (c *Connection) saveConnectionByClientID() error {
return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Update(c).Error
}

View File

@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
@ -159,7 +160,7 @@ func saveHandler(c *gin.Context) {
return
}
err = conn.saveConnection()
err = conn.saveConnectionByClientID()
if err != nil {
c.Error(err)
return
@ -220,14 +221,13 @@ func createHandler(c *gin.Context) {
}
func activityHandler(c *gin.Context) {
var rec v5.ActivityCallback
var (
activity v5.Activity
systemUrl = c.PostForm("systemUrl")
clientId = c.PostForm("clientId")
)
if err := c.ShouldBindJSON(&rec); err != nil {
c.Error(err)
return
}
conn := getConnection(rec.ClientId)
conn := getConnection(clientId)
if conn.ID == 0 {
c.AbortWithStatusJSON(http.StatusBadRequest,
gin.H{
@ -238,9 +238,24 @@ func activityHandler(c *gin.Context) {
return
}
conn.Active = rec.Activity.Active && !rec.Activity.Freeze
err := json.Unmarshal([]byte(c.PostForm("activity")), &activity)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest,
gin.H{
"success": false,
"error": "Wrong data",
},
)
return
}
if err := conn.setConnectionActivity(); err != nil {
conn.Active = activity.Active && !activity.Freeze
if systemUrl != "" {
conn.APIURL = systemUrl
}
if err := conn.saveConnection(); err != nil {
c.Error(err)
return
}

View File

@ -187,14 +187,17 @@ func TestRouting_saveHandler(t *testing.T) {
}
func TestRouting_activityHandler(t *testing.T) {
req, err := http.NewRequest("POST", "/actions/activity",
strings.NewReader(
`{"clientId": "123123","activity": {"active": true}}`,
))
data := url.Values{}
data.Set("clientId", "123123")
data.Set("activity", `{"active": true, "freeze": false}`)
req, err := http.NewRequest("POST", "/actions/activity", strings.NewReader(data.Encode()))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)