From 8ab1e0073d92f07a3b08d044132ff0e7b349d882 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Sun, 2 Jul 2017 01:29:42 +1000 Subject: [PATCH] using crmFindEntity to search wechat id record. --- crmLead.go | 61 ++++++++----------------------------------------- crmLead_test.go | 34 ++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 58 deletions(-) diff --git a/crmLead.go b/crmLead.go index 4fab944..e843643 100644 --- a/crmLead.go +++ b/crmLead.go @@ -1,10 +1,8 @@ package main import ( - "encoding/json" - "io/ioutil" + "fmt" "log" - "net/http" "time" ) @@ -98,63 +96,22 @@ func (m *crmdLead) getTimeLayout() string { } func crmFindOpenID(openID string) (info crmdLead, found bool, err error) { - found = false - req, err := http.NewRequest("GET", CRMConfig.apiLeadURL(), nil) + total, list, err := crmFindEntityByAttr("Lead", "wechat_hitxy_id", openID) if err != nil { - log.Println("crmGetRecord: cannot form good http Request") - log.Print(err) return } - //auth header - req.Header.Set("Authorization", crmAuthHeader()) - req.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01") - req.Header.Set("Content-Type", "application/json") - - //query string - q := req.URL.Query() - q.Add("maxSize", "20") - q.Add("maxSize", "0") - q.Add("sortBy", "createdAt") - q.Add("asc", "false") - q.Add("where[0][type]", "equals") - q.Add("where[0][attribute]", "wechat_hitxy_id") - q.Add("where[0][value]", openID) - req.URL.RawQuery = q.Encode() - - // dump, err := httputil.DumpRequest(req, true) - // if err != nil { - // log.Fatal(err) - // } - // fmt.Printf("dump : %q", dump) - - client := &http.Client{} - r, err := client.Do(req) - - if err != nil { - return - } - defer r.Body.Close() - - b, _ := ioutil.ReadAll(r.Body) - search := crmdSearchLead{} - err = json.Unmarshal(b, &search) - - if err != nil || search.Total <= 0 { + if total == 1 { + found = true + info = list.([]crmdLead)[0] return } + found = false + if total > 1 { - if search.Total > 1 { - log.Printf("ERROR It's wrong to have multiple record for same wechat id %s", openID) - for _, v := range search.List { - log.Println(v) - } - return + msg := fmt.Sprintf("wechat_hitxy_id %s has %d record", openID, total) + log.Printf(msg) } - - //we successfully reach here - info = search.List[0] - found = true return } diff --git a/crmLead_test.go b/crmLead_test.go index 8271aa6..6f6f8f4 100644 --- a/crmLead_test.go +++ b/crmLead_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "log" "testing" + "time" ) func TestDecodeLeadInfo(t *testing.T) { @@ -391,7 +392,7 @@ func TestSearchResultZero(t *testing.T) { } func TestFindOpenIDOnline(t *testing.T) { - + info, found, err := crmFindOpenID("weid1") log.Println(info) log.Println(found) @@ -405,9 +406,30 @@ func TestLeadJsonCompose(t *testing.T) { } func TestGetLead(t *testing.T) { - - id := "595071f8450974b72" - r, err := crmGetLead(id) - AssertEqual(t, err, nil, "decode json should be nil") - AssertEqual(t, r.ID, id, "Lead id should match") + + e := crmdLead{} + e.FirstName = "testGetLeadById" + time.Now().Format("2006-Jan-02 15:04:05") + e.LastName = "crmLead/TestGetLead" + e.WechatHitxyID = "predefined-openid" + time.Now().Format("2006-Jan-02 15:04:05") + e.Password = "some password" + b, _ := json.Marshal(e) + entity, err := crmCreateEntity("Lead", b) + AssertEqual(t, err, nil, "create sample Lead should be successful") + lead := entity.(crmdLead) + id := lead.ID + + AssertEqual(t, lead.FirstName, e.FirstName, "first name should match") + AssertEqual(t, lead.LastName, e.LastName, "lastname should match") + AssertEqual(t, lead.WechatHitxyID, e.WechatHitxyID, "wechat_hitxy_id should match") + AssertEqual(t, lead.Password, e.Password, "password should match") + + //start query + info, found, err := crmFindOpenID(e.WechatHitxyID) + AssertEqual(t, err, nil, "finding Lead record by ID should be nil") + AssertEqual(t, found, true, "we should have found our record of Lead") + AssertEqual(t, info.WechatHitxyID, e.WechatHitxyID, "wechat id should be the same") + + //delete temp record + deleted, _ := crmDeleteEntity("Lead", id) + AssertEqual(t, deleted, true, "test record shold be deleted correctly") }