From ae4ca595266dac20fcb599fb9b2f0ae1e2d9cc64 Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Fri, 11 Mar 2016 16:27:04 -0500 Subject: [PATCH] fix(sidebar): keep trying on 202s Also fixed a bug where the email could come back with different capitalization causing a cache miss --- .../lib/clearbit-data-source.coffee | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/internal_packages/participant-profile/lib/clearbit-data-source.coffee b/internal_packages/participant-profile/lib/clearbit-data-source.coffee index 42a773566..c43499dab 100644 --- a/internal_packages/participant-profile/lib/clearbit-data-source.coffee +++ b/internal_packages/participant-profile/lib/clearbit-data-source.coffee @@ -1,11 +1,15 @@ # This file is in coffeescript just to use the existential operator! {AccountStore, EdgehillAPI} = require 'nylas-exports' +MAX_RETRY = 10 + module.exports = class ClearbitDataSource clearbitAPI: -> return "https://person.clearbit.com/v2/combined" - find: ({email}) -> + find: ({email, tryCount}) -> + if (tryCount ? 0) >= MAX_RETRY + return Promise.resolve(null) tok = AccountStore.tokenForAccountId(AccountStore.accounts()[0].id) new Promise (resolve, reject) => EdgehillAPI.request @@ -14,34 +18,41 @@ module.exports = class ClearbitDataSource pass: "" path: "/proxy/clearbit/#{@clearbitAPI()}/find?email=#{email}", success: (body, response) => - resolve(@parseResponse(body, response, email)) + @parseResponse(body, response, email, tryCount).then(resolve).catch(reject) error: reject # The clearbit -> Nylas adapater - parseResponse: (body={}, response, requestedEmail) -> - # This means it's in the process of fetching. Return null so we don't - # cache and try again. - if response.statusCode isnt 200 - return null + parseResponse: (body={}, response, requestedEmail, tryCount=0) => + new Promise (resolve, reject) => + # This means it's in the process of fetching. Return null so we don't + # cache and try again. + if response.statusCode is 202 + setTimeout => + @find({email: requestedEmail, tryCount: tryCount+1}).then(resolve).catch(reject) + , 1000 + return + else if response.statusCode isnt 200 + resolve(null) + return - person = body.person + person = body.person - # This means there was no data about the person available. Return a - # valid, but empty object for us to cache. This can happen when we - # have company data, but no personal data. - if not person - person = {email: requestedEmail} + # This means there was no data about the person available. Return a + # valid, but empty object for us to cache. This can happen when we + # have company data, but no personal data. + if not person + person = {email: requestedEmail} - return { - cacheDate: Date.now() - email: person.email # Used as checksum - bio: person.bio ? person.twitter?.bio ? person.aboutme?.bio, - location: person.location ? person.geo?.city - currentTitle: person.employment?.title, - currentEmployer: person.employment?.name, - profilePhotoUrl: person.avatar, - socialProfiles: @_socialProfiles(person) - } + resolve({ + cacheDate: Date.now() + email: requestedEmail # Used as checksum + bio: person.bio ? person.twitter?.bio ? person.aboutme?.bio, + location: person.location ? person.geo?.city + currentTitle: person.employment?.title, + currentEmployer: person.employment?.name, + profilePhotoUrl: person.avatar, + socialProfiles: @_socialProfiles(person) + }) _socialProfiles: (person={}) -> profiles = {}