From 1ae69379660a80cf9c206a38c417859819d56c7b Mon Sep 17 00:00:00 2001 From: smarcet Date: Wed, 11 Dec 2019 21:49:15 -0300 Subject: [PATCH] Added summit booking period new fields for summit entity * begin_allow_booking_date * end_allow_booking_date Change-Id: Ib4c297eaaf7d01bc3418c582e57f4c0a1e779d60 --- .../SummitValidationRulesFactory.php | 4 ++ .../Filters/DoctrineHavingFilterMapping.php | 64 +++++++++++++------ .../Summit/AdminSummitSerializer.php | 4 ++ .../Summit/SummitSerializer.php | 6 +- .../Summit/Factories/SummitFactory.php | 17 +++++ .../Locations/SummitBookableVenueRoom.php | 4 +- app/Models/Foundation/Summit/Summit.php | 59 +++++++++++++++++ .../model/Version20190824125218.php | 17 +++-- .../model/Version20191212002736.php | 54 ++++++++++++++++ 9 files changed, 201 insertions(+), 28 deletions(-) create mode 100644 database/migrations/model/Version20191212002736.php diff --git a/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitValidationRulesFactory.php b/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitValidationRulesFactory.php index 9508fb71..056b6266 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitValidationRulesFactory.php +++ b/app/Http/Controllers/Apis/Protected/Summit/Factories/SummitValidationRulesFactory.php @@ -49,6 +49,8 @@ final class SummitValidationRulesFactory 'api_feed_type' => sprintf('nullable|in:%s',implode(',', Summit::$valid_feed_types)), 'api_feed_url' => 'nullable|string|url|required_with:api_feed_type', 'api_feed_key' => 'nullable|string|required_with:api_feed_type', + 'begin_allow_booking_date' => 'nullable|date_format:U', + 'end_allow_booking_date' => 'nullable|required_with:begin_allow_booking_date|date_format:U|after_or_equal:begin_allow_booking_date', ]; } @@ -80,6 +82,8 @@ final class SummitValidationRulesFactory 'api_feed_type' => sprintf('nullable|in:%s',implode(',', Summit::$valid_feed_types)), 'api_feed_url' => 'nullable|string|url|required_with:api_feed_type', 'api_feed_key' => 'nullable|string|required_with:api_feed_type', + 'begin_allow_booking_date' => 'nullable|date_format:U', + 'end_allow_booking_date' => 'nullable|required_with:begin_allow_booking_date|date_format:U|after_or_equal:begin_allow_booking_date', ]; } } \ No newline at end of file diff --git a/app/Http/Utils/Filters/DoctrineHavingFilterMapping.php b/app/Http/Utils/Filters/DoctrineHavingFilterMapping.php index f63d56bb..ee12d474 100644 --- a/app/Http/Utils/Filters/DoctrineHavingFilterMapping.php +++ b/app/Http/Utils/Filters/DoctrineHavingFilterMapping.php @@ -11,7 +11,9 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ + use Doctrine\ORM\QueryBuilder; + /** * Class DoctrineHavingFilterMapping * @package utils @@ -27,6 +29,7 @@ class DoctrineHavingFilterMapping extends DoctrineFilterMapping * @var string */ protected $having; + /** * DoctrineFilterMapping constructor. * @param string $condition @@ -35,7 +38,7 @@ class DoctrineHavingFilterMapping extends DoctrineFilterMapping { parent::__construct($condition); $this->groupBy = $groupBy; - $this->having = $having; + $this->having = $having; } /** @@ -43,34 +46,57 @@ class DoctrineHavingFilterMapping extends DoctrineFilterMapping * @param FilterElement $filter * @return QueryBuilder */ - public function apply(QueryBuilder $query, FilterElement $filter){ - $param_count = $query->getParameters()->count() + 1; + public function apply(QueryBuilder $query, FilterElement $filter) + { + $param_count = $query->getParameters()->count(); $where = $this->where; $has_param = false; - $value = $filter->getValue(); - if(strstr($where,":value")) { - $where = str_replace(":value", ":value_" . $param_count, $where); - $has_param = true; + $value = $filter->getValue(); + + if (!empty($where)) { + if (strstr($where, ":value")) { + ++$param_count; + $where = str_replace(":value", ":value_" . $param_count, $where); + $has_param = true; + } + + if (strstr($where, ":operator")) + $where = str_replace(":operator", $filter->getOperator(), $where); + + $query = $query->andWhere($where); + + if ($has_param) { + $query = $query->setParameter(":value_" . $param_count, $value); + } } - if(strstr($where,":operator")) - $where = str_replace(":operator", $filter->getOperator(), $where); - - $query = $query->andWhere($where); - - if($has_param){ - $query = $query->setParameter(":value_".$param_count, $value); + if (!empty($this->groupBy)) { + $query = $query->addGroupBy($this->groupBy); } - $query = $query->addGroupBy($this->groupBy); + if (!empty($this->having)) { + $has_param = false; + if (strstr($this->having, ":value_count") && is_array($value)) { + $this->having = str_replace(":value_count", count($value), $this->having); + } - if(strstr($this->having,":value_count") && is_array($value)){ - $this->having = str_replace(":value_count", count($value), $this->having); + if (strstr($this->having, ":value")) { + ++$param_count; + $this->having = str_replace(":value", ":value_" . $param_count, $this->having); + $has_param = true; + } + + if (strstr($this->having, ":operator")) + $this->having = str_replace(":operator", $filter->getOperator(), $this->having); + + if ($has_param) { + $query = $query->setParameter(":value_" . $param_count, $value); + } + + $query = $query->andHaving($this->having); } - $query = $query->andHaving($this->having); - return $query; } } \ No newline at end of file diff --git a/app/ModelSerializers/Summit/AdminSummitSerializer.php b/app/ModelSerializers/Summit/AdminSummitSerializer.php index da2b7c0b..776d8d7c 100644 --- a/app/ModelSerializers/Summit/AdminSummitSerializer.php +++ b/app/ModelSerializers/Summit/AdminSummitSerializer.php @@ -26,5 +26,9 @@ final class AdminSummitSerializer extends SummitSerializer 'ExternalSummitId' => 'external_summit_id:json_string', 'CalendarSyncName' => 'calendar_sync_name:json_string', 'CalendarSyncDesc' => 'calendar_sync_desc:json_string', + // External Feeds + 'ApiFeedType' => 'api_feed_type:json_string', + 'ApiFeedUrl' => 'api_feed_url:json_string', + 'ApiFeedKey' => 'api_feed_key:json_string', ]; } \ No newline at end of file diff --git a/app/ModelSerializers/Summit/SummitSerializer.php b/app/ModelSerializers/Summit/SummitSerializer.php index 010446dc..b54fc95c 100644 --- a/app/ModelSerializers/Summit/SummitSerializer.php +++ b/app/ModelSerializers/Summit/SummitSerializer.php @@ -57,10 +57,8 @@ class SummitSerializer extends SilverStripeSerializer 'MeetingRoomBookingEndTime' => 'meeting_room_booking_end_time:datetime_epoch', 'MeetingRoomBookingSlotLength' => 'meeting_room_booking_slot_length:json_int', 'MeetingRoomBookingMaxAllowed' => 'meeting_room_booking_max_allowed:json_int', - // External Feeds - 'ApiFeedType' => 'api_feed_type:json_string', - 'ApiFeedUrl' => 'api_feed_url:json_string', - 'ApiFeedKey' => 'api_feed_key:json_string', + 'BeginAllowBookingDate' => 'begin_allow_booking_date:datetime_epoch', + 'EndAllowBookingDate' => 'end_allow_booking_date:datetime_epoch', ]; protected static $allowed_relations = [ diff --git a/app/Models/Foundation/Summit/Factories/SummitFactory.php b/app/Models/Foundation/Summit/Factories/SummitFactory.php index 0ceb055f..9666c18d 100644 --- a/app/Models/Foundation/Summit/Factories/SummitFactory.php +++ b/app/Models/Foundation/Summit/Factories/SummitFactory.php @@ -69,6 +69,23 @@ final class SummitFactory $summit->setCalendarSyncDesc(trim($data['calendar_sync_desc'])); } + if(array_key_exists('begin_allow_booking_date', $data) && array_key_exists('end_allow_booking_date', $data)) { + if (isset($data['begin_allow_booking_date']) && isset($data['end_allow_booking_date'])) { + $start_datetime = intval($data['begin_allow_booking_date']); + $start_datetime = new \DateTime("@$start_datetime"); + $start_datetime->setTimezone($summit->getTimeZone()); + $end_datetime = intval($data['end_allow_booking_date']); + $end_datetime = new \DateTime("@$end_datetime"); + $end_datetime->setTimezone($summit->getTimeZone()); + // set local time from UTC + $summit->setBeginAllowBookingDate($start_datetime); + $summit->setEndAllowBookingDate($end_datetime); + } + else{ + $summit->clearAllowBookingDates(); + } + } + if(array_key_exists('start_date', $data) && array_key_exists('end_date', $data)) { if (isset($data['start_date']) && isset($data['end_date'])) { $start_datetime = intval($data['start_date']); diff --git a/app/Models/Foundation/Summit/Locations/SummitBookableVenueRoom.php b/app/Models/Foundation/Summit/Locations/SummitBookableVenueRoom.php index 2142bd43..0952266f 100644 --- a/app/Models/Foundation/Summit/Locations/SummitBookableVenueRoom.php +++ b/app/Models/Foundation/Summit/Locations/SummitBookableVenueRoom.php @@ -67,8 +67,10 @@ class SummitBookableVenueRoom extends SummitVenueRoom */ public function addReservation(SummitRoomReservation $reservation){ - $criteria = Criteria::create(); + if(!$this->summit->isBookingPeriodOpen()) + throw new ValidationException(sprintf("booking period is not open for summit %s", $this->summit->getId())); + $criteria = Criteria::create(); $start_date = $reservation->getStartDatetime(); $end_date = $reservation->getEndDatetime(); diff --git a/app/Models/Foundation/Summit/Summit.php b/app/Models/Foundation/Summit/Summit.php index 17745d86..09518e34 100644 --- a/app/Models/Foundation/Summit/Summit.php +++ b/app/Models/Foundation/Summit/Summit.php @@ -200,6 +200,18 @@ class Summit extends SilverstripeBaseModel */ private $meeting_booking_room_allowed_attributes; + /** + * @ORM\Column(name="BeginAllowBookingDate", type="datetime") + * @var \DateTime + */ + private $begin_allow_booking_date; + + /** + * @ORM\Column(name="EndAllowBookingDate", type="datetime") + * @var \DateTime + */ + private $end_allow_booking_date; + /** * @ORM\OneToMany(targetEntity="SummitEvent", mappedBy="summit", cascade={"persist"}, orphanRemoval=true, fetch="EXTRA_LAZY") */ @@ -2738,4 +2750,51 @@ SQL; $this->api_feed_key = $api_feed_key; } + /** + * @return DateTime + */ + public function getBeginAllowBookingDate(): ?DateTime + { + return $this->begin_allow_booking_date; + } + + /** + * @param DateTime $begin_allow_booking_date + */ + public function setBeginAllowBookingDate(DateTime $begin_allow_booking_date): void + { + $this->begin_allow_booking_date = $begin_allow_booking_date; + } + + /** + * @return DateTime + */ + public function getEndAllowBookingDate(): ?DateTime + { + return $this->end_allow_booking_date; + } + + /** + * @param DateTime $end_allow_booking_date + */ + public function setEndAllowBookingDate(DateTime $end_allow_booking_date): void + { + $this->end_allow_booking_date = $end_allow_booking_date; + } + + public function clearAllowBookingDates():void{ + $this->begin_allow_booking_date = $this->end_allow_booking_date = null; + } + + /** + * @return bool + */ + public function isBookingPeriodOpen():bool{ + $now_utc = new \DateTime('now', new \DateTimeZone('UTC')); + if(!is_null($this->begin_allow_booking_date) && !is_null($this->end_allow_booking_date)){ + return $now_utc >= $this->begin_allow_booking_date && $now_utc <= $this->end_allow_booking_date; + } + + return false; + } } diff --git a/database/migrations/model/Version20190824125218.php b/database/migrations/model/Version20190824125218.php index ba4eb3bc..af771a91 100644 --- a/database/migrations/model/Version20190824125218.php +++ b/database/migrations/model/Version20190824125218.php @@ -1,7 +1,16 @@ -hasTable("Summit") && !$builder->hasColumn("Summit", "BeginAllowBookingDate")) { + $builder->table('Summit', function (Table $table) { + $table->dateTime("BeginAllowBookingDate")->setNotnull(false); + $table->dateTime("EndAllowBookingDate")->setNotnull(false); + }); + } + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + $builder = new Builder($schema); + if($schema->hasTable("Summit") && $builder->hasColumn("Summit", "BeginAllowBookingDate")) { + $builder->table('Member', function (Table $table) { + $table->dropColumn("BeginAllowBookingDate"); + $table->dropColumn("EndAllowBookingDate"); + }); + } + } +}