mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Move mask logic to separate module
And cross-reference similar files so we don't miss it next time\!
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Mask user data from suppliers, unless explicitly allowed
|
||||
# See also: lib/reporting/queries/mask_data.rb
|
||||
#
|
||||
module Orders
|
||||
class MaskDataService
|
||||
def initialize(order)
|
||||
|
||||
36
lib/reporting/queries/mask_data.rb
Normal file
36
lib/reporting/queries/mask_data.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Mask user data from suppliers, unless explicitly allowed
|
||||
# See also: app/services/orders/mask_data_service.rb
|
||||
#
|
||||
module Reporting
|
||||
module Queries
|
||||
module MaskData
|
||||
include Tables
|
||||
|
||||
def mask_customer_name(field)
|
||||
masked(field, managed_order_mask_rule(:show_customer_names_to_suppliers))
|
||||
end
|
||||
|
||||
def mask_contact_data(field)
|
||||
masked(field, managed_order_mask_rule(:show_customer_contacts_to_suppliers))
|
||||
end
|
||||
|
||||
def masked(field, mask_rule = nil)
|
||||
Arel::Nodes::Case.new.
|
||||
when(mask_rule).
|
||||
then(field).
|
||||
else(quoted(I18n.t("hidden_field", scope: i18n_scope)))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Show unmasked data if order is managed by user, or if distributor allows suppliers
|
||||
def managed_order_mask_rule(condition_name)
|
||||
id = raw("#{managed_orders_alias.name}.id") # rubocop:disable Rails/OutputSafety
|
||||
line_item_table[:order_id].in(id).
|
||||
or(distributor_alias[condition_name].eq(true))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -5,6 +5,7 @@ module Reporting
|
||||
class QueryBuilder < QueryInterface
|
||||
include Joins
|
||||
include Tables
|
||||
include MaskData
|
||||
|
||||
attr_reader :grouping_fields
|
||||
|
||||
@@ -49,21 +50,6 @@ module Reporting
|
||||
reflect query.order(*instance_exec(&ordering_fields))
|
||||
end
|
||||
|
||||
def mask_customer_name(field)
|
||||
masked(field, managed_order_mask_rule(:show_customer_names_to_suppliers))
|
||||
end
|
||||
|
||||
def mask_contact_data(field)
|
||||
masked(field, managed_order_mask_rule(:show_customer_contacts_to_suppliers))
|
||||
end
|
||||
|
||||
def masked(field, mask_rule = nil)
|
||||
Case.new.
|
||||
when(mask_rule).
|
||||
then(field).
|
||||
else(quoted(I18n.t("hidden_field", scope: i18n_scope)))
|
||||
end
|
||||
|
||||
def distinct_results(fields = nil)
|
||||
return reflect query.distinct if fields.blank?
|
||||
|
||||
@@ -88,13 +74,6 @@ module Reporting
|
||||
|
||||
private
|
||||
|
||||
# Show unmasked data if order is managed by user, or if distributor allows suppliers
|
||||
def managed_order_mask_rule(condition_name)
|
||||
id = raw("#{managed_orders_alias.name}.id") # rubocop:disable Rails/OutputSafety
|
||||
line_item_table[:order_id].in(id).
|
||||
or(distributor_alias[condition_name].eq(true))
|
||||
end
|
||||
|
||||
def summary_row_title
|
||||
I18n.t("total", scope: i18n_scope)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user