From f0bf8684e44d1c0a02dfc72562d1c9d38ba280bf Mon Sep 17 00:00:00 2001 From: Roland Schneider Date: Fri, 8 Oct 2021 23:15:49 +0200 Subject: [PATCH] improve event deletion and event registration cancelation --- common/manager/EventRegistrationManager.php | 104 +++++++++--------- .../event/controllers/EventController.php | 2 +- common/modules/event/views/event/index.php | 3 + .../month-calendar-event.component.ts | 31 +++--- .../event-details.component.html | 4 + .../event-details/event-details.component.ts | 2 +- .../app/src/app/services/event.service.ts | 1 + customerapi/controllers/EventController.php | 2 + .../EventRegistrationController.php | 2 +- .../models/available/EventAvailable.php | 6 +- .../models/details/EventDetailsView.php | 2 + customerapi/models/dto/EventDTO.php | 5 +- 12 files changed, 96 insertions(+), 68 deletions(-) diff --git a/common/manager/EventRegistrationManager.php b/common/manager/EventRegistrationManager.php index b965b25..c55c095 100644 --- a/common/manager/EventRegistrationManager.php +++ b/common/manager/EventRegistrationManager.php @@ -194,12 +194,11 @@ class EventRegistrationManager extends BaseObject */ public function loadRegistration($idRegistration, $idCustomer) { - - $query = EventRegistration::find() + $query = EventRegistration::find() ->andWhere(['id' => $idRegistration]); - if ( isset($idCustomer)){ - $query->andWhere(['id_customer' => $idCustomer]) - } + if (isset($idCustomer)) { + $query->andWhere(['id_customer' => $idCustomer]); + } $registration = $query->one(); if ($registration === null) { throw new NotFoundHttpException('The requested registration does not exist.'); @@ -207,6 +206,18 @@ class EventRegistrationManager extends BaseObject return $registration; } + public function cancelRegistrationTX($registration, $idCustomer, $reason) + { + $tx = \Yii::$app->db->beginTransaction(); + try { + $this->cancelRegistration($registration, $idCustomer, $reason); + $tx->commit(); + } catch (\Exception $e) { + $tx->rollBack(); + throw $e; + } + } + /** * @param EventRegistration $registration * @throws ServerErrorHttpException @@ -230,7 +241,7 @@ class EventRegistrationManager extends BaseObject if (!isset($event)) { throw new BadRequestHttpException('The reservation is already deleted', self::EVENT_NOT_FOUND); } - $tx = \Yii::$app->db->beginTransaction(); + $now = strtotime("now UTC"); if ($reason != EventRegistration::CANCEL_REASON_CUSTOMER) { @@ -240,51 +251,43 @@ class EventRegistrationManager extends BaseObject } } - try { - + $now = strtotime("now"); + // if user cancels then cancel_at will be set + if ( $reason == EventRegistration::CANCEL_REASON_CUSTOMER){ $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; + }else{ + // otherwise ( e.g. event is deleted ) the delete_at will be set + $registration->deleted_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); + } -// /** -// * @param EventRegistration $registration -// * @return bool -// * @throws \yii\base\Exception -// */ -// public function deleteRegistration($registration) -// { -// if (isset($registration->deleted_at)) { -// return false; -// } -// -//// $ticket = Ticket::findOne(['id_ticket' => $registration->id_ticket]); -//// if( !isset($ticket ) ) { -//// throw new \yii\base\Exception('Ticket not found: ' . $registration->id_ticket); -//// } -//// -//// $ticket->restoreReservationCount(1); -//// $ticket->save(false); -// -// $registration->deleted_at = date('Y-m-d H:i:s'); -// return $registration->save(false); -// -// } + public function findAllRegistrations($idEvent, $idCustomer = null) + { + $registrations = + EventRegistration::find() + ->andWhere(['id_event' => $idEvent])->all(); + if (isset($idCustomer)) { + $registrations = EventRegistration::filterForCustomer($registrations, $idCustomer); + } + return $registrations; + } + + public function findActiveRegistrations($idEvent, $idCustomer = null) + { + $registrations = $this->findAllRegistrations($idEvent, $idCustomer); + return EventRegistration::filterActive($registrations); + } /** * Delete an event @@ -298,12 +301,15 @@ class EventRegistrationManager extends BaseObject $tx = $db->beginTransaction(); try { - $registrations = $event->getEventRegistrations()->all(); + $eventRegistrationManager = new EventRegistrationManager(); + $allRegistrations = $eventRegistrationManager->findAllRegistrations($event->id); + $activeRegistrations = $eventRegistrationManager->findActiveRegistrations($event->id); + // //////////////////////////////// // if event has no registrations // we can simply delete it from db // //////////////////////////////// - if (count($registrations) === 0) { + if (count($allRegistrations) === 0) { $event->delete(); } else { // ///////////////////////////// @@ -312,7 +318,7 @@ class EventRegistrationManager extends BaseObject // ///////////////////////////// $event->deleted_at = date('Y-m-d H:i:s'); $event->save(false); - foreach ($registrations as $registration) { + foreach ($activeRegistrations as $registration) { if (!isset($registration->deleted_at)) { /** @var EventRegistration $registration */ $this->cancelRegistration($registration, null, EventRegistration::CANCEL_REASON_EVENT); diff --git a/common/modules/event/controllers/EventController.php b/common/modules/event/controllers/EventController.php index b8a5d9b..623c7c0 100644 --- a/common/modules/event/controllers/EventController.php +++ b/common/modules/event/controllers/EventController.php @@ -230,7 +230,7 @@ class EventController extends Controller $tx = $db->beginTransaction(); try { $registration = $eventRegistrationManager->loadRegistration($id,null); - $eventRegistrationManager->cancelRegistration($registration,null,EventRegistration::CANCEL_REASON_CUSTOMER); + $eventRegistrationManager->cancelRegistrationTX($registration,null,EventRegistration::CANCEL_REASON_CUSTOMER); $tx->commit(); return $this->redirect(['view', 'id' => $registration->id_event]); } catch (Exception $ex) { diff --git a/common/modules/event/views/event/index.php b/common/modules/event/views/event/index.php index 186c6a2..8b7e45b 100644 --- a/common/modules/event/views/event/index.php +++ b/common/modules/event/views/event/index.php @@ -153,6 +153,9 @@ $indexTableTemplateButtons[] = '{reserve-card}'; 'aria-label' => Yii::t('yii', 'Register'), 'data-pjax' => '0', ]; + if ( isset($model['event_deleted_at']) ){ + return ""; + } return Html::a('', $url, $options); }, // 'equipment-types-assignment' => function ($url, $model, $key) { diff --git a/customer/app/src/app/components/month-calendar-event/month-calendar-event.component.ts b/customer/app/src/app/components/month-calendar-event/month-calendar-event.component.ts index 01b0886..8d4d2be 100644 --- a/customer/app/src/app/components/month-calendar-event/month-calendar-event.component.ts +++ b/customer/app/src/app/components/month-calendar-event/month-calendar-event.component.ts @@ -1,6 +1,6 @@ import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import {Event} from "../../services/event.service"; -import { MonthCalendarEvent} from "../month-calendar/month-calendar.component"; +import {MonthCalendarEvent} from "../month-calendar/month-calendar.component"; @Component({ selector: 'app-month-calendar-event', @@ -38,7 +38,7 @@ export class MonthCalendarEventComponent implements OnInit { } maySelect() { - return this.isRegistered() ? true : this.event.hasFreeSeats; + return this.isRegistered() ? true : this.event.hasFreeSeats; } isRegistered() { @@ -46,27 +46,32 @@ export class MonthCalendarEventComponent implements OnInit { } isDisabled() { - return !this.isRegistered() && !this.event.hasFreeSeats; + return !this.isRegistered() && !this.event.hasFreeSeats; } getEventThemeClass(): string { const theme = this.event?.eventType?.theme; let styleClass = "bg-dark"; - if (this.isDisabled()) { + if (this.event.deleted_at) { styleClass = "bg-secondary"; } else { - if (theme != null && theme.length > 0) { - if (this.isRegistered()) { - styleClass += " event-theme-active-" + theme; - } else { - styleClass += " event-theme-normal-" + theme; - } + + if (this.isDisabled()) { + styleClass = "bg-secondary"; } else { - if (this.isRegistered()) { - styleClass += " bg-primary"; + if (theme != null && theme.length > 0) { + if (this.isRegistered()) { + styleClass += " event-theme-active-" + theme; + } else { + styleClass += " event-theme-normal-" + theme; + } } else { - styleClass += " bg-dark"; + if (this.isRegistered()) { + styleClass += " bg-primary"; + } else { + styleClass += " bg-dark"; + } } } } diff --git a/customer/app/src/app/pages/event-details/event-details.component.html b/customer/app/src/app/pages/event-details/event-details.component.html index 2b15954..3e8f610 100644 --- a/customer/app/src/app/pages/event-details/event-details.component.html +++ b/customer/app/src/app/pages/event-details/event-details.component.html @@ -29,6 +29,10 @@
Terem
{{event?.room?.name }}
+
+
Törölve
+
{{ event.deleted_at | eventDate:'datetime' }}
+
diff --git a/customer/app/src/app/pages/event-details/event-details.component.ts b/customer/app/src/app/pages/event-details/event-details.component.ts index 5cb5443..1832244 100644 --- a/customer/app/src/app/pages/event-details/event-details.component.ts +++ b/customer/app/src/app/pages/event-details/event-details.component.ts @@ -49,7 +49,7 @@ export class EventDetailsComponent implements OnInit { } mayRegister(event: Event) { - return event.hasFreeSeats && event.registrations.length == 0; + return event.hasFreeSeats && !event.deleted_at && event.registrations.length == 0; } mayCancel(event: Event) { diff --git a/customer/app/src/app/services/event.service.ts b/customer/app/src/app/services/event.service.ts index 5af08a3..aa2f14c 100644 --- a/customer/app/src/app/services/event.service.ts +++ b/customer/app/src/app/services/event.service.ts @@ -58,6 +58,7 @@ export interface Event { name: string; start: number; end: number; + deleted_at?: number; trainer?: Trainer; seat_count: number; reservationCount: number; diff --git a/customerapi/controllers/EventController.php b/customerapi/controllers/EventController.php index a02a02f..2fe16e0 100644 --- a/customerapi/controllers/EventController.php +++ b/customerapi/controllers/EventController.php @@ -10,6 +10,7 @@ namespace customerapi\controllers; use common\helpers\AppArrayHelper; +use common\helpers\AppDateTimeHelper; use common\models\Event; use common\models\EventRegistration; use common\models\EventType; @@ -140,6 +141,7 @@ class EventController extends \customerapi\controllers\CustomerApiController $result->id = $event->id; $result->start = $event->start; $result->end = $event->end; + $result->deleted_at =AppDateTimeHelper::convertMySqlDatetimeToPhpInteger( $event->deleted_at); $result->seatCount = $event->seat_count; $result->trainer = TrainerDetailsView::findOne($event->id_trainer); $result->room = RoomDetailsView::findOne($event->id_room); diff --git a/customerapi/controllers/EventRegistrationController.php b/customerapi/controllers/EventRegistrationController.php index 5c19606..f373a91 100644 --- a/customerapi/controllers/EventRegistrationController.php +++ b/customerapi/controllers/EventRegistrationController.php @@ -78,7 +78,7 @@ class EventRegistrationController extends CustomerApiController public function actionCancel($idRegistration) { $manager = new \common\manager\EventRegistrationManager(); $registration = $manager->loadRegistration($idRegistration,\Yii::$app->user->id); - $manager->cancelRegistration($registration,\Yii::$app->user->id, EventRegistration::CANCEL_REASON_CUSTOMER); + $manager->cancelRegistrationTX($registration,\Yii::$app->user->id, EventRegistration::CANCEL_REASON_CUSTOMER); $registration = $manager->loadRegistration($idRegistration,\Yii::$app->user->id); return $this->asJson($registration); } diff --git a/customerapi/models/available/EventAvailable.php b/customerapi/models/available/EventAvailable.php index 3c04ef1..8737b2b 100644 --- a/customerapi/models/available/EventAvailable.php +++ b/customerapi/models/available/EventAvailable.php @@ -4,6 +4,7 @@ namespace customerapi\models\available; +use common\helpers\AppDateTimeHelper; use common\models\Event; use common\models\EventRegistration; use common\models\EventType; @@ -41,8 +42,8 @@ class EventAvailable extends Model $available->end = $event->end; $available->seat_count = $event->seat_count; $available->created_at = $event->created_at; - $available->updated_at = $event->updated_at; - $available->deleted_at = $event->deleted_at; + $available->updated_at = AppDateTimeHelper::convertMySqlDatetimeToPhpInteger($event->updated_at); + $available->deleted_at = AppDateTimeHelper::convertMySqlDatetimeToPhpInteger($event->deleted_at); $available->active = $event->active; return $available; @@ -55,6 +56,7 @@ class EventAvailable extends Model "id" => "id", "start" => "start", "end" => "end", + "deleted_at" => "deleted_at", "seat_count" => "seat_count", "active" => "active", "reservationCount" => "registrationCount", diff --git a/customerapi/models/details/EventDetailsView.php b/customerapi/models/details/EventDetailsView.php index 052e16c..9fb3e0c 100644 --- a/customerapi/models/details/EventDetailsView.php +++ b/customerapi/models/details/EventDetailsView.php @@ -24,6 +24,7 @@ class EventDetailsView extends Model // extends Event public $id; public $start; public $end; + public $deleted_at; public $seatCount; // total count of registrations public $reservationCount; @@ -50,6 +51,7 @@ class EventDetailsView extends Model // extends Event "start" => "start", "end" => "end", "seat_count" => "seatCount", + "deleted_at" =>"deleted_at", "reservationCount" => "reservationCount", "registrations" => "registrations", "hasFreeSeats" => "hasFreeSeats" diff --git a/customerapi/models/dto/EventDTO.php b/customerapi/models/dto/EventDTO.php index f5f103b..550255b 100644 --- a/customerapi/models/dto/EventDTO.php +++ b/customerapi/models/dto/EventDTO.php @@ -1,6 +1,7 @@ "id", "start" => "start", "end" => "end", + "deleted_at" => "deleted_at", "seat_count" => "seat_count", "active" => "active", "reservationCount" => "registrationCount", @@ -53,7 +55,8 @@ class EventDTO extends \yii\base\Model $dto->seat_count = $event->seat_count; $dto->created_at = $event->created_at; $dto->updated_at = $event->updated_at; - $dto->deleted_at = $event->deleted_at; + $dto->deleted_at = AppDateTimeHelper::convertMySqlDatetimeToPhpInteger($event->deleted_at); + $dto->active = $event->active; return $dto;