Skip to content

Commit

Permalink
🚸 delete backend notifications when event is processed (#2854)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Aug 13, 2024
1 parent 4d959e4 commit 57039c5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 40 deletions.
3 changes: 2 additions & 1 deletion app/Http/Controllers/Backend/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function suggestEvent(

try {
if (TelegramService::isAdminActive()) {
TelegramService::admin()->sendMessage(
$messageId = TelegramService::admin()->sendMessage(
strtr("<b>New event suggestion:</b>" . PHP_EOL .
"Title: :name" . PHP_EOL .
"Begin: :begin" . PHP_EOL .
Expand All @@ -53,6 +53,7 @@ public static function suggestEvent(
':username' => $eventSuggestion->user->username,
])
);
$eventSuggestion->update(['admin_notification_id' => $messageId]);
}
} catch (TelegramException $exception) {
report($exception);
Expand Down
36 changes: 6 additions & 30 deletions app/Http/Controllers/Frontend/Admin/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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("<b>Event suggestion denied</b>" . 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(
Expand Down Expand Up @@ -183,19 +169,9 @@ public function acceptSuggestion(Request $request): RedirectResponse {
]);

$eventSuggestion->update(['processed' => true]);
if (!App::runningUnitTests() && TelegramService::isAdminActive()) {
try {
TelegramService::admin()->sendMessage(
strtr("<b>Event suggestion accepted</b>" . 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));
Expand Down
35 changes: 27 additions & 8 deletions app/Models/EventSuggestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void {
Schema::table('event_suggestions', static function(Blueprint $table) {
$table->unsignedBigInteger('admin_notification_id')->nullable()->after('hashtag');
});
}

public function down(): void {
Schema::table('event_suggestions', static function(Blueprint $table) {
$table->dropColumn('admin_notification_id');
});
}
};
2 changes: 1 addition & 1 deletion lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>:nom</b> a été modifiée.",
"notifications.eventSuggestionProcessed.lead": "Votre suggestion d'événement <b>:name</b> a été modifiée.",
"other": "Autre",
"exit": "Sortir",
"platform": "Voie",
Expand Down

0 comments on commit 57039c5

Please sign in to comment.