Skip to content

Commit

Permalink
Let On_ASSEMBLE_INSERT / ON_ASSEMBLE_UPDATE events handle the changed…
Browse files Browse the repository at this point in the history
…_at column
  • Loading branch information
sukhwinder33445 committed Jul 12, 2024
1 parent c677b3c commit e511890
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 102 deletions.
9 changes: 1 addition & 8 deletions application/forms/ChannelForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ public function addChannel(): void
$channel = $this->getValues();

$channel['config'] = json_encode($this->filterConfig($channel['config']));
$channel['changed_at'] = time() * 1000;

$this->db->insert('channel', $channel);
}
Expand All @@ -221,8 +220,6 @@ public function editChannel(): void
$storedValues['config'] = json_encode($this->filterConfig($storedValues['config']));

if (! empty(array_diff_assoc($channel, $storedValues))) {
$channel['changed_at'] = time() * 1000;

$this->db->update('channel', $channel, ['id = ?' => $this->channelId]);
}

Expand All @@ -234,11 +231,7 @@ public function editChannel(): void
*/
public function removeChannel(): void
{
$this->db->update(
'channel',
['changed_at' => time() * 1000, 'deleted' => 'y'],
['id = ?' => $this->channelId]
);
$this->db->update('channel', ['deleted' => 'y'], ['id = ?' => $this->channelId]);
}

