Skip to content

Commit b5dd0ac

Browse files
authored
Merge pull request #8685 from ProcessMaker/observation/FOUR-28794
FOUR-28794: Delete notifications tied to case on delete and add test
2 parents 493489f + b0e70a2 commit b5dd0ac

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

ProcessMaker/Http/Controllers/Api/Actions/Cases/DeleteCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function __invoke(string $caseNumber): void
3535
$this->deleteTaskDraftMedia($draftIds);
3636
$this->deleteTaskDrafts($tokenIds);
3737
$this->deleteComments($caseNumber, $requestIds, $tokenIds);
38+
$this->deleteNotifications($requestIds);
3839
$this->deleteRequestMedia($requestIds);
3940
$this->deleteCaseNumbers($requestIds);
4041
$this->deleteCasesStarted($caseNumber);

ProcessMaker/Http/Controllers/Api/Actions/Cases/DeletesCaseRecords.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use ProcessMaker\Models\InboxRule;
1212
use ProcessMaker\Models\InboxRuleLog;
1313
use ProcessMaker\Models\Media;
14+
use ProcessMaker\Models\Notification;
1415
use ProcessMaker\Models\ProcessAbeRequestToken;
1516
use ProcessMaker\Models\ProcessRequest;
1617
use ProcessMaker\Models\ProcessRequestLock;
@@ -199,4 +200,24 @@ private function deleteComments(string $caseNumber, array $requestIds, array $to
199200
})
200201
->delete();
201202
}
203+
204+
private function deleteNotifications(array $requestIds): void
205+
{
206+
if ($requestIds === []) {
207+
return;
208+
}
209+
210+
$notificationTypes = [
211+
'COMMENT',
212+
'FILE_SHARED',
213+
'TASK_CREATED',
214+
'TASK_COMPLETED',
215+
'TASK_REASSIGNED',
216+
];
217+
218+
Notification::query()
219+
->whereIn('data->request_id', $requestIds)
220+
->whereIn('data->type', $notificationTypes)
221+
->delete();
222+
}
202223
}

tests/Feature/Api/CaseDeleteTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use ProcessMaker\Models\InboxRule;
1313
use ProcessMaker\Models\InboxRuleLog;
1414
use ProcessMaker\Models\Media;
15+
use ProcessMaker\Models\Notification;
1516
use ProcessMaker\Models\ProcessAbeRequestToken;
1617
use ProcessMaker\Models\ProcessRequest;
1718
use ProcessMaker\Models\ProcessRequestLock;
@@ -152,6 +153,70 @@ public function testDeleteCaseRemovesDependentRecords(): void
152153
}
153154
}
154155

156+
public function testDeleteCaseRemovesCaseNotifications(): void
157+
{
158+
$caseNumber = 13579;
159+
$request = ProcessRequest::factory()
160+
->withCaseNumber($caseNumber)
161+
->create();
162+
$otherRequest = ProcessRequest::factory()
163+
->withCaseNumber($caseNumber + 1)
164+
->create();
165+
166+
$notificationTypes = [
167+
'COMMENT',
168+
'FILE_SHARED',
169+
'TASK_CREATED',
170+
'TASK_COMPLETED',
171+
'TASK_REASSIGNED',
172+
];
173+
174+
$deletedNotificationIds = [];
175+
foreach ($notificationTypes as $type) {
176+
$deletedNotificationIds[] = Notification::factory()->create([
177+
'notifiable_type' => get_class($this->user),
178+
'notifiable_id' => $this->user->getKey(),
179+
'data' => json_encode([
180+
'type' => $type,
181+
'request_id' => $request->id,
182+
'url' => "/requests/{$request->id}",
183+
]),
184+
'url' => "/requests/{$request->id}",
185+
])->id;
186+
}
187+
188+
$keptDifferentRequest = Notification::factory()->create([
189+
'notifiable_type' => get_class($this->user),
190+
'notifiable_id' => $this->user->getKey(),
191+
'data' => json_encode([
192+
'type' => 'TASK_CREATED',
193+
'request_id' => $otherRequest->id,
194+
'url' => "/requests/{$otherRequest->id}",
195+
]),
196+
'url' => "/requests/{$otherRequest->id}",
197+
]);
198+
199+
$keptDifferentType = Notification::factory()->create([
200+
'notifiable_type' => get_class($this->user),
201+
'notifiable_id' => $this->user->getKey(),
202+
'data' => json_encode([
203+
'type' => 'MESSAGE',
204+
'request_id' => $request->id,
205+
'url' => "/requests/{$request->id}",
206+
]),
207+
'url' => "/requests/{$request->id}",
208+
]);
209+
210+
$response = $this->apiCall('DELETE', route('api.cases.destroy', ['case_number' => $caseNumber]));
211+
212+
$response->assertStatus(204);
213+
foreach ($deletedNotificationIds as $notificationId) {
214+
$this->assertDatabaseMissing('notifications', ['id' => $notificationId]);
215+
}
216+
$this->assertDatabaseHas('notifications', ['id' => $keptDifferentRequest->id]);
217+
$this->assertDatabaseHas('notifications', ['id' => $keptDifferentType->id]);
218+
}
219+
155220
public function testDeleteCaseReturnsNotFoundWhenMissing(): void
156221
{
157222
$caseNumber = 99999;

0 commit comments

Comments
 (0)