Sfoglia il codice sorgente

using crmFindEntity to search wechat id record.

master
Patrick Peng Sun 8 anni fa
parent
commit
8ab1e0073d
2 ha cambiato i file con 37 aggiunte e 58 eliminazioni
  1. +9
    -52
      crmLead.go
  2. +28
    -6
      crmLead_test.go

+ 9
- 52
crmLead.go Vedi File

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


+ 28
- 6
crmLead_test.go Vedi File

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

Loading…
Annulla
Salva