diff --git a/application/controllers/ApiV1ContactgroupsController.php b/application/controllers/ApiV1ContactgroupsController.php index 1dbb2745..f2bc5e8e 100644 --- a/application/controllers/ApiV1ContactgroupsController.php +++ b/application/controllers/ApiV1ContactgroupsController.php @@ -9,6 +9,7 @@ use Icinga\Exception\Http\HttpException; use Icinga\Exception\Http\HttpNotFoundException; use Icinga\Module\Notifications\Common\Database; +use Icinga\Module\Notifications\Model\RotationMember; use Icinga\Util\Environment; use Icinga\Util\Json; use ipl\Sql\Compat\FilterProcessor; @@ -213,8 +214,7 @@ function (Filter\Condition $condition) { $contactgroupId = $this->getContactgroupId($identifier); if ($contactgroupId !== null) { $db->update('contactgroup', ['name' => $data['name']], ['id = ?' => $contactgroupId]); - - $db->delete('contactgroup_member', ['contactgroup_id = ?' => $contactgroupId]); + $db->update('contactgroup_member', ['deleted' => 'y'], ['contactgroup_id = ?' => $contactgroupId, 'deleted = ?' => 'n']); if (! empty($data['users'])) { $this->addUsers($contactgroupId, $data['users']); @@ -374,8 +374,55 @@ private function addUsers(int $contactgroupId, array $users): void */ private function removeContactgroup(int $id): void { - Database::get()->delete('contactgroup_member', ['contactgroup_id = ?' => $id]); - Database::get()->delete('contactgroup', ['id = ?' => $id]); + $db = Database::get(); + $markAsDeleted = ['deleted' => 'y']; + + $db->update( + 'rotation_member', + $markAsDeleted + ['position' => null], + ['contact_id = ?' => $id, 'deleted = ?' => 'n'] + ); + + $rotationIds = $db->fetchCol( + RotationMember::on($db) + ->columns('rotation_id') + ->filter(Filter::equal('contactgroup_id', $id)) + ->assembleSelect() + ); + + if (! empty($rotationIds)) { + $rotationIdsWithOtherMembers = $db->fetchCol( + RotationMember::on($db) + ->columns('rotation_id') + ->filter( + Filter::all( + Filter::equal('rotation_id', $rotationIds), + Filter::unequal('contactgroup_id', $id) + ) + )->assembleSelect() + ); + + $toRemoveRotations = array_diff($rotationIds, $rotationIdsWithOtherMembers); + + if (! empty($toRemoveRotations)) { + $db->update( + 'rotation', + $markAsDeleted + ['priority' => null, 'first_handoff' => null], + ['id IN (?)' => $toRemoveRotations] + ); + } + } + + $db->update( + 'rule_escalation_recipient', + $markAsDeleted, + ['contactgroup_id = ?' => $id, 'deleted = ?' => 'n'] + ); + + $db->update('contactgroup_member', $markAsDeleted, ['contactgroup_id = ?' => $id, 'deleted = ?' => 'n']); + $db->update('contactgroup', $markAsDeleted, ['id = ?' => $id]); + + //TODO: properly remove rotations|escalations with no members as in form } /** diff --git a/application/controllers/ApiV1ContactsController.php b/application/controllers/ApiV1ContactsController.php index 73dc749d..8df36391 100644 --- a/application/controllers/ApiV1ContactsController.php +++ b/application/controllers/ApiV1ContactsController.php @@ -226,11 +226,12 @@ function (Filter\Condition $condition) { $db->update('contact', [ 'full_name' => $data['full_name'], 'username' => $data['username'] ?? null, - 'default_channel_id' => $this->getChannelId($data['default_channel']) + 'default_channel_id' => $this->getChannelId($data['default_channel']), ], ['id = ?' => $contactId]); - $db->delete('contact_address', ['contact_id = ?' => $contactId]); - $db->delete('contactgroup_member', ['contact_id = ?' => $contactId]); + $markAsDeleted = ['deleted' => 'y']; + $db->update('contact_address', $markAsDeleted, ['contact_id = ?' => $contactId, 'deleted = ?' => 'n']); + $db->update('contactgroup_member', $markAsDeleted, ['contact_id = ?' => $contactId, 'deleted = ?' => 'n']); if (! empty($data['addresses'])) { $this->addAddresses($contactId, $data['addresses']); @@ -497,9 +498,14 @@ private function addAddresses(int $contactId, array $addresses): void */ private function removeContact(int $id): void { - Database::get()->delete('contactgroup_member', ['contact_id = ?' => $id]); - Database::get()->delete('contact_address', ['contact_id = ?' => $id]); - Database::get()->delete('contact', ['id = ?' => $id]); + $db = Database::get(); + $markAsDeleted = ['deleted' => 'y']; + + $db->update('contactgroup_member', $markAsDeleted, ['contact_id = ?' => $id, 'deleted = ?' => 'n']); + $db->update('contact_address', $markAsDeleted, ['contact_id = ?' => $id, 'deleted = ?' => 'n']); + $db->update('contact', $markAsDeleted, ['id = ?' => $id]); + + //TODO: properly remove rotations|escalations with no members as in form } /**