Skip to content

Instantly share code, notes, and snippets.

@humphriesjm
Created May 22, 2019 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humphriesjm/93217345f351295821db40345051c038 to your computer and use it in GitHub Desktop.
Save humphriesjm/93217345f351295821db40345051c038 to your computer and use it in GitHub Desktop.
Send pushes to all who received a campaign tile and didn't dismiss it
# -------------------------------------------------------------
# SEND PUSHES TO ALL WHO RECEIVED A TILE AND DIDN'T DISMISS IT
# -------------------------------------------------------------
# `days_threshold` should be just more than 1 cycle of the campaign's delivery time. example: if a campaign is sent each tuesday, and it's the following tuesday, set `days_threshold` to 8
def send_pushes_to_recipients_of_campaign(campaign_id, days_threshold=8)
message = "📈 Rising Seller Scores 📈 Set a Next Step for 1 of these high priority contacts and you're sure to stay on top of your follow up."
push_name = "increasing_sscores_1wk_nudge"
date_name = "05_22_2019"
# specify the campaign to get user recipients from
campaign = Campaign.find(campaign_id)
users = User.not_admin.live_customers
recipients_count = 0
recipients = []
users.each do |user|
user_has_tile = !user.tiles.where({campaign: campaign_id}).order("created_at DESC").limit(1).empty?
next if !user_has_tile
tile = user.tiles.where({campaign: campaign_id}).order("created_at DESC").limit(1).first
# ensure the tile was published recently enough
# ALSO ensure the tile is not dismissed
if user_has_tile && tile.published_at >= days_threshold.days.ago && !tile.dismissed_at?
recipients << user.id
recipients_count += 1
if tile.id.nil?
puts "tile.id (#{tile.id}) for user (#{user.id}) is nil"
# ExpoNotificationService.new.notify(user, message: message, reason: 1, data: {"push_name": push_name, "date_name": date_name})
else
ExpoNotificationService.new.notify(user, message: message, reason: :carousel, data: {"tileId": tile.id, "push_name": push_name, "date_name": date_name})
end
end
end
puts "number of recipients: #{recipients_count}"
return recipients
end
campaign_id = 646
send_pushes_to_recipients_of_campaign(campaign_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment