From 854ea3bf185c0ea70b57a41422432c240a4128b8 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 5 Jul 2024 14:09:24 +0200 Subject: [PATCH] Contact|Schedule|Contactgroup: Cleanup refereces on deletion --- application/forms/ContactGroupForm.php | 45 +++++++++++++++++++ application/forms/ScheduleForm.php | 5 ++- .../Notifications/Web/Form/ContactForm.php | 40 +++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/application/forms/ContactGroupForm.php b/application/forms/ContactGroupForm.php index 1f91bd68..e18f0aba 100644 --- a/application/forms/ContactGroupForm.php +++ b/application/forms/ContactGroupForm.php @@ -9,6 +9,7 @@ use Icinga\Module\Notifications\Common\Links; use Icinga\Module\Notifications\Model\Contact; use Icinga\Module\Notifications\Model\Contactgroup; +use Icinga\Module\Notifications\Model\RotationMember; use Icinga\Web\Session; use ipl\Html\FormElement\SubmitElement; use ipl\Html\HtmlDocument; @@ -291,6 +292,50 @@ public function removeContactgroup(): void $this->db->beginTransaction(); $markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y']; + + $this->db->update( + 'rotation_member', + $markAsDeleted + ['position' => null], + ['contact_id = ?' => $this->contactgroupId] + ); + + $rotationIds = $this->db->fetchCol( + RotationMember::on($this->db) + ->columns('rotation_id') + ->filter(Filter::equal('contactgroup_id', $this->contactgroupId)) + ->assembleSelect() + ); + + if (! empty($rotationIds)) { + $rotationIdsWithOtherMembers = $this->db->fetchCol( + RotationMember::on($this->db) + ->columns('rotation_id') + ->filter( + Filter::all( + Filter::equal('rotation_id', $rotationIds), + Filter::unequal('contactgroup_id', $this->contactgroupId), + Filter::equal('deleted', 'n') + ) + )->assembleSelect() + ); + + $toRemoveRotations = array_diff($rotationIds, $rotationIdsWithOtherMembers); + + if (! empty($toRemoveRotations)) { + $this->db->update( + 'rotation', + $markAsDeleted + ['priority' => null, 'first_handoff' => null], + ['id IN (?)' => $toRemoveRotations] + ); + } + } + + $this->db->update( + 'rule_escalation_recipient', + $markAsDeleted, + ['contactgroup_id = ?' => $this->contactgroupId] + ); + $this->db->update('contactgroup_member', $markAsDeleted, ['contactgroup_id = ?' => $this->contactgroupId]); $this->db->update('contactgroup', $markAsDeleted, ['id = ?' => $this->contactgroupId]); diff --git a/application/forms/ScheduleForm.php b/application/forms/ScheduleForm.php index 167380f8..f4935782 100644 --- a/application/forms/ScheduleForm.php +++ b/application/forms/ScheduleForm.php @@ -114,7 +114,10 @@ public function removeSchedule(int $id): void $rotationConfigForm->wipeRotation($rotation->priority); } - $this->db->update('schedule', ['changed_at' => time() * 1000, 'deleted' => 'y'], ['id = ?' => $id]); + $markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y']; + + $this->db->update('rule_escalation_recipient', $markAsDeleted, ['schedule_id = ?' => $id]); + $this->db->update('schedule', $markAsDeleted, ['id = ?' => $id]); $this->db->commitTransaction(); } diff --git a/library/Notifications/Web/Form/ContactForm.php b/library/Notifications/Web/Form/ContactForm.php index 8f3af313..9ad569bc 100644 --- a/library/Notifications/Web/Form/ContactForm.php +++ b/library/Notifications/Web/Form/ContactForm.php @@ -8,6 +8,7 @@ use Icinga\Module\Notifications\Model\AvailableChannelType; use Icinga\Module\Notifications\Model\Channel; use Icinga\Module\Notifications\Model\Contact; +use Icinga\Module\Notifications\Model\RotationMember; use Icinga\Web\Session; use ipl\Html\Contract\FormSubmitElement; use ipl\Html\FormElement\FieldsetElement; @@ -263,6 +264,45 @@ public function removeContact(): void $this->db->beginTransaction(); $markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y']; + + $this->db->update( + 'rotation_member', + $markAsDeleted + ['position' => null], + ['contact_id = ?' => $this->contactId] + ); + + $rotationIds = $this->db->fetchCol( + RotationMember::on($this->db) + ->columns('rotation_id') + ->filter(Filter::equal('contact_id', $this->contactId)) + ->assembleSelect() + ); + + if (! empty($rotationIds)) { + $rotationIdsWithOtherMembers = $this->db->fetchCol( + RotationMember::on($this->db) + ->columns('rotation_id') + ->filter( + Filter::all( + Filter::equal('rotation_id', $rotationIds), + Filter::unequal('contact_id', $this->contactId), + Filter::equal('deleted', 'n') + ) + )->assembleSelect() + ); + + $toRemoveRotations = array_diff($rotationIds, $rotationIdsWithOtherMembers); + + if (! empty($toRemoveRotations)) { + $this->db->update( + 'rotation', + $markAsDeleted + ['priority' => null, 'first_handoff' => null], + ['id IN (?)' => $toRemoveRotations] + ); + } + } + + $this->db->update('rule_escalation_recipient', $markAsDeleted, ['contact_id = ?' => $this->contactId]); $this->db->update('contactgroup_member', $markAsDeleted, ['contact_id = ?' => $this->contactId]); $this->db->update('contact_address', $markAsDeleted, ['contact_id = ?' => $this->contactId]); $this->db->update('contact', $markAsDeleted + ['username' => null], ['id = ?' => $this->contactId]);