2026-03-08 01:18:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Actions;
|
|
|
|
|
|
|
|
|
|
use App\Models\PlatformAccount;
|
|
|
|
|
use App\Models\PlatformChannel;
|
|
|
|
|
use App\Models\PlatformInstance;
|
2026-03-08 01:42:21 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use RuntimeException;
|
2026-03-08 01:18:12 +01:00
|
|
|
|
|
|
|
|
class CreateChannelAction
|
|
|
|
|
{
|
|
|
|
|
public function execute(string $name, int $platformInstanceId, ?int $languageId = null, ?string $description = null): PlatformChannel
|
|
|
|
|
{
|
|
|
|
|
$platformInstance = PlatformInstance::findOrFail($platformInstanceId);
|
|
|
|
|
|
|
|
|
|
$activeAccounts = PlatformAccount::where('instance_url', $platformInstance->url)
|
|
|
|
|
->where('is_active', true)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
if ($activeAccounts->isEmpty()) {
|
2026-03-08 01:42:21 +01:00
|
|
|
throw new RuntimeException('No active platform accounts found for this instance. Please create a platform account first.');
|
2026-03-08 01:18:12 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-08 01:42:21 +01:00
|
|
|
return DB::transaction(function () use ($name, $platformInstanceId, $languageId, $description, $activeAccounts) {
|
|
|
|
|
$channel = PlatformChannel::create([
|
|
|
|
|
'platform_instance_id' => $platformInstanceId,
|
|
|
|
|
'channel_id' => $name,
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'display_name' => ucfirst($name),
|
|
|
|
|
'description' => $description,
|
|
|
|
|
'language_id' => $languageId,
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$channel->platformAccounts()->attach($activeAccounts->first()->id, [
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
'priority' => 1,
|
|
|
|
|
'created_at' => now(),
|
|
|
|
|
'updated_at' => now(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return $channel->load('platformAccounts');
|
|
|
|
|
});
|
2026-03-08 01:18:12 +01:00
|
|
|
}
|
|
|
|
|
}
|