|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright since 2007 PrestaShop SA and Contributors |
| 4 | + * PrestaShop is an International Registered Trademark & Property of PrestaShop SA |
| 5 | + * |
| 6 | + * NOTICE OF LICENSE |
| 7 | + * |
| 8 | + * This source file is subject to the Academic Free License version 3.0 |
| 9 | + * that is bundled with this package in the file LICENSE.md. |
| 10 | + * It is also available through the world-wide-web at this URL: |
| 11 | + * https://opensource.org/licenses/AFL-3.0 |
| 12 | + * If you did not receive a copy of the license and are unable to |
| 13 | + * obtain it through the world-wide-web, please send an email |
| 14 | + * to [email protected] so we can send you a copy immediately. |
| 15 | + * |
| 16 | + * @author PrestaShop SA and Contributors <[email protected]> |
| 17 | + * @copyright Since 2007 PrestaShop SA and Contributors |
| 18 | + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Discount; |
| 24 | + |
| 25 | +use ApiPlatform\Metadata\ApiProperty; |
| 26 | +use ApiPlatform\Metadata\ApiResource; |
| 27 | +use PrestaShop\Decimal\DecimalNumber; |
| 28 | +use PrestaShop\PrestaShop\Core\Domain\Discount\Command\UpdateDiscountConditionsCommand; |
| 29 | +use PrestaShop\PrestaShop\Core\Domain\Discount\Exception\DiscountConstraintException; |
| 30 | +use PrestaShop\PrestaShop\Core\Domain\Discount\Exception\DiscountNotFoundException; |
| 31 | +use PrestaShop\PrestaShop\Core\Domain\Discount\Query\GetDiscountForEditing; |
| 32 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet; |
| 33 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate; |
| 34 | +use Symfony\Component\HttpFoundation\Response; |
| 35 | + |
| 36 | +#[ApiResource( |
| 37 | + operations: [ |
| 38 | + new CQRSGet( |
| 39 | + uriTemplate: '/discount/{discountId}/conditions', |
| 40 | + requirements: ['discountId' => '\d+'], |
| 41 | + CQRSQuery: GetDiscountForEditing::class, |
| 42 | + scopes: ['discount_read'], |
| 43 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 44 | + ), |
| 45 | + new CQRSPartialUpdate( |
| 46 | + uriTemplate: '/discount/{discountId}/conditions', |
| 47 | + requirements: ['discountId' => '\d+'], |
| 48 | + CQRSCommand: UpdateDiscountConditionsCommand::class, |
| 49 | + CQRSQuery: GetDiscountForEditing::class, |
| 50 | + scopes: ['discount_write'], |
| 51 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 52 | + CQRSCommandMapping: self::COMMAND_MAPPING, |
| 53 | + ), |
| 54 | + ], |
| 55 | + exceptionToStatus: [ |
| 56 | + DiscountNotFoundException::class => Response::HTTP_NOT_FOUND, |
| 57 | + DiscountConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY, |
| 58 | + ], |
| 59 | +)] |
| 60 | +class DiscountConditions |
| 61 | +{ |
| 62 | + #[ApiProperty(identifier: true)] |
| 63 | + public int $discountId; |
| 64 | + |
| 65 | + #[ApiProperty( |
| 66 | + openapiContext: [ |
| 67 | + 'type' => 'integer', |
| 68 | + 'description' => 'Minimum quantity of products required', |
| 69 | + 'minimum' => 0, |
| 70 | + ] |
| 71 | + )] |
| 72 | + public ?int $minimumProductsQuantity; |
| 73 | + |
| 74 | + #[ApiProperty( |
| 75 | + openapiContext: [ |
| 76 | + 'type' => 'array', |
| 77 | + 'description' => 'Product conditions (rule groups)', |
| 78 | + 'items' => [ |
| 79 | + 'type' => 'object', |
| 80 | + 'properties' => [ |
| 81 | + 'quantity' => ['type' => 'integer'], |
| 82 | + 'rules' => [ |
| 83 | + 'type' => 'array', |
| 84 | + 'items' => [ |
| 85 | + 'type' => 'object', |
| 86 | + 'properties' => [ |
| 87 | + 'type' => ['type' => 'string'], |
| 88 | + 'itemIds' => [ |
| 89 | + 'type' => 'array', |
| 90 | + 'items' => ['type' => 'integer'], |
| 91 | + ], |
| 92 | + ], |
| 93 | + ], |
| 94 | + ], |
| 95 | + 'type' => ['type' => 'string'], |
| 96 | + ], |
| 97 | + ], |
| 98 | + ] |
| 99 | + )] |
| 100 | + public ?array $productConditions; |
| 101 | + |
| 102 | + #[ApiProperty( |
| 103 | + openapiContext: [ |
| 104 | + 'type' => 'string', |
| 105 | + 'description' => 'Minimum amount required', |
| 106 | + 'format' => 'decimal', |
| 107 | + ] |
| 108 | + )] |
| 109 | + public ?DecimalNumber $minimumAmount; |
| 110 | + |
| 111 | + #[ApiProperty( |
| 112 | + openapiContext: [ |
| 113 | + 'type' => 'integer', |
| 114 | + 'description' => 'Currency ID for minimum amount', |
| 115 | + ] |
| 116 | + )] |
| 117 | + public ?int $minimumAmountCurrencyId; |
| 118 | + |
| 119 | + #[ApiProperty( |
| 120 | + openapiContext: [ |
| 121 | + 'type' => 'boolean', |
| 122 | + 'description' => 'Whether minimum amount is tax included', |
| 123 | + ] |
| 124 | + )] |
| 125 | + public ?bool $minimumAmountTaxIncluded; |
| 126 | + |
| 127 | + #[ApiProperty( |
| 128 | + openapiContext: [ |
| 129 | + 'type' => 'boolean', |
| 130 | + 'description' => 'Whether minimum amount includes shipping', |
| 131 | + ] |
| 132 | + )] |
| 133 | + public ?bool $minimumAmountShippingIncluded; |
| 134 | + |
| 135 | + #[ApiProperty( |
| 136 | + openapiContext: [ |
| 137 | + 'type' => 'array', |
| 138 | + 'description' => 'Carrier IDs for which the discount is valid', |
| 139 | + 'items' => ['type' => 'integer'], |
| 140 | + ] |
| 141 | + )] |
| 142 | + public ?array $carrierIds; |
| 143 | + |
| 144 | + #[ApiProperty( |
| 145 | + openapiContext: [ |
| 146 | + 'type' => 'array', |
| 147 | + 'description' => 'Country IDs for which the discount is valid', |
| 148 | + 'items' => ['type' => 'integer'], |
| 149 | + ] |
| 150 | + )] |
| 151 | + public ?array $countryIds; |
| 152 | + |
| 153 | + protected const QUERY_MAPPING = [ |
| 154 | + '[minimumProductQuantity]' => '[minimumProductsQuantity]', |
| 155 | + ]; |
| 156 | + |
| 157 | + protected const COMMAND_MAPPING = [ |
| 158 | + '[minimumAmount]' => '[amountDiscount]', |
| 159 | + '[minimumAmountCurrencyId]' => '[currencyId]', |
| 160 | + '[minimumAmountTaxIncluded]' => '[taxIncluded]', |
| 161 | + ]; |
| 162 | +} |
0 commit comments