From 6c545c24774af9695295953a6f3ecd386761cdf8 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Fri, 31 Aug 2018 16:09:44 +0300 Subject: [PATCH] fix activation/deactivation of crm, add rename handling crm --- go.mod | 1 + go.sum | 2 ++ src/repository.go | 4 ++++ src/routing.go | 35 +++++++++++++++++++++++++---------- src/routing_test.go | 11 +++++++---- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 2ff3f9d..bdbdb71 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5b51e88..039727f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/src/repository.go b/src/repository.go index 1c62eed..522f280 100644 --- a/src/repository.go +++ b/src/repository.go @@ -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 } diff --git a/src/routing.go b/src/routing.go index d54a1dd..cc95d47 100644 --- a/src/routing.go +++ b/src/routing.go @@ -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 } diff --git a/src/routing_test.go b/src/routing_test.go index f6483dc..af770ad 100644 --- a/src/routing_test.go +++ b/src/routing_test.go @@ -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)