all data start with crmd all function start with crm so as to easy identify it.master
| @@ -0,0 +1,75 @@ | |||
| package main | |||
| import "log" | |||
| //abstract CRUD operation for espoCRM Entity | |||
| var crmSite = "https://c.hitxy.org.au/" | |||
| func createEntity(entityType string, jsonB []byte) (entity interface{}, err error) { | |||
| url := crmSite + "api/v1/" + entityType | |||
| json, err := postRAW(jsonB, url, crmBuildCommonAPIHeader()) | |||
| if err != nil { | |||
| log.Println(err) | |||
| return | |||
| } | |||
| return json, err | |||
| } | |||
| func updateEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { | |||
| url := crmSite + "api/v1/" + entityType + "/" + id | |||
| json, err := patchRAW(jsonB, url, crmBuildCommonAPIHeader()) | |||
| if err != nil { | |||
| log.Println(err) | |||
| return | |||
| } | |||
| return json, err | |||
| } | |||
| func replaceEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { | |||
| url := crmSite + "api/v1/" + entityType + "/" + id | |||
| json, err := putRAW(jsonB, url, crmBuildCommonAPIHeader()) | |||
| if err != nil { | |||
| log.Println(err) | |||
| return | |||
| } | |||
| return json, err | |||
| } | |||
| func deleteEntity(entityType string, id string) (json string, err error) { | |||
| url := crmSite + "api/v1/" + entityType + "/" + id | |||
| json, err = deleteRAW(url, crmBuildCommonAPIHeader()) | |||
| if err != nil { | |||
| log.Println(err) | |||
| return | |||
| } | |||
| return | |||
| } | |||
| //give an id, return json | |||
| func crmGetEntity(entityType string, id string) (json string, err error) { | |||
| url := crmSite + "api/v1/" + entityType + "/" + id | |||
| json, err = getRAW(url, crmBuildCommonAPIHeader()) | |||
| if err != nil { | |||
| log.Println(err) | |||
| return | |||
| } | |||
| return json, err | |||
| } | |||
| func crmBuildCommonAPIHeader() (headers map[string]string) { | |||
| headers = map[string]string{} | |||
| headers["Authorization"] = crmAuthHeader() | |||
| headers["Accept"] = "application/json" | |||
| headers["Content-Type"] = "application/json" | |||
| return headers | |||
| } | |||
| //given a json string, convert it to Typed structure | |||
| func json2Entity(entityType string, json string) (r interface{}) { | |||
| switch entityType { | |||
| case "Lead": | |||
| r = crmdLeadInfo | |||
| case "Account": | |||
| } | |||
| return | |||
| } | |||
| @@ -0,0 +1,9 @@ | |||
| package main | |||
| import "testing" | |||
| func TestGetEntity(t *testing.T) { | |||
| SetupConfig() | |||
| leadID := "595071f8450974b72" | |||
| crmGetEntity("Lead", leadID) | |||
| } | |||
| @@ -8,7 +8,7 @@ import ( | |||
| "time" | |||
| ) | |||
| type crmLeadInfo struct { | |||
| type crmdLead struct { | |||
| ID string `json:"id"` | |||
| Name string `json:"name,omitempty"` | |||
| Deleted bool `json:"deleted,omitempty"` | |||
| @@ -63,39 +63,39 @@ type crmLeadInfo struct { | |||
| } | |||
| type crmSearchLead struct { | |||
| Total int `json:"total"` | |||
| List []crmLeadInfo `json:"list"` | |||
| Total int `json:"total"` | |||
| List []crmdLead `json:"list"` | |||
| } | |||
| func (m *crmLeadInfo) getCreatedAt() (r time.Time) { | |||
| func (m *crmdLead) getCreatedAt() (r time.Time) { | |||
| layout := m.getTimeLayout() | |||
| r, _ = time.Parse(layout, m.CreatedAt) | |||
| return | |||
| } | |||
| func (m *crmLeadInfo) setCreatedAt(v time.Time) string { | |||
| func (m *crmdLead) setCreatedAt(v time.Time) string { | |||
| layout := m.getTimeLayout() | |||
| m.CreatedAt = v.Format(layout) | |||
| return m.CreatedAt | |||
| } | |||
| func (m *crmLeadInfo) getModifiedAt() (r time.Time) { | |||
| func (m *crmdLead) getModifiedAt() (r time.Time) { | |||
| layout := m.getTimeLayout() | |||
| r, _ = time.Parse(layout, m.ModifiedAt) | |||
| return | |||
| } | |||
| func (m *crmLeadInfo) setModifiedAt(v time.Time) string { | |||
| func (m *crmdLead) setModifiedAt(v time.Time) string { | |||
| layout := m.getTimeLayout() | |||
| m.ModifiedAt = v.Format(layout) | |||
| return m.ModifiedAt | |||
| } | |||
| func (m *crmLeadInfo) getTimeLayout() string { | |||
| func (m *crmdLead) getTimeLayout() string { | |||
| return "2006-01-02 15:04:05" | |||
| } | |||
| func crmFindOpenID(openID string) (info crmLeadInfo, found bool, err error) { | |||
| func crmFindOpenID(openID string) (info crmdLead, found bool, err error) { | |||
| found = false | |||
| req, err := http.NewRequest("GET", "https://c.hitxy.org.au/api/v1/Lead", nil) | |||
| if err != nil { | |||
| @@ -164,3 +164,9 @@ func crmPrepareLeadUploadHTTPHeader() (headers map[string]string) { | |||
| headers["Content-Type"] = "application/json" | |||
| return headers | |||
| } | |||
| func crmGetLeadByID(id string) (r crmdLead, err error) { | |||
| resp, err := crmGetEntity("Lead", id) | |||
| err = json.Unmarshal([]byte(resp), &r) | |||
| return | |||
| } | |||
| @@ -54,7 +54,7 @@ func TestDecodeLeadInfo(t *testing.T) { | |||
| "createdOpportunityId": null | |||
| }` | |||
| r := crmLeadInfo{} | |||
| r := crmdLead{} | |||
| err := json.Unmarshal([]byte(msg), &r) | |||
| //log.Println(r) | |||
| AssertEqual(t, err, nil, "Decode CRM Lead Json shold be correct") | |||
| @@ -104,7 +104,7 @@ func TestDecodeLeadInfo(t *testing.T) { | |||
| } | |||
| func TestCrmLeadInfoByCompleteInfo(t *testing.T) { | |||
| func TestcrmdLeadByCompleteInfo(t *testing.T) { | |||
| msg := `{ | |||
| "id": "58f4aa0682ea74bb8", | |||
| "name": "same email", | |||
| @@ -202,7 +202,7 @@ func TestCrmLeadInfoByCompleteInfo(t *testing.T) { | |||
| }, | |||
| "imagesTypes": {} | |||
| }` | |||
| r := crmLeadInfo{} | |||
| r := crmdLead{} | |||
| err := json.Unmarshal([]byte(msg), &r) | |||
| //log.Println(r) | |||
| AssertEqual(t, err, nil, "") | |||
| @@ -399,7 +399,15 @@ func TestFindOpenIDOnline(t *testing.T) { | |||
| } | |||
| func TestLeadJsonCompose(t *testing.T) { | |||
| info := crmLeadInfo{} | |||
| info := crmdLead{} | |||
| info.FirstName = "中文First" | |||
| } | |||
| func TestGetLead(t *testing.T) { | |||
| SetupConfig() | |||
| id := "595071f8450974b72" | |||
| r, err := crmGetLeadByID(id) | |||
| AssertEqual(t, err, nil, "decode json should be nil") | |||
| AssertEqual(t, r.ID, id, "Lead id should match") | |||
| } | |||
| @@ -87,7 +87,7 @@ func (in *InWechatMsg) askUserFillupBasicInfo() { | |||
| templateSendJoinCommunity(in.header.FromUserName, url, first, remark, communityName, joinDate) | |||
| } | |||
| func sendGreeting4ExistingUser(in InWechatMsg, info crmLeadInfo) { | |||
| func sendGreeting4ExistingUser(in InWechatMsg, info crmdLead) { | |||
| first := "欢迎" + info.Name + "返回澳洲校友会" | |||
| //url := "http://wechat.hitxy.org.au/profile_newly_register" | |||
| @@ -100,7 +100,7 @@ func sendGreeting4ExistingUser(in InWechatMsg, info crmLeadInfo) { | |||
| } | |||
| func (m *WechatUserInfo) registerNewUserWithInfo(in InWechatMsg) { | |||
| u := crmLeadInfo{} | |||
| u := crmdLead{} | |||
| u.FirstName = "\u0020" | |||
| u.LastName = m.NickName | |||
| u.Password = "password" | |||
| @@ -69,6 +69,17 @@ func putRAW(data []byte, targetURL string, headers map[string]string) (resp stri | |||
| return httpRaw("PUT", targetURL, data, headers) | |||
| } | |||
| func patchRAW(data []byte, targetURL string, headers map[string]string) (resp string, err error) { | |||
| return httpRaw("PATCH", targetURL, data, headers) | |||
| } | |||
| func getRAW(targetURL string, headers map[string]string) (resp string, err error) { | |||
| return httpRaw("GET", targetURL, nil, headers) | |||
| } | |||
| func deleteRAW(targetURL string, headers map[string]string) (resp string, err error) { | |||
| return httpRaw("DELETE", targetURL, nil, headers) | |||
| } | |||
| func httpRaw(httpMethod, targetURL string, data []byte, headers map[string]string) (resp string, err error) { | |||
| requestBody := io.Reader(nil) | |||
| if data != nil { | |||