Skip to content

Commit

Permalink
Merge branch 'master' into III-6396-title-translated
Browse files Browse the repository at this point in the history
# Conflicts:
#	tests/BackwardsCompatiblePayloadSerializerFactoryTest.php
  • Loading branch information
LucWollants committed Nov 20, 2024
2 parents b11b577 + 45b215d commit b8b472a
Show file tree
Hide file tree
Showing 25 changed files with 557 additions and 362 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4

[*.feature]
indent_size = 2
1 change: 0 additions & 1 deletion app/Migrations/Version20240306114323.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function up(Schema $schema): void
$table->setPrimaryKey(['id']);
$table->addIndex(['item_id']);
$table->addIndex(['owner_id']);
$table->addUniqueIndex(['item_id', 'owner_id']);
}

public function down(Schema $schema): void
Expand Down
93 changes: 67 additions & 26 deletions features/ownership/request.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Feature: Test requesting ownership

Background:
Given I am using the UDB3 base URL
And I am using an UiTID v1 API key of consumer "uitdatabank"
Expand Down Expand Up @@ -43,10 +44,10 @@ Feature: Test requesting ownership
And the JSON response should be:
"""
{
"type": "https://api.publiq.be/probs/auth/forbidden",
"title": "Forbidden",
"status": 403,
"detail": "You are not allowed to request ownership for this item"
"type": "https://api.publiq.be/probs/auth/forbidden",
"title": "Forbidden",
"status": 403,
"detail": "You are not allowed to request ownership for this item"
}
"""

Expand All @@ -67,42 +68,82 @@ Feature: Test requesting ownership
Given I create a minimal organizer and save the "id" as "organizerId"
And I request ownership for "auth0|64089494e980aedd96740212" on the organizer with organizerId "%{organizerId}" and save the "id" as "ownershipId"
And I set the JSON request payload to:
"""
{
"itemId": "%{organizerId}",
"itemType": "organizer",
"ownerId": "auth0|64089494e980aedd96740212"
}
"""
"""
{
"itemId": "%{organizerId}",
"itemType": "organizer",
"ownerId": "auth0|64089494e980aedd96740212"
}
"""
When I send a POST request to '/ownerships'
Then the response status should be 409
And the JSON response should be:
"""
{
"type": "https://api.publiq.be/probs/uitdatabank/ownership-already-exists",
"title": "Ownership already exists",
"status": 409,
"detail": "An ownership request for this item and owner already exists with id %{ownershipId}"
}
"""

Scenario: Requesting the same ownership of an organizer is not allowed when already approved
Given I create a minimal organizer and save the "id" as "organizerId"
And I request ownership for "auth0|64089494e980aedd96740212" on the organizer with organizerId "%{organizerId}" and save the "id" as "ownershipId"
And I approve the ownership with ownershipId "%{ownershipId}"
And I set the JSON request payload to:
"""
{
"itemId": "%{organizerId}",
"itemType": "organizer",
"ownerId": "auth0|64089494e980aedd96740212"
}
"""
When I send a POST request to '/ownerships'
Then the response status should be 409
And the JSON response should be:
"""
{
"type": "https://api.publiq.be/probs/uitdatabank/ownership-already-exists",
"title": "Ownership already exists",
"status": 409,
"detail": "An ownership request for this item and owner already exists with id %{ownershipId}"
"type": "https://api.publiq.be/probs/uitdatabank/ownership-already-exists",
"title": "Ownership already exists",
"status": 409,
"detail": "An ownership request for this item and owner already exists with id %{ownershipId}"
}
"""

Scenario: Requesting the same ownership of an organizer is allowed when the previous request was rejected
Given I create a minimal organizer and save the "id" as "organizerId"
And I request ownership for "auth0|64089494e980aedd96740212" on the organizer with organizerId "%{organizerId}" and save the "id" as "ownershipId"
And I reject the ownership with ownershipId "%{ownershipId}"
When I request ownership for "auth0|64089494e980aedd96740212" on the organizer with organizerId "%{organizerId}" and save the "id" as "ownershipId"
And I get the ownership with ownershipId "%{ownershipId}"
Then the JSON response at "id" should be "%{ownershipId}"

Scenario: Requesting the same ownership of an organizer is allowed when the previous request was deleted
Given I create a minimal organizer and save the "id" as "organizerId"
And I request ownership for "auth0|64089494e980aedd96740212" on the organizer with organizerId "%{organizerId}" and save the "id" as "ownershipId"
And I delete the ownership with ownershipId "%{ownershipId}"
When I request ownership for "auth0|64089494e980aedd96740212" on the organizer with organizerId "%{organizerId}" and save the "id" as "ownershipId"
And I get the ownership with ownershipId "%{ownershipId}"
Then the JSON response at "id" should be "%{ownershipId}"

