Phase 1: Setup & Database (13 tasks completed) - Add assignments JSONB column to backup_items table - Add group_mapping JSONB column to restore_runs table - Extend BackupItem model with 7 assignment accessor methods - Extend RestoreRun model with 8 group mapping helper methods - Add scopeWithAssignments() query scope to BackupItem - Update graph_contracts.php with assignments endpoints - Create 5 factories: BackupItem, RestoreRun, Tenant, BackupSet, Policy - Add 30 unit tests (15 BackupItem, 15 RestoreRun) - all passing Phase 2: Graph API Integration (16 tasks completed) - Create AssignmentFetcher service with fallback strategy - Create GroupResolver service with orphaned ID handling - Create ScopeTagResolver service with 1-hour caching - Implement fail-soft error handling for all services - Add 17 unit tests (5 AssignmentFetcher, 6 GroupResolver, 6 ScopeTagResolver) - all passing - Total: 71 assertions across all Phase 2 tests Test Results: - Phase 1: 30/30 tests passing (45 assertions) - Phase 2: 17/17 tests passing (71 assertions) - Total: 47/47 tests passing (116 assertions) - Code formatted with Pint (PSR-12 compliant) Next: Phase 3 - US1 Backup with Assignments (12 tasks)
89 lines
2.0 KiB
PHP
89 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Support\Concerns\InteractsWithODataTypes;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class BackupItem extends Model
|
|
{
|
|
use HasFactory;
|
|
use InteractsWithODataTypes;
|
|
use SoftDeletes;
|
|
|
|
protected $guarded = [];
|
|
|
|
protected $casts = [
|
|
'payload' => 'array',
|
|
'metadata' => 'array',
|
|
'assignments' => 'array',
|
|
'captured_at' => 'datetime',
|
|
];
|
|
|
|
public function tenant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Tenant::class);
|
|
}
|
|
|
|
public function backupSet(): BelongsTo
|
|
{
|
|
return $this->belongsTo(BackupSet::class);
|
|
}
|
|
|
|
public function policy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Policy::class);
|
|
}
|
|
|
|
// Assignment helpers
|
|
public function getAssignmentCountAttribute(): int
|
|
{
|
|
return count($this->assignments ?? []);
|
|
}
|
|
|
|
public function hasAssignments(): bool
|
|
{
|
|
return ! empty($this->assignments);
|
|
}
|
|
|
|
public function getGroupIdsAttribute(): array
|
|
{
|
|
return collect($this->assignments ?? [])
|
|
->pluck('target.groupId')
|
|
->filter()
|
|
->unique()
|
|
->values()
|
|
->toArray();
|
|
}
|
|
|
|
public function getScopeTagIdsAttribute(): array
|
|
{
|
|
return $this->metadata['scope_tag_ids'] ?? ['0'];
|
|
}
|
|
|
|
public function getScopeTagNamesAttribute(): array
|
|
{
|
|
return $this->metadata['scope_tag_names'] ?? ['Default'];
|
|
}
|
|
|
|
public function hasOrphanedAssignments(): bool
|
|
{
|
|
return $this->metadata['has_orphaned_assignments'] ?? false;
|
|
}
|
|
|
|
public function assignmentsFetchFailed(): bool
|
|
{
|
|
return $this->metadata['assignments_fetch_failed'] ?? false;
|
|
}
|
|
|
|
// Scopes
|
|
public function scopeWithAssignments($query)
|
|
{
|
|
return $query->whereNotNull('assignments')
|
|
->whereRaw('json_array_length(assignments) > 0');
|
|
}
|
|
}
|