190 lines
5.6 KiB
PHP
190 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\BulkOperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Intune\AuditLogger;
|
|
|
|
class BulkOperationService
|
|
{
|
|
public function __construct(
|
|
protected AuditLogger $auditLogger
|
|
) {}
|
|
|
|
public function createRun(
|
|
Tenant $tenant,
|
|
User $user,
|
|
string $resource,
|
|
string $action,
|
|
array $itemIds,
|
|
int $totalItems
|
|
): BulkOperationRun {
|
|
$run = BulkOperationRun::create([
|
|
'tenant_id' => $tenant->id,
|
|
'user_id' => $user->id,
|
|
'resource' => $resource,
|
|
'action' => $action,
|
|
'status' => 'pending',
|
|
'item_ids' => $itemIds,
|
|
'total_items' => $totalItems,
|
|
'processed_items' => 0,
|
|
'succeeded' => 0,
|
|
'failed' => 0,
|
|
'skipped' => 0,
|
|
'failures' => [],
|
|
]);
|
|
|
|
$auditLog = $this->auditLogger->log(
|
|
tenant: $tenant,
|
|
action: "bulk.{$resource}.{$action}.created",
|
|
context: [
|
|
'metadata' => [
|
|
'bulk_run_id' => $run->id,
|
|
'total_items' => $totalItems,
|
|
],
|
|
],
|
|
actorId: $user->id,
|
|
actorEmail: $user->email,
|
|
actorName: $user->name,
|
|
resourceType: 'bulk_operation_run',
|
|
resourceId: (string) $run->id
|
|
);
|
|
|
|
$run->update(['audit_log_id' => $auditLog->id]);
|
|
|
|
return $run;
|
|
}
|
|
|
|
public function start(BulkOperationRun $run): void
|
|
{
|
|
$run->update(['status' => 'running']);
|
|
}
|
|
|
|
public function recordSuccess(BulkOperationRun $run): void
|
|
{
|
|
$run->increment('processed_items');
|
|
$run->increment('succeeded');
|
|
}
|
|
|
|
public function recordFailure(BulkOperationRun $run, string $itemId, string $reason): void
|
|
{
|
|
$failures = $run->failures ?? [];
|
|
$failures[] = [
|
|
'item_id' => $itemId,
|
|
'reason' => $reason,
|
|
'timestamp' => now()->toIso8601String(),
|
|
];
|
|
|
|
$run->update([
|
|
'failures' => $failures,
|
|
'processed_items' => $run->processed_items + 1,
|
|
'failed' => $run->failed + 1,
|
|
]);
|
|
}
|
|
|
|
public function recordSkipped(BulkOperationRun $run): void
|
|
{
|
|
$run->increment('processed_items');
|
|
$run->increment('skipped');
|
|
}
|
|
|
|
public function recordSkippedWithReason(BulkOperationRun $run, string $itemId, string $reason): void
|
|
{
|
|
$failures = $run->failures ?? [];
|
|
$failures[] = [
|
|
'item_id' => $itemId,
|
|
'reason' => $reason,
|
|
'type' => 'skipped',
|
|
'timestamp' => now()->toIso8601String(),
|
|
];
|
|
|
|
$run->update([
|
|
'failures' => $failures,
|
|
'processed_items' => $run->processed_items + 1,
|
|
'skipped' => $run->skipped + 1,
|
|
]);
|
|
}
|
|
|
|
public function complete(BulkOperationRun $run): void
|
|
{
|
|
$status = $run->failed > 0 ? 'completed_with_errors' : 'completed';
|
|
$run->update(['status' => $status]);
|
|
|
|
$failureEntries = collect($run->failures ?? []);
|
|
$failedReasons = $failureEntries
|
|
->filter(fn (array $entry) => ($entry['type'] ?? 'failed') !== 'skipped')
|
|
->groupBy('reason')
|
|
->map(fn ($group) => $group->count())
|
|
->all();
|
|
|
|
$skippedReasons = $failureEntries
|
|
->filter(fn (array $entry) => ($entry['type'] ?? null) === 'skipped')
|
|
->groupBy('reason')
|
|
->map(fn ($group) => $group->count())
|
|
->all();
|
|
|
|
$this->auditLogger->log(
|
|
tenant: $run->tenant,
|
|
action: "bulk.{$run->resource}.{$run->action}.{$status}",
|
|
context: [
|
|
'metadata' => [
|
|
'bulk_run_id' => $run->id,
|
|
'succeeded' => $run->succeeded,
|
|
'failed' => $run->failed,
|
|
'skipped' => $run->skipped,
|
|
'failed_reasons' => $failedReasons,
|
|
'skipped_reasons' => $skippedReasons,
|
|
],
|
|
],
|
|
actorId: $run->user_id,
|
|
resourceType: 'bulk_operation_run',
|
|
resourceId: (string) $run->id
|
|
);
|
|
}
|
|
|
|
public function fail(BulkOperationRun $run, string $reason): void
|
|
{
|
|
$run->update(['status' => 'failed']);
|
|
|
|
$this->auditLogger->log(
|
|
tenant: $run->tenant,
|
|
action: "bulk.{$run->resource}.{$run->action}.failed",
|
|
context: [
|
|
'reason' => $reason,
|
|
'metadata' => [
|
|
'bulk_run_id' => $run->id,
|
|
],
|
|
],
|
|
actorId: $run->user_id,
|
|
status: 'failure',
|
|
resourceType: 'bulk_operation_run',
|
|
resourceId: (string) $run->id
|
|
);
|
|
}
|
|
|
|
public function abort(BulkOperationRun $run, string $reason): void
|
|
{
|
|
$run->update(['status' => 'aborted']);
|
|
|
|
$this->auditLogger->log(
|
|
tenant: $run->tenant,
|
|
action: "bulk.{$run->resource}.{$run->action}.aborted",
|
|
context: [
|
|
'reason' => $reason,
|
|
'metadata' => [
|
|
'bulk_run_id' => $run->id,
|
|
'succeeded' => $run->succeeded,
|
|
'failed' => $run->failed,
|
|
'skipped' => $run->skipped,
|
|
],
|
|
],
|
|
actorId: $run->user_id,
|
|
status: 'failure',
|
|
resourceType: 'bulk_operation_run',
|
|
resourceId: (string) $run->id
|
|
);
|
|
}
|
|
}
|