negos_app/app/controllers/public/customer_messages_controller.rb
2017-06-06 16:15:40 +02:00

145 lines
3.9 KiB
Ruby

class Public::CustomerMessagesController < ApplicationController
layout "public"
before_filter :auth_customer
def index
@no_search = true
@customer_messages = CustomerMessage.where("destinataire_id = ? or expediteur_id = ?",current_customer.id, current_customer.id).where("parent_id is null")
@customer_messages = @customer_messages.order("last_message_at DESC, created_at DESC").all
reseaux_ids= current_customer.reseauxes.pluck(:id)
#current_customer.reseauxes.each do |r|
# reseaux_ids += r.children.pluck(:id)
#end
@conversations = Conversation.joins(:thing_conversations).where("(thing_conversations.thing_type = ? and thing_conversations.thing_id = ?) or (thing_conversations.thing_type = ? and thing_conversations.thing_id IN (?))", "Customer", current_customer.id, "Reseaux", reseaux_ids ).uniq
end
def show
@customer_message = CustomerMessage.where("destinataire_id = ? or expediteur_id = ?",current_customer.id, current_customer.id).find(params[:id])
@customer_message.readed = true
@customer_message.save
end
def answer
@parent_customer_message = CustomerMessage.where("destinataire_id = ? or expediteur_id = ?",current_customer.id, current_customer.id).find(params[:id])
@customer_message = CustomerMessage.new()
@customer_message.expediteur = current_customer
@customer_message.destinataire = @parent_customer_message.expediteur
end
def answer_save
@parent_customer_message = CustomerMessage.where("destinataire_id = ? or expediteur_id = ?",current_customer.id, current_customer.id).find(params[:id])
@customer_message = CustomerMessage.new(params.require(:customer_message).permit!)
@customer_message.expediteur = current_customer
if @parent_customer_message.expediteur == current_customer
@customer_message.destinataire = @parent_customer_message.destinataire
else
@customer_message.destinataire = @parent_customer_message.expediteur
end
@customer_message.parent_id = @parent_customer_message.id
if @customer_message.save
@parent_customer_message.last_message_at = Time.now
@parent_customer_message.save
else
render :action => :answer
end
end
def new
@customer_message = CustomerMessage.new(:conversation_id => params[:conversation_id], :thing_type => params[:thing_type], :thing_id => params[:thing_id])
end
def edit
end
def create
@customer_message = CustomerMessage.new(params.require(:customer_message).permit!)
@customer_message.expediteur = current_customer
if @customer_message.save
if !@customer_message.conversation
@conversation = Conversation.create()
@conversation.thing_conversations << ThingConversation.create(:thing_type => @customer_message.thing_type, :thing_id => @customer_message.thing_id)
@conversation.thing_conversations << ThingConversation.create(:thing_type => "Customer", :thing_id => current_customer.id)
@conversation.customer_messages << @customer_message
else
end
redirect_to public_customer_messages_path
else
render :action => :new
end
end
def update
@annonce = current_customer.annonces.find(params[:id])
respond_to do |format|
if @annonce.update_attributes(params.require(:annonce).permit!)
format.html {
redirect_to public_my_account_path
}
format.js
else
format.html { render :action => "edit" }
format.js { "" }
end
end
end
def destroy
#@annonce = current_customer.annonces.find(params[:id])
#@annonce.enabled = nil
#@annonce.save
#redirect_to public_my_account_path, :notice => "Votre annonce a bien été désactivée"
end
end