Skip to content

Commit

Permalink
Merge pull request #318 from enoodle/feat/person-email-spark-verifica…
Browse files Browse the repository at this point in the history
…tion

Feat/person email spark verification
  • Loading branch information
enoodle authored Feb 10, 2019
2 parents 3837914 + b83a13b commit 9acabda
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 15 deletions.
7 changes: 7 additions & 0 deletions app/assets/stylesheets/forms.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
@extend %textOverflowEllipsis;
}

.email_valid {
background-color: lighten($c-green, 30%);
}
.email_invalid {
background-color: lighten($c-red, 30%);
}

.menu-heading {
height: 79px;
margin: 28px 0 56px 0;
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/camps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ def update
end
else
respond_to do |format|
flash.now[:alert] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}"
format.html { render :action => "edit" }
flash[:alert] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}"
format.html { redirect_to edit_camp_path(id: @camp.id, step: params[:step].to_i) }
format.json { respond_with_bip(@camp) }
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/persons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class PersonsController < ApplicationController

def export
end

def verify_email
render :json => { verified: Person.email_registered?(params[:email]) }
end
end
24 changes: 15 additions & 9 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Person < ActiveRecord::Base
has_and_belongs_to_many :roles

validates :name, presence: true
validates :email, presence: true
validates_with EmailInSpark, :on => :create

schema_validations whitelist: [:id, :created_at, :updated_at, :camp]
Expand All @@ -50,14 +51,19 @@ def self.to_csv
end

def self.email_registered?(email)
r = HTTParty.post(
URI.join(ENV['SPARK_HOST'], 'volunteers/profiles').to_s,
body: {emails: [email]}.to_json,
headers: {
token: ENV['SPARK_TOKEN'],
'Content-Type': 'application/json'
}
)
JSON.parse(r.body)[0].key? 'user_data'
begin
r = HTTParty.post(
URI.join(ENV['SPARK_HOST'], 'volunteers/profiles').to_s,
body: {emails: [email]}.to_json,
headers: {
token: ENV['SPARK_TOKEN'],
'Content-Type': 'application/json'
},
timeout: 10
)
JSON.parse(r.body)[0].key? 'user_data'
rescue
false
end
end
end
31 changes: 30 additions & 1 deletion app/views/camps/_person_fields.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
:javascript

function verify_person_email(event) {
const email_field = event.target;
if (email_field.value != '') {
verify_email(email_field.value, function (valid) {
console.log(`email is valid: ${valid}`);
if (!valid) {
email_field.classList.remove("email_valid");
email_field.classList.add("email_invalid");
} else {
email_field.classList.remove("email_invalid");
email_field.classList.add("email_valid");
}
});
}
}

function verify_email(email, callback) {
var request = new XMLHttpRequest();
request.open("POST", '/verify_email', true);
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
request.onload = function() {
callback(JSON.parse(request.response)['verified']);
};
request.send(JSON.stringify({ "email": email }));
}

- dream_admin ||= false

.nested-fields.panel.panel-default.col-md-7{ style: 'float:' + I18n.t('lang_direction')}
Expand All @@ -13,7 +42,7 @@
= f.text_field :name, class: 'form-control', readonly: true
= f.label :email
- if (@can_edit)
= f.text_field :email, class: 'form-control'
= f.text_field :email, class: 'form-control person_email', onfocusout: 'verify_person_email(event)'
- else
= f.text_field :email, class: 'form-control', readonly: true
= f.label :phone_number
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ en:
modified_at: Modified at
new_dream_is_disabled: Creating new dreams is disabled at the moment. If you think
this is a mistake please contact bureau of dreams
email_not_in_spark: This email is not registered in Spark
email_not_in_spark: Sorry, but we couldn't find a Midburn profile associated with
dont_miss_out:
banner: Don't miss out! Only %{time} left to %{action}
actions:
Expand Down
2 changes: 1 addition & 1 deletion config/locales/he.yml
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ he:

# Errors
new_dream_is_disabled: "יצירת חלומות חדשים מנוטרלת כרגע. אם אתה חושב שזאת שגיאה אנא צור קשר עם לשכת החלומות"
email_not_in_spark: "האימייל לא רשום בספארק:"
email_not_in_spark: "אופס! לא מצאנו פרופיל מידברן מקושר למייל: "

time:
am: am
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
get '/howcanihelp' => 'howcanihelp#index'

get '/people/export' => 'people#export_csv'

post '/verify_email' => 'persons#verify_email'

get '*unmatched_route' => 'application#not_found'
end
2 changes: 1 addition & 1 deletion spec/controllers/camps_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
recycling: 'recycling plan',
budgetplan: 'budgetplan plan',
cocreation: 'cocreation plan',
people_attributes: {'0' => {name: Faker::Name.name}}
people_attributes: {'0' => {name: Faker::Name.name, email: Faker::Internet.email}}
}
}

Expand Down

0 comments on commit 9acabda

Please sign in to comment.