Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

chahmedejaz
Copy link
Collaborator

@chahmedejaz chahmedejaz commented Feb 1, 2025

⚠️ Please use clockify code #12476 Flower Farms

What? Why?

What should we test?

  • As mention in the issue
  • Along with this, please test the whole existing order module

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

@chahmedejaz chahmedejaz force-pushed the task/13031-allow-producers-to-edit-orders branch from 764ea78 to ae86821 Compare February 1, 2025 23:56
@chahmedejaz chahmedejaz force-pushed the task/13031-allow-producers-to-edit-orders branch from 2e12ce7 to a747c88 Compare February 10, 2025 20:46
@chahmedejaz chahmedejaz force-pushed the task/13031-allow-producers-to-edit-orders branch from d9f0454 to e4ef890 Compare February 15, 2025 20:57
@chahmedejaz chahmedejaz changed the title Task/13031 allow producers to edit orders Allow producer to edit their products on hubs' orders Feb 15, 2025
@chahmedejaz chahmedejaz marked this pull request as ready for review February 15, 2025 21:07

# 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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @chahmedejaz It's a good start, but I would like to see more unit test. It's a bit hard to follow if all changes are covered by some specs as you added them at the end. In general it's better to commit the specs whit the actual changes, at least the unit test.
I am wondering if we should also add a some system spec, at least for the order edit page.

orders = Spree::Order.
where(managed_orders_where_values.
or(coordinated_orders_where_values))
orders = if @user.can_manage_line_items_in_orders_only?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that be can_manage_line_items_in_orders?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

app/services/permissions/order.rb Show resolved Hide resolved
Comment on lines +378 to +381
def is_producer
is_primary_producer && sells == 'none'
end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specs ?

Comment on lines +93 to +94
def can_manage_line_items_in_orders?(user)
user.can_manage_line_items_in_orders?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specs ?

Comment on lines +150 to +163
# 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specs ?

Comment on lines +165 to +167
def can_manage_line_items_in_orders_only?
!can_manage_orders? && can_manage_line_items_in_orders?
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spec ?

Comment on lines +46 to +52
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specs ?

@@ -20,13 +21,14 @@ def search
scope_to_schedule if params[:schedule_id]
scope_to_order_cycle if params[:order_cycle_id]
scope_to_distributor if params[:distributor_id]
scope_to_supplier if spree_current_user.can_manage_line_items_in_orders_only?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spec ?

@@ -4,7 +4,7 @@

module Permissions
RSpec.describe Order do
let(:user) { double(:user) }
let(:user) { double(:user, can_manage_line_items_in_orders_only?: false) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might as well improve the spec here by using instance_doubleinstead of double . instance_double actually checks if the stubbed method would exist on an actual instance see: https://rspec.info/features/3-13/rspec-mocks/verifying-doubles/instance-doubles/

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will look into this improvement. 👍🏻

@chahmedejaz
Copy link
Collaborator Author

chahmedejaz commented Feb 17, 2025

Thanks @rioug for the feedback. Let me incorporate and get back to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress ⚙
Development

Successfully merging this pull request may close these issues.

Allow producer to edit their products on hubs' orders
2 participants