Add timer/scheduler to ticket automation rules
Adds optional delay_days + delay_condition (no_activity) to ticket rules. When a ticket has no activity for X days, timed rules automatically apply actions (e.g., escalate priority to urgent). Checked on each ticket list load. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
58
api.php
58
api.php
@@ -3242,6 +3242,62 @@ switch ($action) {
|
||||
}
|
||||
unset($tc);
|
||||
|
||||
// Ajastinsääntöjen tarkistus (delay_days + no_activity)
|
||||
$timedRules = array_filter(dbLoadTicketRules($comp['id']), function($r) {
|
||||
return $r['enabled'] && $r['delay_days'] > 0 && $r['delay_condition'] === 'no_activity';
|
||||
});
|
||||
if (!empty($timedRules)) {
|
||||
foreach ($tickets as &$tc) {
|
||||
if (in_array($tc['status'], ['suljettu', 'ratkaistu'])) continue;
|
||||
$lastActivity = $tc['updated'] ?? $tc['created'];
|
||||
$inactiveDays = (strtotime($now) - strtotime($lastActivity)) / 86400;
|
||||
foreach ($timedRules as $rule) {
|
||||
if ($inactiveDays < $rule['delay_days']) continue;
|
||||
// Tarkista ehdot (from/to/subject)
|
||||
$match = true;
|
||||
if (!empty($rule['from_contains'])) {
|
||||
if (stripos(($tc['from_email'] ?? '') . ' ' . ($tc['from_name'] ?? ''), $rule['from_contains']) === false) $match = false;
|
||||
}
|
||||
if (!empty($rule['subject_contains'])) {
|
||||
if (stripos($tc['subject'] ?? '', $rule['subject_contains']) === false) $match = false;
|
||||
}
|
||||
if (!empty($rule['to_contains'])) {
|
||||
if (stripos($tc['to_email'] ?? '', $rule['to_contains']) === false) $match = false;
|
||||
}
|
||||
if (!$match) continue;
|
||||
// Sovella toimenpiteet
|
||||
$changed = false;
|
||||
if (!empty($rule['set_priority']) && ($tc['priority'] ?? '') !== $rule['set_priority']) {
|
||||
$tc['priority'] = $rule['set_priority'];
|
||||
$changed = true;
|
||||
}
|
||||
if (!empty($rule['status_set']) && ($tc['status'] ?? '') !== $rule['status_set']) {
|
||||
$tc['status'] = $rule['status_set'];
|
||||
$changed = true;
|
||||
}
|
||||
if (!empty($rule['type_set']) && ($tc['type'] ?? '') !== $rule['type_set']) {
|
||||
$tc['type'] = $rule['type_set'];
|
||||
$changed = true;
|
||||
}
|
||||
if (!empty($rule['set_tags'])) {
|
||||
$ruleTags = array_map('trim', explode(',', $rule['set_tags']));
|
||||
$existingTags = $tc['tags'] ?? [];
|
||||
$merged = array_values(array_unique(array_merge($existingTags, $ruleTags)));
|
||||
if ($merged !== $existingTags) {
|
||||
$tc['tags'] = $merged;
|
||||
$changed = true;
|
||||
}
|
||||
}
|
||||
if ($changed) {
|
||||
$tc['updated'] = $now;
|
||||
dbSaveTicket($comp['id'], $tc);
|
||||
}
|
||||
break; // Vain yksi ajastinsääntö per tiketti
|
||||
}
|
||||
}
|
||||
unset($tc);
|
||||
}
|
||||
|
||||
// Resolve mailbox names for this company
|
||||
$mailboxes = dbLoadMailboxes($comp['id']);
|
||||
$mailboxNames = [];
|
||||
@@ -3967,6 +4023,8 @@ switch ($action) {
|
||||
'set_priority' => $input['set_priority'] ?? '',
|
||||
'set_tags' => trim($input['set_tags'] ?? ''),
|
||||
'auto_close_days' => intval($input['auto_close_days'] ?? 0),
|
||||
'delay_days' => intval($input['delay_days'] ?? 0),
|
||||
'delay_condition' => trim($input['delay_condition'] ?? ''),
|
||||
'enabled' => $input['enabled'] ?? true,
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user