Skip to content

Conversation

yksflip
Copy link
Member

@yksflip yksflip commented Jul 9, 2025

implements #1007

@yksflip yksflip force-pushed the feat/1007-max-order-quantity branch 3 times, most recently from fb2f5d6 to 2f32e39 Compare July 9, 2025 10:42
@lentschi
Copy link
Contributor

@yksflip Just to make sure before looking at it in depth - is this ready for review? (If not, please mark the PR as draft. 😉 )

I tried it out locally and got an unhandled error right away:

NoMethodError (undefined method `quantity' for nil:NilClass):
  
app/models/group_order.rb:67:in `block (2 levels) in load_data'
app/models/group_order.rb:41:in `each'
app/models/group_order.rb:41:in `block in load_data'
app/models/group_order.rb:40:in `each'
app/models/group_order.rb:40:in `load_data'
app/controllers/group_orders_controller.rb:23:in `new'
app/controllers/application_controller.rb:92:in `set_currency'
app/controllers/application_controller.rb:80:in `set_time_zone'

All I did to get there was create a new order including an article with a maximum_order_quantity and then try to open the new group order form for that order.

Also, I think there's (at least) some logic missing in app/assets/javascripts/article-form.js and app/assets/javascripts/group-order-form.js - they both would need to handle the new field similar to the current logic for minimum_order_quantity, right? (If you need help with the clunky jquery, let me know! It's really about time we tackle #1194 😅 )

@yksflip
Copy link
Member Author

yksflip commented Jul 15, 2025

thanks lentschi! I thought it would be ready, but there are few loops to take. I'll mark it a draft and ping you again once I'm ready :)

@yksflip yksflip marked this pull request as draft July 15, 2025 20:57
@yksflip yksflip force-pushed the feat/1007-max-order-quantity branch from 2f32e39 to b48c721 Compare July 15, 2025 21:42
@yksflip
Copy link
Member Author

yksflip commented Jul 15, 2025

I tried it out locally and got an unhandled error right away:

I fixed it by adding a null check.

Also, I think there's (at least) some logic missing in app/assets/javascripts/article-form.js

Indeed! I've added it almost identitcal to max-quantity ..

and app/assets/javascripts/group-order-form.js

I think that is not needed as I used the quantity_available in group_order.rb ( https://github.com/foodcoops/foodsoft/pull/1201/files#diff-b946eb642b5614e4a0519a74fc97aec4c0bcec98db9c99c790ea7c048349b5acR63 ) that was already implemented for the stock-order feature I think

@lentschi
Copy link
Contributor

lentschi commented Jul 24, 2025

I think that is not needed as I used the quantity_available in group_order.rb ( https://github.com/foodcoops/foodsoft/pull/1201/files#diff-b946eb642b5614e4a0519a74fc97aec4c0bcec98db9c99c790ea7c048349b5acR63 ) that was already implemented for the stock-order feature I think

Ah, right, seems to work now 👍

But maybe we should also change the error message when entering an invalid value manually (exceeding max):
image
(It now only mentions min and granularity)

I know, you could argue, that this has never been in there, but quantity_available has so far only been checked for stock orders and those have always been quirky and aren't widely used. 😅

@lentschi
Copy link
Contributor

lentschi commented Jul 24, 2025

Also, I think there's (at least) some logic missing in app/assets/javascripts/article-form.js

Indeed! I've added it almost identitcal to max-quantity ..

👍
Maybe we should also add a validation that minimum_order_quantity must be <= maximum_order_quantity?

@yksflip yksflip force-pushed the feat/1007-max-order-quantity branch 2 times, most recently from a02df13 to a00843d Compare October 14, 2025 13:51
@yksflip
Copy link
Member Author

yksflip commented Oct 14, 2025

I've added backend and client side validations for this now.
The javascript client-side validation were quite complicated, good old friend ai helped a little bit out here. Not sure if it needs to be a custom validation like this. see app/assets/javascripts/validation-utils.js
maybe there is a better / more idiomatic way to do this?

image

@yksflip yksflip force-pushed the feat/1007-max-order-quantity branch from a00843d to 8f043e4 Compare October 14, 2025 13:56
@yksflip yksflip marked this pull request as ready for review October 14, 2025 13:58
Copy link
Contributor

@lentschi lentschi left a comment

Choose a reason for hiding this comment

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

I've added backend and client side validations for this now.
The javascript client-side validation were quite complicated, good old friend ai helped a little bit out here. Not sure if it needs to be a custom validation like this. see app/assets/javascripts/validation-utils.js
maybe there is a better / more idiomatic way to do this?

I've added some code comments.

In addition, two general issues I'd like to throw in:

  1. Currently when two users open the group order form at the same time, then one adds a quantity that is below maximum_order_quantity and the second user tries the same but it's exceeding maximum_order_quantity when combined with the first user's quantity, the second user just gets a "The order couldn’t be updated due to a bug." So maybe add some custom error flash here if the maximum_order_quantity is exceeded? (It would be better to have the form error inside the form, but I guess that's more difficult to implement.)
  2. I haven't tried, but have you checked, if "using up" tolerance, can't be causing to somehow exceed maximum_order_quantity?

this.onSupplierUnitChanged();

// Add validation for minimum/maximum order quantity
this.initializeOrderQuantityValidation();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you add client side validation to this form? I think, since other form fields are validated by the backend, this makes this kind of inconsistent. If I remove that line, it works just as fine - after clicking "Update article", I see the following error:

image

Of course frontend validation is always a nice-to-have in terms of usability, as the users can be made aware as soon as they blur the erroneous field. But I was hoping to only add this when finally building a PWA style client and not to further complicate the already ugly jquery code. What do you think?

* @param {jQuery} input$ - The input element
* @param {Object} options - Validation options
*/
static setupInputValidation(input$, options = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Method seems to be unused - AI slop?

/**
* Hides all validation messages
*/
static hideAllValidationMessages() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Method seems to be unused - AI slop?


validates :maximum_order_quantity, numericality: { allow_nil: true }
# validates_uniqueness_of :name, :scope => [:supplier_id, :deleted_at, :type], if: Proc.new {|a| a.supplier.shared_sync_method.blank? or a.supplier.shared_sync_method == 'import' }
# validates_uniqueness_of :name, :scope => [:supplier_id, :deleted_at, :type, :unit, :unit_quantity]
Copy link
Contributor

Choose a reason for hiding this comment

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

It's odd that this is shown as part of a change although it's the same as in the current master - maybe due to a faulty rebase...?

- quantity_data['used_quantity'] = @ordering_data[:order_articles][order_article.id][:used_quantity]
- quantity_data['price'] = @ordering_data[:order_articles][order_article.id][:price]
- quantity_data['minimum_order_quantity'] = @ordering_data[:order_articles][order_article.id][:minimum_order_quantity] unless @ordering_data[:order_articles][order_article.id][:minimum_order_quantity].nil?
- quantity_data['maximum_order_quantity'] = @ordering_data[:order_articles][order_article.id][:maximum_order_quantity] unless @ordering_data[:order_articles][order_article.id][:maximum_order_quantity].nil?
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm not mistaken, @ordering_data[:order_articles][order_article.id][:maximum_order_quantity] is never set, right? (At least the above fields are all set in GroupOrder.load_data, but maximum_order_quantity isn't.)

Anyhow - quantity_available seems sufficient, right?

//= require_self
//= require big
//= require units-converter
//= require validation-utils
Copy link
Contributor

@lentschi lentschi Oct 18, 2025

Choose a reason for hiding this comment

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

I think the AI added some unused methods for these validation-utils (I added review comments for where I seem to have found dead code) and also the solution it provided supersedes some old logic, which it also failed to remove (e.g. the %span.numeric-step-error in the group order form template no longer seems to be required).

Instead of reviewing the AI code in depth (which seems cumbersome, as I think there's quite some AI slop), here's my quick go on this:

https://github.com/foodcoops/foodsoft/pull/1232/files#diff-ade87a685fd4a0e008033752780385cce4bbfff5d1355fab4b35cafffc2b3080R44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants