From eda3fc280b34059a01505e5d521adcd2623283b1 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Fri, 6 Mar 2020 19:27:44 -0500 Subject: [PATCH] Mostly enriching supplier's user info with number of lists finished at the restaurant --- app/controllers/suppliers/lists_controller.rb | 3 +- .../suppliers/suppliers_controller.rb | 6 ++- app/models/list.rb | 53 +++++++++++++++++++ app/models/user.rb | 4 +- app/serializers/suppliers/user_serializer.rb | 2 +- config/environments/development.rb | 4 +- config/routes.rb | 2 +- 7 files changed, 66 insertions(+), 8 deletions(-) diff --git a/app/controllers/suppliers/lists_controller.rb b/app/controllers/suppliers/lists_controller.rb index ad90573c..c4111910 100644 --- a/app/controllers/suppliers/lists_controller.rb +++ b/app/controllers/suppliers/lists_controller.rb @@ -19,7 +19,8 @@ module Suppliers else @lists = List.for_supplier(current_supplier, page: params[:page], per_page: params[:per_page] || 25) 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[ orders diff --git a/app/controllers/suppliers/suppliers_controller.rb b/app/controllers/suppliers/suppliers_controller.rb index 6014e714..a6815847 100644 --- a/app/controllers/suppliers/suppliers_controller.rb +++ b/app/controllers/suppliers/suppliers_controller.rb @@ -31,8 +31,10 @@ module Suppliers def switch_to @switch_supplier = Supplier.find(params[:id]) - session[:supplier_id] = params[:id] if @switch_supplier.employee_ids.include? current_employee.id - redirect_to supplier_root_path(other: '/my-account') + #TODO sessions currently not working, just tokens + 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 def mark_as_open diff --git a/app/models/list.rb b/app/models/list.rb index 3c7d1da0..2372a334 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -34,6 +34,14 @@ class List emit([doc.supplier_id, doc.table_id], 1); } }|, 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){ #if(doc.ruby_class == 'List' && doc.state == 'active'){ #emit(doc.table_id, 1); @@ -60,6 +68,7 @@ class List } }|, reduce_function: '_sum' + #TODO: depricate view :active_users_view, type: :custom, map_function: %|function(doc){ if(doc.ruby_class == 'List' && doc.state == 'active'){ doc.user_ids && doc.user_ids.forEach(function(uid){ @@ -94,6 +103,49 @@ class List list 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 def self.from_table_by_employee table, employee unless list = table.active_list @@ -316,6 +368,7 @@ class List 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_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[ diff --git a/app/models/user.rb b/app/models/user.rb index 69262b0b..90b5f5db 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,7 @@ class User include SimplyStored::Couch include ActiveModel::SerializerSupport + attr_accessor :number_of_lists_at_supplier property :name property :active_list_id @@ -93,7 +94,8 @@ class User # This is the user name as it is shown to the supplier 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 # This is the user name as it is shown to other users diff --git a/app/serializers/suppliers/user_serializer.rb b/app/serializers/suppliers/user_serializer.rb index 7253ab8e..1899a412 100644 --- a/app/serializers/suppliers/user_serializer.rb +++ b/app/serializers/suppliers/user_serializer.rb @@ -1,5 +1,5 @@ class Suppliers::UserSerializer include Qwaiter::SupplierBaseSerializer - attributes :email, :provider, :uid, :avatar + attributes :email, :provider, :uid, :avatar, :number_of_lists_at_supplier attribute(:name) { object.supplier_name } end diff --git a/config/environments/development.rb b/config/environments/development.rb index 29282e0c..897e2c7d 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -30,8 +30,8 @@ Qwaiter::Application.configure do # Don't care if the mailer can't send config.action_mailer.raise_delivery_errors = false config.action_mailer.delivery_method = :letter_opener - config.action_mailer.default_url_options = { host: 'https://www.mozo.local' } - config.action_controller.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', port: 80 } # Print deprecation notices to the Rails logger config.active_support.deprecation = :log diff --git a/config/routes.rb b/config/routes.rb index ee0416a2..69e69d1c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -130,7 +130,7 @@ Qwaiter::Application.routes.draw do get 'employee_and_supplier', controller: 'application' resources :suppliers do member do - get :switch_to + post :switch_to post :mark_as_open post :mark_as_closed end