| @@ -1,6 +1,8 @@ | |||
| package main | |||
| import ( | |||
| "encoding/json" | |||
| "errors" | |||
| "fmt" | |||
| "log" | |||
| ) | |||
| @@ -67,7 +69,7 @@ type crmdSearchLead struct { | |||
| List []crmdLead `json:"list"` | |||
| } | |||
| func crmFindOpenID(openID string) (info crmdLead, found bool, err error) { | |||
| func crmFindLeadByOpenID(openID string) (info crmdLead, found bool, err error) { | |||
| total, list, err := crmFindEntityByAttr("Lead", "wechat_hitxy_id", openID) | |||
| if err != nil { | |||
| return | |||
| @@ -92,3 +94,22 @@ func crmGetLead(id string) (r crmdLead, err error) { | |||
| r = entity.(crmdLead) | |||
| return | |||
| } | |||
| func crmPatchLeadInfo(newInfo crmdLead) (updatedLead crmdLead, err error) { | |||
| b, err := json.Marshal(newInfo) | |||
| if err != nil { | |||
| log.Printf("Failed to Patch new Info for Lead") | |||
| log.Println(newInfo) | |||
| return | |||
| } | |||
| newEntity, err := crmUpdateEntity("Lead", newInfo.ID, b) | |||
| if err != nil { | |||
| return | |||
| } | |||
| updatedLead, ok := newEntity.(crmdLead) | |||
| if !ok { | |||
| err = errors.New("Update Lead result in wrong entityType") | |||
| } | |||
| return | |||
| } | |||
| @@ -409,11 +409,19 @@ func TestGetLead(t *testing.T) { | |||
| AssertEqual(t, lead.Password, e.Password, "password should match") | |||
| //start query | |||
| info, found, err := crmFindOpenID(e.WechatHitxyID) | |||
| info, found, err := crmFindLeadByOpenID(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") | |||
| //test patch lead | |||
| newStatus := crmdLead{} | |||
| newStatus.Status = "Dead" | |||
| newStatus.ID = id | |||
| newLead, err := crmPatchLeadInfo(newStatus) | |||
| AssertEqual(t, err, nil, "patch status for lead shold be correct") | |||
| AssertEqual(t, newLead.Status, "Dead", "Newstatus should be dead") | |||
| //delete temp record | |||
| deleted, _ := crmDeleteEntity("Lead", id) | |||
| AssertEqual(t, deleted, true, "test record shold be deleted correctly") | |||
| @@ -13,7 +13,7 @@ import ( | |||
| func onSubscribe(in InWechatMsg) { | |||
| openID := in.header.FromUserName | |||
| //check whether we have his own record in the CRM system | |||
| info, found, err := crmFindOpenID(openID) | |||
| info, found, err := crmFindLeadByOpenID(openID) | |||
| if err != nil { | |||
| log.Println(err) | |||
| @@ -128,5 +128,19 @@ func (m *WechatUserInfo) registerNewLeadWithInfo(in InWechatMsg) (newuser crmdLe | |||
| //when user left | |||
| func onUnSubscribe(in InWechatMsg) { | |||
| //TODO: record unSubscribe | |||
| info, found, err := crmFindLeadByOpenID(in.header.FromUserName) | |||
| if err != nil { | |||
| log.Printf("Error happened when trying to unsubscribe a user %s", in.header.FromUserName) | |||
| log.Println(err) | |||
| } | |||
| if found == false { | |||
| log.Printf("not found openid %s", in.header.FromUserName) | |||
| return | |||
| } | |||
| //mark status | |||
| newStatus := crmdLead{} | |||
| newStatus.ID = info.ID | |||
| newStatus.Status = "Dead" | |||
| crmPatchLeadInfo(newStatus) | |||
| } | |||
| @@ -12,6 +12,7 @@ func (ss *openIDSessionData) serveEvents(in InWechatMsg) (processed bool) { | |||
| case "unsubscribe": | |||
| processed = true | |||
| in.replyText("") | |||
| onUnSubscribe(in) | |||
| return | |||
| case "SCAN": | |||
| case "LOCATION": | |||