#591 - notification-channels

This commit is contained in:
Saeed Vaziry
2025-05-19 20:05:38 +02:00
parent 563b9c5909
commit cdc012c270
16 changed files with 611 additions and 100 deletions

View File

@ -5,6 +5,7 @@
use App\Models\NotificationChannel;
use App\Models\User;
use Exception;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
@ -17,10 +18,12 @@ class AddChannel
*/
public function add(User $user, array $input): void
{
Validator::make($input, self::rules($input))->validate();
$channel = new NotificationChannel([
'user_id' => $user->id,
'provider' => $input['provider'],
'label' => $input['label'],
'label' => $input['name'],
'project_id' => isset($input['global']) && $input['global'] ? null : $user->current_project_id,
]);
$channel->data = $channel->provider()->createData($input);
@ -63,7 +66,7 @@ public static function rules(array $input): array
'required',
Rule::in(config('core.notification_channels_providers')),
],
'label' => 'required',
'name' => 'required',
];
return array_merge($rules, self::providerRules($input));

View File

@ -13,7 +13,7 @@ class EditChannel
public function edit(NotificationChannel $notificationChannel, User $user, array $input): void
{
$notificationChannel->fill([
'label' => $input['label'],
'label' => $input['name'],
'project_id' => isset($input['global']) && $input['global'] ? null : $user->current_project_id,
]);
$notificationChannel->save();
@ -26,7 +26,7 @@ public function edit(NotificationChannel $notificationChannel, User $user, array
public static function rules(array $input): array
{
return [
'label' => 'required',
'name' => 'required',
];
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace App\Http\Controllers;
use App\Actions\NotificationChannels\AddChannel;
use App\Actions\NotificationChannels\EditChannel;
use App\Http\Resources\NotificationChannelResource;
use App\Models\NotificationChannel;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Inertia\Inertia;
use Inertia\Response;
use Spatie\RouteAttributes\Attributes\Delete;
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Middleware;
use Spatie\RouteAttributes\Attributes\Patch;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Prefix;
#[Prefix('settings/notification-channels')]
#[Middleware(['auth'])]
class NotificationChannelController extends Controller
{
#[Get('/', name: 'notification-channels')]
public function index(): Response
{
$this->authorize('viewAny', NotificationChannel::class);
return Inertia::render('notification-channels/index', [
'notificationChannels' => NotificationChannelResource::collection(NotificationChannel::getByProjectId(user()->current_project_id)->simplePaginate(config('web.pagination_size'))),
]);
}
#[Get('/json', name: 'notification-channels.json')]
public function json(): ResourceCollection
{
$this->authorize('viewAny', NotificationChannel::class);
return NotificationChannelResource::collection(NotificationChannel::getByProjectId(user()->current_project_id)->get());
}
#[Post('/', name: 'notification-channels.store')]
public function store(Request $request): RedirectResponse
{
$this->authorize('create', NotificationChannel::class);
app(AddChannel::class)->add(user(), $request->all());
return back()->with('success', 'Notification channel created.');
}
#[Patch('/{notificationChannel}', name: 'notification-channels.update')]
public function update(Request $request, NotificationChannel $notificationChannel): RedirectResponse
{
$this->authorize('update', $notificationChannel);
app(EditChannel::class)->edit($notificationChannel, user(), $request->all());
return back()->with('success', 'Notification channel updated.');
}
#[Delete('{notificationChannel}', name: 'notification-channels.destroy')]
public function destroy(NotificationChannel $notificationChannel): RedirectResponse
{
$this->authorize('delete', $notificationChannel);
$notificationChannel->delete();
return to_route('notification-channels')->with('success', 'Notification channel deleted.');
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Http\Resources;
use App\Models\NotificationChannel;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/** @mixin NotificationChannel */
class NotificationChannelResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'project_id' => $this->project_id,
'global' => is_null($this->project_id),
'name' => $this->label,
'provider' => $this->provider,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}

View File

@ -3,6 +3,7 @@
namespace App\Models;
use App\Notifications\NotificationInterface;
use Database\Factories\NotificationChannelFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -14,11 +15,11 @@
* @property array<string, mixed> $data
* @property string $label
* @property bool $connected
* @property int $project_id
* @property ?int $project_id
*/
class NotificationChannel extends AbstractModel
{
/** @use HasFactory<\Database\Factories\NotificationChannelFactory> */
/** @use HasFactory<NotificationChannelFactory> */
use HasFactory;
use Notifiable;