Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
GetImportReportQuery
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 execute
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Importing\Application\Queries;
6
7use ImovelZapAi\Importing\Infrastructure\Persistence\Eloquent\ImportErrorModel;
8use ImovelZapAi\Importing\Infrastructure\Persistence\Eloquent\ImportJobModel;
9use ImovelZapAi\Importing\Infrastructure\Persistence\Eloquent\ImportLogModel;
10
11final class GetImportReportQuery
12{
13    /**
14     * @return array<string, mixed>
15     */
16    public function execute(ImportJobModel $job): array
17    {
18        $issues = ImportErrorModel::query()
19            ->where('import_job_id', $job->id)
20            ->orderBy('listing_index')
21            ->get()
22            ->map(static fn (ImportErrorModel $error): array => [
23                'id' => $error->id,
24                'severity' => $error->severity->value ?? (string) $error->severity,
25                'code' => $error->code,
26                'external_id' => $error->external_id,
27                'listing_index' => $error->listing_index,
28                'path' => $error->path,
29                'message' => $error->message,
30                'status' => $error->status,
31                'resolution' => $error->resolution,
32            ])
33            ->all();
34
35        $itemResults = ImportLogModel::query()
36            ->where('import_job_id', $job->id)
37            ->whereNotNull('listing_index')
38            ->orderBy('listing_index')
39            ->get()
40            ->map(static fn (ImportLogModel $log): array => [
41                'listing_index' => $log->listing_index,
42                'external_id' => $log->external_id,
43                'property_id' => $log->property_id,
44                'event' => $log->event->value ?? (string) $log->event,
45                'result' => $log->result,
46                'message' => $log->message,
47                'photos_count' => $log->photos_count,
48                'duration_ms' => $log->duration_ms,
49            ])
50            ->all();
51
52        return [
53            'id' => $job->id,
54            'status' => $job->status->value ?? (string) $job->status,
55            'summary' => $job->report_summary ?? [
56                'imported' => $job->imported_count,
57                'updated' => $job->updated_count,
58                'unchanged' => $job->unchanged_count,
59                'ignored' => $job->ignored_count,
60                'failed' => $job->failed_count,
61                'photos_imported' => $job->photos_imported,
62            ],
63            'issues' => $issues,
64            'items' => $itemResults,
65            'completed_at' => $job->completed_at?->toIso8601String(),
66        ];
67    }
68}