Commit 22e84d3a authored by Rafael Ravedutti's avatar Rafael Ravedutti

Add more information on citizen upload logs

Signed-off-by: Rafael Ravedutti's avatarRafael Ravedutti <rrlmachado@inf.ufpr.br>
parent 3a8e2cc4
Pipeline #16709 passed with stages
in 4 minutes and 8 seconds
......@@ -7,10 +7,7 @@ class CitizenUpload < ApplicationRecord
has_attached_file :log,
path: "/data/citizen_upload/:id/log.csv"
# Validates format of logs
# validates_attachment_content_type :log,
# :content_type => ['text/csv']
# Do not validate format of logs
do_not_validate_attachment_file_type :log
# @params params [ActionController::Parameters] Parameters for searching
......
......@@ -31,15 +31,6 @@ class CitizenUploadWorker
status: 1 # parsing content
)
# Parse citizens from the CSV data
citizens = CSV.parse(content).map { |row| Hash[columns.zip(row)] }
# Remove headers
citizens = citizens.drop(1)
# Number of citizens to be uploaded
upload_size = citizens.length
# Line number starts with one
line_number = 1
# Hash with errors
......@@ -49,6 +40,41 @@ class CitizenUploadWorker
# Buffer containing accounts to create
account_to_create = Array.new
begin
# Parse citizens from the CSV data
citizens = CSV.parse(content).map { |row| Hash[columns.zip(row)] }
# Remove headers
citizens = citizens.drop(1)
# Number of citizens to be uploaded
upload_size = citizens.length
rescue
# Set citizens to nil
citizens = []
# Upload size is zero
upload_size = 1
# Add parsing error to log
errors.push([
0,
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"Could not parse CSV file!"
])
end
# Update task status to in progress
CitizenUpload.update(
upload_id,
......@@ -70,16 +96,34 @@ class CitizenUploadWorker
provider: "cpf"
})
# Create default password for current citizen
account.password = citizen.birth_date.strftime('%d%m%y')
# Citizen remaining info is added when .valid? method is called
if citizen.valid? and account.valid?
if citizen.valid?
# Create default password for current citizen
account.password = citizen.birth_date.strftime('%d%m%y')
end
# Check if account is valid
if account.valid?
# Check for permissions on the citizen to be added
if permission != "adm_c3sl" and citizen.city_id != city_id
# If there was a permission error, store it in the errors hash
# errors.push("%d,Permission denied for this city" % [line_number])
errors.push([line_number, "Permission denied for this city"])
errors.push([
line_number,
citizen_params[:name],
citizen_params[:cpf],
citizen_params[:rg],
citizen_params[:birth_date],
citizen_params[:cep],
citizen_params[:address_number],
citizen_params[:address_complement],
citizen_params[:phone1],
citizen_params[:phone2],
citizen_params[:email],
citizen_params[:pcd],
citizen_params[:note],
"Permission denied for this city"
])
else
# Add valid citizen with complete info to to_create array
inst = [
......@@ -118,8 +162,22 @@ class CitizenUploadWorker
# Go through error messages
citizen.errors.full_messages.each do |message|
# Add current error in the list of errors
# errors.push("%d,%s" % [line_number, message])
errors.push([line_number, message])
errors.push([
line_number,
citizen_params[:name],
citizen_params[:cpf],
citizen_params[:rg],
citizen_params[:birth_date],
citizen_params[:cep],
citizen_params[:address_number],
citizen_params[:address_complement],
citizen_params[:phone1],
citizen_params[:phone2],
citizen_params[:email],
citizen_params[:pcd],
citizen_params[:note],
message
])
end
end
......@@ -187,8 +245,16 @@ class CitizenUploadWorker
# Get CSV path
path = "#{Rails.root.to_s}/tmp/citizen_uploads/#{upload_id}.csv"
# Open log CSV file for writing the log
CSV.open(path, "wb") do |csv|
csv << ["Line", "Error Message"]
# Add headers to log
csv << [
"Linha", "Nome", "CPF", "RG", "Data de Nascimento", "CEP",
"Numero", "Complemento", "Telefone 1", "Telefone 2", "E-mail",
"Deficiencia", "Observacao", "Erro"
]
# Go through the errors to add them into the log
errors.each do |error|
csv << error
end
......
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