Commit 882e7585 authored by Rafael Ravedutti's avatar Rafael Ravedutti

Treat special cases for CEPs

Signed-off-by: Rafael Ravedutti's avatarRafael Ravedutti <rrlmachado@inf.ufpr.br>
parent ee6a646b
Pipeline #17074 passed with stages
in 3 minutes and 41 seconds
......@@ -40,6 +40,7 @@ class Address < ApplicationRecord
address = Address.new(
zipcode: result[:zipcode],
address: result[:address],
number: result[:number],
neighborhood: result[:neighborhood],
city_id: get_city_id(zipcode),
state_id: get_state_id(zipcode),
......@@ -49,6 +50,7 @@ class Address < ApplicationRecord
else
address.zipcode = result[:zipcode]
address.address = result[:address]
address.number = result[:number]
address.neighborhood = result[:neighborhood]
address.city_id = get_city_id(zipcode)
address.state_id = get_state_id(zipcode)
......
......@@ -40,7 +40,7 @@ class Citizen < ApplicationRecord
validates_length_of :rg, maximum: 13
validates_length_of :address_number, within: 0..10, allow_blank: true
validates_numericality_of :address_number, only_integer: true,
validates_numericality_of :address_number, only_integer: true,
allow_blank: true
validates_format_of :name,
......@@ -64,16 +64,16 @@ class Citizen < ApplicationRecord
before_validation :set_address
# Scopes #
scope :all_active, -> {
where(active: true, responsible_id: nil)
scope :all_active, -> {
where(active: true, responsible_id: nil)
}
scope :local, ->(city_id) {
where(city_id: city_id)
scope :local, ->(city_id) {
where(city_id: city_id)
}
scope :dependants, -> {
where(responsible_id: self.id)
scope :dependants, -> {
where(responsible_id: self.id)
}
......@@ -170,7 +170,7 @@ class Citizen < ApplicationRecord
# @params params [ActionController::Parameters] Parameters for searching
# @params npage [String] number of page to be returned
# @params permission [String] Permission of current user
# @return [ActiveRecords] filtered citizens
# @return [ActiveRecords] filtered citizens
def self.filter(params, npage, permission)
return search(search_params(params, permission), npage)
end
......@@ -192,7 +192,7 @@ class Citizen < ApplicationRecord
filter = {"name" => "name_cont", "cpf" => "cpf_eq", "s" => "s"}
end
return filter_search_params(params, filter, sortable)
return filter_search_params(params, filter, sortable)
end
......@@ -220,6 +220,10 @@ class Citizen < ApplicationRecord
self.address_street = address.address
end
if not address.number.nil?
self.address_number = address.number
end
if not address.neighborhood.empty?
self.neighborhood = address.neighborhood
end
......
......@@ -149,6 +149,10 @@ class CityHall < ApplicationRecord
self.city_id = address.city_id
self.address_street = address.address
self.neighborhood = address.neighborhood
if not address.number.nil?
self.address_number = address.number
end
else
self.errors["cep"] << "#{self.cep} is invalid."
return false
......
......@@ -33,7 +33,7 @@ class ServicePlace < ApplicationRecord
validates_presence_of :cep
validates_length_of :name, maximum: 255
validates_length_of :address_number,
validates_length_of :address_number,
within: 0..10,
allow_blank: true
......@@ -48,7 +48,7 @@ class ServicePlace < ApplicationRecord
where(active: true)
}
scope :local_city_hall, -> (city_hall_id) {
scope :local_city_hall, -> (city_hall_id) {
where(city_hall_id: city_hall_id)
}
......@@ -59,7 +59,7 @@ class ServicePlace < ApplicationRecord
delegate :state_name, to: :city
# Get every available schedule from the current service_place given a
# Get every available schedule from the current service_place given a
# service_type
#
# @param service_type_id [Integer] id from specified service_type
......@@ -83,14 +83,14 @@ class ServicePlace < ApplicationRecord
end
# Returns json response to index service_types
# Returns json response to index service_types
# @return [Json] response
def self.index_response
self.all.as_json(only: [
:id, :name, :cep, :active, :neighborhood, :phone1
], methods: %w(city_hall_name city_name state_name))
end
# @return [Json] detailed service_type's data
def complete_info_response
......@@ -118,7 +118,7 @@ class ServicePlace < ApplicationRecord
:ibge_code, :created_at, :updated_at
])})
.merge({address: address.as_json(except: [
:created_at, :updated_at, :state_id, :city_id
:created_at, :updated_at, :state_id, :city_id, :number
])})
end
......@@ -126,14 +126,14 @@ class ServicePlace < ApplicationRecord
# Get every service_place and its schedules given a service_type
#
# @param service_type [ServiceType] specified ServiceType
# @return [Json] array containing the reponses from get_schedules for every
# @return [Json] array containing the reponses from get_schedules for every
# service_place
def self.get_schedule_response(service_type)
service_places = ServicePlace.where(active: true)
.find(service_type.service_place_ids)
# Add reponses from get_schedules obtained from every service_place
# containing the specified service_type
# containing the specified service_type
response = [].as_json
for i in service_places
response << i.get_schedules(service_type.id)
......@@ -162,46 +162,46 @@ class ServicePlace < ApplicationRecord
case permission
when "adm_c3sl"
sortable = [
"name",
"cep",
"city_hall_name",
"active",
"name",
"cep",
"city_hall_name",
"active",
"neighborhood"
]
filter = {
"name" => "name_cont",
"active" => "active_eq",
"name" => "name_cont",
"active" => "active_eq",
"neighborhood" => "neighborhood_cont",
"cep" => "cep_cont",
"city_hall_id" => "city_hall_id_eq",
"cep" => "cep_cont",
"city_hall_id" => "city_hall_id_eq",
"s" => "s"
}
when "adm_prefeitura"
sortable = [
"name",
"name",
"cep",
"active",
"active",
"neighborhood"
]
filter = {
"name" => "name_cont",
"active" => "active_eq",
"name" => "name_cont",
"active" => "active_eq",
"neighborhood" => "neighborhood_cont",
"cep" => "cep_cont",
"cep" => "cep_cont",
"role" => "professionals_service_places_role_eq",
"s" => "s"
}
end
return filter_search_params(params, filter, sortable)
return filter_search_params(params, filter, sortable)
end
# Method called when creating a service_place. It associates
# Method called when creating a service_place. It associates
# the address to the service place given a cep
def create_service_place
address = Address.get_address(self.cep)
......@@ -223,6 +223,10 @@ class ServicePlace < ApplicationRecord
self.address_street = address.address
self.neighborhood = address.neighborhood
if not address.number.nil?
self.address_number = address.number
end
else
self.errors["cep"] << "#{self.cep} is invalid."
return false
......
class AddNumberToAddresses < ActiveRecord::Migration[5.0]
def change
add_column :addresses, :number, :integer
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180511112959) do
ActiveRecord::Schema.define(version: 20180803163556) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -52,6 +52,7 @@ ActiveRecord::Schema.define(version: 20180511112959) do
t.datetime "updated_at", null: false
t.integer "city_id"
t.integer "state_id"
t.integer "number"
t.index ["city_id"], name: "index_addresses_on_city_id", using: :btree
t.index ["state_id"], name: "index_addresses_on_state_id", using: :btree
end
......
......@@ -16,6 +16,10 @@
require 'curb'
require 'nokogiri'
def is_number? string
true if Float(string) rescue false
end
module Agendador
module CEP
class Finder
......@@ -72,6 +76,34 @@ module Agendador
address[:neighborhood] = doc.xpath("//bairro").text
address[:city] = doc.xpath("//cidade").text
address[:state] = doc.xpath("//estado").text
address[:number] = nil
# Get the last three digits from the zipcode
zipcode_suffix = address[:zipcode].split(//).last(3).join
# Check specific cases of CEP codes that may have a number included
# in its address field, the following address shows more details:
# https://www.correios.com.br/precisa-de-ajuda/
# o-que-e-cep-e-por-que-usa-lo/estrutura-do-cep
if zipcode_suffix.to_i >= 900 and zipcode_suffix.to_i <= 969
# Split the address by comma
address_splitted = address[:address].split(',')
# If there is more elements in the array, so there's a comma in the
# address (probably separating address and number)
if address_splitted.length > 1
# Get possible number from address
number = address_splitted.last.strip
# Verify if number is numeric, if it is, change the address to
# the part of address without the number and set the number
# field in the address
if is_number?(number)
address[:address] = address_splitted[0].strip
address[:number] = number.to_i
end
end
end
end
return address
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment