mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Build import job API endpoint.
This commit is contained in:
parent
00fb809ab9
commit
3b15415a1b
173
app/Api/V1/Controllers/ImportController.php
Normal file
173
app/Api/V1/Controllers/ImportController.php
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ImportController.php
|
||||||
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||||
|
*
|
||||||
|
* This file is part of Firefly III.
|
||||||
|
*
|
||||||
|
* Firefly III is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Firefly III is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace FireflyIII\Api\V1\Controllers;
|
||||||
|
|
||||||
|
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
||||||
|
use FireflyIII\Helpers\Filter\InternalTransferFilter;
|
||||||
|
use FireflyIII\Models\ImportJob;
|
||||||
|
use FireflyIII\Models\TransactionType;
|
||||||
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||||
|
use FireflyIII\Support\Http\Api\Transactions;
|
||||||
|
use FireflyIII\Transformers\ImportJobTransformer;
|
||||||
|
use FireflyIII\Transformers\TransactionTransformer;
|
||||||
|
use FireflyIII\User;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use League\Fractal\Manager;
|
||||||
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||||
|
use League\Fractal\Resource\Collection as FractalCollection;
|
||||||
|
use League\Fractal\Resource\Item;
|
||||||
|
use League\Fractal\Serializer\JsonApiSerializer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ImportController
|
||||||
|
*/
|
||||||
|
class ImportController extends Controller
|
||||||
|
{
|
||||||
|
use Transactions;
|
||||||
|
/** @var ImportJobRepositoryInterface Import job repository. */
|
||||||
|
private $repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LinkTypeController constructor.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->middleware(
|
||||||
|
function ($request, $next) {
|
||||||
|
/** @var User $user */
|
||||||
|
$user = auth()->user();
|
||||||
|
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||||
|
$this->repository->setUser($user);
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
*
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function listAll(Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
// create some objects:
|
||||||
|
$manager = new Manager;
|
||||||
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
|
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
||||||
|
|
||||||
|
// get list of accounts. Count it and split it.
|
||||||
|
$collection = $this->repository->get();
|
||||||
|
$count = $collection->count();
|
||||||
|
$importJobs = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
|
||||||
|
|
||||||
|
// make paginator:
|
||||||
|
$paginator = new LengthAwarePaginator($importJobs, $count, $pageSize, $this->parameters->get('page'));
|
||||||
|
$paginator->setPath(route('api.v1.import.list') . $this->buildParams());
|
||||||
|
|
||||||
|
// present to user.
|
||||||
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
$resource = new FractalCollection($importJobs, new ImportJobTransformer($this->parameters), 'import_jobs');
|
||||||
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||||
|
|
||||||
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
* @param ImportJob $importJob
|
||||||
|
*
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function show(Request $request, ImportJob $importJob): JsonResponse
|
||||||
|
{
|
||||||
|
$manager = new Manager;
|
||||||
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
$resource = new Item($importJob, new ImportJobTransformer($this->parameters), 'import_jobs');
|
||||||
|
|
||||||
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show all transactions
|
||||||
|
*
|
||||||
|
* @param ImportJob $importJob
|
||||||
|
*
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function transactions(Request $request, ImportJob $importJob): JsonResponse
|
||||||
|
{
|
||||||
|
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
|
||||||
|
$type = $request->get('type') ?? 'default';
|
||||||
|
$this->parameters->set('type', $type);
|
||||||
|
|
||||||
|
$types = $this->mapTypes($this->parameters->get('type'));
|
||||||
|
$manager = new Manager();
|
||||||
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
|
||||||
|
$tag = $importJob->tag;
|
||||||
|
$transactions = new Collection();
|
||||||
|
$paginator = new LengthAwarePaginator($transactions, 0, $pageSize);
|
||||||
|
$paginator->setPath(route('api.v1.import.transactions', [$importJob->key]) . $this->buildParams());
|
||||||
|
|
||||||
|
if (null !== $tag) {
|
||||||
|
/** @var User $admin */
|
||||||
|
$admin = auth()->user();
|
||||||
|
/** @var TransactionCollectorInterface $collector */
|
||||||
|
$collector = app(TransactionCollectorInterface::class);
|
||||||
|
$collector->setUser($admin);
|
||||||
|
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
|
||||||
|
$collector->setAllAssetAccounts();
|
||||||
|
$collector->setTag($tag);
|
||||||
|
|
||||||
|
if (\in_array(TransactionType::TRANSFER, $types, true)) {
|
||||||
|
$collector->removeFilter(InternalTransferFilter::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
|
||||||
|
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
|
||||||
|
}
|
||||||
|
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
|
||||||
|
$collector->setTypes($types);
|
||||||
|
$paginator = $collector->getPaginatedTransactions();
|
||||||
|
$paginator->setPath(route('api.v1.transactions.index') . $this->buildParams());
|
||||||
|
$transactions = $paginator->getCollection();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$repository = app(JournalRepositoryInterface::class);
|
||||||
|
$resource = new FractalCollection($transactions, new TransactionTransformer($this->parameters, $repository), 'transactions');
|
||||||
|
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||||
|
|
||||||
|
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -47,6 +47,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|||||||
* @property array extended_status
|
* @property array extended_status
|
||||||
* @property int id
|
* @property int id
|
||||||
* @property Carbon $created_at
|
* @property Carbon $created_at
|
||||||
|
* @property Carbon $updated_at
|
||||||
*/
|
*/
|
||||||
class ImportJob extends Model
|
class ImportJob extends Model
|
||||||
{
|
{
|
||||||
|
@ -170,6 +170,16 @@ class ImportJobRepository implements ImportJobRepositoryInterface
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all import jobs.
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function get(): Collection
|
||||||
|
{
|
||||||
|
return $this->user->importJobs()->get();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all attachments for job.
|
* Return all attachments for job.
|
||||||
*
|
*
|
||||||
|
@ -35,7 +35,6 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|||||||
*/
|
*/
|
||||||
interface ImportJobRepositoryInterface
|
interface ImportJobRepositoryInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add message to job.
|
* Add message to job.
|
||||||
*
|
*
|
||||||
@ -77,6 +76,13 @@ interface ImportJobRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function findByKey(string $key): ?ImportJob;
|
public function findByKey(string $key): ?ImportJob;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all import jobs.
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function get(): Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all attachments for job.
|
* Return all attachments for job.
|
||||||
*
|
*
|
||||||
|
93
app/Transformers/ImportJobTransformer.php
Normal file
93
app/Transformers/ImportJobTransformer.php
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ImportJobTransformer.php
|
||||||
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||||
|
*
|
||||||
|
* This file is part of Firefly III.
|
||||||
|
*
|
||||||
|
* Firefly III is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Firefly III is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace FireflyIII\Transformers;
|
||||||
|
|
||||||
|
|
||||||
|
use FireflyIII\Models\ImportJob;
|
||||||
|
use League\Fractal\TransformerAbstract;
|
||||||
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ImportJobTransformer
|
||||||
|
*/
|
||||||
|
class ImportJobTransformer extends TransformerAbstract
|
||||||
|
{
|
||||||
|
/** @var ParameterBag */
|
||||||
|
protected $parameters;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PiggyBankTransformer constructor.
|
||||||
|
*
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
*
|
||||||
|
* @param ParameterBag $parameters
|
||||||
|
*/
|
||||||
|
public function __construct(ParameterBag $parameters)
|
||||||
|
{
|
||||||
|
$this->parameters = $parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform the import job.
|
||||||
|
*
|
||||||
|
* @param ImportJob $importJob
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function transform(ImportJob $importJob): array
|
||||||
|
{
|
||||||
|
$tag= $importJob->tag;
|
||||||
|
$tagId = null;
|
||||||
|
$tagTag = null;
|
||||||
|
if(null !== $tag) {
|
||||||
|
$tagId = $tag->id;
|
||||||
|
$tagTag = $tag->tag;
|
||||||
|
}
|
||||||
|
$data = [
|
||||||
|
'id' => (int)$importJob->id,
|
||||||
|
'updated_at' => $importJob->updated_at->toAtomString(),
|
||||||
|
'created_at' => $importJob->created_at->toAtomString(),
|
||||||
|
'key' => $importJob->key,
|
||||||
|
'file_type' => $importJob->file_type,
|
||||||
|
'provider' => $importJob->provider,
|
||||||
|
'status' => $importJob->status,
|
||||||
|
'stage' => $importJob->stage,
|
||||||
|
'configuration' => $importJob->configuration,
|
||||||
|
'extended_status' => $importJob->extended_status,
|
||||||
|
'transactions' => $importJob->transactions,
|
||||||
|
'errors' => $importJob->errors,
|
||||||
|
'tag_id' => $tagId,
|
||||||
|
'tag_tag' => $tagTag,
|
||||||
|
'links' => [
|
||||||
|
[
|
||||||
|
'rel' => 'self',
|
||||||
|
'uri' => '/import/' . $importJob->key,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
@ -144,13 +144,15 @@ Route::group(
|
|||||||
);
|
);
|
||||||
|
|
||||||
Route::group(
|
Route::group(
|
||||||
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'journal_links', 'as' => 'api.v1.journal_links.'],
|
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'import', 'as' => 'api.v1.import.'],
|
||||||
function () {
|
function () {
|
||||||
|
|
||||||
|
// Transaction Links API routes:
|
||||||
|
Route::get('list', ['uses' => 'ImportController@listAll', 'as' => 'list']);
|
||||||
|
Route::get('{importJob}', ['uses' => 'ImportController@show', 'as' => 'show']);
|
||||||
|
Route::get('{importJob}/transactions', ['uses' => 'ImportController@transactions', 'as' => 'transactions']);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
Route::group(
|
Route::group(
|
||||||
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'link_types', 'as' => 'api.v1.link_types.'],
|
['middleware' => ['auth:api', 'bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'link_types', 'as' => 'api.v1.link_types.'],
|
||||||
function () {
|
function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user