Add order discount endpoint#324
Conversation
Fill the AddCartRuleToOrderCommand gap: PUT /orders/{orderId}/discounts (scope
order_write) to add a discount (percent, amount or free_shipping) to an order.
Related to PrestaShop/PrestaShop#39630
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
mattgoud
left a comment
There was a problem hiding this comment.
This wraps an Add…Command but exposes it as CQRSUpdate (PUT) on a plural collection URI /orders/{orderId}/discounts. The operation is non-idempotent (calling it twice adds two discounts), so per the module conventions it should be CQRSCreate (POST).
Also missing a testInvalid* (422) test — empty cartRuleName or a null value for an amount/percent discount both throw OrderConstraintException (422) and would make good negative cases.
- AddCartRuleToOrderCommand is non-idempotent (calling it twice adds two
discounts), so per the module conventions the operation should be POST,
not PUT. Changed the operation from CQRSUpdate to CQRSCreate on the same
/orders/{orderId}/discounts URI; kept output: false so the response stays
a 201 without a body (there's no CQRSQuery for a single order-cart-rule).
- Added Assert\NotBlank on cartRuleName and Assert\Choice on cartRuleType
(percent, amount, free_shipping) so malformed payloads are rejected at
the validator layer with the standard 422 payload.
- Test: switched testAddCartRuleToOrder to createItem (POST) with
HTTP_CREATED; updated getProtectedEndpoints to POST; added
testAddCartRuleWithEmptyName asserting the 422 + assertValidationErrors
on cartRuleName.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
|
Thanks @mattgoud — addressed in the last push.
Ready for another look. |
|
Thanks for switching to |
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
📋 Summary of changesAdds a new ⏱️ Estimated review time5–8 minutes — small surface (one resource class, one test), but several convention gaps need attention. 🎯 Scope
🧱 API Platform / CQRS architecture compliance
new CQRSCreate(
uriTemplate: '/orders/{orderId}/discounts',
requirements: ['orderId' => '\d+'],
output: false,
CQRSCommand: AddCartRuleToOrderCommand::class,
scopes: ['order_write'],
validationContext: ['groups' => ['Default', 'Create']], // ← missing
),
Command mapping — no issues
The command accepts URI, scopes, identifier — correct
PR description says The PR description's "How to test" section reads 💡 Improvement suggestionsTest teardown: replace manual SQL with
public static function tearDownAfterClass(): void
{
DatabaseDump::restoreTables(['orders', 'order_cart_rule', 'order_discount_tax']);
parent::tearDownAfterClass();
}Test method naming: rename to
Add a test for invalid Only empty
✅ Pre-review checklistURI & routing
Operations & scopes
API Resource properties
CQRS mapping
Forbidden practices (CI-enforced)
Exception handling & validation
Multi-shop
Listing field alignment — N/A (no list endpoint) Integration test
|
|
Two small robustness/convention points (from the AI pre-review, verified):
|
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Two identical calls create two credit slips/vouchers, so PUT's replace semantics don't fit. Mirrors the CQRSCreate switch made for add-discount in PrestaShop#324. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
mattgoud
left a comment
There was a problem hiding this comment.
Thanks, validationContext ['Default','Create'] and Assert\NotBlank on cartRuleType are in. LGTM.
AddCartRuleToOrderCommandgap:PUT /orders/{orderId}/discounts(scopeorder_write) to add a discount to an order (cartRuleName,cartRuleType= percent/amount/free_shipping,value, optionalorderInvoiceId).PUT /orders/{id}/discountswith{ "cartRuleName": "...", "cartRuleType": "amount", "value": "1" }(client grantedorder_write) adds the discount. Covered byOrderDiscountEndpointTest.