From 7787168c831c4e25807f0cf900933250083f175f Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Wed, 10 Jul 2024 08:57:46 +0200 Subject: [PATCH] wip --- .../ApiV1ContactgroupsController.php | 60 +++++++++++++++++-- .../controllers/ApiV1ContactsController.php | 18 ++++-- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/application/controllers/ApiV1ContactgroupsController.php b/application/controllers/ApiV1ContactgroupsController.php index 1dbb2745..8255e856 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; @@ -212,9 +213,15 @@ function (Filter\Condition $condition) { $contactgroupId = $this->getContactgroupId($identifier); if ($contactgroupId !== null) { - $db->update('contactgroup', ['name' => $data['name']], ['id = ?' => $contactgroupId]); + $changedAt = time() * 1000; + $db->update( + 'contactgroup', + ['name' => $data['name']] + ['changed_at' => $changedAt], + ['id = ?' => $contactgroupId] + ); - $db->delete('contactgroup_member', ['contactgroup_id = ?' => $contactgroupId]); + $markAsDeleted = ['changed_at' => $changedAt, 'deleted' => 'y']; + $db->update('contactgroup_member', $markAsDeleted, ['contactgroup_id = ?' => $contactgroupId]); if (! empty($data['users'])) { $this->addUsers($contactgroupId, $data['users']); @@ -374,8 +381,53 @@ 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 = ['changed_at' => time() * 1000, 'deleted' => 'y']; + + $db->update( + 'rotation_member', + $markAsDeleted + ['position' => null], + ['contact_id = ?' => $id] + ); + + $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] + ); + + $db->update('contactgroup_member', $markAsDeleted, ['contactgroup_id = ?' => $id]); + $db->update('contactgroup', $markAsDeleted, ['id = ?' => $id]); } /** diff --git a/application/controllers/ApiV1ContactsController.php b/application/controllers/ApiV1ContactsController.php index 73dc749d..a60bfeb7 100644 --- a/application/controllers/ApiV1ContactsController.php +++ b/application/controllers/ApiV1ContactsController.php @@ -223,14 +223,17 @@ function (Filter\Condition $condition) { $this->assertUniqueUsername($data['username'], $contactId); } + $changedAt = time() * 1000; $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']), + 'changed_at' => $changedAt ], ['id = ?' => $contactId]); - $db->delete('contact_address', ['contact_id = ?' => $contactId]); - $db->delete('contactgroup_member', ['contact_id = ?' => $contactId]); + $markAsDeleted = ['changed_at' => $changedAt, 'deleted' => 'y']; + $db->update('contact_address', $markAsDeleted, ['contact_id = ?' => $contactId]); + $db->update('contactgroup_member', $markAsDeleted, ['contact_id = ?' => $contactId]); if (! empty($data['addresses'])) { $this->addAddresses($contactId, $data['addresses']); @@ -497,9 +500,12 @@ 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 = ['changed_at' => time() * 1000, 'deleted' => 'y']; + + $db->update('contactgroup_member', $markAsDeleted, ['contact_id = ?' => $id]); + $db->update('contact_address', $markAsDeleted, ['contact_id = ?' => $id]); + $db->update('contact', $markAsDeleted, ['id = ?' => $id]); } /**