Commit 714521ee authored by Matheus Horstmann's avatar Matheus Horstmann 🐴

Merge branch 'develop' into 'master'

Develop

See merge request !74
parents 949c4b16 6793f3ff
Pipeline #16914 passed with stages
in 1 minute and 41 seconds
......@@ -2,3 +2,5 @@ AGENDADOR_SECRET_KEY_BASE=6e58e1f0e806642a44a504672e22665606100838a0c80c8b3b6766
AGENDADOR_API_DB_USER=agendador
AGENDADOR_API_DB_PASSWORD=123mudar
AGENDADOR_REDIS_PASSWORD=123mudar
MAIL_USERNAME: agendador
MAIL_PASSWORD: 123mudar
\ No newline at end of file
module Api::V1
module Api::V1
class Accounts::PasswordsController < DeviseTokenAuth::PasswordsController
# this action is responsible for generating password reset tokens and
......@@ -11,20 +11,20 @@ module Api::V1
# fall back to default value if provided
@redirect_url ||= DeviseTokenAuth.default_password_reset_url
unless @redirect_url
return render_create_error_missing_redirect_url
end
@resource = Account.find_by(uid: params[:cpf])
if @resource.citizen.email.nil? or @resource.citizen.email.empty?
@resource = Account.find_by(uid: params[:cpf])
if @resource.nil? or @resource.citizen.email.nil? or @resource.citizen.email.empty?
render json: {
errors: ["User #{params[:cpf]} does not have an email registered."]
}, status: 422
return
end
@resource.email = @resource.citizen.email
@resource.email = @resource.citizen.email
@resource.save
@email = @resource.email
......
......@@ -32,10 +32,21 @@ module Api::V1
# Sort uploads by date in descending order
@uploads = @uploads.order("created_at DESC")
# Uploads with professional id
uploads_with_professional = @uploads.as_json
# Add professional id to uploads
uploads_with_professional.each do |upload|
upload_citizen = Citizen.find(upload['citizen_id'])
professional = Professional.where(account_id: upload_citizen.account_id).first
upload[:professional_id] = professional.id
end
# Create response object
response = Hash.new
response[:num_entries] = @uploads.total_count
response[:entries] = @uploads.as_json
response[:entries] = uploads_with_professional
# Render uploads in JSON format
render json: response.to_json
......@@ -133,6 +144,22 @@ module Api::V1
def destroy
end
# GET /citizen_uploads/example_file_xls
def example_xls
filename = "#{Rails.root.to_s}/public/citizen_upload_example.xls"
content_type = "application/xls"
send_file filename, :type => content_type, :x_sendfile => true
end
# GET /citizen_uploads/example_file_ods
def example_ods
filename = "#{Rails.root.to_s}/public/citizen_upload_example.ods"
content_type = "application/vnd.oasis.opendocument.spreadsheet"
send_file filename, :type => content_type, :x_sendfile => true
end
private
# Use callbacks to share common setup or constraints between actions.
......
......@@ -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([
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],
line_number,
"Permission denied for this city"
])
else
# Add valid citizen with complete info to to_create array
inst = [
......@@ -115,11 +159,37 @@ class CitizenUploadWorker
end
else
# Full messages string
full_messages_string = nil
# 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])
if full_messages_string.present?
full_messages_string = "#{full_messages_string} / #{message}"
else
full_messages_string = message
end
end
# If there are errors, insert them into the log
if full_messages_string.present?
# Add current citizen in the list of errors
errors.push([
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],
line_number,
full_messages_string
])
end
end
......@@ -187,8 +257,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 << [
"Nome", "CPF", "RG", "Data de Nascimento", "CEP",
"Numero", "Complemento", "Telefone 1", "Telefone 2", "E-mail",
"Deficiencia", "Observacao", "Linha", "Erros"
]
# Go through the errors to add them into the log
errors.each do |error|
csv << error
end
......
......@@ -36,6 +36,9 @@ Rails.application.routes.draw do
end
end
get "citizen_uploads/example_ods" => "citizen_uploads#example_ods"
get "citizen_uploads/example_xls" => "citizen_uploads#example_xls"
resources :citizen_uploads
resources :occupations
......
......@@ -38,6 +38,9 @@ services:
SECRET_KEY_BASE: ${AGENDADOR_SECRET_KEY_BASE}
PGHOST: agendador-postgres
REDISHOST: agendador-redis
MAIL_USERNAME: ${MAIL_USERNAME}
MAIL_PASSWORD: ${MAIL_PASSWORD}
ports:
- '3000:3000'
depends_on:
......
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