Conceptual architectural change for CLI user app

This commit is contained in:
2015-09-07 19:10:54 +02:00
parent 4ef7ecba41
commit 1caa488524
15 changed files with 163 additions and 140 deletions
@@ -0,0 +1,7 @@
App.ButtonRemoveListNeedsPaymentComponent = Ember.Component.extend
tagName: 'button'
layoutName: 'components/button/remove-list-needs-payment'
classNames: ['remove_list_needs_payment']
classNameBindings: ['content.needs_payment:show:hide']
click: ->
@get('content').invoke 'remove_needs_payment'
@@ -115,17 +115,17 @@ App.ApplicationRoute = Ember.Route.extend
close: -> table.rollbackAttributes() close: -> table.rollbackAttributes()
events: events:
list_needs_help: (data) -> list_needs_help: (data) ->
if list = @store.getById('list', data.id) if list = @store.peekRecord('list', data.id)
list.isNeedingHelp() list.isNeedingHelp()
return if @get('globals.active_section.id') and list.get('section.id') isnt @get('globals.active_section.id') return if @get('globals.active_section.id') and list.get('section.id') isnt @get('globals.active_section.id')
@set 'globals.flash_message', t('table.needs_help.flash_message', number: list.get('table.number') || -1) @set 'globals.flash_message', t('table.needs_help.flash_message', number: list.get('table.number') || -1)
try ion.sound.play 'bell_ring' try ion.sound.play 'bell_ring'
list_needs_payment: (data) -> list_needs_payment: (data) ->
if list = @store.getById('list', data.id) if list = @store.peekRecord('list', data.id)
list.isNeedingPayment() list.isNeedingPayment()
return if @get('globals.active_section.id') and list.get('section.id') isnt @get('globals.active_section.id') return if @get('globals.active_section.id') and list.get('section.id') isnt @get('globals.active_section.id')
try ion.sound.play 'water_droplet_3' try ion.sound.play 'water_droplet_3'
list_is_paid: (data) -> list.isPaid() if list = @store.getById('list', data.id) list_is_paid: (data) -> list.isPaid() if list = @store.peekRecord('list', data.id)
list_update: (data) -> list_update: (data) ->
new_order_id = data.new_order_id new_order_id = data.new_order_id
delete data.new_order_id delete data.new_order_id
@@ -19,6 +19,7 @@ td.actions
i.fa.fa-ban.revoke i.fa.fa-ban.revoke
/= view "mark-list-helped-button" content=list /= view "mark-list-helped-button" content=list
= button-mark-list-helped content=list = button-mark-list-helped content=list
= button-remove-list-needs-payment content=list
/= view "remove-list-needs-payment" content=list /= view "remove-list-needs-payment" content=list
/= view "close-list-button" content=list /= view "close-list-button" content=list
= button-close-list content=list = button-close-list content=list
@@ -1,7 +0,0 @@
#App.RemoveListNeedsPaymentView = Ember.View.extend
# templateName: 'remove_list_needs_payment_button'
# classNames: ['remove_list_needs_payment']
# classNameBindings: ['content.needs_payment:show:hide']
# tagName: 'button'
# click: (e)->
# @get('content').then (l)->l.remove_needs_payment()
@@ -156,6 +156,12 @@ module Suppliers
head :ok head :ok
end end
def remove_needs_payment
@list = List.find_by_supplier_id_and_id(current_supplier.id, params[:id])
@list.remove_needs_payment!
head :ok
end
private private
def list_params def list_params
@@ -30,12 +30,6 @@ module Suppliers
current_supplier.mark_as_closed! current_supplier.mark_as_closed!
head :ok head :ok
end end
def remove_needs_payment
@list = List.find_by_supplier_id_and_id(current_supplier.id, params[:id])
@list.remove_needs_payment!
head :ok
end
private private
-80
View File
@@ -29,54 +29,6 @@ class UserController < Users::ApplicationController
#end #end
#end #end
# GET /user/table_info.json
# used for moving table request
# TODO wrap logic of actions
# - table_info
# - move_table
# into separate class and implement security in a non stupid way as it is now
def table_info
respond_to do |format|
format.json do
render json: json_alert('messages.table_not_found') and return unless params[:table_id].present?
@table = Table.find(params[:table_id])
res = {}
res[:occupied] = @table.occupied?
res[:reserved] = @table.reserved?
res[:supplier_closed] = @table.supplier.closed?
res[:table] = @table.attributes
if list.present?
res[:other_supplier] = true if list.supplier_id != @table.supplier_id
res[:current_table_id] = list.table_id
end
render json: res
end
end
end
# POST /user/move_table.json
# used to move the table
# TODO wrap logic of actions
# - table_info
# - move_table
# into separate class and implement security in a non stupid way as it is now
def move_table
render json: json_alert('messages.no_active_list', list_active: false) and return unless list.present?
render json: json_alert('messages.table_not_found') and return unless params[:table_id].present?
@table = Table.find(params[:table_id])
if @table.occupied?
render json: {occupied: true}
elsif @table.reserved?
render json: {reserved: true}
elsif list.supplier_id != @table.supplier_id
res[:other_supplier] = true if list.supplier_id != @table.supplier_id
res[:current_table_id] = list.table_id
else
list.move_to_table! @table
render json: {occupied: false, reserved: false}
end
end
# GET /suppliers/1/product_list # GET /suppliers/1/product_list
# GET /suppliers/1/product_list.json # GET /suppliers/1/product_list.json
def list_products def list_products
@@ -119,16 +71,6 @@ class UserController < Users::ApplicationController
end end
end end
# POST /user/join_occupied_table
def request_to_join_occupied_table
render json: json_alert('messages.table_not_found') and return unless params[:table_id].present?
@table = Table.find(params[:table_id])
if @list = @table.active_list
@list.send_table_join_request_for_user! current_user
end
render nothing: true
end
# POST /user/reject_join_request?user_id=1 # POST /user/reject_join_request?user_id=1
def reject_join_request def reject_join_request
render js: '' and return unless params[:user_id].present? render js: '' and return unless params[:user_id].present?
@@ -203,28 +145,6 @@ class UserController < Users::ApplicationController
render json: {} render json: {}
end end
# POST /user/needs_help.json
def needs_help
respond_to do |format|
format.json do
render json: json_alert('messages.no_active_list', list_active: false) and return unless list.present?
list.needs_help!
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: json_alert('messages.no_active_list', list_active: false) and return unless list.present?
list.needs_payment!
render json: list.as_json.merge(list_active: list.active?)
end
end
end
## ##
# Displays the closed lists of the user # Displays the closed lists of the user
# GET /user/list_history # GET /user/list_history
@@ -34,5 +34,14 @@ module Users
obj[:ok] = true unless obj.has_key?(:ok) obj[:ok] = true unless obj.has_key?(:ok)
obj obj
end end
def new_order_product_orders
case params[:product_orders]
when String then JSON.parse(params[:product_orders]) rescue []
when Hash then params[:product_orders].values
else
[]
end
end
end end
end end
+47
View File
@@ -29,5 +29,52 @@ module Users
render json: {}, status: :not_found and return unless @list.present? && Array.wrap(@list.user_ids).include?(current_user.id) render json: {}, status: :not_found and return unless @list.present? && Array.wrap(@list.user_ids).include?(current_user.id)
render json: JSONAPI::Serializer.serialize(@list, serializer: Users::ListSerializer, include: %w[supplier users]) render json: JSONAPI::Serializer.serialize(@list, serializer: Users::ListSerializer, include: %w[supplier users])
end end
# POST /user/list_needs_payment.json
def needs_payment
@list = active_list
render json: json_alert('messages.no_active_list', list_active: false) and return unless @list.try(:id).to_s == params[:id]
@list.needs_payment!
render json: JSONAPI::Serializer.serialize(@list, serializer: Users::ListSerializer)
end
# POST /user/lists/:id/move_table.json?table_id=....
# used to move the table
# TODO wrap logic of actions
# - table_info
# - move_table
# into separate class and implement security in a non stupid way as it is now
def move_to_table
render json: json_alert('messages.no_active_list', list_active: false) and return unless active_list.present?
render json: json_alert('messages.table_not_found') and return unless params[:table_id].present?
@table = Table.find(params[:table_id])
if @table.occupied?
render json: {occupied: true}
elsif @table.reserved?
render json: {reserved: true}
elsif active_list.supplier_id != @table.supplier_id
res[:other_supplier] = true if active_list.supplier_id != @table.supplier_id
res[:current_table_id] = active_list.table_id
else
active_list.move_to_table! @table
render json: {occupied: false, reserved: false}
end
end
# Used by the user Ember app
# please also look and mirror this action in the tables controller
# POST /user/lists/:id/order_products
def order_products
res = {}
res[:supplier_closed] = active_list.supplier.closed?
unless res[:supplier_closed]
# Create new list
active_list.place_order product_orders: new_order_product_orders, user: current_user, first_order: false
# do not add payload, since relevant data is added through message bus (Faye)
#res[:payload] = JSONAPI::Serializer.serialize(order, serializer: Users::OrderSerializer, include: %w[product_orders])
end
render json: res
end
end end
end end
@@ -11,31 +11,5 @@ module Users
render json: JSONAPI::Serializer.serialize(orders, serializer: Users::OrderSerializer, include: %w[list product_orders product_orders.order], is_collection: true) render json: JSONAPI::Serializer.serialize(orders, serializer: Users::OrderSerializer, include: %w[list product_orders product_orders.order], is_collection: true)
end end
# Used by the user Ember app
# POST /user/orders
def create
# render json: {}, status: :unprocessable_entity and return unless params[:order].present? && params[:order][:product_orders].present?
if list = current_user.active_list
render json: {}, status: :not_acceptable and return unless list.supplier.open?
else
#TODO: More logic about creating a new list!!!!!, usercontroller table_info should become irrelevant
#NOTE: security bug here!!!!!!
# - supplier.open?
# - etc....
render json: {}, status: :unprocessable_entity and return unless params[:table_id].present?
table = Table.find(params[:table_id])
render json: {}, status: :not_acceptable and return unless table.supplier.open?
if table.occupied?
#render json: json_alert('messages.table_is_occupied', location: :join_occupied_table, location_params: {table_id: @table.id})
render json: {}, status: :not_acceptable and return
end
list = List.from_table( table, current_user )
end
order = list.place_order product_orders: params[:product_orders], user: current_user
render json: order, serializer: Users::OrderSerializer
#render nothing: true
end
end end
end end
@@ -1,5 +1,6 @@
module Users module Users
class TablesController < Users::ApplicationController class TablesController < Users::ApplicationController
respond_to :json
def show def show
@table = Table.find(params[:id]) @table = Table.find(params[:id])
render json: JSONAPI::Serializer.serialize(@table, serializer: Users::TableSerializer) render json: JSONAPI::Serializer.serialize(@table, serializer: Users::TableSerializer)
@@ -11,5 +12,65 @@ module Users
supplier.product_categories.include_relations(:products) supplier.product_categories.include_relations(:products)
render json: JSONAPI::Serializer.serialize(supplier, serializer: Users::SupplierSerializer, include: %w[product_categories product_categories.products product_categories.supplier]) render json: JSONAPI::Serializer.serialize(supplier, serializer: Users::SupplierSerializer, include: %w[product_categories product_categories.products product_categories.supplier])
end end
# POST /tables/:id/needs_help.json
def needs_help
#@table = Table.find(params[:id])
render json: json_alert('messages.no_active_list', list_active: false) and return unless active_list.present?
active_list.needs_help!
#render json: JSONAPI::Serializer.serialize(@table, serializer: Users::TableSerializer)
render json: {}
end
# POST /user/tables/:id/join
def join
render json: json_alert('messages.table_not_found') and return unless params[:id].present?
@table = Table.find(params[:id])
if @list = @table.active_list
@list.send_table_join_request_for_user! current_user
end
head :ok
end
# GET /user/table_info.json
# used for moving table request
# TODO wrap logic of actions
# - table_info
# - move_table
# into separate class and implement security in a non stupid way as it is now
def status_info
render json: json_alert('messages.table_not_found') and return unless params[:table_id].present?
table = Table.find(params[:table_id])
res = {}
res[:occupied] = table.occupied?
res[:reserved] = table.reserved?
res[:supplier_closed] = table.supplier.closed?
res[:table] = table.attributes
if active_list.present?
res[:other_supplier] = true if active_list.supplier_id != table.supplier_id
res[:current_table_id] = active_list.table_id
res[:current_list_id] = active_list.id
end
render json: res
end
# Used by the user Ember app
# POST /user/tables/:id/order_products
def order_products
table = Table.find(params[:id])
res = {}
res[:occupied] = table.occupied?
res[:reserved] = table.reserved?
res[:supplier_closed] = table.supplier.closed?
res[:no_product_orders] = true unless product_orders = new_order_product_orders.presence
unless res[:occupied] or res[:supplier_closed] or res[:no_product_orders]
# Create new list
list = List.from_table( table, current_user )
res[:active_list_id] = list.id # used to set the active list in the app
order = list.place_order product_orders: product_orders, user: current_user, first_order: true
res[:payload] = JSONAPI::Serializer.serialize(order, serializer: Users::OrderSerializer, include: %w[list product_orders])
end
render json: res
end
end end
end end
+10 -6
View File
@@ -282,14 +282,14 @@ class List
state == 'closed' state == 'closed'
end end
def place_order(product_orders: [], user: nil, employee: nil) def place_order(product_orders: [], user: nil, employee: nil, first_order: true)
return false unless product_orders.any? return false unless product_orders.any?
order = Order.create list: self, supplier: supplier, user: user, employee: employee, section_id: section_id, table_id: table_id order = Order.create list: self, supplier: supplier, user: user, employee: employee, section_id: section_id, table_id: table_id
return unless order.id return unless order.id
orders_placed_count = supplier.increment_orders_placed_count! orders_placed_count = supplier.increment_orders_placed_count!
loaded_products = self.class.database.load_document product_orders.map{|po| po['product_id']} loaded_products = self.class.database.load_document product_orders.map{|po| po['product_id'] || po['product']}
product_orders.each do |product_order| product_orders.each do |product_order|
next unless product = loaded_products.find{|p| p.id == product_order['product_id']} # to get the price next unless product = loaded_products.find{|p| p.id == product_order['product_id'] or p.id == product_order['product']} # to get the price and current product name
quantity = product_order['quantity'].to_i quantity = product_order['quantity'].to_i
if quantity > 0 if quantity > 0
ProductOrder.create( ProductOrder.create(
@@ -306,12 +306,16 @@ class List
save save
# broadcast_users 'new_order', order: order.with_products_as_json, total_amount: price if first_order
broadcast_users 'new_order', Users::OrderSerializer.new(order).as_json
# broadcast_users 'orders_placed_count', count: orders_placed_count # broadcast_users 'orders_placed_count', count: orders_placed_count
broadcast_supplier supplier.id, 'list_update', Supplier::ListSerializer.new(self).as_json.merge(new_order_id: order.id) broadcast_supplier supplier.id, 'list_update', Supplier::ListSerializer.new(self).as_json.merge(new_order_id: order.id)
# broadcast_supplier supplier.id, 'new_order', OrderSerializer.new(order) # broadcast_supplier supplier.id, 'new_order', OrderSerializer.new(order)
else
# broadcast_users 'new_order', order: order.with_products_as_json, total_amount: price
user_payload = JSONAPI::Serializer.serialize(order, serializer: Users::OrderSerializer, include: %w[product_orders])
broadcast_users 'new_order', user_payload
broadcast_supplier supplier.id, 'list_update', Supplier::ListSerializer.new(self).as_json.merge(new_order_id: order.id)
end
broadcast_supplier supplier.id, 'orders_placed_count', count: orders_placed_count broadcast_supplier supplier.id, 'orders_placed_count', count: orders_placed_count
order order
end end
+2 -2
View File
@@ -6,9 +6,9 @@ class Users::ListSerializer
:is_paid, :price, :closed_at :is_paid, :price, :closed_at
has_many :users, serializer: Users::UserSerializer has_many :users, serializer: Users::UserSerializer
has_many(:orders, serializer: OrderSerializer){} has_many(:orders, serializer: Users::OrderSerializer){}
has_one :supplier, serializer: Users::SupplierSerializer has_one :supplier, serializer: Users::SupplierSerializer
has_one :table has_one :table, serializer: Users::TableSerializer
def include_orders? def include_orders?
@_include_linkages.any?{|link| link =~ /orders/} @_include_linkages.any?{|link| link =~ /orders/}
+14 -7
View File
@@ -57,18 +57,18 @@ Qwaiter::Application.routes.draw do
#get '/user' => 'user#index' #get '/user' => 'user#index'
#get '/user/active_list(.:format)' => 'user#active_list', as: :user_active_list #get '/user/active_list(.:format)' => 'user#active_list', as: :user_active_list
#get '/user/list_info' => 'user#list_info', as: :user_list_info, via: [:get, :options] #get '/user/list_info' => 'user#list_info', as: :user_list_info, via: [:get, :options]
post '/user/needs_help' => 'user#needs_help', as: :user_needs_help #post '/user/needs_help' => 'user#needs_help', as: :user_needs_help
post '/user/list_needs_payment' => 'user#list_needs_payment', as: :user_list_needs_payment #post '/user/list_needs_payment' => 'user#list_needs_payment', as: :user_list_needs_payment
#post '/user/create_list' => 'user#create_list', as: :user_create_list #post '/user/create_list' => 'user#create_list', as: :user_create_list
#get '/user/list_products' => 'user#list_products', as: :user_list_products #get '/user/list_products' => 'user#list_products', as: :user_list_products
#get '/user/list_products_for_table' => 'user#list_products_for_table', as: :user_list_products_for_table #get '/user/list_products_for_table' => 'user#list_products_for_table', as: :user_list_products_for_table
#get '/user/list_history' => 'user#list_history', as: :user_list_history #get '/user/list_history' => 'user#list_history', as: :user_list_history
#get '/user/history_list' => 'user#history_list', as: :user_history_list #get '/user/history_list' => 'user#history_list', as: :user_history_list
#post '/user/order_selected_products' => 'user#order_selected_products', as: :user_order_selected_products #post '/user/order_selected_products' => 'user#order_selected_products', as: :user_order_selected_products
post '/user/move_table' => 'user#move_table', as: :user_move_table #post '/user/move_table' => 'user#move_table', as: :user_move_table
get '/user/table_info' => 'user#table_info', as: :user_table_info #get '/user/table_info' => 'user#table_info', as: :user_table_info
# get '/user/join_occupied_table' => 'user#join_occupied_table', as: :user_join_occupied_table # get '/user/join_occupied_table' => 'user#join_occupied_table', as: :user_join_occupied_table
post '/user/join_occupied_table' => 'user#request_to_join_occupied_table' #post '/user/join_occupied_table' => 'user#request_to_join_occupied_table'
post '/user/reject_join_request' => 'user#reject_join_request' post '/user/reject_join_request' => 'user#reject_join_request'
post '/user/approve_join_request' => 'user#approve_join_request' post '/user/approve_join_request' => 'user#approve_join_request'
@@ -86,13 +86,20 @@ Qwaiter::Application.routes.draw do
end end
member do member do
get :table get :table
post :needs_payment
post :move_to_table
post :order_products
end end
resources :orders, only: [:index] resources :orders, only: [:index]
end end
resources :orders, only: [:create] #resources :orders, only: [:create]
resources :tables, only: [:show] do resources :tables do
member do member do
post :needs_help
post :join
post :order_products
get :status_info
get :supplier get :supplier
end end
end end