| package main | package main | ||||
| import ( | import ( | ||||
| "encoding/json" | |||||
| "io/ioutil" | |||||
| "fmt" | |||||
| "log" | "log" | ||||
| "net/http" | |||||
| "time" | "time" | ||||
| ) | ) | ||||
| } | } | ||||
| func crmFindOpenID(openID string) (info crmdLead, found bool, err error) { | 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 { | if err != nil { | ||||
| log.Println("crmGetRecord: cannot form good http Request") | |||||
| log.Print(err) | |||||
| return | 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 | 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 | return | ||||
| } | } | ||||
| "encoding/json" | "encoding/json" | ||||
| "log" | "log" | ||||
| "testing" | "testing" | ||||
| "time" | |||||
| ) | ) | ||||
| func TestDecodeLeadInfo(t *testing.T) { | func TestDecodeLeadInfo(t *testing.T) { | ||||
| } | } | ||||
| func TestFindOpenIDOnline(t *testing.T) { | func TestFindOpenIDOnline(t *testing.T) { | ||||
| info, found, err := crmFindOpenID("weid1") | info, found, err := crmFindOpenID("weid1") | ||||
| log.Println(info) | log.Println(info) | ||||
| log.Println(found) | log.Println(found) | ||||
| } | } | ||||
| func TestGetLead(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") | |||||
| } | } |