-
-
Notifications
You must be signed in to change notification settings - Fork 729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow producer to edit their products on hubs' orders #13113
base: master
Are you sure you want to change the base?
Changes from all commits
d199126
f42711c
a63dba8
79f0f9e
173110b
cf42a64
a868ed1
e4ef890
90c1863
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,11 @@ def initialize(user) | |
add_group_management_abilities user if can_manage_groups? user | ||
add_product_management_abilities user if can_manage_products? user | ||
add_order_cycle_management_abilities user if can_manage_order_cycles? user | ||
add_order_management_abilities user if can_manage_orders? user | ||
if can_manage_orders? user | ||
add_order_management_abilities user | ||
elsif can_manage_line_items_in_orders? user | ||
add_manage_line_items_abilities user | ||
end | ||
add_relationship_management_abilities user if can_manage_relationships? user | ||
end | ||
|
||
|
@@ -81,7 +85,13 @@ def can_manage_order_cycles?(user) | |
|
||
# Users can manage orders if they have a sells own/any enterprise. | ||
def can_manage_orders?(user) | ||
( user.enterprises.map(&:sells) & %w(own any) ).any? | ||
user.can_manage_orders? | ||
end | ||
|
||
# Users can manage line items in orders if they have producer enterprise and | ||
# any of order distributors allow them to edit their orders. | ||
def can_manage_line_items_in_orders?(user) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding the same user method names here for the sake of writing ability specs to better show the assigned abilities |
||
user.can_manage_line_items_in_orders? | ||
Comment on lines
+93
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. specs ? |
||
end | ||
|
||
def can_manage_relationships?(user) | ||
|
@@ -343,6 +353,26 @@ def add_order_management_abilities(user) | |
end | ||
end | ||
|
||
def add_manage_line_items_abilities(user) | ||
can_edit_order_lambda = lambda do |order| | ||
return unless order.distributor&.enable_producers_to_edit_orders | ||
|
||
order.variants.any? { |variant| user.enterprises.ids.include?(variant.supplier_id) } | ||
end | ||
Comment on lines
+357
to
+361
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that should be it's own method instead of having disguised as lambda here. |
||
|
||
can [:admin, :read, :index, :edit, :update, :bulk_management], Spree::Order do |order| | ||
can_edit_order_lambda.call(order) | ||
end | ||
can [:admin, :index, :create, :destroy, :update], Spree::LineItem do |item| | ||
can_edit_order_lambda.call(item.order) | ||
end | ||
can [:index, :create, :add, :read, :edit, :update], Spree::Shipment do |shipment| | ||
can_edit_order_lambda.call(shipment.order) | ||
end | ||
|
||
can [:visible], Enterprise | ||
end | ||
|
||
def add_relationship_management_abilities(user) | ||
can [:admin, :index, :create], EnterpriseRelationship | ||
can [:destroy], EnterpriseRelationship do |enterprise_relationship| | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,25 @@ def affiliate_enterprises | |
Enterprise.joins(:connected_apps).merge(ConnectedApps::AffiliateSalesData.ready) | ||
end | ||
|
||
# Users can manage orders if they have a sells own/any enterprise. or is admin | ||
def can_manage_orders? | ||
@can_manage_orders ||= (enterprises.pluck(:sells).intersect?(%w(own any)) or admin?) | ||
end | ||
|
||
# Users can manage line items in orders if they have producer enterprise and | ||
# any of order distributors allow them to edit their orders. | ||
def can_manage_line_items_in_orders? | ||
@can_manage_line_items_in_orders ||= begin | ||
has_any_producer = enterprises.any?(&:is_producer) | ||
has_producer_editable_orders = Spree::Order.editable_by_producers(enterprises).exists? | ||
has_any_producer && has_producer_editable_orders | ||
end | ||
end | ||
Comment on lines
+150
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. specs ? |
||
|
||
def can_manage_line_items_in_orders_only? | ||
!can_manage_orders? && can_manage_line_items_in_orders? | ||
end | ||
Comment on lines
+165
to
+167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spec ? |
||
|
||
protected | ||
|
||
def password_required? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,16 @@ def visible_orders | |
|
||
# Any orders that the user can edit | ||
def editable_orders | ||
orders = Spree::Order. | ||
where(managed_orders_where_values. | ||
or(coordinated_orders_where_values)) | ||
orders = if @user.can_manage_line_items_in_orders_only? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't that be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be, however, the _only method would make sure it won't be able to manage the orders and can only manage line items in the orders. |
||
Spree::Order.joins(:distributor).where( | ||
id: produced_orders.select(:id), | ||
rioug marked this conversation as resolved.
Show resolved
Hide resolved
|
||
distributor: { enable_producers_to_edit_orders: true } | ||
) | ||
else | ||
Spree::Order.where( | ||
managed_orders_where_values.or(coordinated_orders_where_values) | ||
) | ||
end | ||
|
||
filtered_orders(orders) | ||
end | ||
|
@@ -36,7 +43,13 @@ def visible_line_items | |
|
||
# Any line items that I can edit | ||
def editable_line_items | ||
Spree::LineItem.where(order_id: editable_orders.select(:id)) | ||
if @user.can_manage_line_items_in_orders_only? | ||
Spree::LineItem.editable_by_producers( | ||
@permissions.managed_enterprises.select("enterprises.id") | ||
) | ||
else | ||
Spree::LineItem.where(order_id: editable_orders.select(:id)) | ||
end | ||
Comment on lines
+46
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. specs ? |
||
end | ||
|
||
private | ||
|
@@ -79,6 +92,13 @@ def coordinated_orders_where_values | |
reduce(:and) | ||
end | ||
|
||
def produced_orders | ||
Spree::Order.with_line_items_variants_and_products_outer. | ||
where( | ||
spree_variants: { supplier_id: @permissions.managed_enterprises.select("enterprises.id") } | ||
) | ||
end | ||
|
||
def produced_orders_where_values | ||
Spree::Order.with_line_items_variants_and_products_outer. | ||
where( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddEnableProducersToEditOrdersToEnterprises < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :enterprises, :enable_producers_to_edit_orders, :boolean, default: false, null: false | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
specs ?