ShopApiPlugin icon indicating copy to clipboard operation
ShopApiPlugin copied to clipboard

Why don't perform availability check when add item to cart

Open kayue opened this issue 6 years ago • 18 comments

Right now PutVariantBasedConfigutableItemToCartRequest only perform very simple check

May I know is it intentional to leave it availability checking out?

kayue avatar Nov 01 '19 07:11 kayue

Good question.

mamazu avatar Nov 01 '19 08:11 mamazu

Actually there is no inventory check throughout the entire API checkout.

kayue avatar Nov 04 '19 03:11 kayue

At least in the checkout complete step there should be some kind of validation. Is it not caught by the Sylius internal logic that tries to reserve the items?

mamazu avatar Nov 04 '19 13:11 mamazu

Nope that didn't happen.

kayue avatar Nov 05 '19 07:11 kayue

Just noticed this duplicated with #509

kayue avatar Nov 06 '19 15:11 kayue

Any ETA for this bug fix?

gomcodoctor avatar Dec 29 '19 10:12 gomcodoctor

If it is as easy as it seems then it is just adding a validation on some requests. But I want to get @lchrusciel opinion on it as I am not aware of all the ins and outs of Sylius and its stock tracking.

mamazu avatar Dec 30 '19 09:12 mamazu

I already added validation on change quantity request, it working fine.

issue with adding new product to cart request validation - we can add validation like above but it will need EXTRA database query for searching products, I personally feel its not good idea, we should add validation on command to save DB query.

kindly give your suggestions, i may be wrong

gomcodoctor avatar Dec 30 '19 11:12 gomcodoctor

I haven't considered this but I know that there is a validator for availability what about this one?

mamazu avatar Dec 30 '19 12:12 mamazu

We have to implement this

https://github.com/Sylius/Sylius/blob/a70a38983770583c706991abd3328f2b77e7c42e/src/Sylius/Bundle/CoreBundle/Validator/Constraints/CartItemAvailabilityValidator.php

this is request which is currently validated

https://github.com/Sylius/ShopApiPlugin/blob/master/src/Request/Cart/PutVariantBasedConfigurableItemToCartRequest.php

this is handler

https://github.com/Sylius/ShopApiPlugin/blob/master/src/Handler/Cart/PutVariantBasedConfigurableItemToCartHandler.php

gomcodoctor avatar Dec 30 '19 14:12 gomcodoctor

Sounds good. Maybe we can also reuse the validator.

mamazu avatar Dec 31 '19 16:12 mamazu

ya we can reuse validator,

i think its better to add validator directly in

https://github.com/Sylius/ShopApiPlugin/blob/master/src/Handler/Cart/PutVariantBasedConfigurableItemToCartHandler.php

this is will save double queries

gomcodoctor avatar Dec 31 '19 17:12 gomcodoctor

Well the usual behaviour is to double validate it. Once for the view layer so that the Controller can return a 400 when the product is not in stock and once in the actual logic, which doesn't need to be tied to the shop api using it.

mamazu avatar Jan 01 '20 12:01 mamazu

As @mamazu said. Validation on request to show proper info + on command level an exception.

It should be a part of 1.1

lchrusciel avatar Jan 16 '20 13:01 lchrusciel

would #648 be the correct way to fix this issue?

alexander-schranz avatar May 25 '20 13:05 alexander-schranz

No, it should be implemented as validator and return validation error instead of 500 critical error.

Case: Last item is bought by someone else within browsing session of current customer. He sees outdated catalog and tries to add out of stock item to cart and sees 500 error instead of 400 error

diimpp avatar May 25 '20 14:05 diimpp

@diimpp Ok if I look at ProductInCartChannel it seems there exist both a Validator:

https://github.com/Sylius/ShopApiPlugin/blob/fc87069bf912209947a3b08b1e3a218d9545e71f/src/Resources/config/validation/cart/PutSimpleItemToCartRequest.xml#L16

and a check in the Handler:

https://github.com/Sylius/ShopApiPlugin/blob/d5c273af695f0f567b79567d300d8c6ebb5cb12b/src/Handler/Cart/PutSimpleItemToCartHandler.php#L52

Think then we should do also both in this cases or?

alexander-schranz avatar May 25 '20 14:05 alexander-schranz

@alexander-schranz Yes, that would be correct to put check both in handler and in validator. Sorry, I've misread originally, thought it was in Action itself.

diimpp avatar May 25 '20 14:05 diimpp