-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from itk-dev/feature/booking-289-update-cache…
…-table-on-create BOOKING-289: Moved cache entry creation into message handler to enabl…
- Loading branch information
Showing
6 changed files
with
162 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Message; | ||
|
||
use App\Entity\Main\Booking; | ||
|
||
class AddBookingToCacheMessage | ||
{ | ||
public function __construct(private readonly Booking $booking, private readonly string $iCalUID) | ||
{ | ||
} | ||
|
||
public function getBooking(): Booking | ||
{ | ||
return $this->booking; | ||
} | ||
|
||
public function getICalUID(): string | ||
{ | ||
return $this->iCalUID; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App\MessageHandler; | ||
|
||
use App\Message\AddBookingToCacheMessage; | ||
use App\Service\BookingServiceInterface; | ||
use App\Service\UserBookingCacheServiceInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException; | ||
|
||
/** | ||
* @see https://github.com/itk-dev/os2forms_selvbetjening/blob/develop/web/modules/custom/os2forms_rest_api/README.md | ||
*/ | ||
#[AsMessageHandler] | ||
class AddBookingToCacheHandler | ||
{ | ||
public function __construct( | ||
private readonly BookingServiceInterface $bookingService, | ||
private readonly UserBookingCacheServiceInterface $userBookingCacheService, | ||
private readonly LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function __invoke(AddBookingToCacheMessage $message): void | ||
{ | ||
$this->logger->info('AddBookingToCacheHandler invoked.'); | ||
|
||
$booking = $message->getBooking(); | ||
$id = $this->bookingService->getBookingIdFromICalUid($message->getICalUID()) ?? null; | ||
|
||
if (null != $id) { | ||
$this->userBookingCacheService->addCacheEntryFromArray([ | ||
'subject' => $booking->getSubject(), | ||
'id' => $id, | ||
'body' => $booking->getBody(), | ||
'start' => $booking->getStartTime(), | ||
'end' => $booking->getEndTime(), | ||
'status' => 'AWAITING_APPROVAL', | ||
'resourceMail' => $booking->getResourceEmail(), | ||
]); | ||
} else { | ||
throw new RecoverableMessageHandlingException(sprintf('Booking id could not be retrieved for booking with iCalUID: %s', $message->getICalUID())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace App\Tests\Handler; | ||
|
||
use App\Entity\Main\Booking; | ||
use App\Message\AddBookingToCacheMessage; | ||
use App\MessageHandler\AddBookingToCacheHandler; | ||
use App\Security\Voter\BookingVoter; | ||
use App\Service\BookingServiceInterface; | ||
use App\Service\UserBookingCacheServiceInterface; | ||
use App\Tests\AbstractBaseApiTestCase; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class AddBookingToCacheHandlerTest extends AbstractBaseApiTestCase | ||
{ | ||
public function testHandlerVoter(): void | ||
{ | ||
$bookingServiceMock = $this->getMockBuilder(BookingServiceInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$bookingServiceMock->expects($this->exactly(1))->method('getBookingIdFromICalUid')->willReturn('abcde'); | ||
|
||
$userBookingCacheServiceMock = $this->getMockBuilder(UserBookingCacheServiceInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$userBookingCacheServiceMock->expects($this->exactly(1))->method('addCacheEntryFromArray'); | ||
|
||
$loggerMock = $this->createMock(LoggerInterface::class); | ||
|
||
$handler = new AddBookingToCacheHandler( | ||
$bookingServiceMock, | ||
$userBookingCacheServiceMock, | ||
$loggerMock, | ||
); | ||
|
||
$booking = new Booking(); | ||
$booking->setBody('test'); | ||
$booking->setSubject('test'); | ||
$booking->setResourceName('test'); | ||
$booking->setResourceEmail('[email protected]'); | ||
$booking->setStartTime(new \DateTime()); | ||
$booking->setEndTime(new \DateTime()); | ||
$booking->setUserName('author1'); | ||
$booking->setUserMail('[email protected]'); | ||
$booking->setMetaData([ | ||
'meta_data_4' => '1, 2, 3', | ||
'meta_data_5' => 'a1, b2, c3', | ||
'meta_data_1' => 'This is a metadata field', | ||
'meta_data_2' => 'This is also metadata', | ||
'meta_data_3' => 'Lorem ipsum metadata', | ||
]); | ||
$booking->setUserPermission(BookingVoter::PERMISSION_CITIZEN); | ||
$booking->setUserId('1234567890'); | ||
|
||
$message = new AddBookingToCacheMessage( | ||
$booking, | ||
'12345' | ||
); | ||
|
||
$handler->__invoke($message); | ||
} | ||
} |