Files
mozo-backend/app/controllers/user_controller.rb
T

317 lines
9.5 KiB
Ruby

class UserController < ApplicationController
before_filter :allow_mobile
before_filter :user_authentication #, except: :obtain_token
layout 'phone'
def user_authentication
if params[:auth_token].present?
user = User.find_by_authentication_token(params[:auth_token])
sign_in user if user
sign_out current_user if current_user && !user # Other token attempt of logged in user
else
authenticate_user!
end
unless current_user.present?
respond_to do |format|
format.html {redirect_to new_user_session_path}
format.json {render json: json_response(ok: false, status: 401)}
end
end
end
def authenticate_user_wrapper!
authenticate_user! unless request.format.html?
authenticate_user! if params[:action] == 'obtain_token'
end
def obtain_token
end
alias :list :active_list
def allow_mobile
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Request-Method'] = '*'
headers['Access-Control-Allow-Headers'] = '*'
end
def home
handle_message_params
end
# POST /user/create_list {table_id: 1234}
#DEPRICATED
def create_list
@table = Table.find(params[:table_id])
if @table.occupied?
respond_to do |format|
format.html { redirect_to root_path, alert: t('table.is_occupied') }
format.json { render json: js_alert(t('messages.table_is_occupied'))}
end
else
if @list = List.from_table( @table, current_user )
end
respond_to do |format|
format.html { redirect_to user_list_products_path }
format.json { render json: js_notice('table_created')}
end
end
end
def table_info
@table = Table.find(params[:table_id])
res = {}
res[:occupied] = @table.occupied?
res[:reserved] = @table.reserved?
res[:supplier_closed] = @table.supplier.closed?
if list.present?
res[:other_supplier] = true if list.supplier_id != @table.supplier_id
res[:current_table_id] = list.table_id
end
respond_to do |format|
format.json do
render json: res
end
end
end
# GET /suppliers/1/product_list
# GET /suppliers/1/product_list.json
def list_products
@supplier = list.supplier
respond_to do |format|
format.html do
handle_message_params
end
format.json do
products = list.supplier.products
products.include_relation(:product_categories)
products.sort_by!{|p| p.product_category.try(:position) || 90000}
h = products.inject({table_number: list.table_number, supplier_name: @supplier.name}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
render json: h
end
end
end
def join_occupied_table
@table = Table.find(params[:table_id])
end
def request_to_join_occupied_table
@table = Table.find(params[:table_id])
if @list = @table.active_list
unless @list.join_requests.include?(current_user.id)
@list.join_requests << current_user.id
@list.is_dirty
@list.save
end
end
render nothing: true
end
def reject_join_request
return unless params[:user_id]
if list && list.join_requests.include?(params[:user_id])
list.join_requests.delete(params[:user_id])
list.is_dirty
list.save
end
render js: ''
end
def approve_join_request
return unless params[:user_id]
@user = User.find(params[:user_id])
if list && list.join_requests.include?(params[:user_id])
list.join_requests.delete(params[:user_id])
@user.active_list_id = list.id
list.add_user(@user)
@user.save
list.is_dirty
list.save
end
render nothing: true
end
# POST /user/check_table_join_status table_id:12345
def check_table_join_status
@table = Table.find(params[:table_id])
if @list = @table.active_list
if @list.user_ids.include?(current_user.id)
render json: {approved: true}
elsif @list.join_requests.include?(current_user.id)
render json: {waiting: true}
else
render json: {rejected: true}
end
else
render json: {rejected: true}
end
end
def list_products_for_table
@table = Table.find(params[:table_id])
respond_to do |format|
format.html do
end
format.json do
products = @table.supplier.products
products.include_relation(:product_categories)
products.sort_by!{|p| p.product_category.try(:position) || 90000}
h = products.inject({
table_number: @table.number,
supplier_name: @table.supplier.name,
has_occupied_info: true,
is_occupied: @table.occupied?
}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h}
render json: h
end
end
end
# GET /user/current_list.json
# Information about the currently active list
# This information includes detailed order information
def active_list
respond_to do |format|
format.html do
redirect_to(root_path, alert: t('messages.there_is_no_list_active')) and return unless list.present?
end
format.json do
render json: js_alert(t('messages.the_list_has_been_closed')) and return unless list.present?
render json: list.with_orders_and_join_requests_as_json.merge(supplier_name: list.supplier.name)
end
end
end
# GET /user/list_info.json
# Information about the currently active list
# Fast version to verify wether the is is still currently active
# for handle_active_list
def list_info
respond_to do |format|
format.json do
if list.present?
if !list.try(:active?)
current_user.list_is_closed!
render json: json_response(list_active: false)
return
else
list_obj = list.as_json.merge(list_active: list.active? ).merge(list.join_requests_as_json)
render json: json_response(list_obj)
end
else
render json: json_response(not_present: true)
end
end
end
end
# POST /user/needs_help.json
def needs_help
respond_to do |format|
format.json do
render json: {list_active: false} and return unless list.present?
list.needs_help = true
list.save
render json: list.as_json.merge(list_active: list.active?)
end
end
end
# POST /user/list_needs_payment.json
def list_needs_payment
respond_to do |format|
format.json do
render json: {list_active: false} and return unless list.present?
list.needs_payment = true
list.save
render json: list.as_json.merge(list_active: list.active?)
end
end
end
##
# Displays the closed lists of the user
# GET /user/list_history
def list_history
@lists = List.for_user(current_user, page: params[:page], per_page: params[:per_page].presence || 14)
@lists.include_relation(:supplier)
respond_to do |format|
format.html {}
format.json do
render json: @lists.inject(lists: [], current_page: @lists.current_page, num_pages: @lists.num_pages, total_count: @lists.total_count){|h, l| h[:lists] << l.as_json.merge(supplier_name: l.supplier.name); h}
end
end
end
##
# Displays a closed list of the user
# GET /user/list_history/:list_id
def history_list
@list = List.find(params[:list_id])
if params[:list_closed].present? && current_user.active_list_id == @list.id
current_user.list_is_closed!
flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human)
end
redirect_to user_root_path, alert: t('messages.illegal_history_list_attempt') and return unless @list.user_ids.include?(current_user.id)
respond_to do |format|
format.html do
end
format.json do
render json: @list.with_orders_as_json.merge(supplier_name: @list.supplier.name)
end
end
end
def order_selected_products
if list.present?
@list = list
else
@table = Table.find(params[:table_id])
if @table.occupied?
#TODO handle placint order on occupied table
else
if @list = List.from_table( @table, current_user )
else
#TODO handle second list creation for user
end
end
end
respond_to do |format|
format.html do
redirect_to(root_path, alert: t('messages.cannot_order_on_non_active_list')) and return unless @list.active?
@list.place_order current_user, params[:products]
redirect_to root_path, notice: t('messages.order_is_placed')
end
format.js do
render js: js_alert(t('messages.cannot_order_on_non_active_list')) and return unless @list.active?
@list.place_order current_user, params[:products]
render js: js_notice( t('messages.order_is_placed'), location: :active_list)
end
end
end
def move_table
return unless list.present?
@table = Table.find(params[:table_id])
if @table.occupied?
render json: {occupied: true}
else
list.move_to_table @table
render json: {occupied: false}
end
end
private
def handle_message_params
flash.now[:notice] = t('messages.the_list_has_been_closed', list: List.model_name.human) if params[:list_closed].present?
flash.now[:notice] = t("messages.#{params[:message]}", list: List.model_name.human, supplier: Supplier.model_name.human) if params[:message].present? && params[:message] =~ /^\w+$/
end
def json_response(obj = {})
obj[:ok] = true unless obj.has_key?(:ok)
obj
end
end