Skip to Content Skip to Search
Methods
R
U

Class Public methods

rewardful_get(page:)

# File lib/tasks/src/update_referred_customers_task.rb, line 24
def rewardful_get(page:)
  rewardful_api_key = ENV.fetch("REWARDFUL_API_KEY", "")
  res = RestClient.get(
    "https://api.getrewardful.com/v1/referrals?conversion_state=Conversion&limit=100&page=#{page}",
    {Authorization: "Bearer #{rewardful_api_key}"}
  )
  JSON.parse(res.body)
end

run!()

# File lib/tasks/src/update_referred_customers_task.rb, line 3
def run!
  update_affiliate_referrals(page: 1)
end

update_affiliate_referrals(page:)

# File lib/tasks/src/update_referred_customers_task.rb, line 7
def update_affiliate_referrals(page:)
  body = rewardful_get(page:)

  emails = body["data"].map { |item| item["customer"]["email"] }
  update_metadata(emails:)

  np = body["pagination"]["next_page"]
  update_affiliate_referrals(page: np) if np
end

update_metadata(emails:)

# File lib/tasks/src/update_referred_customers_task.rb, line 17
def update_metadata(emails:)
  customers = DB::Site.memberspace.members.where(email: emails)
  customers.update_all(metadata: {affiliate_referral: true})
  p "======= Updating the following customers ======="
  p customers.map { |customer| customer.email }
end