Ejemplo n.º 1
0
func fetchMemberByEmail(forceApi *force.ForceApi, email string) (interface{}, int) {

	list := &ContactQueryResponse{}
	err := forceApi.Query("select id, name, firstname, lastname, email, mailingcountry, topcoder_handle__c, topcoder_last_login__c, topcoder_member_status__c, topcoder_user_id__c from contact where email = '"+email+"' limit 1", list)
	if err != nil {
		panic(err)
	} else {

		// found member in salesforce
		if len(list.Records) == 1 {

			id, _ := strconv.ParseInt(list.Records[0].Topcoder_User_Id__c, 0, 64)
			member := map[string]interface{}{
				"id":        id,
				"firstname": list.Records[0].Firstname,
				"lastname":  list.Records[0].Lastname,
				"name":      list.Records[0].Name,
				"handle":    list.Records[0].Topcoder_Handle__c,
				"email":     list.Records[0].Email,
				"country":   list.Records[0].MailingCountry,
				"status":    list.Records[0].Topcoder_Member_Status__c,
				"lastLogin": list.Records[0].Topcoder_Last_Login__c,
			}
			return member, 200

			// not found in sfdc, try topcoder
		} else {
			return map[string]interface{}{}, 404
		}
	}

}
Ejemplo n.º 2
0
func fetchMemberByHandle(forceApi *force.ForceApi, handle string) (interface{}, int) {

	list := &ContactQueryResponse{}
	err := forceApi.Query("select id, name, firstname, lastname, email, mailingcountry, topcoder_handle__c, topcoder_last_login__c, topcoder_member_status__c, topcoder_user_id__c from contact where topcoder_handle__c = '"+handle+"' limit 1", list)
	if err != nil {
		panic(err)
	} else {

		// found member in salesforce
		if len(list.Records) == 1 {

			id, _ := strconv.ParseInt(list.Records[0].Topcoder_User_Id__c, 0, 64)
			member := map[string]interface{}{
				"id":        id,
				"firstname": list.Records[0].Firstname,
				"lastname":  list.Records[0].Lastname,
				"name":      list.Records[0].Name,
				"handle":    list.Records[0].Topcoder_Handle__c,
				"email":     list.Records[0].Email,
				"country":   list.Records[0].MailingCountry,
				"status":    list.Records[0].Topcoder_Member_Status__c,
				"lastLogin": list.Records[0].Topcoder_Last_Login__c,
			}
			return member, 200

			// not found in sfdc, try topcoder
		} else {

			// call the topcoder api
			resp, err := http.Get(os.Getenv("TC_ENDPOINT") + "/" + handle + "?apiKey=" + os.Getenv("TC_API_KEY"))
			if err != nil {
				panic(err)
			}
			// if 200 then we are good and send the email and handle back
			if resp.StatusCode == 200 {
				defer resp.Body.Close()
				body, _ := ioutil.ReadAll(resp.Body)
				byt := []byte(string(body))
				var dat map[string]interface{}
				if err := json.Unmarshal(byt, &dat); err != nil {
					panic(err)
				}
				return map[string]interface{}{
					"handle": handle,
					"email":  dat["email"],
				}, 200
			} else {
				return map[string]interface{}{}, resp.StatusCode
			}

		}
	}

}