From 57039c5236b51ab85df8162af64502d85841187f Mon Sep 17 00:00:00 2001 From: Kris Date: Tue, 13 Aug 2024 08:37:33 +0200 Subject: [PATCH] :children_crossing: delete backend notifications when event is processed (#2854) --- .../Controllers/Backend/EventController.php | 3 +- .../Frontend/Admin/EventController.php | 36 ++++--------------- app/Models/EventSuggestion.php | 35 +++++++++++++----- ...n_notification_id_to_event_suggestions.php | 20 +++++++++++ lang/fr.json | 2 +- 5 files changed, 56 insertions(+), 40 deletions(-) create mode 100644 database/migrations/2024_08_13_000000_add_admin_notification_id_to_event_suggestions.php diff --git a/app/Http/Controllers/Backend/EventController.php b/app/Http/Controllers/Backend/EventController.php index 1c193da92..2b7a61049 100644 --- a/app/Http/Controllers/Backend/EventController.php +++ b/app/Http/Controllers/Backend/EventController.php @@ -40,7 +40,7 @@ public static function suggestEvent( try { if (TelegramService::isAdminActive()) { - TelegramService::admin()->sendMessage( + $messageId = TelegramService::admin()->sendMessage( strtr("New event suggestion:" . PHP_EOL . "Title: :name" . PHP_EOL . "Begin: :begin" . PHP_EOL . @@ -53,6 +53,7 @@ public static function suggestEvent( ':username' => $eventSuggestion->user->username, ]) ); + $eventSuggestion->update(['admin_notification_id' => $messageId]); } } catch (TelegramException $exception) { report($exception); diff --git a/app/Http/Controllers/Frontend/Admin/EventController.php b/app/Http/Controllers/Frontend/Admin/EventController.php index 97099bc8f..77f99619d 100644 --- a/app/Http/Controllers/Frontend/Admin/EventController.php +++ b/app/Http/Controllers/Frontend/Admin/EventController.php @@ -4,7 +4,6 @@ use App\Enum\EventRejectionReason; use App\Exceptions\HafasException; -use App\Exceptions\TelegramException; use App\Http\Controllers\Backend\Admin\EventController as AdminEventBackend; use App\Http\Controllers\Controller; use App\Http\Controllers\HafasController; @@ -15,7 +14,6 @@ use Carbon\Carbon; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; -use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\DB; use Illuminate\Validation\Rules\Enum; use Illuminate\View\View; @@ -108,21 +106,9 @@ public function denySuggestion(Request $request): RedirectResponse { ]); $eventSuggestion = EventSuggestion::find($validated['id']); $eventSuggestion->update(['processed' => true]); - if (!App::runningUnitTests() && TelegramService::isAdminActive()) { - try { - TelegramService::admin()->sendMessage( - strtr("Event suggestion denied" . PHP_EOL . - "Title: :name" . PHP_EOL - . "Denial reason: :reason" . PHP_EOL - . "Denial user: :username" . PHP_EOL, [ - ':name' => $eventSuggestion->name, - ':reason' => EventRejectionReason::from($validated['rejectionReason'])->getReason(), - ':username' => auth()->user()->username, - ]) - ); - } catch (TelegramException $exception) { - report($exception); - } + + if ($eventSuggestion->admin_notification_id !== null) { + TelegramService::admin()->deleteMessage($eventSuggestion->admin_notification_id); } $eventSuggestion->user->notify( @@ -183,19 +169,9 @@ public function acceptSuggestion(Request $request): RedirectResponse { ]); $eventSuggestion->update(['processed' => true]); - if (!App::runningUnitTests() && TelegramService::isAdminActive()) { - try { - TelegramService::admin()->sendMessage( - strtr("Event suggestion accepted" . PHP_EOL . - "Title: :name" . PHP_EOL - . "Accepting user: :username" . PHP_EOL, [ - ':name' => $eventSuggestion->name, - ':username' => auth()->user()->username, - ]) - ); - } catch (TelegramException $exception) { - report($exception); - } + + if ($eventSuggestion->admin_notification_id !== null) { + TelegramService::admin()->deleteMessage($eventSuggestion->admin_notification_id); } $eventSuggestion->user->notify(new EventSuggestionProcessed($eventSuggestion, $event)); diff --git a/app/Models/EventSuggestion.php b/app/Models/EventSuggestion.php index 18d164b62..334d72f51 100644 --- a/app/Models/EventSuggestion.php +++ b/app/Models/EventSuggestion.php @@ -2,23 +2,42 @@ namespace App\Models; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +/** + * // properties + * @property int $id + * @property int $user_id + * @property string $name + * @property string $host + * @property string $url + * @property int $station_id + * @property Carbon $begin + * @property Carbon $end + * @property string $hashtag + * @property int $admin_notification_id + * @property bool $processed + */ class EventSuggestion extends Model { use HasFactory; - protected $fillable = ['user_id', 'name', 'host', 'url', 'station_id', 'begin', 'end', 'hashtag', 'processed']; + protected $fillable = [ + 'user_id', 'name', 'host', 'url', 'station_id', 'begin', 'end', 'hashtag', + 'admin_notification_id', 'processed' + ]; protected $casts = [ - 'id' => 'integer', - 'user_id' => 'integer', - 'station_id' => 'integer', - 'begin' => 'datetime', - 'end' => 'datetime', - 'hashtag' => 'string', - 'processed' => 'boolean', + 'id' => 'integer', + 'user_id' => 'integer', + 'station_id' => 'integer', + 'begin' => 'datetime', + 'end' => 'datetime', + 'hashtag' => 'string', + 'admin_notification_id' => 'integer', + 'processed' => 'boolean', ]; public function user(): BelongsTo { diff --git a/database/migrations/2024_08_13_000000_add_admin_notification_id_to_event_suggestions.php b/database/migrations/2024_08_13_000000_add_admin_notification_id_to_event_suggestions.php new file mode 100644 index 000000000..749d30eb8 --- /dev/null +++ b/database/migrations/2024_08_13_000000_add_admin_notification_id_to_event_suggestions.php @@ -0,0 +1,20 @@ +unsignedBigInteger('admin_notification_id')->nullable()->after('hashtag'); + }); + } + + public function down(): void { + Schema::table('event_suggestions', static function(Blueprint $table) { + $table->dropColumn('admin_notification_id'); + }); + } +}; diff --git a/lang/fr.json b/lang/fr.json index 50892dc64..3a80582ae 100644 --- a/lang/fr.json +++ b/lang/fr.json @@ -514,7 +514,7 @@ "overlapping-checkin.force-yes": "Forcer.", "report-bug": "Signaler une erreur", "export.error.time": "Vous ne pouvez exporter des voyages que pour une période maximale de 365 jours.", - "notifications.eventSuggestionProcessed.lead": "Votre suggestion d'événement :nom a été modifiée.", + "notifications.eventSuggestionProcessed.lead": "Votre suggestion d'événement :name a été modifiée.", "other": "Autre", "exit": "Sortir", "platform": "Voie",