Ver código fonte

search entity by id now using search by attribute as its' underlinng support.

master
Patrick Peng Sun 8 anos atrás
pai
commit
0ac81c9920
2 arquivos alterados com 29 adições e 22 exclusões
  1. +28
    -22
      crmEntity.go
  2. +1
    -0
      crmEntity_test.go

+ 28
- 22
crmEntity.go Ver arquivo

@@ -196,37 +196,31 @@ func crmFindEntityByID(entityType string, id string) (entity interface{}, err er
return
}

filters := []crmdSearchFilter{
{"id", "equals", id},
}
cs, err := crmSearchEntity(entityType, filters)
if err != nil || cs.Total < 1 {
return
}
total, list, err := crmFindEntityByAttr(entityType, "id", id)

if cs.Total > 1 {
log.Printf("ERROR: more than one(%d) %s associated with %s", cs.Total, entityType, id)
if total > 1 {
log.Printf("ERROR: more than one(%d) %s associated with %s", total, entityType, id)
}

if err != nil {
return
}
switch entityType {
case "Lead":
e := []crmdLead{}
err = json.Unmarshal(*cs.List, &e)
if (err != nil) || (len(e) != cs.Total) {
e, ok := list.([]crmdLead)
if (!ok) || (len(e) != total) {
return
}
entity = e[0]
case "Livecast":
e := []crmdLiveCast{}
err = json.Unmarshal(*cs.List, &e)
if (err != nil) || (len(e) != cs.Total) {
e, ok := list.([]crmdLiveCast)
if (!ok) || (len(e) != total) {
return
}
entity = e[0]
case "Meeting":
e := []crmdMeeting{}
err = json.Unmarshal(*cs.List, &e)
if (err != nil) || (len(e) != cs.Total) {
e, ok := list.([]crmdMeeting)
if (!ok) || (len(e) != total) {
return
}
entity = e[0]
@@ -245,15 +239,27 @@ func crmFindEntityByAttr(entityType, attribute, value string) (total int, list i
if err != nil || cs.Total < 1 {
return
}
total = cs.Total
return cs.toEntityList(entityType)
}

func (m crmdSearchResult) toEntityList(entityType string) (total int, list interface{}, err error) {
total = m.Total
switch entityType {
case "Lead":
e := []crmdLead{}
err = json.Unmarshal(*cs.List, &e)
err = json.Unmarshal(*m.List, &e)
list = e
case "Livecast":
e := []crmdLiveCast{}
err = json.Unmarshal(*m.List, &e)
list = e
case "Account":
return

case "Meeting":
e := []crmdMeeting{}
err = json.Unmarshal(*m.List, &e)
list = e
default:
err = errors.New("unknown entity type " + entityType)
}
return
}

+ 1
- 0
crmEntity_test.go Ver arquivo

@@ -105,6 +105,7 @@ func TestCrmReplaceEntity(t *testing.T) {
e.Status = "New"
e.EmailAddress = "abc@gmail.com"
e.WechatHitxyID = "someopenid"
e.ForceDuplicate = true
b, _ := json.Marshal(e)
entity, err := crmCreateEntity("Lead", b)
if err != nil {

Carregando…
Cancelar
Salvar