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
Hide users with no completed orders from a hub's customers list #10704
Hide users with no completed orders from a hub's customers list #10704
Conversation
ae5c705
to
bab45fa
Compare
3e2bcbe
to
de0175c
Compare
I used the scope Order::complete which includes:
For some reason, there is one test failing. I don't know why. |
a789882
to
77aee44
Compare
Following spec fails: openfoodnetwork/spec/controllers/admin/customers_controller_spec.rb Lines 47 to 55 in bab45fa
This spec is testing openfoodnetwork/app/controllers/admin/customers_controller.rb Lines 69 to 81 in bab45fa
app/controllers/admin/resource_controller.rb )
Error is saying:
so it appears that
I suppose it should be defined like this: let(:customers){ Customer.visible.managed_by(user).where(enterprise_id: enterprise.id) } and then the spec pass. What do you think? |
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.
This mainly good. We just need to enable the API v1 to create visible customers as well.
I personally would have preferred an active
field because that could be re-used for other purposes later but I'm okay with your given the current requirements.
@@ -0,0 +1,5 @@ | |||
class AddCreatedManuallyFlagToCustomer < ActiveRecord::Migration[7.0] | |||
def change | |||
add_column :customers, :created_manually, :boolean, default: false |
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.
Customers that were created manually in the past will disappear now.
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.
Yes, the customers that were created manually and don't have at least one completed/resumed order will disappear.
There's no way to distinguish between the two.
I think a subset of them are the customers who don't have any orders associated with them (regardless of the state of the order). Maybe we can add a migration to update their flag?
if we add that migration, we'll only lose the customers created manually, have at least incomplete order, and don't have any completed order (the three conditions must be verified).
what do you think?
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.
That's a good idea. We can look for customers without any orders and it's safe to assume that they were added manually. We cant distinguish the others... hm, what about the created_at column of order and customer? If a customer record was created more than a minute before the order record, it must have been created manually.
Uh, another thing is tags. If enterprises assigned tags to customers then they probably want to continue to see them. That makes me wonder what happens if an enterprise tries to create a customer which already exists but is invisible. It probably fails. And there's no way of tagging such a customer. We need a way to "activate" customers or we just switch them to manually created if we detect this.
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.
what happens if an enterprise tries to create a customer which already exists but is invisible. It probably fails. And there's no way of tagging such a customer.
^ This seems really important. We probably should provide a filter option so that users can choose to show/hide based on this field (I think this would apply to the admin interface and API).
Even then, it may not be clear when you get the error "customer already exists" but you can't see them. So we might need to add a hint in the "customer already exists" message (assuming there is one).
def visible_customers | ||
Customer.managed_by(current_api_user) | ||
Customer.visible.managed_by(current_api_user) | ||
end | ||
|
||
def customer_params |
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.
I think we should allow this field to be set in the API. One use case of the API is to import customer records which needs this field set. So maybe it should be on by default.
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.
done
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.
It's not on by default but that's probably okay.
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.
Sorry, I misunderstood your comment.
I'll set that flag by default to the imported records new record.
Yes, you're right @jibees So, It's comparing the queries and not the results of the queries. I was confused because in the logs I could not see any differences between the given and the expected values. Thank you 🙏 |
691e60c
to
5adb6cb
Compare
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.
Good improvements. It would be easier to review if you only added commits after it has been reviewed. And I think I found another scenario that needs addressing before we proceed.
@@ -0,0 +1,5 @@ | |||
class AddCreatedManuallyFlagToCustomer < ActiveRecord::Migration[7.0] | |||
def change | |||
add_column :customers, :created_manually, :boolean, default: false |
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.
That's a good idea. We can look for customers without any orders and it's safe to assume that they were added manually. We cant distinguish the others... hm, what about the created_at column of order and customer? If a customer record was created more than a minute before the order record, it must have been created manually.
Uh, another thing is tags. If enterprises assigned tags to customers then they probably want to continue to see them. That makes me wonder what happens if an enterprise tries to create a customer which already exists but is invisible. It probably fails. And there's no way of tagging such a customer. We need a way to "activate" customers or we just switch them to manually created if we detect this.
def visible_customers | ||
Customer.managed_by(current_api_user) | ||
Customer.visible.managed_by(current_api_user) | ||
end | ||
|
||
def customer_params |
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.
It's not on by default but that's probably okay.
5adb6cb
to
875e996
Compare
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.
Good effort. I think that we definitely need an index on the new column and the migration may blow up on large datasets in production.
db/migrate/20230525081252_update_created_manually_flag_on_customers.rb
Outdated
Show resolved
Hide resolved
db/migrate/20230525081252_update_created_manually_flag_on_customers.rb
Outdated
Show resolved
Hide resolved
db/migrate/20230516072511_add_created_manually_flag_to_customer.rb
Outdated
Show resolved
Hide resolved
db/migrate/20230525081252_update_created_manually_flag_on_customers.rb
Outdated
Show resolved
Hide resolved
db/migrate/20230525081252_update_created_manually_flag_on_customers.rb
Outdated
Show resolved
Hide resolved
db/migrate/20230525081252_update_created_manually_flag_on_customers.rb
Outdated
Show resolved
Hide resolved
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.
This looks good, but I'm hesitant to approve because it sounds like a user could be blocked from finding/updating customers. Is that the case?
db/migrate/20230525081252_update_created_manually_flag_on_customers.rb
Outdated
Show resolved
Hide resolved
.or(Customer.where.not(id: customers_with_at_least_one_order)) | ||
.or( | ||
Customer.where(id: customers_created_more_than_1min_before_orders) | ||
.where.not(id: customers_created_no_more_than_1min_before_orders) |
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.
This seems redundant to me, it's the same as the previous line isn't it?
That is, they are mutually exclusive. If
"#{creation_time_difference_in_seconds_sql} >= 60"
then it cannot be
"#{creation_time_difference_in_seconds_sql} < 60"
@@ -0,0 +1,5 @@ | |||
class AddCreatedManuallyFlagToCustomer < ActiveRecord::Migration[7.0] | |||
def change | |||
add_column :customers, :created_manually, :boolean, default: false |
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.
what happens if an enterprise tries to create a customer which already exists but is invisible. It probably fails. And there's no way of tagging such a customer.
^ This seems really important. We probably should provide a filter option so that users can choose to show/hide based on this field (I think this would apply to the admin interface and API).
Even then, it may not be clear when you get the error "customer already exists" but you can't see them. So we might need to add a hint in the "customer already exists" message (assuming there is one).
After discussion with @abdellani we've added the |
6b04272
to
ed2deb3
Compare
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.
I'm happy with my own changes. 😉
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.
Great team effort!
As Lynne confirms, we won't support accessing of hidden users in this PR.
The form only submits enterprise id and email address. We don't need to assign any other attributes.
The API endpoint merges the created_manually flag in the params already. No need to write it separately.
It should only be true or false. This was flagged by Rubocop. I also added another Rubocop suggestion and combined two migrations because they are related.
e6a27c8
to
a46917a
Compare
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.
I removed the error handling again, sorry @rioug. And I cleaned up a little. Would be good to get another perspective here.
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.
Great, I'm happy with this, knowing the spec now covers the situation.
But I can see room for improvement. It looks like a simple change so I'll try it out..
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.
All good on my end now. Either @mkllnk or @abdellani, if you're happy to approve, I think we are ok for testing?
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.
Great change!
Four people workin on one PR...
1cee987
to
d195882
Compare
Strange to have a controller spec randomly fail. Works locally for me. Retrying...
|
I've reproduced this locally both on master and after pulling this branch, opened issue here. The branch does not make it worse, so we're good for testing it. |
Hey @abdellani , Thanks so much for this effort 🙏 Before staging, I've created three customers:
Before staging this PR, the After this PR: Customer 1) is not appearing, but also, customer 2) (created manually) is missing; only customer 3) appears: I've looked at your tests and they seemed good to me, so the next step, was to look at the DB:
It looks like the Does this test help you move forward? Is there a particular scenario which you feel needs to be tested? |
The migration looks good to me. Maybe it was already applied before it didn't run again? I just reverted and repeated the migration:
Then checked the database again. Thank you for posting your query:
It seems to work. I'll let you finish the testing. |
Ahh interesting, thanks @mkllnk , I did not think the migration hadn't worked, as the We see that, in the
I was wondering what happens if we cancel an order: would then the customer disappear from the customers list? I've tested this and the answer is no - for a customer with one order only:
I think this is great and makes sense, as the user already has completed orders in the past. This looks great! |
b175793
into
openfoodfoundation:master
What? Why?
I update the three endpoints that return customers list to only return:
Lists should not return customer that have started (and not completed) a checkout with split checkout.
What should we test?
Release notes
Changelog Category: User facing changes
The title of the pull request will be included in the release notes.
Dependencies
Documentation updates