Skip to content

Commit

Permalink
Contact|Schedule|Contactgroup: Cleanup refereces on deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jul 5, 2024
1 parent b8723a6 commit 854ea3b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
45 changes: 45 additions & 0 deletions application/forms/ContactGroupForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);

Expand Down
5 changes: 4 additions & 1 deletion application/forms/ScheduleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
40 changes: 40 additions & 0 deletions library/Notifications/Web/Form/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit 854ea3b

Please sign in to comment.