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

Allow editing of unscheduled downtimes that started over 48 hours ago #309

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
31 changes: 24 additions & 7 deletions lib/Gocdb_Services/Downtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,11 @@ public function validate($dtData) {
/**
* Validate the passed downtime data's dates
* @param \DateTime $start Start time
* @return \DateTime $end End date
* @param \DateTime $end End date
* @param \DateTime $oldStart Original start time, if editing a downtime
* @throws \Exception With a human readable message if the dates are invalid
*/
private function validateDates(\DateTime $start, \DateTime $end) {
private function validateDates(\DateTime $start, \DateTime $end, \DateTime $oldStart=null) {
$now = new \DateTime(null, new \DateTimeZone('UTC'));
$start->setTimezone(new \DateTimeZone('UTC'));
$end->setTimezone(new \DateTimeZone('UTC'));
Expand All @@ -302,10 +303,22 @@ private function validateDates(\DateTime $start, \DateTime $end) {
$di = \DateInterval::createFromDateString('2 days');
$twoDaysAgo = $now->sub($di);

// check that start date is later than the threshold limit
// If editing a downtime, check if the start time has changed
$isEditing = ($oldStart !== null);
$startChanged = false;
if ($isEditing) {
$startDiff = abs($oldStart->getTimestamp() - $start->getTimestamp());
$startChanged = ($startDiff !== 0);
}

// Downtimes are only allowed to be declared up to 48 hours in the past
if ($start < $twoDaysAgo) {
throw new \Exception ("Error - The requested start time of the downtime must be within the last 48 hrs"); //"Downtimes can't be declared more than 48 hours in the past.");
if ($start < $twoDaysAgo && !$isEditing) {
throw new \Exception ("Error - The requested start time of the downtime must be within the last 48 hrs");
}

// Prevent start date being edited if downtime began over 48 hours in the past
if ($oldStart < $twoDaysAgo && $startChanged) {
throw new \Exception ("Start time cannot be changed if downtime started over 48 hours ago");
}
}

Expand Down Expand Up @@ -466,7 +479,7 @@ public function editDowntime(\Downtime $dt, $newValues, \User $user = null) {
// check the new start/end times of the downtime are valid according
// to GOCDB business rules
$this->editValidation($dt, $newStart, $newEnd);
$this->validateDates($newStart, $newEnd);
$this->validateDates($newStart, $newEnd, $dt->getStartDate()->setTimezone(new \DateTimeZone('UTC')));

// recalculate classification
$nowPlus1Day = new \DateTime(null, new \DateTimeZone('UTC'));
Expand Down Expand Up @@ -582,6 +595,11 @@ private function editValidation(\Downtime $dt, \DateTime $newStart, \DateTime $n
throw new \Exception("Downtime start date can only be changed to a date in the future"); //Downtime can't start in the past.");
}

// New end date cannot be in the past
if ($newEnd < $now) {
throw new \Exception("Downtime end date can only be changed to a date in the future");
}

$oneDay = \DateInterval::createFromDateString('1 days');
$tomorrow = $now->add($oneDay);

Expand Down Expand Up @@ -620,7 +638,6 @@ public function endDowntime(\Downtime $dt, \User $user = null) {
$sixtySecs = \DateInterval::createFromDateString('1 minutes');
$now = new \DateTime(null, new \DateTimeZone('UTC'));
$sixtySecsFromNow = $now->add($sixtySecs);
//$this->validateDates($dt->getStartDate(), $end);

// dt start date is in the future
if ($dt->getStartDate()->setTimezone(new \DateTimeZone('UTC')) >= $sixtySecsFromNow) {
Expand Down