From 9bb0fa3a1df9b14a968e9422301a5700501266d9 Mon Sep 17 00:00:00 2001 From: adonis Date: Mon, 24 Aug 2026 21:57:20 +0330 Subject: [PATCH] fix(automations): always append app name footer to SMS with fallbacks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolve display name from name_fa/name/name_en, skip duplicate lines, and support %نام_اپ placeholder in templates. Co-authored-by: Cursor --- app/Services/Automation/AutomationSender.php | 35 +++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/app/Services/Automation/AutomationSender.php b/app/Services/Automation/AutomationSender.php index 1efbb8b..383cda7 100644 --- a/app/Services/Automation/AutomationSender.php +++ b/app/Services/Automation/AutomationSender.php @@ -113,38 +113,63 @@ class AutomationSender private function ensureAppNameFooter(string $message): string { - $appName = trim($this->config->getString('name_fa')); + $appName = $this->resolveAppDisplayName(); $trimmed = rtrim($message); if ($appName === '') { return $trimmed; } - $lines = preg_split('/\R/u', $trimmed) ?: []; - $last = trim((string) end($lines)); - if ($last === $appName) { + if ($this->messageContainsAppName($trimmed, $appName)) { return $trimmed; } return $trimmed === '' ? $appName : ($trimmed."\n".$appName); } + private function resolveAppDisplayName(): string + { + foreach (['name_fa', 'name', 'name_en'] as $key) { + $value = trim($this->config->getString($key)); + if ($value !== '') { + return $value; + } + } + + return ''; + } + + private function messageContainsAppName(string $message, string $appName): bool + { + $lines = preg_split('/\R/u', $message) ?: []; + foreach ($lines as $line) { + if (trim((string) $line) === $appName) { + return true; + } + } + + return false; + } + private function render(string $text, Customer $customer): string { $name = trim((string) ($customer->name ?: trim(($customer->fname ?? '').' '.($customer->lname ?? '')))); $fname = (string) ($customer->fname ?? ''); $mobile = (string) ($customer->mobile ?? ''); + $appName = $this->resolveAppDisplayName(); // سازگاری با قالب قدیمی {{name}} $out = strtr($text, [ '{{name}}' => $name, '{{fname}}' => $fname, '{{mobile}}' => $mobile, + '{{app_name}}' => $appName, ]); // الگوی پیامک/تنظیمات: %نام ، ٪نام ، % name ، %name% // نام_کوچک قبل از نام — وگرنه %نام داخل %نام_کوچک زودتر match می‌شود $out = preg_replace('/[%٪]\s*(?:fname|نام_کوچک|نام‌کوچک)[%٪]?/iu', $fname, $out) ?? $out; - $out = preg_replace('/[%٪]\s*(?:name|نام)(?![_‌]?کوچک)[%٪]?/iu', $name, $out) ?? $out; + $out = preg_replace('/[%٪]\s*(?:app_name|نام_اپ|نام‌اپ)[%٪]?/iu', $appName, $out) ?? $out; + $out = preg_replace('/[%٪]\s*(?:name|نام)(?![_‌]?کوچک)(?![_‌]?اپ)[%٪]?/iu', $name, $out) ?? $out; $out = preg_replace('/[%٪]\s*(?:mobile|موبایل)[%٪]?/iu', $mobile, $out) ?? $out; return $out;