Scenario: Requesting the ownership of a non-existing organizer is not allowed
When I set the JSON request payload to:
"""
{
"itemId": "b192b05f-9294-4c07-a3f9-6a15e267d746",
"itemType": "organizer",
"ownerId": "auth0|64089494e980aedd96740212"
}
"""
"""
{
"itemId": "b192b05f-9294-4c07-a3f9-6a15e267d746",
"itemType": "organizer",
"ownerId": "auth0|64089494e980aedd96740212"
}
"""
When I send a POST request to '/ownerships'
Then the response status should be 404
And the JSON response should be:
"""
{
"type": "https://api.publiq.be/probs/url/not-found",
"title": "Not Found",
"status": 404,
"detail": "The Organizer with id \"b192b05f-9294-4c07-a3f9-6a15e267d746\" was not found."
"type": "https://api.publiq.be/probs/url/not-found",
"title": "Not Found",
"status": 404,
"detail": "The Organizer with id \"b192b05f-9294-4c07-a3f9-6a15e267d746\" was not found."
}
"""
9 changes: 5 additions & 4 deletions src/Cdb/CdbXmlPriceInfoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

use CultureFeed_Cdb_Data_Detail;
use CultureFeed_Cdb_Data_Price;
use CultuurNet\UDB3\Model\ValueObject\Price\Tariff;
use CultuurNet\UDB3\Model\ValueObject\Price\TariffName;
use CultuurNet\UDB3\Model\ValueObject\Price\TranslatedTariffName;
use CultuurNet\UDB3\Model\ValueObject\Translation\Language;
use CultuurNet\UDB3\MoneyFactory;
use CultuurNet\UDB3\PriceInfo\BasePrice;
use CultuurNet\UDB3\PriceInfo\PriceInfo;
use CultuurNet\UDB3\PriceInfo\Tariff;
use CultuurNet\UDB3\ValueObject\MultilingualString;
use Money\Currency;

