bug fixing

This commit is contained in:
Roland Schneider
2021-10-06 18:31:56 +02:00
parent ad2be423d9
commit d26581e338
28 changed files with 527 additions and 83 deletions

View File

@@ -38,6 +38,12 @@ class EventRegistrationManager extends BaseObject
const MAX_SEAT_COUNT_EXCEEDED = 8;
const EVENT_UNAVAILABLE = 9;
const ALREADY_REGISTERED = 10;
const EVENT_START_DATE_IN_PAST = 11;
const EVENT_NOT_FOUND = 12;
const ALREADY_CANCELLED = 13;
const ALREADY_DELETED = 14;
const CANCEL_TIME_LIMIT_REACHED = 15;
public static $STATES = [
self::CARD_NOT_FOUND => 'CARD_NOT_FOUND',
@@ -77,7 +83,7 @@ class EventRegistrationManager extends BaseObject
$activeTickets = $card->getActiveTickets();
if (count($activeTickets) === 0) {
throw new NotFoundHttpException('Ticket not found1', self::TICKET_NOT_FOUND);
throw new BadRequestHttpException('Ticket not found1', self::TICKET_NOT_FOUND);
}
/** @var Event $event */
@@ -86,10 +92,16 @@ class EventRegistrationManager extends BaseObject
throw new NotFoundHttpException('Event not found: ' . $cardEventForm->event_id);
}
if ( isset($event->deleted_at)){
if (isset($event->deleted_at)) {
throw new BadRequestHttpException('Event deleted', self::EVENT_UNAVAILABLE);
}
$now = strtotime("now utc");
if ($event->start < $now) {
throw new BadRequestHttpException('Event start date in past', self::EVENT_START_DATE_IN_PAST);
}
if (!$event->hasFreeSeats()) {
throw new BadRequestHttpException('No free seats', self::NO_FREE_SEATS);
}
@@ -104,8 +116,8 @@ class EventRegistrationManager extends BaseObject
/** @var EventRegistration[] $registrations */
$registrations = $event->getActiveEventRegistrations()->all();
foreach ($registrations as $registration ){
if ($registration->id_customer == $card->customer->id_customer){
foreach ($registrations as $registration) {
if ($registration->id_customer == $card->customer->id_customer) {
throw new BadRequestHttpException("Already registered", self::ALREADY_REGISTERED);
}
}
@@ -113,12 +125,16 @@ class EventRegistrationManager extends BaseObject
$selectedTicket = $eventType->findTicketAllowingEventType($activeTickets);
if (!isset($selectedTicket)) {
throw new NotFoundHttpException('Ticket not found2', self::TICKET_INSUFFICIENT);
throw new BadRequestHttpException('Ticket not found2', self::TICKET_INSUFFICIENT);
}
$selectedTicket->consumeReservationCount(1);
try {
$selectedTicket->consumeReservationCount(1);
} catch (\Exception $e) {
throw new BadRequestHttpException('Max ticket seat count exceeded', self::MAX_SEAT_COUNT_EXCEEDED);
}
$selectedTicket->save();
$selectedTicket->save(false);
$registration = new EventRegistration();
$registration->id_event = $event->id;
@@ -174,7 +190,7 @@ class EventRegistrationManager extends BaseObject
{
$registration = EventRegistration::find()->andWhere(['id' => $idRegistration])->one();
if ( $registration === null) {
if ($registration === null) {
throw new NotFoundHttpException('The requested registration does not exist.');
}
return $registration;
@@ -187,15 +203,44 @@ class EventRegistrationManager extends BaseObject
public function cancelRegistration($registration)
{
if (isset($registration->canceled_at)) {
throw new ServerErrorHttpException('The registration is already canceled');
throw new BadRequestHttpException('The registration is already canceled', self::ALREADY_CANCELLED);
}
if (isset($registration->deleted_at)) {
throw new ServerErrorHttpException('The reservation is already deleted');
throw new BadRequestHttpException('The reservation is already deleted', self::ALREADY_DELETED);
}
$registration->canceled_at = date('Y-m-d H:i:s');
$registration->save(false);
$event = Event::findOne($registration->id_event);
if (!isset($event)) {
throw new BadRequestHttpException('The reservation is already deleted', self::EVENT_NOT_FOUND);
}
$tx = \Yii::$app->db->beginTransaction();
try {
$now = strtotime("now UTC");
$timeUntilEventStart = $event->start - $now;
if ( $timeUntilEventStart < 30 * 60){
throw new BadRequestHttpException('The reservation is already deleted', self::CANCEL_TIME_LIMIT_REACHED);
}
$registration->canceled_at = date('Y-m-d H:i:s', $now);
$registration->save(false);
$ticket = Ticket::findOne($registration->id_ticket);
if (!isset($ticket)) {
throw new BadRequestHttpException('The ticket is not found');
}
$ticket->restoreReservationCount(1);
$ticket->save(false);
$tx->commit();
} catch (\Exception $e) {
$tx->rollBack();
throw $e;
}
}
@@ -240,7 +285,7 @@ class EventRegistrationManager extends BaseObject
// if event has no registrations
// we can simply delete it from db
// ////////////////////////////////
if ( count($registrations) === 0 ) {
if (count($registrations) === 0) {
$event->delete();
} else {
// /////////////////////////////