2025-07-05 01:55:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2025-07-06 01:35:59 +02:00
|
|
|
use Database\Factories\PlatformAccountFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2025-07-05 01:55:53 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2025-07-05 02:19:59 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2025-07-05 01:55:53 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
|
use App\Enums\PlatformEnum;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @property int $id
|
|
|
|
|
* @property PlatformEnum $platform
|
|
|
|
|
* @property string $instance_url
|
|
|
|
|
* @property string $username
|
|
|
|
|
* @property string $password
|
|
|
|
|
* @property string $settings
|
|
|
|
|
* @property bool $is_active
|
|
|
|
|
* @property Carbon $last_tested_at
|
|
|
|
|
* @property string $status
|
|
|
|
|
* @property Carbon $created_at
|
|
|
|
|
* @property Carbon $updated_at
|
2025-07-05 02:29:50 +02:00
|
|
|
* @property Collection $activeChannels
|
2025-07-05 01:55:53 +02:00
|
|
|
* @method static where(string $string, PlatformEnum $platform)
|
|
|
|
|
* @method static orderBy(string $string)
|
|
|
|
|
* @method static create(array $validated)
|
|
|
|
|
*/
|
|
|
|
|
class PlatformAccount extends Model
|
|
|
|
|
{
|
2025-07-06 01:35:59 +02:00
|
|
|
/** @use HasFactory<PlatformAccountFactory> */
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
2025-07-05 01:55:53 +02:00
|
|
|
protected $fillable = [
|
|
|
|
|
'platform',
|
|
|
|
|
'instance_url',
|
|
|
|
|
'username',
|
|
|
|
|
'password',
|
|
|
|
|
'api_token',
|
|
|
|
|
'settings',
|
|
|
|
|
'is_active',
|
|
|
|
|
'last_tested_at',
|
|
|
|
|
'status'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'platform' => PlatformEnum::class,
|
|
|
|
|
'settings' => 'array',
|
|
|
|
|
'is_active' => 'boolean',
|
|
|
|
|
'last_tested_at' => 'datetime'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Encrypt password when storing
|
|
|
|
|
protected function password(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
get: fn ($value) => $value ? Crypt::decryptString($value) : null,
|
|
|
|
|
set: fn ($value) => $value ? Crypt::encryptString($value) : null,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Encrypt API token when storing
|
|
|
|
|
protected function apiToken(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
get: fn ($value) => $value ? Crypt::decryptString($value) : null,
|
|
|
|
|
set: fn ($value) => $value ? Crypt::encryptString($value) : null,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the active accounts for a platform (returns collection)
|
|
|
|
|
public static function getActive(PlatformEnum $platform): Collection
|
|
|
|
|
{
|
|
|
|
|
return static::where('platform', $platform)
|
|
|
|
|
->where('is_active', true)
|
|
|
|
|
->get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set this account as active (deactivates others for same platform)
|
|
|
|
|
public function setAsActive(): void
|
|
|
|
|
{
|
|
|
|
|
// Deactivate other accounts for this platform
|
|
|
|
|
static::where('platform', $this->platform)
|
|
|
|
|
->where('id', '!=', $this->id)
|
|
|
|
|
->update(['is_active' => false]);
|
|
|
|
|
|
|
|
|
|
// Activate this account
|
|
|
|
|
$this->update(['is_active' => true]);
|
|
|
|
|
}
|
2025-07-05 02:19:59 +02:00
|
|
|
|
2025-07-05 02:29:50 +02:00
|
|
|
public function channels(): BelongsToMany
|
2025-07-05 02:19:59 +02:00
|
|
|
{
|
2025-07-05 02:29:50 +02:00
|
|
|
return $this->belongsToMany(PlatformChannel::class, 'platform_account_channels')
|
2025-07-05 02:19:59 +02:00
|
|
|
->withPivot(['is_active', 'priority'])
|
|
|
|
|
->withTimestamps();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-05 02:29:50 +02:00
|
|
|
public function activeChannels(): BelongsToMany
|
2025-07-05 02:19:59 +02:00
|
|
|
{
|
2025-07-05 02:29:50 +02:00
|
|
|
return $this->channels()
|
2025-07-05 02:19:59 +02:00
|
|
|
->wherePivot('is_active', true)
|
|
|
|
|
->orderByPivot('priority', 'desc');
|
|
|
|
|
}
|
2025-07-05 01:55:53 +02:00
|
|
|
}
|