Skip to content

Commit b141b41

Browse files
wip
1 parent 559b3e3 commit b141b41

2 files changed

Lines changed: 63 additions & 10 deletions

File tree

application/controllers/ApiV1ContactgroupsController.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Icinga\Exception\Http\HttpException;
1010
use Icinga\Exception\Http\HttpNotFoundException;
1111
use Icinga\Module\Notifications\Common\Database;
12+
use Icinga\Module\Notifications\Model\RotationMember;
1213
use Icinga\Util\Environment;
1314
use Icinga\Util\Json;
1415
use ipl\Sql\Compat\FilterProcessor;
@@ -213,8 +214,7 @@ function (Filter\Condition $condition) {
213214
$contactgroupId = $this->getContactgroupId($identifier);
214215
if ($contactgroupId !== null) {
215216
$db->update('contactgroup', ['name' => $data['name']], ['id = ?' => $contactgroupId]);
216-
217-
$db->delete('contactgroup_member', ['contactgroup_id = ?' => $contactgroupId]);
217+
$db->update('contactgroup_member', ['deleted' => 'y'], ['contactgroup_id = ?' => $contactgroupId, 'deleted = ?' => 'n']);
218218

219219
if (! empty($data['users'])) {
220220
$this->addUsers($contactgroupId, $data['users']);
@@ -374,8 +374,55 @@ private function addUsers(int $contactgroupId, array $users): void
374374
*/
375375
private function removeContactgroup(int $id): void
376376
{
377-
Database::get()->delete('contactgroup_member', ['contactgroup_id = ?' => $id]);
378-
Database::get()->delete('contactgroup', ['id = ?' => $id]);
377+
$db = Database::get();
378+
$markAsDeleted = ['deleted' => 'y'];
379+
380+
$db->update(
381+
'rotation_member',
382+
$markAsDeleted + ['position' => null],
383+
['contact_id = ?' => $id, 'deleted = ?' => 'n']
384+
);
385+
386+
$rotationIds = $db->fetchCol(
387+
RotationMember::on($db)
388+
->columns('rotation_id')
389+
->filter(Filter::equal('contactgroup_id', $id))
390+
->assembleSelect()
391+
);
392+
393+
if (! empty($rotationIds)) {
394+
$rotationIdsWithOtherMembers = $db->fetchCol(
395+
RotationMember::on($db)
396+
->columns('rotation_id')
397+
->filter(
398+
Filter::all(
399+
Filter::equal('rotation_id', $rotationIds),
400+
Filter::unequal('contactgroup_id', $id)
401+
)
402+
)->assembleSelect()
403+
);
404+
405+
$toRemoveRotations = array_diff($rotationIds, $rotationIdsWithOtherMembers);
406+
407+
if (! empty($toRemoveRotations)) {
408+
$db->update(
409+
'rotation',
410+
$markAsDeleted + ['priority' => null, 'first_handoff' => null],
411+
['id IN (?)' => $toRemoveRotations]
412+
);
413+
}
414+
}
415+
416+
$db->update(
417+
'rule_escalation_recipient',
418+
$markAsDeleted,
419+
['contactgroup_id = ?' => $id, 'deleted = ?' => 'n']
420+
);
421+
422+
$db->update('contactgroup_member', $markAsDeleted, ['contactgroup_id = ?' => $id, 'deleted = ?' => 'n']);
423+
$db->update('contactgroup', $markAsDeleted, ['id = ?' => $id]);
424+
425+
//TODO: properly remove rotations|escalations with no members as in form
379426
}
380427

381428
/**

application/controllers/ApiV1ContactsController.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,12 @@ function (Filter\Condition $condition) {
226226
$db->update('contact', [
227227
'full_name' => $data['full_name'],
228228
'username' => $data['username'] ?? null,
229-
'default_channel_id' => $this->getChannelId($data['default_channel'])
229+
'default_channel_id' => $this->getChannelId($data['default_channel']),
230230
], ['id = ?' => $contactId]);
231231

232-
$db->delete('contact_address', ['contact_id = ?' => $contactId]);
233-
$db->delete('contactgroup_member', ['contact_id = ?' => $contactId]);
232+
$markAsDeleted = ['deleted' => 'y'];
233+
$db->update('contact_address', $markAsDeleted, ['contact_id = ?' => $contactId, 'deleted = ?' => 'n']);
234+
$db->update('contactgroup_member', $markAsDeleted, ['contact_id = ?' => $contactId, 'deleted = ?' => 'n']);
234235

235236
if (! empty($data['addresses'])) {
236237
$this->addAddresses($contactId, $data['addresses']);
@@ -497,9 +498,14 @@ private function addAddresses(int $contactId, array $addresses): void
497498
*/
498499
private function removeContact(int $id): void
499500
{
500-
Database::get()->delete('contactgroup_member', ['contact_id = ?' => $id]);
501-
Database::get()->delete('contact_address', ['contact_id = ?' => $id]);
502-
Database::get()->delete('contact', ['id = ?' => $id]);
501+
$db = Database::get();
502+
$markAsDeleted = ['deleted' => 'y'];
503+
504+
$db->update('contactgroup_member', $markAsDeleted, ['contact_id = ?' => $id, 'deleted = ?' => 'n']);
505+
$db->update('contact_address', $markAsDeleted, ['contact_id = ?' => $id, 'deleted = ?' => 'n']);
506+
$db->update('contact', $markAsDeleted, ['id = ?' => $id]);
507+
508+
//TODO: properly remove rotations|escalations with no members as in form
503509
}
504510

505511
/**

0 commit comments

Comments
 (0)