Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jul 16, 2024
1 parent 174226a commit 7787168
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
60 changes: 56 additions & 4 deletions application/controllers/ApiV1ContactgroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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]);
}

/**
Expand Down
18 changes: 12 additions & 6 deletions application/controllers/ApiV1ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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]);
}

/**
Expand Down

0 comments on commit 7787168

Please sign in to comment.