| @@ -80,6 +80,10 @@ func crmJSON2Entity(entityType string, data string) (r interface{}, err error) { | |||
| r = e | |||
| case "Account": | |||
| //r = crmdAccount{} | |||
| case "Location": | |||
| e := crmdLocation{} | |||
| err = json.Unmarshal([]byte(data), &e) | |||
| r = e | |||
| case "Contact": | |||
| e := crmdContact{} | |||
| err = json.Unmarshal([]byte(data), &e) | |||
| @@ -184,3 +184,32 @@ func (m crmdLead) crmLeadConvert2Contact() (newContact crmdContact) { | |||
| newContact.convertFromLead(m.ID) | |||
| return | |||
| } | |||
| func (m crmdLead) Save() (newlead crmdLead, err error) { | |||
| jsonB, err := json.Marshal(m) | |||
| if err != nil { | |||
| return | |||
| } | |||
| if m.ID != "" { | |||
| entity, err := crmUpdateEntity("Lead", m.ID, jsonB) | |||
| if err != nil { | |||
| return newlead, err | |||
| } | |||
| newlead = entity.(crmdLead) | |||
| } else { | |||
| entity, err := crmCreateEntity("Lead", jsonB) | |||
| if err != nil { | |||
| return newlead, err | |||
| } | |||
| newlead = entity.(crmdLead) | |||
| } | |||
| return | |||
| } | |||
| func (m crmdLead) Delete() bool { | |||
| if m.ID != "" { | |||
| deleted, err := crmDeleteEntity("Lead", m.ID) | |||
| return err == nil && deleted == true | |||
| } | |||
| return false | |||
| } | |||
| @@ -0,0 +1,54 @@ | |||
| package main | |||
| import ( | |||
| "encoding/json" | |||
| "errors" | |||
| "log" | |||
| ) | |||
| type crmdLocation struct { | |||
| crmdEntityBase | |||
| Latitude float64 `json:"latitude"` | |||
| Longtitude float64 `json:"longtitude"` | |||
| Precision float64 `json:"precision"` | |||
| LeadID string `json:"leadId,omitempty"` | |||
| } | |||
| //record location | |||
| func onLocation(in InWechatMsg) { | |||
| openID := in.header.FromUserName | |||
| lead, found, err := crmFindLeadByOpenID(openID) | |||
| if found && err == nil { | |||
| e := in.body.(EventMsg) | |||
| //save info | |||
| info := crmdLocation{} | |||
| info.Name = in.header.FromUserName | |||
| info.LeadID = lead.ID | |||
| info.Longtitude = e.Longitude | |||
| info.Latitude = e.Latitude | |||
| info.Precision = e.Precision | |||
| info.Save() | |||
| } | |||
| } | |||
| func (m crmdLocation) Save() (location crmdLocation, err error) { | |||
| jsonB, err := json.Marshal(m) | |||
| if err != nil { | |||
| log.Println(err) | |||
| return | |||
| } | |||
| entity, err := crmCreateEntity("Location", jsonB) | |||
| if err != nil { | |||
| log.Println(err) | |||
| return | |||
| } | |||
| location, ok := entity.(crmdLocation) | |||
| if !ok { | |||
| err = errors.New("incorrect entity cannot convert to Location") | |||
| } | |||
| log.Println(location) | |||
| return | |||
| } | |||
| @@ -0,0 +1,41 @@ | |||
| package main | |||
| import "testing" | |||
| import "log" | |||
| func TestSaveLocation(t *testing.T) { | |||
| tmpLead := createTempLead() | |||
| info := crmdLocation{} | |||
| info.Name = "name" | |||
| info.LeadID = tmpLead.ID | |||
| info.Longtitude = 10.77 | |||
| info.Latitude = 20.18 | |||
| info.Precision = 150 | |||
| location, err := info.Save() | |||
| AssertEqual(t, err, nil, "") | |||
| AssertEqual(t, location.Name, info.Name, "name mismatch ") | |||
| AssertEqual(t, location.LeadID, info.LeadID, "lead mismatch") | |||
| AssertEqual(t, location.Longtitude, info.Longtitude, "longtitude mismatch ") | |||
| AssertEqual(t, location.Latitude, info.Latitude, "latitude mismatch ") | |||
| AssertEqual(t, location.Precision, info.Precision, "precision mismatch ") | |||
| //clean up | |||
| crmDeleteEntity("Location", location.ID) | |||
| AssertEqual(t, tmpLead.Delete(), true, "") | |||
| } | |||
| func createTempLead() (r crmdLead) { | |||
| info := crmdLead{} | |||
| info.FirstName = "testlocation" | |||
| info.LastName = "canbedeleted" | |||
| info.Password = "pass" | |||
| info.Status = "Deletable" | |||
| info.ForceDuplicate = true | |||
| r, err := info.Save() | |||
| if err != nil { | |||
| log.Fatal(err) | |||
| } | |||
| return | |||
| } | |||
| @@ -19,7 +19,8 @@ func (ss *openIDSessionData) serveEvents(in InWechatMsg) (processed bool) { | |||
| case "SCAN": | |||
| in.replyText("SCAN/" + e.EventKey) | |||
| case "LOCATION": | |||
| in.replyText("LOCATION") | |||
| in.replyText("") | |||
| onLocation(in) | |||
| case "CLICK": | |||
| in.replyText("") | |||
| onClick(in) | |||