diff --git a/model/dao/TaskDAO/PostgreSQLTaskDAO.php b/model/dao/TaskDAO/PostgreSQLTaskDAO.php index 2ea6b8f0b..ada16c957 100644 --- a/model/dao/TaskDAO/PostgreSQLTaskDAO.php +++ b/model/dao/TaskDAO/PostgreSQLTaskDAO.php @@ -638,7 +638,7 @@ public function getVacationsDates(UserVO $userVO, DateTime $initDate = NULL, Dat * @throws {@link SQLQueryErrorException} */ public function partialUpdate(DirtyTaskVO $taskVO) { - $affectedRows = 0; + $result = new OperationResult(false); if($taskVO->getId() != "") { $currTaskVO = $this->getById($taskVO->getId()); @@ -703,11 +703,27 @@ public function partialUpdate(DirtyTaskVO $taskVO) { $sql = $sql . ", updated_at=now() WHERE id=".$taskVO->getId(); $res = pg_query($this->connect, $sql); - if ($res == NULL) throw new SQLQueryErrorException(pg_last_error()); - $affectedRows = pg_affected_rows($res); + if ($res == NULL) { + $errorMessage = pg_last_error(); + $resultMessage = "Error updating task:\n"; + if(strpos($errorMessage, "end_after_init_task")) { + $resultMessage .= "Start time later than end time."; + } + else { + $resultMessage .= $errorMessage; + } + $result->setMessage($resultMessage); + $result->setIsSuccessful(false); + $result->setResponseCode(500); + } + else if (pg_affected_rows($res) == 1) { + $result->setIsSuccessful(true); + $result->setMessage('Task created successfully.'); + $result->setResponseCode(201); + } } - return $affectedRows; + return $result; } /** Update a batch of DirtyTaskVO objects. @@ -720,22 +736,23 @@ public function partialUpdate(DirtyTaskVO $taskVO) { * @param array $tasks the Task value objects we want to update. Must be * DirtyTaskVO objects which contain also the information about * which fields must be updated. - * @return int the number of rows that have been affected. It should match - * the length of $tasks, otherwise there was an error. - * @throws {@link SQLQueryErrorException}, {@link SQLUniqueViolationException} + * @return array OperationResult the array of {@link OperationResult} with information about operation status */ public function batchPartialUpdate($tasks) { if (!$this->checkOverlappingWithDBTasks($tasks)) { - return 0; + $result = new OperationResult(false); + $result->setErrorNumber(10); + $result->setMessage("Task creation failed:\nDetected overlapping times."); + $result->setResponseCode(500); + return array($result); } - $affectedRows = 0; - + $results = array(); foreach ($tasks as $task) { - $affectedRows += $this->partialUpdate($task); + $results[] = $this->partialUpdate($task); } - return $affectedRows; + return $results; } /** diff --git a/model/dao/TaskDAO/TaskDAO.php b/model/dao/TaskDAO/TaskDAO.php index d202a6974..cce3c9c98 100644 --- a/model/dao/TaskDAO/TaskDAO.php +++ b/model/dao/TaskDAO/TaskDAO.php @@ -224,8 +224,7 @@ public abstract function getVacationsDates(UserVO $userVO, DateTime $initDate = * @param DirtyTaskVO $taskVO the {@link TaskVO} with the data we want to * update on database and the information about which fields must be * updated. - * @return int the number of rows that have been affected (it should be 1). - * @throws {@link SQLQueryErrorException} + * @return OperationResult the result {@link OperationResult} with information about operation status */ public abstract function partialUpdate(DirtyTaskVO $taskVO); @@ -234,8 +233,7 @@ public abstract function partialUpdate(DirtyTaskVO $taskVO); * Equivalent to {@see partialUpdate} for arrays of tasks. * * @param array $tasks array of {@link DirtyTaskVO} objects to be updated. - * @return int the number of rows that have been affected (it should be - * equal to the size of $tasks). + * @return array OperationResult the array of {@link OperationResult} with information about operation */ public abstract function batchPartialUpdate($tasks); diff --git a/model/facade/TasksFacade.php b/model/facade/TasksFacade.php index b04ca52fd..267a38e82 100644 --- a/model/facade/TasksFacade.php +++ b/model/facade/TasksFacade.php @@ -129,11 +129,10 @@ static function DeleteReports($tasks) { * @param DirtyTaskVO $task the Task value object we want to update. Must be * a DirtyTaskVO object which contains also the information about * which fields must be updated. - * @return int it just indicates if there was any error (-1) or not (0). - * @throws {@link SQLQueryErrorException}, {@link SQLUniqueViolationException} + * @return OperationResult the result {@link OperationResult} with information about operation status */ static function PartialUpdateReport(DirtyTaskVO $task) { - return TasksFacade::PartialUpdateReports(array($task)); + return TasksFacade::PartialUpdateReports(array($task))[0]; } /** Partial Update Tasks Function @@ -144,8 +143,7 @@ static function PartialUpdateReport(DirtyTaskVO $task) { * @param array $tasks the Task value objects we want to update. Must be * a DirtyTaskVO object which contains also the information about * which fields must be updated. - * @return int it just indicates if there was any error (-1) or not (0). - * @throws {@link SQLQueryErrorException}, {@link SQLUniqueViolationException} + * @return array OperationResult the array of {@link OperationResult} with information about operation status */ static function PartialUpdateReports($tasks) { $action = new PartialUpdateTasksAction($tasks); diff --git a/model/facade/action/PartialUpdateTasksAction.php b/model/facade/action/PartialUpdateTasksAction.php index 4a262dd2c..47997be72 100644 --- a/model/facade/action/PartialUpdateTasksAction.php +++ b/model/facade/action/PartialUpdateTasksAction.php @@ -72,20 +72,25 @@ public function __construct($tasks) { * * Runs the action itself. * - * @return int it just indicates if there was any error (-1) - * or not (0). + * @return array OperationResult the array of {@link OperationResult} with information about operation status */ protected function doExecute() { $configDao = DAOFactory::getConfigDAO(); $taskDao = DAOFactory::getTaskDAO(); $projectDAO = DAOFactory::getProjectDAO(); $discardedTasks = array(); + $discardedResults = array(); //first check permission on task write foreach ($this->tasks as $i => $task) { // Do not allow assigning a task to a locked date if ($task->isDateDirty()) { if(!$configDao->isWriteAllowedForDate($task->getDate())) { + $result = new OperationResult(false); + $result->setErrorNumber(20); + $result->setResponseCode(500); + $result->setMessage("Error updating task:\nNot allowed to write to date."); + $discardedResults[] = $result; $discardedTasks[] = $task; unset($this->tasks[$i]); continue; @@ -94,16 +99,36 @@ protected function doExecute() { $oldTask = $taskDao->getById($task->getId()); if (!isset($oldTask)) { + $result = new OperationResult(false); + $result->setErrorNumber(40); + $result->setResponseCode(500); + $result->setMessage("Error updating task:\nTask does not exist."); + $discardedResults[] = $result; $discardedTasks[] = $task; unset($this->tasks[$i]); continue; } - // Do not allow updating tasks saved in locked dates or belonging - // to a different user - if(!$configDao->isWriteAllowedForDate($oldTask->getDate()) || - (!$taskDao->checkTaskUserId( - $task->getId(), $task->getUserId()))) { + // Do not allow updating tasks saved in locked dates + if(!$configDao->isWriteAllowedForDate($oldTask->getDate())) { + $result = new OperationResult(false); + $result->setErrorNumber(20); + $result->setResponseCode(500); + $result->setMessage("Error updating task:\nNot allowed to write to date."); + $discardedResults[] = $result; + $discardedTasks[] = $task; + unset($this->tasks[$i]); + continue; + } + + // Do not allow updating tasks belonging to a different user + if(!$taskDao->checkTaskUserId( + $task->getId(), $task->getUserId())) { + $result = new OperationResult(false); + $result->setErrorNumber(50); + $result->setResponseCode(500); + $result->setMessage("Error updating task:\nBelongs to a different user."); + $discardedResults[] = $result; $discardedTasks[] = $task; unset($this->tasks[$i]); continue; @@ -114,6 +139,11 @@ protected function doExecute() { $projectId = $task->getProjectId(); $projectVO = $projectDAO->getById($projectId); if (!$projectVO || !$projectVO->getActivation()) { + $result = new OperationResult(false); + $result->setErrorNumber(30); + $result->setResponseCode(500); + $result->setMessage("Error updating task:\nNot allowed to write to project."); + $discardedResults[] = $result; $discardedTasks[] = $task; unset($this->tasks[$i]); continue; @@ -124,8 +154,14 @@ protected function doExecute() { $projectId = $oldTask->getProjectId(); $projectVO = $projectDAO->getById($projectId); if (!$projectVO || !$projectVO->getActivation()) { + $result = new OperationResult(false); + $result->setErrorNumber(30); + $result->setResponseCode(500); + $result->setMessage("Error updating task:\nNot allowed to write to project."); + $discardedResults[] = $result; $discardedTasks[] = $task; unset($this->tasks[$i]); + continue; } if ($task->isInitDirty() & !$task->isEndDirty()) { @@ -153,17 +189,13 @@ protected function doExecute() { } } - if ($taskDao->batchPartialUpdate($this->tasks) < count($this->tasks)) { - return -1; - } + $results = $taskDao->batchPartialUpdate($this->tasks); //TODO: do something meaningful with the list of discarded tasks - if (empty($discardedTasks)) { - return 0; - } - else { - return -1; + if (!empty($discardedTasks)) { + return array_merge($discardedResults, $results); } + return $results; } } diff --git a/web/services/updateTasksService.php b/web/services/updateTasksService.php index 7a6f1d855..9ef770c49 100644 --- a/web/services/updateTasksService.php +++ b/web/services/updateTasksService.php @@ -266,12 +266,20 @@ } while ($parser->read()); - if (count($updateTasks) >= 1) - if (TasksFacade::PartialUpdateReports($updateTasks) == -1) { - http_response_code(500); - $string = "falseThere was some error while updating the tasks"; + $operationResults = TasksFacade::PartialUpdateReports($updateTasks); + $errors = array_filter($operationResults, function ($item) { + return (!$item->getIsSuccessful()); + }); + if ($errors) { + //if multiple failures, let's just return a 500 + http_response_code(500); + $string = ""; + foreach((array) $errors as $result){ + if (!$result->getIsSuccessful()) + $string .= "" . $result->getMessage() . ""; } - + $string .= ""; + } if (!isset($string)) $string = "trueOperation Success!";