Skip to content

Commit c9e9cf3

Browse files
committed
Fixing bug -- deleted assignments now remove their scheduled mail notification (async jobs).
1 parent df1b82d commit c9e9cf3

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

app/V1Module/presenters/AssignmentsPresenter.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use App\Helpers\MetaFormats\Validators\VBool;
99
use App\Helpers\MetaFormats\Validators\VDouble;
1010
use App\Helpers\MetaFormats\Validators\VInt;
11-
use App\Helpers\MetaFormats\Validators\VMixed;
1211
use App\Helpers\MetaFormats\Validators\VString;
1312
use App\Helpers\MetaFormats\Validators\VTimestamp;
1413
use App\Helpers\MetaFormats\Validators\VUuid;
@@ -668,7 +667,13 @@ public function checkRemove(string $id)
668667
#[Path("id", new VUuid(), "Identifier of the assignment to be removed", required: true)]
669668
public function actionRemove(string $id)
670669
{
671-
$this->assignments->remove($this->assignments->findOrThrow($id));
670+
$assignment = $this->assignments->findOrThrow($id);
671+
$asyncJobs = $this->asyncJobs->findPendingJobs(null, true, null, $assignment); // all jobs of the assignment
672+
foreach ($asyncJobs as $job) {
673+
$this->dispatcher->unschedule($job);
674+
}
675+
676+
$this->assignments->remove($assignment);
672677
$this->sendSuccessResponse("OK");
673678
}
674679

app/async/handlers/AssignmentNotificationJobHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function execute(AsyncJob $job)
4444
}
4545

4646
$assignment = $job->getAssociatedAssignment();
47-
if ($assignment) {
47+
if ($assignment && !$assignment->isDeleted()) {
4848
$this->assignmentEmailsSender->assignmentCreated($assignment);
4949
}
5050
}

app/async/handlers/ResubmitAllAsyncJobHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function execute(AsyncJob $job)
4444
$this->canceled = false;
4545

4646
$assignment = $job->getAssociatedAssignment();
47-
if (!$assignment) {
47+
if (!$assignment || $assignment->isDeleted()) {
4848
throw new InvalidArgumentException("Resubmit all async job is not attached to any assignment.");
4949
}
5050

tests/Presenters/AssignmentsPresenter.phpt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use App\Helpers\FileStorage\LocalFileStorage;
2222
use App\Helpers\FileStorage\LocalHashFileStorage;
2323
use App\V1Module\Presenters\AssignmentsPresenter;
2424
use App\Security\Roles;
25+
use App\Async\Handler\AssignmentNotificationJobHandler;
2526
use Doctrine\ORM\EntityManagerInterface;
2627
use Tester\Assert;
2728
use App\Helpers\JobConfig;
@@ -922,9 +923,18 @@ class TestAssignmentsPresenter extends Tester\TestCase
922923

923924
public function testRemove()
924925
{
925-
$token = PresenterTestHelper::loginDefaultAdmin($this->container);
926+
PresenterTestHelper::loginDefaultAdmin($this->container);
927+
$user = PresenterTestHelper::getUser($this->container);
926928

927929
$assignment = current($this->assignments->findAll());
930+
$assignment->setVisibleFrom((new DateTime())->modify('+1 day'));
931+
$this->presenter->assignments->persist($assignment);
932+
933+
AssignmentNotificationJobHandler::scheduleAsyncJob($this->presenter->dispatcher, $user, $assignment);
934+
935+
$mockDispatcher = Mockery::mock(\App\Async\Dispatcher::class);
936+
$mockDispatcher->shouldReceive("unschedule")->once();
937+
$this->presenter->dispatcher = $mockDispatcher;
928938

929939
$request = new Nette\Application\Request(
930940
'V1:Assignments',

0 commit comments

Comments
 (0)