Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Publish events when responding a survey
Browse files Browse the repository at this point in the history
* Assigning member to action by akid or email
  • Loading branch information
rodrei committed Oct 11, 2016
1 parent 8f93131 commit fe36c1b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -100,7 +100,7 @@ gem 'config'
# SEO and to improve page targeting for A/B testing using Optimizely.
gem 'metamagic'
gem 'jwt'
gem 'actionkit_connector', github: 'SumOfUs/actionkit_connector', branch: 'master'
gem 'actionkit_connector', github: 'SumOfUs/actionkit_connector', branch: 'update-petition-action'

gem 'timecop'

Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
@@ -1,9 +1,9 @@
GIT
remote: git://github.com/SumOfUs/actionkit_connector.git
revision: dd773c527ee1e411332c04bfb155ac7730837d83
branch: master
revision: 4606d62c9df2dbd25ff6dfbfbd4734a5429ba741
branch: update-petition-action
specs:
actionkit_connector (0.4.3)
actionkit_connector (0.4.4)
httparty (~> 0.13)

GIT
Expand Down
2 changes: 1 addition & 1 deletion app/services/action_builder.rb
Expand Up @@ -12,7 +12,7 @@ def build_action(extra_attrs = {})
subscribed_member: subscribed_member
}.merge(extra_attrs))

ActionQueue::Pusher.push(action)
ActionQueue::Pusher.push(:new_action, action)
Analytics::Page.increment(page.id, new_member: subscribed_member)

action
Expand Down
42 changes: 28 additions & 14 deletions app/services/action_queue.rb
Expand Up @@ -31,10 +31,10 @@ def meta
title: page.title,
uri: "/a/#{page.slug}",
slug: page.slug,
first_name: member.first_name,
last_name: member.last_name,
first_name: member.try(:first_name),
last_name: member.try(:last_name),
created_at: @action.created_at,
country: country(member.country),
country: country(member.try(:country)),
action_id: @action.id,
subscribed_member: @action.subscribed_member
}
Expand Down Expand Up @@ -76,20 +76,25 @@ def action_fields
end

class Pusher
def self.push(action)
if action.donation
if action.form_data.fetch('payment_provider', '').inquiry.go_cardless?
DirectDebitAction.push(action)
def self.push(event, action)
case event
when :new_action
if action.donation
if action.form_data.fetch('payment_provider', '').inquiry.go_cardless?
NewDirectDebitAction.push(action)
else
NewDonationAction.push(action)
end
else
DonationAction.push(action)
NewPetitionAction.push(action)
end
else
PetitionAction.push(action)
when :new_survey_response
NewSurveyResponse.push(action)
end
end
end

class PetitionAction
class NewPetitionAction
include Donatable
include Enqueable

Expand All @@ -109,12 +114,21 @@ def payload
}
.merge(@action.form_data)
.merge(UserLanguageISO.for(page.language))
.tap { |params| params[:country] = country(member.country) if member.country.present? }
.tap { |params| params[:country] = country(member.country) if member.try(:country).present? }
}.deep_symbolize_keys
end
end

class DirectDebitAction
#TODO Refactor
class NewSurveyResponse < NewPetitionAction
def payload
super.tap do |p|
p[:type] = 'new_survey_response'
end
end
end

class NewDirectDebitAction
include Enqueable
include Donatable

Expand Down Expand Up @@ -190,7 +204,7 @@ def get_payment_account
end
end

class DonationAction
class NewDonationAction
include Enqueable
include Donatable

Expand Down
22 changes: 18 additions & 4 deletions app/services/manage_survey_response.rb
Expand Up @@ -11,7 +11,9 @@ def initialize(page:, form:, params:, action_id:nil)
def run
if @validator.valid?
assign_member
update_member
update_action
publish_event
end

@validator.valid?
Expand All @@ -23,9 +25,21 @@ def errors

private

def sanitize_params(params, form)
params.slice(*form.form_elements.map(&:name).map(&:to_sym))
end

def assign_member
if @params[:email].present?
@action.member = Member.find_or_create_by!(email: @params[:email])
@action.member ||= if @params[:akid].present?
Member.find_by_akid(@params[:akid])
elsif @params[:email].present?
Member.find_or_initialize_by(email: @params[:email])
end
end

def update_member
if @action.member.present?
MemberUpdater.run(@action.member, @params)
end
end

Expand All @@ -34,7 +48,7 @@ def update_action
@action.save!
end

def sanitize_params(params, form)
params.slice(*form.form_elements.map(&:name).map(&:to_sym))
def publish_event
ActionQueue::Pusher.push(:new_survey_response, @action)
end
end
10 changes: 7 additions & 3 deletions lib/champaign_queue.rb
@@ -1,19 +1,23 @@
# frozen_string_literal: true
require_relative 'champaign_queue/clients/sqs'
require_relative 'champaign_queue/clients/direct'
require_relative 'champaign_queue/clients/http'

module ChampaignQueue
extend self

def push(opts)
if Rails.env.production?
if Rails.env.production? || Settings.publish_champaign_events
client.push(opts)
else
false
end
end

def client
Clients::Sqs
if Settings.champaign_queue_client == 'http'
Clients::HTTP
else
Clients::Sqs
end
end
end

0 comments on commit fe36c1b

Please sign in to comment.