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 559b3e3 commit b141b41
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
55 changes: 51 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 @@ -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']);
Expand Down Expand Up @@ -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
}

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

/**
Expand Down

0 comments on commit b141b41

Please sign in to comment.