diff --git a/crmEntity.go b/crmEntity.go index 89d76c3..e250ded 100644 --- a/crmEntity.go +++ b/crmEntity.go @@ -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) diff --git a/crmLead.go b/crmLead.go index c31b098..76b45f0 100644 --- a/crmLead.go +++ b/crmLead.go @@ -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 +} diff --git a/location.go b/location.go new file mode 100644 index 0000000..2564208 --- /dev/null +++ b/location.go @@ -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 +} diff --git a/location_test.go b/location_test.go new file mode 100644 index 0000000..0469b02 --- /dev/null +++ b/location_test.go @@ -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 +} diff --git a/serveEvents.go b/serveEvents.go index 9096d2d..46e979a 100644 --- a/serveEvents.go +++ b/serveEvents.go @@ -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)