|
9 | 9 | use Icinga\Exception\Http\HttpException; |
10 | 10 | use Icinga\Exception\Http\HttpNotFoundException; |
11 | 11 | use Icinga\Module\Notifications\Common\Database; |
| 12 | +use Icinga\Module\Notifications\Model\RotationMember; |
12 | 13 | use Icinga\Util\Environment; |
13 | 14 | use Icinga\Util\Json; |
14 | 15 | use ipl\Sql\Compat\FilterProcessor; |
@@ -213,8 +214,7 @@ function (Filter\Condition $condition) { |
213 | 214 | $contactgroupId = $this->getContactgroupId($identifier); |
214 | 215 | if ($contactgroupId !== null) { |
215 | 216 | $db->update('contactgroup', ['name' => $data['name']], ['id = ?' => $contactgroupId]); |
216 | | - |
217 | | - $db->delete('contactgroup_member', ['contactgroup_id = ?' => $contactgroupId]); |
| 217 | + $db->update('contactgroup_member', ['deleted' => 'y'], ['contactgroup_id = ?' => $contactgroupId, 'deleted = ?' => 'n']); |
218 | 218 |
|
219 | 219 | if (! empty($data['users'])) { |
220 | 220 | $this->addUsers($contactgroupId, $data['users']); |
@@ -374,8 +374,55 @@ private function addUsers(int $contactgroupId, array $users): void |
374 | 374 | */ |
375 | 375 | private function removeContactgroup(int $id): void |
376 | 376 | { |
377 | | - Database::get()->delete('contactgroup_member', ['contactgroup_id = ?' => $id]); |
378 | | - Database::get()->delete('contactgroup', ['id = ?' => $id]); |
| 377 | + $db = Database::get(); |
| 378 | + $markAsDeleted = ['deleted' => 'y']; |
| 379 | + |
| 380 | + $db->update( |
| 381 | + 'rotation_member', |
| 382 | + $markAsDeleted + ['position' => null], |
| 383 | + ['contact_id = ?' => $id, 'deleted = ?' => 'n'] |
| 384 | + ); |
| 385 | + |
| 386 | + $rotationIds = $db->fetchCol( |
| 387 | + RotationMember::on($db) |
| 388 | + ->columns('rotation_id') |
| 389 | + ->filter(Filter::equal('contactgroup_id', $id)) |
| 390 | + ->assembleSelect() |
| 391 | + ); |
| 392 | + |
| 393 | + if (! empty($rotationIds)) { |
| 394 | + $rotationIdsWithOtherMembers = $db->fetchCol( |
| 395 | + RotationMember::on($db) |
| 396 | + ->columns('rotation_id') |
| 397 | + ->filter( |
| 398 | + Filter::all( |
| 399 | + Filter::equal('rotation_id', $rotationIds), |
| 400 | + Filter::unequal('contactgroup_id', $id) |
| 401 | + ) |
| 402 | + )->assembleSelect() |
| 403 | + ); |
| 404 | + |
| 405 | + $toRemoveRotations = array_diff($rotationIds, $rotationIdsWithOtherMembers); |
| 406 | + |
| 407 | + if (! empty($toRemoveRotations)) { |
| 408 | + $db->update( |
| 409 | + 'rotation', |
| 410 | + $markAsDeleted + ['priority' => null, 'first_handoff' => null], |
| 411 | + ['id IN (?)' => $toRemoveRotations] |
| 412 | + ); |
| 413 | + } |
| 414 | + } |
| 415 | + |
| 416 | + $db->update( |
| 417 | + 'rule_escalation_recipient', |
| 418 | + $markAsDeleted, |
| 419 | + ['contactgroup_id = ?' => $id, 'deleted = ?' => 'n'] |
| 420 | + ); |
| 421 | + |
| 422 | + $db->update('contactgroup_member', $markAsDeleted, ['contactgroup_id = ?' => $id, 'deleted = ?' => 'n']); |
| 423 | + $db->update('contactgroup', $markAsDeleted, ['id = ?' => $id]); |
| 424 | + |
| 425 | + //TODO: properly remove rotations|escalations with no members as in form |
379 | 426 | } |
380 | 427 |
|
381 | 428 | /** |
|
0 commit comments