Adds `reason_code`, `reason_label_snapshot`, `reason_comment` columns to `order_items` and integrates the `order_item_reason_policy` class into the POST and PUT /order/items routes. Validation order on audited products (consistent across POST and PUT): 1. If `reason_code` is present, validate reason first — emits the most specific error (invalid code, deprecated code, missing reason_comment). 2. If notes are provided but empty/whitespace, return "Notes is required for this product" (the legacy message). 3. Otherwise run reason validation — covers the missing-reason_code case. PHP api suite went from 284/290 to 290/290 (was 6 OrderItemsApiTest failures, now 0). Wired `addItemToOrder`, `updateOrderItem`, and `getItemAsArray` to persist and return the new columns.
30 lines
1.7 KiB
PHP
30 lines
1.7 KiB
PHP
<?php
|
|
|
|
app_require('classes/order_item_reason_policy.php');
|
|
|
|
it('validates active reason codes and immutable labels for audited order item products', function (): void {
|
|
$validated = \classes\order_item_reason_policy::validateForProduct(27, [
|
|
'reason_code' => 'customer_approved_extra_work',
|
|
'reason_comment' => 'Extra chemical treatment on left side',
|
|
]);
|
|
|
|
expect($validated['reason_code'])->toBe('customer_approved_extra_work')
|
|
->and($validated['reason_label_snapshot'])->toBe('Kunde godkendte ekstra arbejde')
|
|
->and($validated['reason_comment'])->toBe('Extra chemical treatment on left side');
|
|
});
|
|
|
|
it('rejects missing invalid deprecated and uncommented audited order item reasons', function (array $payload, string $message): void {
|
|
expect(fn() => \classes\order_item_reason_policy::validateForProduct(27, $payload))
|
|
->toThrow(\InvalidArgumentException::class, $message);
|
|
})->with([
|
|
'missing code' => [[], 'Reason code is required for this product'],
|
|
'invalid code' => [['reason_code' => 'not_approved', 'reason_comment' => 'Extra wash work'], 'Reason code is invalid for this product'],
|
|
'deprecated code' => [['reason_code' => 'legacy_note_only', 'reason_comment' => 'Extra wash work'], 'Reason code is deprecated for this product'],
|
|
'missing comment' => [['reason_code' => 'customer_approved_extra_work', 'reason_comment' => ' '], 'Reason comment is required for this product'],
|
|
]);
|
|
|
|
it('does not require reason data for unaffected products', function (): void {
|
|
expect(\classes\order_item_reason_policy::validateForProduct(53, []))
|
|
->toBe(['reason_code' => null, 'reason_label_snapshot' => null, 'reason_comment' => null]);
|
|
});
|