diff --git a/app/models/verification/management/document.rb b/app/models/verification/management/document.rb index ee9e5462d..481073397 100644 --- a/app/models/verification/management/document.rb +++ b/app/models/verification/management/document.rb @@ -32,7 +32,7 @@ class Verification::Management::Document end def under_sixteen?(response) - 16.years.ago < string_to_date(response.date_of_birth) + 16.years.ago < response.date_of_birth end def verified? @@ -43,4 +43,4 @@ class Verification::Management::Document user.update(verified_at: Time.now) if user? end -end \ No newline at end of file +end diff --git a/app/models/verification/residence.rb b/app/models/verification/residence.rb index 0c1ed5f9a..661659a01 100644 --- a/app/models/verification/residence.rb +++ b/app/models/verification/residence.rb @@ -31,6 +31,8 @@ class Verification::Residence user.update(document_number: document_number, document_type: document_type, geozone: self.geozone, + date_of_birth: date_of_birth.to_datetime, + genre: genre, residence_verified_at: Time.now) end @@ -75,6 +77,10 @@ class Verification::Residence @census_api_response.district_code end + def genre + @census_api_response.genre + end + private def call_census_api @@ -84,7 +90,7 @@ class Verification::Residence def residency_valid? @census_api_response.valid? && @census_api_response.postal_code == postal_code && - @census_api_response.date_of_birth == date_to_string(date_of_birth) + @census_api_response.date_of_birth == date_of_birth end def clean_document_number diff --git a/db/migrate/20160411161531_add_genre_and_dob_to_users.rb b/db/migrate/20160411161531_add_genre_and_dob_to_users.rb new file mode 100644 index 000000000..aa8100fa3 --- /dev/null +++ b/db/migrate/20160411161531_add_genre_and_dob_to_users.rb @@ -0,0 +1,6 @@ +class AddGenreAndDobToUsers < ActiveRecord::Migration + def change + add_column :users, :genre, :string, index: true, limit: 10 + add_column :users, :date_of_birth, :datetime, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index e86f4db3b..cd05a0d2f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160406163649) do +ActiveRecord::Schema.define(version: 20160411161531) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -422,6 +422,8 @@ ActiveRecord::Schema.define(version: 20160406163649) do t.string "oauth_email" t.integer "geozone_id" t.string "redeemable_code" + t.string "genre", limit: 10 + t.datetime "date_of_birth" end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree diff --git a/lib/active_model/dates.rb b/lib/active_model/dates.rb index a519e3840..f9605ddbd 100644 --- a/lib/active_model/dates.rb +++ b/lib/active_model/dates.rb @@ -13,13 +13,4 @@ module ActiveModel::Dates attrs.except("#{field}(1i)", "#{field}(2i)", "#{field}(3i)") end - def date_to_string(date) - date.strftime("%d-%m-%Y") - end - - def string_to_date(value) - day, month, year = value.split("-") - Date.new(year.to_i, month.to_i, day.to_i) - end - -end \ No newline at end of file +end diff --git a/lib/census_api.rb b/lib/census_api.rb index 737f7f873..2b8c7d7a2 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -38,7 +38,10 @@ class CensusApi end def date_of_birth - data[:datos_habitante][:item][:fecha_nacimiento_string] + str = data[:datos_habitante][:item][:fecha_nacimiento_string] + day, month, year = str.match(/(\d\d?)\D(\d\d?)\D(\d\d\d?\d?)/)[1..3] + return nil unless day.present? && month.present? && year.present? + Date.new(year.to_i, month.to_i, day.to_i) end def postal_code @@ -49,6 +52,15 @@ class CensusApi data[:datos_vivienda][:item][:codigo_distrito] end + def genre # "Varón" or "Mujer" + case data[:datos_habitante][:item][:descripcion_sexo] + when "Varón" + "male" + when "Mujer" + "female" + end + end + private def data @@ -86,7 +98,7 @@ class CensusApi end def stubbed_response_body - {:get_habita_datos_response=>{:get_habita_datos_return=>{:hay_errores=>false, :datos_habitante=>{:item=>{:fecha_nacimiento_string=>"31-12-1980", :identificador_documento=>"12345678Z", }}, :datos_vivienda=>{:item=>{:codigo_postal=>"28013", :codigo_distrito=>"01"}}}}} + {get_habita_datos_response: {get_habita_datos_return: {hay_errores: false, datos_habitante: { item: {fecha_nacimiento_string: "31-12-1980", identificador_documento: "12345678Z", descripcion_sexo: "Varón" }}, datos_vivienda: {item: {codigo_postal: "28013", codigo_distrito: "01"}}}}} end def is_dni?(document_type) diff --git a/lib/tasks/users.rake b/lib/tasks/users.rake index 0ce4cdfee..18dad53bc 100644 --- a/lib/tasks/users.rake +++ b/lib/tasks/users.rake @@ -40,5 +40,23 @@ namespace :users do end end + desc "Associates demographic information to users" + task assign_demographic: :environment do + User.residence_verified.where(genre: nil).find_each do |u| + begin + response = CensusApi.new.call(u.document_type, u.document_number) + if response.valid? + u.genre = response.genre + u.date_of_birth = response.date_of_birth.to_datetime + u.save + print "." + else + print "X" + end + rescue + puts "Could not assign genre/dob for user: #{u.id}" + end + end + end end diff --git a/spec/lib/census_api_spec.rb b/spec/lib/census_api_spec.rb index a49133d23..fb14f39c5 100644 --- a/spec/lib/census_api_spec.rb +++ b/spec/lib/census_api_spec.rb @@ -29,7 +29,7 @@ describe CensusApi do describe '#call' do let(:invalid_body) { {get_habita_datos_response: {get_habita_datos_return: {datos_habitante: {}}}} } - let(:valid_body){ {get_habita_datos_response: {get_habita_datos_return: {datos_habitante: {item: {fecha_nacimiento_string: "1/1/1980"}}}}} } + let(:valid_body){ {get_habita_datos_response: {get_habita_datos_return: {datos_habitante: {item: {fecha_nacimiento_string: "1-1-1980"}}}}} } it "returns the response for the first valid variant" do allow(api).to receive(:get_response_body).with(1, "00123456").and_return(invalid_body) @@ -39,7 +39,7 @@ describe CensusApi do response = api.call(1, "123456") expect(response).to be_valid - expect(response.date_of_birth).to eq('1/1/1980') + expect(response.date_of_birth).to eq(Date.new(1980,1,1)) end it "returns the last failed response" do diff --git a/spec/models/residence_spec.rb b/spec/models/residence_spec.rb index fbc33d39c..faa81b5c3 100644 --- a/spec/models/residence_spec.rb +++ b/spec/models/residence_spec.rb @@ -85,7 +85,7 @@ describe Verification::Residence do describe "save" do - it "should store document number, document type, and geozone" do + it "should store document number, document type, geozone, date of birth and genre" do user = create(:user) residence.user = user residence.save @@ -93,6 +93,10 @@ describe Verification::Residence do user.reload expect(user.document_number).to eq('12345678Z') expect(user.document_type).to eq("1") + expect(user.date_of_birth.year).to eq(1980) + expect(user.date_of_birth.month).to eq(12) + expect(user.date_of_birth.day).to eq(31) + expect(user.genre).to eq('male') expect(user.geozone).to eq(geozone) end