diff --git a/Modules/Helpdesk/Services/AutomationRuleService.php b/Modules/Helpdesk/Services/AutomationRuleService.php index f0fde459..2ebf33d5 100644 --- a/Modules/Helpdesk/Services/AutomationRuleService.php +++ b/Modules/Helpdesk/Services/AutomationRuleService.php @@ -29,18 +29,7 @@ final class AutomationRuleService return; } - $updates = array_filter([ - 'priority_id' => $rule->set_priority_id, - 'category_id' => $rule->set_category_id, - 'team_id' => $rule->set_team_id, - 'project_id' => $rule->set_project_id, - 'status_id' => $rule->set_status_id, - 'agent_id' => $rule->set_agent_id, - ], static fn ($value): bool => $value !== null); - - if ($updates !== []) { - $ticket->forceFill($updates + ['last_activity_at' => now()])->save(); - } + $this->applyRuleUpdates($rule, $ticket); }); return $ticket->fresh() ?? $ticket; @@ -48,19 +37,30 @@ final class AutomationRuleService private function matches(AutomationRule $rule, Ticket $ticket, string $sender, string $mailbox): bool { - if ($rule->match_sender && ! Str::contains(Str::lower($sender), Str::lower($rule->match_sender))) { - return false; - } - if ($rule->match_subject && ! Str::contains(Str::lower($ticket->subject), Str::lower($rule->match_subject))) { - return false; - } - if ($rule->match_mailbox && ! Str::contains(Str::lower($mailbox), Str::lower($rule->match_mailbox))) { - return false; - } - if ($rule->match_category_id && (int) $ticket->category_id !== (int) $rule->match_category_id) { - return false; - } + return $this->containsWhenConfigured($sender, $rule->match_sender) + && $this->containsWhenConfigured($ticket->subject, $rule->match_subject) + && $this->containsWhenConfigured($mailbox, $rule->match_mailbox) + && (! $rule->match_category_id || (int) $ticket->category_id === (int) $rule->match_category_id); + } - return true; + private function containsWhenConfigured(string $value, ?string $match): bool + { + return ! $match || Str::contains(Str::lower($value), Str::lower($match)); + } + + private function applyRuleUpdates(AutomationRule $rule, Ticket $ticket): void + { + $updates = array_filter([ + 'priority_id' => $rule->set_priority_id, + 'category_id' => $rule->set_category_id, + 'team_id' => $rule->set_team_id, + 'project_id' => $rule->set_project_id, + 'status_id' => $rule->set_status_id, + 'agent_id' => $rule->set_agent_id, + ], static fn ($value): bool => $value !== null); + + if ($updates !== []) { + $ticket->forceFill($updates + ['last_activity_at' => now()])->save(); + } } }