final class CdbXmlPriceInfoParser
Expand Down Expand Up @@ -100,13 +101,13 @@ function (\CultureFeed_Cdb_Data_Detail $detail) use ($mainLanguage) {
foreach ($translatedTariffs as $tariffName => $tariffPrice) {
if (!isset($tariffs[$tariffIndex])) {
$tariff = new Tariff(
new MultilingualString(new Language($language), (string) $tariffName),
new TranslatedTariffName(new Language($language), new TariffName((string) $tariffName)),
MoneyFactory::create($tariffPrice, new Currency('EUR'))
);
} else {
$tariff = $tariffs[$tariffIndex];
$name = $tariff->getName();
$name = $name->withTranslation(new Language($language), (string) $tariffName);
$name = $name->withTranslation(new Language($language), new TariffName($tariffName));
$tariff = new Tariff(
$name,
$tariff->getPrice()
Expand Down
3 changes: 1 addition & 2 deletions src/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
use CultuurNet\UDB3\Offer\Offer;
use CultuurNet\UDB3\Offer\OfferType;
use CultuurNet\UDB3\PriceInfo\PriceInfo;
use CultuurNet\UDB3\PriceInfo\Tariff;
use CultuurNet\UDB3\Theme;
use CultuurNet\UDB3\Model\ValueObject\Text\Title;
use DateTimeImmutable;
Expand Down Expand Up @@ -506,7 +505,7 @@ public function updateUiTPASPrices(Tariffs $tariffs): void

$legacyUiTPASTariffs = [];
foreach ($tariffs as $tariff) {
$legacyUiTPASTariffs[] = Tariff::fromUdb3ModelTariff($tariff);
$legacyUiTPASTariffs[] = $tariff;
}
$newPriceInfo = $this->priceInfo->withUiTPASTariffs($legacyUiTPASTariffs);

Expand Down
37 changes: 23 additions & 14 deletions src/Http/Ownership/RequestOwnershipRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

use Broadway\CommandHandling\CommandBus;
use CultuurNet\UDB3\Http\ApiProblem\ApiProblem;
use CultuurNet\UDB3\Http\Ownership\Search\SearchParameter;
use CultuurNet\UDB3\Http\Ownership\Search\SearchQuery;
use CultuurNet\UDB3\Http\Request\Body\DenormalizingRequestBodyParser;
use CultuurNet\UDB3\Http\Request\Body\JsonSchemaLocator;
use CultuurNet\UDB3\Http\Request\Body\JsonSchemaValidatingRequestBodyParser;
use CultuurNet\UDB3\Http\Request\Body\RequestBodyParserFactory;
use CultuurNet\UDB3\Http\Response\JsonResponse;
use CultuurNet\UDB3\Model\ValueObject\Identity\ItemType;
use CultuurNet\UDB3\Ownership\Commands\RequestOwnership;
use CultuurNet\UDB3\Ownership\Repositories\OwnershipItemNotFound;
use CultuurNet\UDB3\Ownership\OwnershipState;
use CultuurNet\UDB3\Ownership\Repositories\OwnershipItem;
use CultuurNet\UDB3\Ownership\Repositories\Search\OwnershipSearchRepository;
use CultuurNet\UDB3\Ownership\Serializers\RequestOwnershipDenormalizer;
use CultuurNet\UDB3\ReadModel\DocumentDoesNotExist;
Expand Down Expand Up @@ -71,19 +74,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
/** @var RequestOwnership $requestOwnership */
$requestOwnership = $requestBodyParser->parse($request)->getParsedBody();

// Make sure there is no open request for this item and owner
try {
$ownershipItem = $this->ownershipSearchRepository->getByItemIdAndOwnerId(
$requestOwnership->getItemId()->toString(),
$requestOwnership->getOwnerId()->toString()
);

throw ApiProblem::ownerShipAlreadyExists(
'An ownership request for this item and owner already exists with id ' . $ownershipItem->getId()
);
} catch (OwnershipItemNotFound $e) {
}

// Make sure the organizer does exist
if ($requestOwnership->getItemType()->sameAs(ItemType::organizer())) {
try {
Expand All @@ -93,12 +83,31 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}
}

// Make sure the current user is allowed to request ownership for this organizer
$this->ownershipStatusGuard->isAllowedToRequest(
$requestOwnership->getItemId()->toString(),
$requestOwnership->getOwnerId()->toString(),
$this->currentUser
);

// Make sure there is no open request for this item and owner
$existingOwnershipItems = $this->ownershipSearchRepository->search(
new SearchQuery([
new SearchParameter('itemId', $requestOwnership->getItemId()->toString()),
new SearchParameter('ownerId', $requestOwnership->getOwnerId()->toString()),
])
);

/** @var OwnershipItem $ownershipItem */
foreach ($existingOwnershipItems as $ownershipItem) {
if ($ownershipItem->getState() === OwnershipState::requested()->toString() ||
$ownershipItem->getState() === OwnershipState::approved()->toString()) {
throw ApiProblem::ownerShipAlreadyExists(
'An ownership request for this item and owner already exists with id ' . $ownershipItem->getId()
);
}
}

$this->commandBus->dispatch($requestOwnership);

return new JsonResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public function denormalize($data, $class, $format = null, array $context = []):

public function supportsDenormalization($data, $type, $format = null)
{
return $data === AudienceType::class;
return $type === AudienceType::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function denormalize($data, $class, $format = null, array $context = []):

public function supportsDenormalization($data, $type, $format = null): bool
{
return $data === OpeningHour::class;
return $type === OpeningHour::class;
}
}
33 changes: 33 additions & 0 deletions src/Model/Serializer/ValueObject/Price/TariffDenormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace CultuurNet\UDB3\Model\Serializer\ValueObject\Price;

use CultuurNet\UDB3\Model\ValueObject\Price\Tariff;
use CultuurNet\UDB3\Model\ValueObject\Price\TranslatedTariffName;
use CultuurNet\UDB3\MoneyFactory;
use Money\Currency;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

final class TariffDenormalizer implements DenormalizerInterface
{
public function denormalize($data, $class, $format = null, array $context = []): Tariff
{
/** @var TranslatedTariffName $tariffName */
$tariffName = (new TranslatedTariffNameDenormalizer())->denormalize(
$data['name'],
TranslatedTariffName::class
);

return new Tariff(
$tariffName,
MoneyFactory::createFromCents($data['price'], new Currency($data['currency']))
);
}

public function supportsDenormalization($data, $type, $format = null): bool
{
return $type === Tariff::class;
}
}
28 changes: 28 additions & 0 deletions src/Model/Serializer/ValueObject/Price/TariffNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace CultuurNet\UDB3\Model\Serializer\ValueObject\Price;

use CultuurNet\UDB3\Model\ValueObject\Price\Tariff;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class TariffNormalizer implements NormalizerInterface
{
/**
* @param Tariff $tariff
*/
public function normalize($tariff, $format = null, array $context = []): array
{
return [
'name' => (new TranslatedTariffNameNormalizer())->normalize($tariff->getName()),
'price' => $tariff->getPrice()->getAmount(),
'currency' => $tariff->getPrice()->getCurrency()->getName(),
];
}

public function supportsNormalization($data, $format = null): bool
{
return $data === Tariff::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace CultuurNet\UDB3\Model\Serializer\ValueObject\Price;

use CultuurNet\UDB3\Model\Serializer\ValueObject\Translation\TranslatedValueObjectNormalizer;
use CultuurNet\UDB3\Model\ValueObject\Price\TranslatedTariffName;

final class TranslatedTariffNameNormalizer extends TranslatedValueObjectNormalizer
{
public function supportsNormalization($data, $format = null): bool
{
return $data === TranslatedTariffName::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace CultuurNet\UDB3\Model\Serializer\ValueObject\Translation;

use CultuurNet\UDB3\Model\ValueObject\Translation\Language;
use CultuurNet\UDB3\Model\ValueObject\Translation\TranslatedValueObject;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

abstract class TranslatedValueObjectNormalizer implements NormalizerInterface
{
/**
* @param TranslatedValueObject $object
*/
public function normalize($object, $format = null, array $context = []): array
{
$data = [];

/** @var Language $language */
foreach ($object->getLanguages() as $language) {
$data[$language->toString()] = $object->getTranslation($language)->toString();
}

return $data;
}
}
Loading

0 comments on commit b8b472a

Please sign in to comment.