phase 4
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class AlertRule extends Model
|
||||
{
|
||||
use HasUlids;
|
||||
|
||||
protected $fillable = [
|
||||
'project_id',
|
||||
'type',
|
||||
'enabled',
|
||||
'config',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'enabled' => 'boolean',
|
||||
'config' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
|
||||
public function getSpikeThreshold(): int
|
||||
{
|
||||
return $this->config['threshold'] ?? 10;
|
||||
}
|
||||
|
||||
public function getSpikeWindowMinutes(): int
|
||||
{
|
||||
return $this->config['window_minutes'] ?? 5;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class NotificationPreference extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'project_id',
|
||||
'email_enabled',
|
||||
'database_enabled',
|
||||
'muted',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_enabled' => 'boolean',
|
||||
'database_enabled' => 'boolean',
|
||||
'muted' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,11 @@ class Project extends Model
|
||||
return $this->hasMany(Event::class);
|
||||
}
|
||||
|
||||
public function alertRules(): HasMany
|
||||
{
|
||||
return $this->hasMany(AlertRule::class);
|
||||
}
|
||||
|
||||
public function regenerateKeys(): void
|
||||
{
|
||||
$this->update([
|
||||
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
@@ -37,4 +38,18 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->projects()->wherePivot('role', 'owner');
|
||||
}
|
||||
|
||||
public function notificationPreferences(): HasMany
|
||||
{
|
||||
return $this->hasMany(NotificationPreference::class);
|
||||
}
|
||||
|
||||
public function notificationPreferenceFor(Project $project): NotificationPreference
|
||||
{
|
||||
return $this->notificationPreferences()
|
||||
->firstOrCreate(
|
||||
['project_id' => $project->id],
|
||||
['email_enabled' => true, 'database_enabled' => true, 'muted' => false],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user