Some checks failed
Tests / PHP 8.1 (push) Has been cancelled
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Http\Controllers\Api;
|
|
|
|
use App\Abstracts\Http\ApiController;
|
|
use Illuminate\Database\QueryException;
|
|
use Modules\EcommerceFinance\Http\Requests\ImportBatch as Request;
|
|
use Modules\EcommerceFinance\Jobs\ProcessImportBatch;
|
|
use Modules\EcommerceFinance\Models\ImportBatch;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ImportBatches extends ApiController
|
|
{
|
|
public function index()
|
|
{
|
|
$batches = ImportBatch::with('store.channel')
|
|
->latest('id')
|
|
->paginate($this->limit());
|
|
|
|
return response()->json($batches);
|
|
}
|
|
|
|
public function show(ImportBatch $importBatch)
|
|
{
|
|
return response()->json($importBatch->load('store.channel'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
if (! Storage::disk('uploads')->exists($request->input('source_file'))) {
|
|
return $this->errorBadRequest('The source file does not exist in the private uploads storage.');
|
|
}
|
|
|
|
try {
|
|
$batch = ImportBatch::create(array_merge($request->validated(), [
|
|
'company_id' => company_id(),
|
|
'status' => 'queued',
|
|
'created_by' => user_id(),
|
|
'created_from' => 'ecommerce-finance',
|
|
]));
|
|
} catch (QueryException $exception) {
|
|
return $this->errorBadRequest('An identical import batch has already been created.');
|
|
}
|
|
|
|
$this->dispatch(new ProcessImportBatch($batch->id));
|
|
|
|
$batch->refresh();
|
|
|
|
return response()
|
|
->json($batch, 201)
|
|
->header('Location', route('api.ecommerce-finance.import-batches.show', $batch));
|
|
}
|
|
|
|
private function limit(): int
|
|
{
|
|
return min(max((int) request('limit', 25), 1), 100);
|
|
}
|
|
}
|