42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Models\PlatformAccount;
|
|
use App\Models\PlatformChannel;
|
|
use App\Models\PlatformInstance;
|
|
|
|
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()) {
|
|
throw new \RuntimeException('No active platform accounts found for this instance. Please create a platform account first.');
|
|
}
|
|
|
|
$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');
|
|
}
|
|
}
|