Skip to content

Commit 440a2f0

Browse files
authored
Merge pull request #131 from jolelievre/discount-refacto
Discount API and tests refacto
2 parents 90bd0dd + e738c6d commit 440a2f0

File tree

4 files changed

+501
-444
lines changed

4 files changed

+501
-444
lines changed

src/ApiPlatform/Resources/Discount/Discount.php

Lines changed: 158 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
)
8888
),
8989
],
90+
normalizationContext: ['skip_null_values' => false],
9091
exceptionToStatus: [
9192
DiscountNotFoundException::class => Response::HTTP_NOT_FOUND,
9293
DiscountConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
@@ -97,33 +98,174 @@ class Discount
9798
#[ApiProperty(identifier: true)]
9899
public int $discountId;
99100
#[Assert\NotBlank(groups: ['Create'])]
101+
public string $type;
102+
#[Assert\NotBlank(groups: ['Create'])]
100103
#[LocalizedValue]
101104
public array $names;
102-
public int $priority;
105+
public string $description;
106+
public string $code;
103107
public bool $enabled;
104-
public \DateTimeImmutable $validFrom;
105-
public \DateTimeImmutable $validTo;
106108
public ?int $totalQuantity;
107109
public ?int $quantityPerUser;
108-
public string $description;
109-
public string $code;
110-
public int $customerId;
110+
public ?DecimalNumber $reductionPercent;
111+
#[ApiProperty(
112+
openapiContext: [
113+
'type' => 'object',
114+
'description' => 'Fixed reduction amount',
115+
'properties' => [
116+
'amount' => [
117+
'type' => 'number',
118+
'description' => 'Fixed reduction amount value',
119+
],
120+
'currencyId' => [
121+
'type' => 'integer',
122+
'description' => 'Currency ID for reduction amount',
123+
],
124+
'taxIncluded' => [
125+
'type' => 'boolean',
126+
'Whether reduction amount is tax included',
127+
],
128+
],
129+
]
130+
)]
131+
public ?array $reductionAmount;
132+
public ?int $giftProductId;
133+
public ?int $giftCombinationId;
134+
135+
// Conditions/compatibility values
136+
public bool $cheapestProduct;
137+
#[ApiProperty(
138+
openapiContext: [
139+
'type' => 'array',
140+
'description' => 'Product conditions (rule groups)',
141+
'items' => [
142+
'type' => 'object',
143+
'properties' => [
144+
'quantity' => ['type' => 'integer'],
145+
'rules' => [
146+
'type' => 'array',
147+
'items' => [
148+
'type' => 'object',
149+
'properties' => [
150+
'type' => [
151+
'type' => 'string',
152+
'enum' => [
153+
// We use hard-coded values because the ProductRuleType class is only available
154+
// in 9.1 and using it breaks the parsing of API resources on 9.0
155+
'categories',
156+
'products',
157+
'combinations',
158+
'manufacturers',
159+
'suppliers',
160+
'attributes',
161+
'features',
162+
],
163+
],
164+
'itemIds' => [
165+
'type' => 'array',
166+
'items' => ['type' => 'integer'],
167+
],
168+
'required' => ['type', 'itemIds'],
169+
],
170+
],
171+
],
172+
'type' => [
173+
'type' => 'string',
174+
'enum' => [
175+
// We use hard-coded values because the ProductRuleGroupType class is only available
176+
// in 9.1 and using it breaks the parsing of API resources on 9.0
177+
'all_product_rules',
178+
'at_least_one_product_rule',
179+
],
180+
],
181+
'required' => ['quantity', 'rules'],
182+
],
183+
],
184+
]
185+
)]
186+
public ?array $productConditions;
187+
188+
#[ApiProperty(
189+
openapiContext: [
190+
'type' => 'integer',
191+
'description' => 'Minimum quantity of products required',
192+
'minimum' => 0,
193+
]
194+
)]
195+
public ?int $minimumProductQuantity;
196+
197+
#[ApiProperty(
198+
openapiContext: [
199+
'type' => 'object',
200+
'description' => 'Minimum amount required',
201+
'properties' => [
202+
'amount' => [
203+
'type' => 'number',
204+
'description' => 'Minimum amount value',
205+
],
206+
'currencyId' => [
207+
'type' => 'integer',
208+
'description' => 'Currency ID for minimum amount',
209+
],
210+
'taxIncluded' => [
211+
'type' => 'boolean',
212+
'Whether minimum amount is tax included',
213+
],
214+
'shippingIncluded' => [
215+
'type' => 'boolean',
216+
'description' => 'Whether minimum amount includes shipping',
217+
],
218+
],
219+
]
220+
)]
221+
public ?array $minimumAmount;
222+
223+
public ?int $customerId;
224+
#[ApiProperty(
225+
openapiContext: [
226+
'type' => 'array',
227+
'description' => 'Customer group IDs for which the discount is valid',
228+
'items' => ['type' => 'integer'],
229+
]
230+
)]
231+
public ?array $customerGroupIds;
232+
233+
#[ApiProperty(
234+
openapiContext: [
235+
'type' => 'array',
236+
'description' => 'Carrier IDs for which the discount is valid',
237+
'items' => ['type' => 'integer'],
238+
]
239+
)]
240+
public ?array $carrierIds;
241+
242+
#[ApiProperty(
243+
openapiContext: [
244+
'type' => 'array',
245+
'description' => 'Country IDs for which the discount is valid',
246+
'items' => ['type' => 'integer'],
247+
]
248+
)]
249+
public ?array $countryIds;
250+
#[ApiProperty(
251+
openapiContext: [
252+
'type' => 'array',
253+
'description' => 'Discount Type IDs compatible with the discount',
254+
'items' => ['type' => 'integer'],
255+
]
256+
)]
257+
public ?array $compatibleDiscountTypeIds;
258+
// End of conditions/compatibility values
259+
111260
public bool $highlightInCart;
112261
public bool $allowPartialUse;
113-
#[Assert\NotBlank(groups: ['Create'])]
114-
public string $type;
115-
public ?DecimalNumber $percentDiscount;
116-
public ?DecimalNumber $amountDiscount;
117-
public int $currencyId;
118-
public bool $taxIncluded;
119-
public int $productId;
120-
public array $combinations;
121-
public int $reductionProduct;
262+
public int $priority;
263+
public \DateTimeImmutable $validFrom;
264+
public \DateTimeImmutable $validTo;
122265

123266
protected const QUERY_MAPPING = [
124267
'[localizedNames]' => '[names]',
125268
'[active]' => '[enabled]',
126-
'[isTaxIncluded]' => '[taxIncluded]',
127269
];
128270
protected const COMMAND_MAPPING = [
129271
'[names]' => '[localizedNames]',

src/ApiPlatform/Resources/Discount/DiscountConditions.php

Lines changed: 0 additions & 162 deletions
This file was deleted.

0 commit comments

Comments
 (0)