| package main | package main | ||||
| import ( | import ( | ||||
| "encoding/json" | |||||
| "errors" | |||||
| "fmt" | "fmt" | ||||
| "log" | "log" | ||||
| ) | ) | ||||
| List []crmdLead `json:"list"` | 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) | total, list, err := crmFindEntityByAttr("Lead", "wechat_hitxy_id", openID) | ||||
| if err != nil { | if err != nil { | ||||
| return | return | ||||
| r = entity.(crmdLead) | r = entity.(crmdLead) | ||||
| return | 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 | |||||
| } |
| AssertEqual(t, lead.Password, e.Password, "password should match") | AssertEqual(t, lead.Password, e.Password, "password should match") | ||||
| //start query | //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, err, nil, "finding Lead record by ID should be nil") | ||||
| AssertEqual(t, found, true, "we should have found our record of Lead") | AssertEqual(t, found, true, "we should have found our record of Lead") | ||||
| AssertEqual(t, info.WechatHitxyID, e.WechatHitxyID, "wechat id should be the same") | 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 | //delete temp record | ||||
| deleted, _ := crmDeleteEntity("Lead", id) | deleted, _ := crmDeleteEntity("Lead", id) | ||||
| AssertEqual(t, deleted, true, "test record shold be deleted correctly") | AssertEqual(t, deleted, true, "test record shold be deleted correctly") |
| func onSubscribe(in InWechatMsg) { | func onSubscribe(in InWechatMsg) { | ||||
| openID := in.header.FromUserName | openID := in.header.FromUserName | ||||
| //check whether we have his own record in the CRM system | //check whether we have his own record in the CRM system | ||||
| info, found, err := crmFindOpenID(openID) | |||||
| info, found, err := crmFindLeadByOpenID(openID) | |||||
| if err != nil { | if err != nil { | ||||
| log.Println(err) | log.Println(err) | ||||
| //when user left | //when user left | ||||
| func onUnSubscribe(in InWechatMsg) { | 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) | |||||
| } | } |
| case "unsubscribe": | case "unsubscribe": | ||||
| processed = true | processed = true | ||||
| in.replyText("") | in.replyText("") | ||||
| onUnSubscribe(in) | |||||
| return | return | ||||
| case "SCAN": | case "SCAN": | ||||
| case "LOCATION": | case "LOCATION": |