diff --git a/app/controllers/contact_us_conversations_controller.rb b/app/controllers/contact_us_conversations_controller.rb index ce67033..46479d6 100644 --- a/app/controllers/contact_us_conversations_controller.rb +++ b/app/controllers/contact_us_conversations_controller.rb @@ -1,71 +1,68 @@ class ContactUsConversationsController < ApplicationController skip_before_action :verify_authenticity_token + before_action :basic_api_access, only: [:index] + before_action :user_data_access, only: [:conversa_usuario, :adicionar_conversa, :detalhar_conversa, :atualizar_conversa] def index - if params[:school_initials].present? - if params[:was_answered] == true - conversations = ContactUsConversation.all.where(school_initials: params[:school_initials], was_answered: true).order("updated_at") - else - conversations = ContactUsConversation.all.where(school_initials: params[:school_initials], was_answered: false).order("created_at") - end - if params[:limit].present? - hash_conversation = conversations.page(params[:page]).per(params[:limit]).map do |c| - { + if params[:was_answered] == true + conversations = ContactUsConversation.where(school_id: @school.id, was_answered: true).order("updated_at") + else + conversations = ContactUsConversation.where(school_id: @school.id, was_answered: false).order("created_at") + end + if params[:limit].present? + hash_conversation = conversations.page(params[:page]).per(params[:limit]).map do |c| + { 'id_conversa' => c.id, 'titulo' => c.title, 'cpf' => c.cpf, 'data_mensagem' => c.created_at.to_date, 'situacao' => c.was_answered - } - end - render json:hash_conversation, status: 200, quantidade_mensagem: conversations.size - else - hash_conversation = conversations.page(params[:page]).map do |c| - { + } + end + render json: hash_conversation, status: 200, quantidade_mensagem: conversations.size + else + hash_conversation = conversations.page(params[:page]).map do |c| + { 'id_conversa' => c.id, 'titulo' => c.title, 'cpf' => c.cpf, 'data_mensagem' => c.created_at.to_date, 'situacao' => c.was_answered - } - end - render json:hash_conversation, status: 200, quantidade_mensagem: conversations.size + } end - else - render status: 400, json: { - message: "Identifique a escola" - }.to_json + render json: hash_conversation, status: 200, quantidade_mensagem: conversations.size end end def conversa_usuario - user = User.find_by(cpf: params[:cpf]) + user = User.find_by(cpf: @api[:cpf]) @conversation = user.contact_us_conversations.all hash_conversation_user = @conversation.map do |c| { - 'id_conversa' => c.id, - 'titulo' => c.title, - 'cpf' => c.cpf, - 'data_mensagem' => c.created_at, - 'situacao' => c.was_answered + 'id_conversa' => c.id, + 'titulo' => c.title, + 'cpf' => c.cpf, + 'data_mensagem' => c.created_at, + 'situacao' => c.was_answered } end if @conversation.empty? render status: 400, json: { - message: "Não existem conversas." + message: "Não existem conversas." }.to_json else - render json:hash_conversation_user + render json: hash_conversation_user end - rescue StandardError => e - render status: 400, json: { + rescue StandardError => e + render status: 400, json: { message: "Não existe usuário com este cpf." - }.to_json + }.to_json end - def adicionar_conversa + def adicionar_conversa # TODO apenas com a chave do usuário ou o admin também pode adicionar? if params[:conversation_id].present? # Se a conversa já existir - @conversation = ContactUsConversation.find(params[:conversation_id]) + user = User.find_by(cpf: @api[:cpf]) + @conversation = ContactUsConversation.find_by(id: params[:conversation_id], user: user) if params[:is_student] != false puts params[:is_student] @conversation.update(:was_answered => false) @@ -73,92 +70,115 @@ def adicionar_conversa @conversation.update(:was_answered => true) end else # Conversa ainda não existe - user=User.find_by(cpf: params[:cpf]) + user = User.find_by(cpf: @api[:cpf]) if user.nil? return render status: 404, json: { - message: "Ocorreu um erro! CPF não encontrado no sistema." + message: "Ocorreu um erro! CPF não encontrado no sistema." }.to_json end - if params[:school_initials].present? == true and user.present? + if params[:school_initials].present? == true and user.present? # TODO enteender como isso pode ser utilizado com o usuário @conversation = user.contact_us_conversations.create(contact_us_conversation_params.merge(was_answered: false)) else return render status: 400, json: { - message: "Ocorreu um erro! Conversa ou escola não identificada." + message: "Ocorreu um erro! Conversa ou escola não identificada." }.to_json end end @conversation.contact_us_message.create(contact_us_message_params) if @conversation.save render status: 200, json: { - message: "Conversa criada com sucesso", + message: "Conversa criada com sucesso", }.to_json else render status: 400, json: { - message: "Ocorreu um erro" + message: "Ocorreu um erro" }.to_json end - rescue StandardError => e - render status: 400, json: { + rescue StandardError => e + render status: 400, json: { message: "Não existe usuário com este cpf." - }.to_json + }.to_json end def detalhar_conversa - @conversation = ContactUsConversation.where(id: params[:conversation_id]).first - if @conversation.present? - messages = @conversation.contact_us_message.all.order(:created_at) - hash_messages_school = messages.map do |c| - { - 'data_mensagem' => c.created_at, - 'cpf' => c.cpf, - 'aluno' => c.is_student, - 'texto_mensagem' => c.description - } - end - if messages.empty? + user = User.find_by(cpf: @api[:cpf]) + if user.present? + @conversation = user.role.id == 1 ? ContactUsConversation.find_by(id: params[:conversation_id]) : ContactUsConversation.find_by(id: params[:conversation_id], user: user) + if @conversation.present? + messages = @conversation.contact_us_message.all.order(:created_at) + hash_messages_school = messages.map do |c| + { + 'data_mensagem' => c.created_at, + 'cpf' => c.cpf, + 'aluno' => c.is_student, + 'texto_mensagem' => c.description + } + end + if messages.empty? + render status: 400, json: { + message: "Não existem mensagens nessa conversa." + }.to_json + else + render json: hash_messages_school + end + else render status: 400, json: { - message: "Não existem mensagens nessa conversa." + message: "Conversa não encontrada." }.to_json - else - render json:hash_messages_school end else render status: 400, json: { - message: "Conversa não encontrada." + message: "Conversa não encontrada." }.to_json end end # Método não utilizado atualmente def atualizar_conversa - @conversation = ContactUsConversation.where(id: params[:conversation_id]).first - if @conversation.update(contact_us_conversation_params) - render status: 200, json: { - message: "Conversa atualizada com sucesso", - }.to_json + user = User.find_by(cpf: @api[:cpf]) + if user.present? + @conversation = user.role.id == 1 ? ContactUsConversation.find_by(id: params[:conversation_id]) : ContactUsConversation.find_by(id: params[:conversation_id], user: user) + if @conversation.update(contact_us_conversation_params) + render status: 200, json: { + message: "Conversa atualizada com sucesso", + }.to_json + else + render status: 400, json: { + message: "Ocorreu um erro" + }.to_json + end else render status: 400, json: { - message: "Ocorreu um erro" + message: "Ocorreu um erro" }.to_json end end def apagar_conversa - @conversation = ContactUsConversation.where(id: params[:conversation_id]).first - if @conversation.present? - @conversation.destroy - render json:@conversation + user = User.find_by(cpf: @api[:cpf]) + if user.present? + @conversation = user.role.id == 1 ? ContactUsConversation.find_by(id: params[:conversation_id]) : ContactUsConversation.find_by(id: params[:conversation_id], user: user) + if @conversation.present? + @conversation.destroy + render json: @conversation + else + render status: 400, json: { + message: "Conversa não encontrada" + }.to_json + end else render status: 400, json: { - message: "Conversa não encontrada" + message: "Conversa não encontrada" }.to_json end end private + def contact_us_conversation_params params.permit(:type_conversation, :title, :school_initials, :course_id, :course_category_id, :description, :cpf, :name, :email, :user_id) end + def contact_us_message_params params.permit(:user_id, :cpf, :name, :email, :phone, :description, :is_student) end diff --git a/app/controllers/course_registrations_controller.rb b/app/controllers/course_registrations_controller.rb index 68ed73c..7d7492f 100644 --- a/app/controllers/course_registrations_controller.rb +++ b/app/controllers/course_registrations_controller.rb @@ -1,5 +1,6 @@ class CourseRegistrationsController < ApplicationController skip_before_action :verify_authenticity_token + before_action :admin_access, only: [:cursos_destaque] #before_action :user_data_access def new @@ -172,24 +173,17 @@ def confirmacao_matricula end def cursos_destaque - api_key = ApiAccess.find_by(key: params[:key]) - if api_key.present? - cursos = CourseRegistration.group(:course_id).count(:course_id).sort_by {|k, v| -v}[0..8] + cursos = CourseRegistration.group(:course_id).count(:course_id).sort_by { |k, v| -v }[0..8] - cursos_ids = [] - cursos.each do |id, c| - cursos_ids.push(id) - end + cursos_ids = [] + cursos.each do |id, c| + cursos_ids.push(id) + end - coursos_destaque = Course.where(id: cursos_ids) + coursos_destaque = Course.where(id: cursos_ids) - render status: 200, json: { - coursos_destaque: coursos_destaque, - }.to_json - else - render status: 400, json: { - erro: 'Chave não encontrada' - }.to_json - end + render status: 200, json: { + coursos_destaque: coursos_destaque, + }.to_json end end