diff --git a/app/controllers/camps_controller.rb b/app/controllers/camps_controller.rb index 584b88d2..54e5236c 100644 --- a/app/controllers/camps_controller.rb +++ b/app/controllers/camps_controller.rb @@ -56,7 +56,7 @@ def create return redirect_to edit_camp_path(id: @camp.id) end - flash.now[:notice] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}" + flash.now[:alert] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}" render :new end diff --git a/app/models/person.rb b/app/models/person.rb index b644a0d7..f0822647 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -14,6 +14,14 @@ # needs_early_arrival :boolean # +class EmailInSpark < ActiveModel::Validator + def validate(record) + if !Person.email_registered?(record.email) + record.errors[:email] << I18n.t("email_not_in_spark") + " #{record.email}" + end + end +end + class Person < ActiveRecord::Base CSV_ATTRIBUTES = %w{email name phone_number responsibilities dream_name dream_number}.freeze @@ -21,6 +29,7 @@ class Person < ActiveRecord::Base has_and_belongs_to_many :roles validates :name, presence: true + validates_with EmailInSpark, :on => :create schema_validations whitelist: [:id, :created_at, :updated_at, :camp] @@ -39,4 +48,16 @@ def self.to_csv end end 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' + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index ddacf424..ca5f99d4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -590,6 +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 dont_miss_out: banner: Don't miss out! Only %{time} left to %{action} actions: diff --git a/config/locales/he.yml b/config/locales/he.yml index d77fa43f..993027ed 100755 --- a/config/locales/he.yml +++ b/config/locales/he.yml @@ -564,6 +564,7 @@ he: # Errors new_dream_is_disabled: "יצירת חלומות חדשים מנוטרלת כרגע. אם אתה חושב שזאת שגיאה אנא צור קשר עם לשכת החלומות" + email_not_in_spark: "האימייל לא רשום בספארק:" time: am: am diff --git a/spec/controllers/camps_controller_spec.rb b/spec/controllers/camps_controller_spec.rb index c0708382..aa0cbb0b 100644 --- a/spec/controllers/camps_controller_spec.rb +++ b/spec/controllers/camps_controller_spec.rb @@ -53,7 +53,7 @@ it 'won\'t create a camp without manager' do post :create, camp: camp_attributes.merge!(people_attributes: {}) expect(Camp.find_by_contact_name(camp_leader)).to be_nil - expect(flash[:notice]).to_not be_nil + expect(flash[:alert]).to_not be_nil end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 61e27385..0d02943f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -29,6 +29,11 @@ # ...rather than: # # => "be bigger than 2" expectations.include_chain_clauses_in_custom_matcher_descriptions = true + + # Dont do email verification in tests: + config.before(:each) do + allow(Person).to receive(:email_registered?).and_return(true) + end end # rspec-mocks config goes here. You can use an alternate test double