From 0ac81c99208c08de9b784c0f2d5d8254ab9abb70 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Sat, 15 Jul 2017 19:15:10 +1000 Subject: [PATCH] search entity by id now using search by attribute as its' underlinng support. --- crmEntity.go | 50 ++++++++++++++++++++++++++--------------------- crmEntity_test.go | 1 + 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/crmEntity.go b/crmEntity.go index 16e4efe..d6c41da 100644 --- a/crmEntity.go +++ b/crmEntity.go @@ -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 } diff --git a/crmEntity_test.go b/crmEntity_test.go index 40e4269..54a1244 100644 --- a/crmEntity_test.go +++ b/crmEntity_test.go @@ -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 {