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
+53
View File
@@ -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[
+3 -1
View File
@@ -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