Mostly enriching supplier's user info with number of lists finished at the restaurant

This commit is contained in:
2020-03-06 19:27:44 -05:00
parent f1ef618dae
commit eda3fc280b
7 changed files with 66 additions and 8 deletions
@@ -19,7 +19,8 @@ module Suppliers
else else
@lists = List.for_supplier(current_supplier, page: params[:page], per_page: params[:per_page] || 25) @lists = List.for_supplier(current_supplier, page: params[:page], per_page: params[:per_page] || 25)
end end
@lists.include_relation(:table, :users, orders: {user: nil, product_orders: :product}) @lists.include_relations(:table, :users, orders: {user: nil, product_orders: :product})
List.enrich_users_number_of_lists_at_supplier current_supplier.id, @lists
render json: @lists, include: %w[ render json: @lists, include: %w[
orders orders
@@ -31,8 +31,10 @@ module Suppliers
def switch_to def switch_to
@switch_supplier = Supplier.find(params[:id]) @switch_supplier = Supplier.find(params[:id])
session[:supplier_id] = params[:id] if @switch_supplier.employee_ids.include? current_employee.id #TODO sessions currently not working, just tokens
redirect_to supplier_root_path(other: '/my-account') session[:supplier_id] = @switch_supplier.id if @switch_supplier.employee_ids.include? current_employee.id
render json: {ok: true}
#redirect_to supplier_root_path(other: '/my-account')
end end
def mark_as_open def mark_as_open
+53
View File
@@ -34,6 +34,14 @@ class List
emit([doc.supplier_id, doc.table_id], 1); emit([doc.supplier_id, doc.table_id], 1);
} }
}|, reduce_function: '_sum' }|, reduce_function: '_sum'
view :supplier_user_lists, type: :raw, map_function: %|function(doc){
if(doc.ruby_class == 'List' && doc.user_ids){
doc.user_ids.forEach(function(user_id){
emit([doc.supplier_id, user_id], 1);
})
}
}|, reduce_function: '_sum'
#view :active_by_table_id_view, type: :custom, map_function: %|function(doc){ #view :active_by_table_id_view, type: :custom, map_function: %|function(doc){
#if(doc.ruby_class == 'List' && doc.state == 'active'){ #if(doc.ruby_class == 'List' && doc.state == 'active'){
#emit(doc.table_id, 1); #emit(doc.table_id, 1);
@@ -60,6 +68,7 @@ class List
} }
}|, reduce_function: '_sum' }|, reduce_function: '_sum'
#TODO: depricate
view :active_users_view, type: :custom, map_function: %|function(doc){ view :active_users_view, type: :custom, map_function: %|function(doc){
if(doc.ruby_class == 'List' && doc.state == 'active'){ if(doc.ruby_class == 'List' && doc.state == 'active'){
doc.user_ids && doc.user_ids.forEach(function(uid){ doc.user_ids && doc.user_ids.forEach(function(uid){
@@ -94,6 +103,49 @@ class List
list list
end end
# return object in form:
# {
# supplier1_id: {
# user_1_id: user_1_at_supplier_1_count,
# user_2_id: user_1_at_supplier_1_count
# },
# supplier2_id: {
# user_1_id: user_1_at_supplier_2_count
# user_3_id: user_3_at_supplier_2_count
# }
# }
# or the supplier subset if provided with a supplier id
def self.get_suppliers_users_count(lists, supplier_id: nil)
unique_supplier_user_ids = Set.new
lists.each do |list|
list.user_ids.each do |uid|
unique_supplier_user_ids << [list.supplier_id, uid]
end
end
spec = supplier_user_lists(group: true, keys: unique_supplier_user_ids)
query_result = database.view spec
aggregate_result = {}
Array.wrap(query_result['rows']).each do |row|
next if supplier_id and row['key'][0] != supplier_id # ensure 1 supplier
aggregate_result[row['key'][0]] ||= {}
aggregate_result[row['key'][0]][row['key'][1]] = row['value']
end
if supplier_id
aggregate_result[supplier_id] || {}
else
aggregate_result
end
end
def self.enrich_users_number_of_lists_at_supplier(supplier_id, lists)
counts = get_suppliers_users_count(lists, supplier_id: supplier_id)
lists.each do |list|
list.users.each do |user|
user.number_of_lists_at_supplier = counts[user.id] || 1
end
end
end
# Create, a list given a table and a employee # Create, a list given a table and a employee
def self.from_table_by_employee table, employee def self.from_table_by_employee table, employee
unless list = table.active_list unless list = table.active_list
@@ -316,6 +368,7 @@ class List
if first_order if first_order
self.class.enrich_users_number_of_lists_at_supplier supplier_id, [self]
# broadcast_users 'orders_placed_count', count: orders_placed_count # broadcast_users 'orders_placed_count', count: orders_placed_count
broadcast_supplier supplier.id, 'list_update', Suppliers::ListSerializer.new(self).as_json.merge(new_order_id: order.id) broadcast_supplier supplier.id, 'list_update', Suppliers::ListSerializer.new(self).as_json.merge(new_order_id: order.id)
supplier_payload = JSONAPI::Serializer.serialize(self, serializer: Suppliers::ListSerializer, include: %w[ supplier_payload = JSONAPI::Serializer.serialize(self, serializer: Suppliers::ListSerializer, include: %w[
+3 -1
View File
@@ -1,6 +1,7 @@
class User class User
include SimplyStored::Couch include SimplyStored::Couch
include ActiveModel::SerializerSupport include ActiveModel::SerializerSupport
attr_accessor :number_of_lists_at_supplier
property :name property :name
property :active_list_id property :active_list_id
@@ -93,7 +94,8 @@ class User
# This is the user name as it is shown to the supplier # This is the user name as it is shown to the supplier
def supplier_name def supplier_name
auth_data['info']['name'] rescue I18n.t('supplier.user.unknown_name') name = auth_data.try(:[], 'info').try(:[], 'name')
name || email.to_s.sub(/@.*/, '')
end end
# This is the user name as it is shown to other users # This is the user name as it is shown to other users
+1 -1
View File
@@ -1,5 +1,5 @@
class Suppliers::UserSerializer class Suppliers::UserSerializer
include Qwaiter::SupplierBaseSerializer include Qwaiter::SupplierBaseSerializer
attributes :email, :provider, :uid, :avatar attributes :email, :provider, :uid, :avatar, :number_of_lists_at_supplier
attribute(:name) { object.supplier_name } attribute(:name) { object.supplier_name }
end end
+2 -2
View File
@@ -30,8 +30,8 @@ Qwaiter::Application.configure do
# Don't care if the mailer can't send # Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :letter_opener config.action_mailer.delivery_method = :letter_opener
config.action_mailer.default_url_options = { host: 'https://www.mozo.local' } config.action_mailer.default_url_options = { host: 'https://www.mozo.local', port: 80 }
config.action_controller.default_url_options = { host: 'https://www.mozo.local' } config.action_controller.default_url_options = { host: 'https://www.mozo.local', port: 80 }
# Print deprecation notices to the Rails logger # Print deprecation notices to the Rails logger
config.active_support.deprecation = :log config.active_support.deprecation = :log
+1 -1
View File
@@ -130,7 +130,7 @@ Qwaiter::Application.routes.draw do
get 'employee_and_supplier', controller: 'application' get 'employee_and_supplier', controller: 'application'
resources :suppliers do resources :suppliers do
member do member do
get :switch_to post :switch_to
post :mark_as_open post :mark_as_open
post :mark_as_closed post :mark_as_closed
end end