/**
Expand Down
22 changes: 7 additions & 15 deletions application/forms/ContactGroupForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ public function addGroup(): int

$this->db->beginTransaction();

$changedAt = time() * 1000;
$this->db->insert('contactgroup', ['name' => trim($data['group_name']), 'changed_at' => $changedAt]);
$this->db->insert('contactgroup', ['name' => trim($data['group_name'])]);

$groupIdentifier = $this->db->lastInsertId();

Expand All @@ -193,8 +192,7 @@ public function addGroup(): int
'contactgroup_member',
[
'contactgroup_id' => $groupIdentifier,
'contact_id' => $contactId,
'changed_at' => $changedAt
'contact_id' => $contactId
]
);
}
Expand All @@ -217,13 +215,8 @@ public function editGroup(): void

$storedValues = $this->fetchDbValues();

$changedAt = time() * 1000;
if ($values['group_name'] !== $storedValues['group_name']) {
$this->db->update(
'contactgroup',
['name' => $values['group_name'], 'changed_at' => $changedAt],
['id = ?' => $this->contactgroupId]
);
$this->db->update('contactgroup', ['name' => $values['group_name']], ['id = ?' => $this->contactgroupId]);
}

$storedContacts = [];
Expand All @@ -242,7 +235,7 @@ public function editGroup(): void
if (! empty($toDelete)) {
$this->db->update(
'contactgroup_member',
['changed_at' => $changedAt, 'deleted' => 'y'],
['deleted' => 'y'],
[
'contactgroup_id = ?' => $this->contactgroupId,
'contact_id IN (?)' => $toDelete,
Expand All @@ -269,16 +262,15 @@ public function editGroup(): void
'contactgroup_member',
[
'contactgroup_id' => $this->contactgroupId,
'contact_id' => $contactId,
'changed_at' => $changedAt
'contact_id' => $contactId
]
);
}

if (! empty($contactsMarkedAsDeleted)) {
$this->db->update(
'contactgroup_member',
['changed_at' => $changedAt, 'deleted' => 'n'],
['deleted' => 'n'],
[
'contactgroup_id = ?' => $this->contactgroupId,
'contact_id IN (?)' => $contactsMarkedAsDeleted
Expand All @@ -297,7 +289,7 @@ public function removeContactgroup(): void
{
$this->db->beginTransaction();

$markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y'];
$markAsDeleted = ['deleted' => 'y'];
$updateCondition = ['contactgroup_id = ?' => $this->contactgroupId, 'deleted = ?' => 'n'];

$rotationAndMemberIds = $this->db->fetchPairs(
Expand Down
7 changes: 3 additions & 4 deletions application/forms/MoveRotationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ protected function onSuccess()

$this->scheduleId = $rotation->schedule_id;

$changedAt = time() * 1000;
// Free up the current priority used by the rotation in question
$this->db->update('rotation', ['priority' => null, 'deleted' => 'y'], ['id = ?' => $rotationId]);

Expand All @@ -120,7 +119,7 @@ protected function onSuccess()
foreach ($affectedRotations as $rotation) {
$this->db->update(
'rotation',
['priority' => new Expression('priority + 1'), 'changed_at' => $changedAt],
['priority' => new Expression('priority + 1')],
['id = ?' => $rotation->id]
);
}
Expand All @@ -139,7 +138,7 @@ protected function onSuccess()
foreach ($affectedRotations as $rotation) {
$this->db->update(
'rotation',
['priority' => new Expression('priority - 1'), 'changed_at' => $changedAt],
['priority' => new Expression('priority - 1')],
['id = ?' => $rotation->id]
);
}
Expand All @@ -148,7 +147,7 @@ protected function onSuccess()
// Now insert the rotation at the new priority
$this->db->update(
'rotation',
['priority' => $newPriority, 'changed_at' => $changedAt, 'deleted' => 'n'],
['priority' => $newPriority, 'deleted' => 'n'],
['id = ?' => $rotationId]
);

Expand Down
24 changes: 8 additions & 16 deletions application/forms/RotationConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,10 @@ private function createRotation(int $priority): Generator
$data['actual_handoff'] = $firstHandoff->format('U.u') * 1000.0;
}

$changedAt = time() * 1000;
$data['changed_at'] = $changedAt;
$this->db->insert('rotation', $data);
$rotationId = $this->db->lastInsertId();

$this->db->insert('timeperiod', ['owned_by_rotation_id' => $rotationId, 'changed_at' => $changedAt]);
$this->db->insert('timeperiod', ['owned_by_rotation_id' => $rotationId]);
$timeperiodId = $this->db->lastInsertId();

$knownMembers = [];
Expand All @@ -332,15 +330,13 @@ private function createRotation(int $priority): Generator
$this->db->insert('rotation_member', [
'rotation_id' => $rotationId,
'contact_id' => $id,
'position' => $position,
'changed_at' => $changedAt
'position' => $position
]);
} elseif ($type === 'group') {
$this->db->insert('rotation_member', [
'rotation_id' => $rotationId,
'contactgroup_id' => $id,
'position' => $position,
'changed_at' => $changedAt
'position' => $position
]);
}

Expand All @@ -363,8 +359,7 @@ private function createRotation(int $priority): Generator
'end_time' => $endTime,
'until_time' => $untilTime,
'timezone' => $rrule->getStartDate()->getTimezone()->getName(),
'rrule' => $rrule->getString(Rule::TZ_FIXED),
'changed_at' => $changedAt
'rrule' => $rrule->getString(Rule::TZ_FIXED)
]);
}
}
Expand Down Expand Up @@ -420,14 +415,13 @@ public function editRotation(int $rotationId): void
$createStmt = $this->createRotation((int) $priority);

$allEntriesRemoved = true;
$changedAt = time() * 1000;
$markAsDeleted = ['changed_at' => $changedAt, 'deleted' => 'y'];
$markAsDeleted = ['deleted' => 'y'];
if (self::EXPERIMENTAL_OVERRIDES) {
// We only show a single name, even in case of multiple versions of a rotation.
// To avoid confusion, we update all versions upon change of the name
$this->db->update(
'rotation',
['name' => $this->getValue('name'), 'changed_at' => $changedAt],
['name' => $this->getValue('name')],
['schedule_id = ?' => $this->scheduleId, 'priority = ?' => $priority]
);

Expand All @@ -453,8 +447,7 @@ public function editRotation(int $rotationId): void
'start_time' => $gapStart->format('U.u') * 1000.0,
'end_time' => $gapEnd->format('U.u') * 1000.0,
'until_time' => $gapEnd->format('U.u') * 1000.0,
'timezone' => $gapStart->getTimezone()->getName(),
'changed_at' => $changedAt
'timezone' => $gapStart->getTimezone()->getName()
]);
}

Expand All @@ -470,8 +463,7 @@ public function editRotation(int $rotationId): void
$allEntriesRemoved = false;
$this->db->update('timeperiod_entry', [
'until_time' => $lastShiftEnd->format('U.u') * 1000.0,
'rrule' => $rrule->setUntil($lastHandoff)->getString(Rule::TZ_FIXED),
'changed_at' => $changedAt
'rrule' => $rrule->setUntil($lastHandoff)->getString(Rule::TZ_FIXED)
], ['id = ?' => $timeperiodEntry->id]);
}
}
Expand Down
35 changes: 11 additions & 24 deletions application/forms/SaveEventRuleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,10 @@ public function addRule(array $config): int

$db->beginTransaction();

$changedAt = time() * 1000;
$db->insert('rule', [
'name' => $config['name'],
'timeperiod_id' => $config['timeperiod_id'] ?? null,
'object_filter' => $config['object_filter'] ?? null,
'changed_at' => $changedAt
'object_filter' => $config['object_filter'] ?? null
]);
$ruleId = $db->lastInsertId();

Expand All @@ -227,16 +225,14 @@ public function addRule(array $config): int
'position' => $position,
'condition' => $escalationConfig['condition'] ?? null,
'name' => $escalationConfig['name'] ?? null,
'fallback_for' => $escalationConfig['fallback_for'] ?? null,
'changed_at' => $changedAt
'fallback_for' => $escalationConfig['fallback_for'] ?? null
]);
$escalationId = $db->lastInsertId();

foreach ($escalationConfig['recipient'] ?? [] as $recipientConfig) {
$data = [
'rule_escalation_id' => $escalationId,
'channel_id' => $recipientConfig['channel_id'],
'changed_at' => $changedAt
'channel_id' => $recipientConfig['channel_id']
];

switch (true) {
Expand Down Expand Up @@ -272,16 +268,14 @@ public function addRule(array $config): int
*/
private function insertOrUpdateEscalations($ruleId, array $escalations, Connection $db, bool $insert = false): void
{
$changedAt = time() * 1000;
foreach ($escalations as $position => $escalationConfig) {
if ($insert) {
$db->insert('rule_escalation', [
'rule_id' => $ruleId,
'position' => $position,
'condition' => $escalationConfig['condition'] ?? null,
'name' => $escalationConfig['name'] ?? null,
'fallback_for' => $escalationConfig['fallback_for'] ?? null,
'changed_at' => $changedAt
'fallback_for' => $escalationConfig['fallback_for'] ?? null
]);

$escalationId = $db->lastInsertId();
Expand All @@ -291,8 +285,7 @@ private function insertOrUpdateEscalations($ruleId, array $escalations, Connecti
'position' => $position,
'condition' => $escalationConfig['condition'] ?? null,
'name' => $escalationConfig['name'] ?? null,
'fallback_for' => $escalationConfig['fallback_for'] ?? null,
'changed_at' => $changedAt
'fallback_for' => $escalationConfig['fallback_for'] ?? null
], ['id = ?' => $escalationId, 'rule_id = ?' => $ruleId]);
$recipientsToRemove = [];

Expand All @@ -318,7 +311,7 @@ function (array $element) use ($recipientId) {
if (! empty($recipientsToRemove)) {
$db->update(
'rule_escalation_recipient',
['changed_at' => $changedAt, 'deleted' => 'y'],
['deleted' => 'y'],
['id IN (?)' => $recipientsToRemove, 'deleted = ?' => 'n']
);
}
Expand All @@ -327,8 +320,7 @@ function (array $element) use ($recipientId) {
foreach ($escalationConfig['recipient'] ?? [] as $recipientConfig) {
$data = [
'rule_escalation_id' => $escalationId,
'channel_id' => $recipientConfig['channel_id'],
'changed_at' => $changedAt
'channel_id' => $recipientConfig['channel_id']
];

switch (true) {
Expand All @@ -352,11 +344,7 @@ function (array $element) use ($recipientId) {
if (! isset($recipientConfig['id'])) {
$db->insert('rule_escalation_recipient', $data);
} else {
$db->update(
'rule_escalation_recipient',
$data + ['changed_at' => $changedAt],
['id = ?' => $recipientConfig['id']]
);
$db->update('rule_escalation_recipient', $data, ['id = ?' => $recipientConfig['id']]);
}
}
}
Expand Down Expand Up @@ -390,9 +378,8 @@ public function editRule(int $id, array $config): void
$data['object_filter'] = $values['object_filter'];
}

$changedAt = time() * 1000;
if (! empty($data)) {
$db->update('rule', $data + ['changed_at' => $changedAt], ['id = ?' => $id]);
$db->update('rule', $data, ['id = ?' => $id]);
}

if (! isset($values['rule_escalation'])) {
Expand Down Expand Up @@ -426,7 +413,7 @@ public function editRule(int $id, array $config): void
// Escalations to add
$escalationsToAdd = $escalationsInCache;

$markAsDeleted = ['changed_at' => $changedAt, 'deleted' => 'y'];
$markAsDeleted = ['deleted' => 'y'];
if (! empty($escalationsToRemove)) {
$db->update(
'rule_escalation_recipient',
Expand Down Expand Up @@ -472,7 +459,7 @@ public function removeRule(int $id): void
->assembleSelect()
);

$markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y'];
$markAsDeleted = ['deleted' => 'y'];
if (! empty($escalationsToRemove)) {
$db->update(
'rule_escalation_recipient',
Expand Down
12 changes: 3 additions & 9 deletions application/forms/ScheduleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ public function loadSchedule(int $id): void

public function addSchedule(): int
{
$this->db->insert('schedule', [
'name' => $this->getValue('name'),
'changed_at' => time() * 1000
]);
$this->db->insert('schedule', ['name' => $this->getValue('name')]);

return $this->db->lastInsertId();
}
Expand All @@ -90,10 +87,7 @@ public function editSchedule(int $id): void
return;
}

$this->db->update('schedule', [
'name' => $values['name'],
'changed_at' => time() * 1000
], ['id = ?' => $id]);
$this->db->update('schedule', ['name' => $values['name']], ['id = ?' => $id]);

$this->db->commitTransaction();
}
Expand All @@ -112,7 +106,7 @@ public function removeSchedule(int $id): void
$rotation->delete();
}

$markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y'];
$markAsDeleted = ['deleted' => 'y'];

$escalationIds = $this->db->fetchCol(
RuleEscalationRecipient::on($this->db)
Expand Down
Loading

0 comments on commit e511890

Please sign in to comment.