Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.36% covered (warning)
86.36%
38 / 44
66.67% covered (warning)
66.67%
6 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImportWizardController
86.36% covered (warning)
86.36%
38 / 44
66.67% covered (warning)
66.67%
6 / 9
12.37
0.00% covered (danger)
0.00%
0 / 1
 index
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 store
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 show
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 updateMapping
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
2.26
 confirm
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
2.09
 retry
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
2.26
 report
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 findJob
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Importing\Presentation\Http\Controllers\Web;
6
7use App\Http\Controllers\Controller;
8use Illuminate\Http\RedirectResponse;
9use Illuminate\Http\Request;
10use Illuminate\View\View;
11use ImovelZapAi\Importing\Application\Actions\ConfirmImportAction;
12use ImovelZapAi\Importing\Application\Actions\RetryImportAction;
13use ImovelZapAi\Importing\Application\Actions\UpdateImportMappingAction;
14use ImovelZapAi\Importing\Application\Actions\UploadImportAction;
15use ImovelZapAi\Importing\Application\Queries\GetImportDashboardQuery;
16use ImovelZapAi\Importing\Application\Queries\GetImportReportQuery;
17use ImovelZapAi\Importing\Domain\Exceptions\ImportException;
18use ImovelZapAi\Importing\Infrastructure\Persistence\Eloquent\ImportJobModel;
19use ImovelZapAi\Importing\Presentation\Http\Requests\StoreImportRequest;
20use ImovelZapAi\Importing\Presentation\Http\Requests\UpdateImportMappingRequest;
21use ImovelZapAi\Tenancy\Support\TenantContext;
22
23final class ImportWizardController extends Controller
24{
25    public function index(GetImportDashboardQuery $dashboardQuery): View
26    {
27        return view('importing.dashboard', [
28            'dashboard' => $dashboardQuery->execute((string) TenantContext::id()),
29            'jobs' => ImportJobModel::query()->latest()->limit(20)->get(),
30        ]);
31    }
32
33    public function create(): View
34    {
35        return view('importing.upload');
36    }
37
38    public function store(StoreImportRequest $request, UploadImportAction $action): RedirectResponse
39    {
40        $job = $action->execute(
41            (string) TenantContext::id(),
42            $request->user(),
43            $request->file('file'),
44            $request->validated('idempotency_key'),
45        );
46
47        return redirect()
48            ->route('importing.show', $job)
49            ->with('status', 'Arquivo enviado. A análise está em andamento.');
50    }
51
52    public function show(string $import): View
53    {
54        $job = $this->findJob($import);
55
56        return view('importing.show', [
57            'job' => $job->load(['file', 'errors']),
58        ]);
59    }
60
61    public function updateMapping(
62        string $import,
63        UpdateImportMappingRequest $request,
64        UpdateImportMappingAction $action,
65    ): RedirectResponse {
66        $job = $this->findJob($import);
67
68        try {
69            $action->execute($job, $request->validated());
70        } catch (ImportException $e) {
71            return back()->withErrors(['mapping' => $e->getMessage()]);
72        }
73
74        return back()->with('status', 'Mapeamento atualizado.');
75    }
76
77    public function confirm(string $import, ConfirmImportAction $action): RedirectResponse
78    {
79        $job = $this->findJob($import);
80
81        try {
82            $action->execute($job);
83        } catch (ImportException $e) {
84            return back()->withErrors(['confirm' => $e->getMessage()]);
85        }
86
87        return redirect()
88            ->route('importing.show', $job)
89            ->with('status', 'Importação enfileirada.');
90    }
91
92    public function retry(string $import, RetryImportAction $action): RedirectResponse
93    {
94        $job = $this->findJob($import);
95
96        try {
97            $action->execute($job);
98        } catch (ImportException $e) {
99            return back()->withErrors(['retry' => $e->getMessage()]);
100        }
101
102        return back()->with('status', 'Retry enfileirado.');
103    }
104
105    public function report(string $import, GetImportReportQuery $query): View
106    {
107        $job = $this->findJob($import);
108
109        return view('importing.report', [
110            'job' => $job,
111            'report' => $query->execute($job),
112        ]);
113    }
114
115    private function findJob(string $id): ImportJobModel
116    {
117        return ImportJobModel::query()
118            ->where('tenant_id', TenantContext::id())
119            ->whereKey($id)
120            ->firstOrFail();
121    }
122}