Browse Source

locatoin save tested.

master
Patrick Peng Sun 8 years ago
parent
commit
a1cf89895e
5 changed files with 130 additions and 1 deletions
  1. +4
    -0
      crmEntity.go
  2. +29
    -0
      crmLead.go
  3. +54
    -0
      location.go
  4. +41
    -0
      location_test.go
  5. +2
    -1
      serveEvents.go

+ 4
- 0
crmEntity.go View File

@@ -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)

+ 29
- 0
crmLead.go View File

@@ -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
}

+ 54
- 0
location.go View File

@@ -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
}

+ 41
- 0
location_test.go View File

@@ -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
}

+ 2
- 1
serveEvents.go View File

@@ -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)

Loading…
Cancel
Save