Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing order attendee count validation #241

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use HiEvents\Events\OrderStatusChangedEvent;
use HiEvents\Jobs\Order\SendOrderDetailsEmailJob;

readonly class SendOrderDetailsEmailListener
class SendOrderDetailsEmailListener
{
public function handle(OrderStatusChangedEvent $changedEvent): void
{
Expand Down
38 changes: 29 additions & 9 deletions backend/app/Services/Handlers/Order/CompleteOrderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
use HiEvents\DomainObjects\Status\AttendeeStatus;
use HiEvents\DomainObjects\Status\OrderPaymentStatus;
use HiEvents\DomainObjects\Status\OrderStatus;
use HiEvents\DomainObjects\TicketDomainObject;
use HiEvents\DomainObjects\TicketPriceDomainObject;
use HiEvents\Events\OrderStatusChangedEvent;
use HiEvents\Exceptions\ResourceConflictException;
use HiEvents\Helper\IdHelper;
use HiEvents\Repository\Eloquent\Value\Relationship;
use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface;
use HiEvents\Repository\Interfaces\OrderRepositoryInterface;
use HiEvents\Repository\Interfaces\QuestionAnswerRepositoryInterface;
Expand All @@ -37,14 +39,14 @@
/**
* @todo - Tidy this up
*/
readonly class CompleteOrderHandler
class CompleteOrderHandler
{
public function __construct(
private OrderRepositoryInterface $orderRepository,
private AttendeeRepositoryInterface $attendeeRepository,
private QuestionAnswerRepositoryInterface $questionAnswersRepository,
private TicketQuantityUpdateService $ticketQuantityUpdateService,
private TicketPriceRepositoryInterface $ticketPriceRepository,
private readonly OrderRepositoryInterface $orderRepository,
private readonly AttendeeRepositoryInterface $attendeeRepository,
private readonly QuestionAnswerRepositoryInterface $questionAnswersRepository,
private readonly TicketQuantityUpdateService $ticketQuantityUpdateService,
private readonly TicketPriceRepositoryInterface $ticketPriceRepository,
)
{
}
Expand Down Expand Up @@ -89,14 +91,14 @@ public function handle(string $orderShortId, CompleteOrderDTO $orderData): Order
private function createAttendees(Collection $attendees, OrderDomainObject $order): void
{
$inserts = [];
$publicIdIndex = 1;

$ticketsPrices = $this->ticketPriceRepository->findWhereIn(
field: TicketPriceDomainObjectAbstract::ID,
values: $attendees->pluck('ticket_price_id')->toArray(),
);

$this->validateTicketPriceIdsMatchOrder($order, $ticketsPrices);
$this->validateAttendees($order, $attendees);

foreach ($attendees as $attendee) {
$ticketId = $ticketsPrices->first(
Expand Down Expand Up @@ -192,7 +194,7 @@ private function createAttendeeQuestions(
private function validateOrder(OrderDomainObject $order): void
{
if ($order->getEmail() !== null) {
throw new ResourceConflictException(__('This order is has already been processed'));
throw new ResourceConflictException(__('This order has already been processed'));
}

if (Carbon::createFromTimeString($order->getReservedUntil())->isPast()) {
Expand All @@ -210,7 +212,11 @@ private function validateOrder(OrderDomainObject $order): void
private function getOrder(string $orderShortId): OrderDomainObject
{
$order = $this->orderRepository
->loadRelation(OrderItemDomainObject::class)
->loadRelation(
new Relationship(
domainObject: OrderItemDomainObject::class,
nested: [new Relationship(TicketDomainObject::class, name: 'ticket')]
))
->findByShortId($orderShortId);

if ($order === null) {
Expand Down Expand Up @@ -258,4 +264,18 @@ private function validateTicketPriceIdsMatchOrder(OrderDomainObject $order, Coll
throw new ResourceConflictException(__('There is an unexpected ticket price ID in the order'));
}
}

/**
* @throws ResourceConflictException
*/
private function validateAttendees(OrderDomainObject $order, Collection $attendees): void
{
$orderAttendeeCount = $order->getOrderItems()->sum(fn(OrderItemDomainObject $orderItem) => $orderItem->getQuantity());

if ($orderAttendeeCount !== $attendees->count()) {
throw new ResourceConflictException(
__('The number of attendees does not match the number of tickets in the order')
);
}
}
}
2 changes: 1 addition & 1 deletion backend/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_DATABASE" value="testing"/>
<!-- <env name="DB_DATABASE" value="testing"/>-->
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
Expand Down
Loading
Loading