Skip to content

Commit 6093511

Browse files
authored
Merge pull request PrestaShop#47 from jolelievre/product-api
Improve Product API mapping
2 parents 5c2e3e3 + c8260c9 commit 6093511

File tree

8 files changed

+564
-37
lines changed

8 files changed

+564
-37
lines changed

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,49 @@ Required only for development:
1919
## Installation
2020

2121
Install all dependencies.
22-
```
22+
```bash
2323
composer install
2424
```
2525

26+
## Run tests locally
27+
28+
### Initialize tmp shop environment
29+
30+
First initialize the test environment with this command that will install a PrestaShop shop in a temporary folder so that the integration tests can run:
31+
32+
```bash
33+
composer create-test-db
34+
```
35+
36+
### Customize tmp shop environment
37+
38+
You can define custom values when setting up the tmp shop:
39+
40+
```bash
41+
composer clear-test-cache
42+
composer setup-local-tests -- [arguments]
43+
arguments:
44+
--force-clone Force cloning the repository even if cloned repository is detected (when no repository is detected the clone is automatic)
45+
--build-assets Force building assets even if they are already built (when no assets are detected the build is automatic)
46+
--build-db Force building DB by installing the default shop data (when no DB is detected the DB shop is installed automatically)
47+
--update-local-parameters Force copying parameters from the `test/local-parameters` folder (when no parameter file is detectec they are automatically copied)
48+
--force Force all the previous arguments
49+
--core-branch Use a specific branch, you can use a branch from the original repository (ex: `develop`, `9.0.x`, ...) or from a fork (ex: `myfork:my-dev-branch`) (By default branch develop is used)
50+
```
51+
52+
Example:
53+
```bash
54+
composer setup-local-tests -- --force --core-branch=jolelievre:product-api
55+
```
56+
57+
### Run tests
58+
59+
Then you can run the tests with this command:
60+
61+
```bash
62+
composer run-module-tests
63+
```
64+
2665
## Contributing
2766
2867
PrestaShop modules are open source extensions to the [PrestaShop e-commerce platform][prestashop]. Everyone is welcome and even encouraged to contribute with their own improvements!

config.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<name>ps_apiresources</name>
44
<displayName><![CDATA[PrestaShop API Resources]]></displayName>
55
<version><![CDATA[0.1.0]]></version>
6-
<description><![CDATA[Includes the resources allowing using the API for the PrestaShop domain.]]></description>
6+
<description><![CDATA[Includes the resources allowing using the API for the PrestaShop domain, all endpoints are based on CQRS commands/queries from the Core and we APIPlatform framework is used as a base.]]></description>
77
<author><![CDATA[PrestaShop]]></author>
88
<tab><![CDATA[administration]]></tab>
99
<is_configurable>0</is_configurable>
1010
<need_instance>0</need_instance>
11-
</module>
11+
</module>

src/ApiPlatform/Resources/CustomerGroup.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use ApiPlatform\Metadata\ApiProperty;
2727
use ApiPlatform\Metadata\ApiResource;
28+
use PrestaShop\Decimal\DecimalNumber;
2829
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\AddCustomerGroupCommand;
2930
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\DeleteCustomerGroupCommand;
3031
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\EditCustomerGroupCommand;
@@ -106,7 +107,7 @@ class CustomerGroup
106107
#[LocalizedValue]
107108
public array $localizedNames;
108109

109-
public float $reductionPercent;
110+
public DecimalNumber $reductionPercent;
110111

111112
public bool $displayPriceTaxExcluded;
112113

src/ApiPlatform/Resources/Product/Product.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
use ApiPlatform\Metadata\ApiProperty;
2626
use ApiPlatform\Metadata\ApiResource;
27+
use PrestaShop\Decimal\DecimalNumber;
2728
use PrestaShop\PrestaShop\Core\Domain\Product\Command\AddProductCommand;
2829
use PrestaShop\PrestaShop\Core\Domain\Product\Command\DeleteProductCommand;
2930
use PrestaShop\PrestaShop\Core\Domain\Product\Command\UpdateProductCommand;
@@ -102,6 +103,116 @@ class Product
102103
#[LocalizedValue]
103104
public array $descriptions;
104105

106+
#[LocalizedValue]
107+
public array $shortDescriptions;
108+
109+
#[LocalizedValue]
110+
public array $tags;
111+
112+
public DecimalNumber $priceTaxExcluded;
113+
114+
public DecimalNumber $priceTaxIncluded;
115+
116+
public DecimalNumber $ecotaxTaxExcluded;
117+
118+
public DecimalNumber $ecotaxTaxIncluded;
119+
120+
public int $taxRulesGroupId;
121+
122+
public bool $onSale;
123+
124+
public DecimalNumber $wholesalePrice;
125+
126+
public DecimalNumber $unitPriceTaxExcluded;
127+
128+
public DecimalNumber $unitPriceTaxIncluded;
129+
130+
public string $unity;
131+
132+
public DecimalNumber $unitPriceRatio;
133+
134+
public string $visibility;
135+
136+
public bool $availableForOrder;
137+
138+
public bool $onlineOnly;
139+
140+
public bool $showPrice;
141+
142+
public string $condition;
143+
144+
public bool $showCondition;
145+
146+
public int $manufacturerId;
147+
148+
public string $isbn;
149+
150+
public string $upc;
151+
152+
public string $gtin;
153+
154+
public string $mpn;
155+
156+
public string $reference;
157+
158+
public DecimalNumber $width;
159+
160+
public DecimalNumber $height;
161+
162+
public DecimalNumber $depth;
163+
164+
public DecimalNumber $weight;
165+
166+
public DecimalNumber $additionalShippingCost;
167+
168+
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
169+
public array $carrierReferenceIds;
170+
171+
public int $deliveryTimeNoteType;
172+
173+
#[LocalizedValue]
174+
public array $deliveryTimeInStockNotes;
175+
176+
#[LocalizedValue]
177+
public array $deliveryTimeOutOfStockNotes;
178+
179+
#[LocalizedValue]
180+
public array $metaTitles;
181+
182+
#[LocalizedValue]
183+
public array $metaDescriptions;
184+
185+
#[LocalizedValue]
186+
public array $linkRewrites;
187+
188+
public string $redirectType;
189+
190+
public ?int $redirectTarget = null;
191+
192+
public int $packStockType;
193+
194+
public int $outOfStockType;
195+
196+
public int $quantity;
197+
198+
public int $minimalQuantity;
199+
200+
public int $lowStockThreshold;
201+
202+
public bool $lowStockAlertEnabled;
203+
204+
#[LocalizedValue]
205+
public array $availableNowLabels;
206+
207+
public string $location;
208+
209+
#[LocalizedValue]
210+
public array $availableLaterLabels;
211+
212+
public ?\DateTimeImmutable $availableDate = null;
213+
214+
public string $coverThumbnailUrl;
215+
105216
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
106217
public array $shopIds;
107218

@@ -110,12 +221,72 @@ class Product
110221
'[_context][langId]' => '[displayLanguageId]',
111222
'[basicInformation][localizedNames]' => '[names]',
112223
'[basicInformation][localizedDescriptions]' => '[descriptions]',
224+
'[basicInformation][localizedShortDescriptions]' => '[shortDescriptions]',
225+
'[basicInformation][localizedTags]' => '[tags]',
226+
'[pricesInformation][price]' => '[priceTaxExcluded]',
227+
'[pricesInformation][priceTaxIncluded]' => '[priceTaxIncluded]',
228+
'[pricesInformation][ecotax]' => '[ecotaxTaxExcluded]',
229+
'[pricesInformation][ecotaxTaxIncluded]' => '[ecotaxTaxIncluded]',
230+
'[pricesInformation][taxRulesGroupId]' => '[taxRulesGroupId]',
231+
'[pricesInformation][onSale]' => '[onSale]',
232+
'[pricesInformation][wholesalePrice]' => '[wholesalePrice]',
233+
'[pricesInformation][unitPrice]' => '[unitPriceTaxExcluded]',
234+
'[pricesInformation][unitPriceTaxIncluded]' => '[unitPriceTaxIncluded]',
235+
'[pricesInformation][unity]' => '[unity]',
236+
'[pricesInformation][unitPriceRatio]' => '[unitPriceRatio]',
237+
'[options][visibility]' => '[visibility]',
238+
'[options][availableForOrder]' => '[availableForOrder]',
239+
'[options][onlineOnly]' => '[onlineOnly]',
240+
'[options][showPrice]' => '[showPrice]',
241+
'[options][condition]' => '[condition]',
242+
'[options][showCondition]' => '[showCondition]',
243+
'[options][manufacturerId]' => '[manufacturerId]',
244+
'[details][isbn]' => '[isbn]',
245+
'[details][upc]' => '[upc]',
246+
'[details][gtin]' => '[gtin]',
247+
'[details][mpn]' => '[mpn]',
248+
'[details][reference]' => '[reference]',
249+
'[shippingInformation][width]' => '[width]',
250+
'[shippingInformation][height]' => '[height]',
251+
'[shippingInformation][depth]' => '[depth]',
252+
'[shippingInformation][weight]' => '[weight]',
253+
'[shippingInformation][additionalShippingCost]' => '[additionalShippingCost]',
254+
'[shippingInformation][carrierReferences]' => '[carrierReferenceIds]',
255+
'[shippingInformation][deliveryTimeNoteType]' => '[deliveryTimeNoteType]',
256+
'[shippingInformation][localizedDeliveryTimeInStockNotes]' => '[deliveryTimeInStockNotes]',
257+
'[shippingInformation][localizedDeliveryTimeOutOfStockNotes]' => '[deliveryTimeOutOfStockNotes]',
258+
'[productSeoOptions][localizedMetaTitles]' => '[metaTitles]',
259+
'[productSeoOptions][localizedMetaDescriptions]' => '[metaDescriptions]',
260+
'[productSeoOptions][localizedLinkRewrites]' => '[linkRewrites]',
261+
'[productSeoOptions][redirectType]' => '[redirectType]',
262+
'[productSeoOptions][redirectTarget][id]' => '[redirectTarget]',
263+
'[stockInformation][packStockType]' => '[packStockType]',
264+
'[stockInformation][outOfStockType]' => '[outOfStockType]',
265+
'[stockInformation][quantity]' => '[quantity]',
266+
'[stockInformation][minimalQuantity]' => '[minimalQuantity]',
267+
'[stockInformation][lowStockThreshold]' => '[lowStockThreshold]',
268+
'[stockInformation][lowStockAlertEnabled]' => '[lowStockAlertEnabled]',
269+
'[stockInformation][localizedAvailableNowLabels]' => '[availableNowLabels]',
270+
'[stockInformation][localizedAvailableLaterLabels]' => '[availableLaterLabels]',
271+
'[stockInformation][location]' => '[location]',
272+
'[stockInformation][availableDate]' => '[availableDate]',
113273
];
114274

115275
public const UPDATE_MAPPING = [
116276
'[_context][shopConstraint]' => '[shopConstraint]',
117277
'[type]' => '[productType]',
118278
'[names]' => '[localizedNames]',
119279
'[descriptions]' => '[localizedDescriptions]',
280+
'[shortDescriptions]' => '[localizedShortDescriptions]',
281+
'[metaTitles]' => '[localizedMetaTitles]',
282+
'[metaDescriptions]' => '[localizedMetaDescriptions]',
283+
'[linkRewrites]' => '[localizedLinkRewrites]',
284+
'[deliveryTimeInStockNotes]' => '[localizedDeliveryTimeInStockNotes]',
285+
'[deliveryTimeOutOfStockNotes]' => '[localizedDeliveryTimeOutOfStockNotes]',
286+
'[availableNowLabels]' => '[localizedAvailableNowLabels]',
287+
'[availableLaterLabels]' => '[localizedAvailableLaterLabels]',
288+
'[priceTaxExcluded]' => '[price]',
289+
'[unitPriceTaxExcluded]' => '[unitPrice]',
290+
'[ecotaxTaxExcluded]' => '[ecotax]',
120291
];
121292
}

src/ApiPlatform/Resources/Product/ProductList.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use ApiPlatform\Metadata\ApiProperty;
2727
use ApiPlatform\Metadata\ApiResource;
28+
use PrestaShop\Decimal\DecimalNumber;
2829
use PrestaShop\PrestaShop\Adapter\Product\Grid\Data\Factory\ProductGridDataFactoryDecorator;
2930
use PrestaShop\PrestaShop\Core\Domain\Product\Exception\ProductNotFoundException;
3031
use PrestaShop\PrestaShop\Core\Domain\Shop\Exception\ShopAssociationNotFound;
@@ -41,7 +42,8 @@
4142
scopes: ['product_read'],
4243
ApiResourceMapping: [
4344
'[id_product]' => '[productId]',
44-
'[final_price_tax_excluded]' => '[price]',
45+
'[price_tax_excluded_decimal_value]' => '[priceTaxExcluded]',
46+
'[price_tax_included_decimal_value]' => '[priceTaxIncluded]',
4547
],
4648
gridDataFactory: ProductGridDataFactoryDecorator::class,
4749
filtersClass: ProductFilters::class,
@@ -65,7 +67,9 @@ class ProductList
6567

6668
public int $quantity;
6769

68-
public string $price;
70+
public DecimalNumber $priceTaxExcluded;
71+
72+
public DecimalNumber $priceTaxIncluded;
6973

7074
public string $category;
7175
}

0 commit comments

Comments
 (0)