| r = e | r = e | ||||
| case "Account": | case "Account": | ||||
| //r = crmdAccount{} | //r = crmdAccount{} | ||||
| case "Location": | |||||
| e := crmdLocation{} | |||||
| err = json.Unmarshal([]byte(data), &e) | |||||
| r = e | |||||
| case "Contact": | case "Contact": | ||||
| e := crmdContact{} | e := crmdContact{} | ||||
| err = json.Unmarshal([]byte(data), &e) | err = json.Unmarshal([]byte(data), &e) |
| newContact.convertFromLead(m.ID) | newContact.convertFromLead(m.ID) | ||||
| return | 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 | |||||
| } |
| 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 | |||||
| } |
| 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 | |||||
| } |
| case "SCAN": | case "SCAN": | ||||
| in.replyText("SCAN/" + e.EventKey) | in.replyText("SCAN/" + e.EventKey) | ||||
| case "LOCATION": | case "LOCATION": | ||||
| in.replyText("LOCATION") | |||||
| in.replyText("") | |||||
| onLocation(in) | |||||
| case "CLICK": | case "CLICK": | ||||
| in.replyText("") | in.replyText("") | ||||
| onClick(in) | onClick(in) |