diff --git a/app/V1Module/presenters/AssignmentsPresenter.php b/app/V1Module/presenters/AssignmentsPresenter.php index 6b4fad875..1e968b635 100644 --- a/app/V1Module/presenters/AssignmentsPresenter.php +++ b/app/V1Module/presenters/AssignmentsPresenter.php @@ -8,7 +8,6 @@ use App\Helpers\MetaFormats\Validators\VBool; use App\Helpers\MetaFormats\Validators\VDouble; use App\Helpers\MetaFormats\Validators\VInt; -use App\Helpers\MetaFormats\Validators\VMixed; use App\Helpers\MetaFormats\Validators\VString; use App\Helpers\MetaFormats\Validators\VTimestamp; use App\Helpers\MetaFormats\Validators\VUuid; @@ -668,7 +667,13 @@ public function checkRemove(string $id) #[Path("id", new VUuid(), "Identifier of the assignment to be removed", required: true)] public function actionRemove(string $id) { - $this->assignments->remove($this->assignments->findOrThrow($id)); + $assignment = $this->assignments->findOrThrow($id); + $asyncJobs = $this->asyncJobs->findPendingJobs(null, true, null, $assignment); // all jobs of the assignment + foreach ($asyncJobs as $job) { + $this->dispatcher->unschedule($job); + } + + $this->assignments->remove($assignment); $this->sendSuccessResponse("OK"); } diff --git a/app/async/handlers/AssignmentNotificationJobHandler.php b/app/async/handlers/AssignmentNotificationJobHandler.php index a83f80b0e..14f5a47c9 100644 --- a/app/async/handlers/AssignmentNotificationJobHandler.php +++ b/app/async/handlers/AssignmentNotificationJobHandler.php @@ -44,7 +44,7 @@ public function execute(AsyncJob $job) } $assignment = $job->getAssociatedAssignment(); - if ($assignment) { + if ($assignment && !$assignment->isDeleted()) { $this->assignmentEmailsSender->assignmentCreated($assignment); } } diff --git a/app/async/handlers/ResubmitAllAsyncJobHandler.php b/app/async/handlers/ResubmitAllAsyncJobHandler.php index d4f6726e9..788ffdee9 100644 --- a/app/async/handlers/ResubmitAllAsyncJobHandler.php +++ b/app/async/handlers/ResubmitAllAsyncJobHandler.php @@ -44,7 +44,7 @@ public function execute(AsyncJob $job) $this->canceled = false; $assignment = $job->getAssociatedAssignment(); - if (!$assignment) { + if (!$assignment || $assignment->isDeleted()) { throw new InvalidArgumentException("Resubmit all async job is not attached to any assignment."); } diff --git a/tests/Presenters/AssignmentsPresenter.phpt b/tests/Presenters/AssignmentsPresenter.phpt index 31e58033e..3a650ab69 100644 --- a/tests/Presenters/AssignmentsPresenter.phpt +++ b/tests/Presenters/AssignmentsPresenter.phpt @@ -22,6 +22,7 @@ use App\Helpers\FileStorage\LocalFileStorage; use App\Helpers\FileStorage\LocalHashFileStorage; use App\V1Module\Presenters\AssignmentsPresenter; use App\Security\Roles; +use App\Async\Handler\AssignmentNotificationJobHandler; use Doctrine\ORM\EntityManagerInterface; use Tester\Assert; use App\Helpers\JobConfig; @@ -922,9 +923,18 @@ class TestAssignmentsPresenter extends Tester\TestCase public function testRemove() { - $token = PresenterTestHelper::loginDefaultAdmin($this->container); + PresenterTestHelper::loginDefaultAdmin($this->container); + $user = PresenterTestHelper::getUser($this->container); $assignment = current($this->assignments->findAll()); + $assignment->setVisibleFrom((new DateTime())->modify('+1 day')); + $this->presenter->assignments->persist($assignment); + + AssignmentNotificationJobHandler::scheduleAsyncJob($this->presenter->dispatcher, $user, $assignment); + + $mockDispatcher = Mockery::mock(\App\Async\Dispatcher::class); + $mockDispatcher->shouldReceive("unschedule")->once(); + $this->presenter->dispatcher = $mockDispatcher; $request = new Nette\Application\Request( 'V1:Assignments',