Passed
Push — master ( 49de82...9f1fc0 )
by James
35:45 queued 23:44
created

TransactionController::showByJournal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * TransactionController.php
5
 * Copyright (c) 2019 [email protected]
6
 *
7
 * This file is part of Firefly III (https://github.com/firefly-iii).
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace FireflyIII\Api\V1\Controllers;
26
27
use FireflyIII\Api\V1\Requests\TransactionStoreRequest;
28
use FireflyIII\Api\V1\Requests\TransactionUpdateRequest;
29
use FireflyIII\Events\StoredTransactionGroup;
30
use FireflyIII\Events\UpdatedTransactionGroup;
31
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
32
use FireflyIII\Models\TransactionGroup;
33
use FireflyIII\Models\TransactionJournal;
34
use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
35
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
36
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
37
use FireflyIII\Support\Http\Api\TransactionFilter;
38
use FireflyIII\Transformers\AttachmentTransformer;
39
use FireflyIII\Transformers\PiggyBankEventTransformer;
40
use FireflyIII\Transformers\TransactionGroupTransformer;
41
use FireflyIII\User;
42
use Illuminate\Http\JsonResponse;
43
use Illuminate\Http\Request;
44
use Illuminate\Support\Collection;
45
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
46
use League\Fractal\Resource\Collection as FractalCollection;
47
use League\Fractal\Resource\Item;
48
use Log;
49
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
50
51
/**
52
 * Class TransactionController
53
 */
54
class TransactionController extends Controller
55
{
56
    use TransactionFilter;
57
58
    /** @var TransactionGroupRepositoryInterface Group repository. */
59
    private $groupRepository;
60
    /** @var JournalAPIRepositoryInterface Journal API repos */
61
    private $journalAPIRepository;
62
    /** @var JournalRepositoryInterface The journal repository */
63
    private $repository;
64
65
    /**
66
     * TransactionController constructor.
67
     *
68
     * @codeCoverageIgnore
69
     */
70
    public function __construct()
71
    {
72
        parent::__construct();
73
        $this->middleware(
74
            function ($request, $next) {
75
                /** @var User $admin */
76
                $admin = auth()->user();
77
78
                $this->repository           = app(JournalRepositoryInterface::class);
79
                $this->groupRepository      = app(TransactionGroupRepositoryInterface::class);
80
                $this->journalAPIRepository = app(JournalAPIRepositoryInterface::class);
81
                $this->repository->setUser($admin);
82
                $this->groupRepository->setUser($admin);
83
                $this->journalAPIRepository->setUser($admin);
84
85
                return $next($request);
86
            }
87
        );
88
    }
89
90
    /**
91
     * @param TransactionGroup $transactionGroup
92
     *
93
     * @return JsonResponse
94
     * @codeCoverageIgnore
95
     */
96
    public function attachments(TransactionGroup $transactionGroup): JsonResponse
97
    {
98
        $manager     = $this->getManager();
99
        $attachments = new Collection;
100
        foreach ($transactionGroup->transactionJournals as $transactionJournal) {
101
            $attachments = $this->journalAPIRepository->getAttachments($transactionJournal)->merge($attachments);
102
        }
103
104
        /** @var AttachmentTransformer $transformer */
105
        $transformer = app(AttachmentTransformer::class);
106
        $transformer->setParameters($this->parameters);
107
108
        $resource = new FractalCollection($attachments, $transformer, 'attachments');
109
110
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
111
112
    }
113
114
    /**
115
     * Remove the specified resource from storage.
116
     *
117
     * @param TransactionGroup $transactionGroup
118
     *
119
     * @return JsonResponse
120
     * @codeCoverageIgnore
121
     */
122
    public function delete(TransactionGroup $transactionGroup): JsonResponse
123
    {
124
        $this->repository->destroyGroup($transactionGroup);
125
126
        return response()->json([], 204);
127
    }
128
129
    /**
130
     * Remove the specified resource from storage.
131
     *
132
     * @param TransactionJournal $transactionJournal
133
     *
134
     * @codeCoverageIgnore
135
     * @return JsonResponse
136
     */
137
    public function deleteJournal(TransactionJournal $transactionJournal): JsonResponse
138
    {
139
        $this->repository->destroyJournal($transactionJournal);
140
141
        return response()->json([], 204);
142
    }
143
144
    /**
145
     * Show all transactions.
146
     *
147
     * @param Request $request
148
     *
149
     * @return JsonResponse
150
     * @codeCoverageIgnore
151
     */
152
    public function index(Request $request): JsonResponse
153
    {
154
        $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
155
        $type     = $request->get('type') ?? 'default';
156
        $this->parameters->set('type', $type);
157
158
        $types   = $this->mapTransactionTypes($this->parameters->get('type'));
159
        $manager = $this->getManager();
160
        /** @var User $admin */
161
        $admin = auth()->user();
162
163
        // use new group collector:
164
        /** @var GroupCollectorInterface $collector */
165
        $collector = app(GroupCollectorInterface::class);
166
        $collector
167
            ->setUser($admin)
168
            // all info needed for the API:
169
            ->withAPIInformation()
170
            // set page size:
171
            ->setLimit($pageSize)
172
            // set page to retrieve
173
            ->setPage($this->parameters->get('page'))
174
            // set types of transactions to return.
175
            ->setTypes($types);
176
177
178
        if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
179
            $collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
180
        }
181
        $paginator = $collector->getPaginatedGroups();
182
        $paginator->setPath(route('api.v1.transactions.index') . $this->buildParams());
183
        $transactions = $paginator->getCollection();
184
185
        /** @var TransactionGroupTransformer $transformer */
186
        $transformer = app(TransactionGroupTransformer::class);
187
        $transformer->setParameters($this->parameters);
188
189
        $resource = new FractalCollection($transactions, $transformer, 'transactions');
190
        $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
191
192
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
193
    }
194
195
    /**
196
     * @param TransactionGroup $transactionGroup
197
     *
198
     * @return JsonResponse
199
     * @codeCoverageIgnore
200
     */
201
    public function piggyBankEvents(TransactionGroup $transactionGroup): JsonResponse
202
    {
203
        $manager = $this->getManager();
204
        $events  = new Collection;
205
        foreach ($transactionGroup->transactionJournals as $transactionJournal) {
206
            $events = $this->journalAPIRepository->getPiggyBankEvents($transactionJournal)->merge($events);
207
        }
208
209
        /** @var PiggyBankEventTransformer $transformer */
210
        $transformer = app(PiggyBankEventTransformer::class);
211
        $transformer->setParameters($this->parameters);
212
213
        $resource = new FractalCollection($events, $transformer, 'piggy_bank_events');
214
215
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
216
217
    }
218
219
    /**
220
     * Show a single transaction.
221
     *
222
     * @param TransactionGroup $transactionGroup
223
     *
224
     * @return JsonResponse
225
     * @codeCoverageIgnore
226
     */
227
    public function show(TransactionGroup $transactionGroup): JsonResponse
228
    {
229
        $manager = $this->getManager();
230
        /** @var User $admin */
231
        $admin = auth()->user();
232
        // use new group collector:
233
        /** @var GroupCollectorInterface $collector */
234
        $collector = app(GroupCollectorInterface::class);
235
        $collector
236
            ->setUser($admin)
237
            // filter on transaction group.
238
            ->setTransactionGroup($transactionGroup)
239
            // all info needed for the API:
240
            ->withAPIInformation();
241
242
        $selectedGroup = $collector->getGroups()->first();
243
        if (null === $selectedGroup) {
244
            throw new NotFoundHttpException();
245
        }
246
        /** @var TransactionGroupTransformer $transformer */
247
        $transformer = app(TransactionGroupTransformer::class);
248
        $transformer->setParameters($this->parameters);
249
        $resource = new Item($selectedGroup, $transformer, 'transactions');
250
251
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
252
    }
253
254
    /**
255
     * Show a single transaction, by transaction journal.
256
     *
257
     * @param TransactionJournal $transactionJournal
258
     *
259
     * @return JsonResponse
260
     * @codeCoverageIgnore
261
     */
262
    public function showByJournal(TransactionJournal $transactionJournal): JsonResponse
263
    {
264
        return $this->show($transactionJournal->transactionGroup);
265
    }
266
267
    /**
268
     * Store a new transaction.
269
     *
270
     * @param TransactionStoreRequest $request
271
     *
272
     * @return JsonResponse
273
     */
274
    public function store(TransactionStoreRequest $request): JsonResponse
275
    {
276
        $data         = $request->getAll();
277
        $data['user'] = auth()->user()->id;
278
279
        Log::channel('audit')
280
           ->info('Store new transaction over API.', $data);
281
282
        $transactionGroup = $this->groupRepository->store($data);
283
284
        event(new StoredTransactionGroup($transactionGroup));
285
286
        $manager = $this->getManager();
287
        /** @var User $admin */
288
        $admin = auth()->user();
289
        // use new group collector:
290
        /** @var GroupCollectorInterface $collector */
291
        $collector = app(GroupCollectorInterface::class);
292
        $collector
293
            ->setUser($admin)
294
            // filter on transaction group.
295
            ->setTransactionGroup($transactionGroup)
296
            // all info needed for the API:
297
            ->withAPIInformation();
298
299
        $selectedGroup = $collector->getGroups()->first();
300
        if (null === $selectedGroup) {
301
            throw new NotFoundHttpException(); // @codeCoverageIgnore
302
        }
303
        /** @var TransactionGroupTransformer $transformer */
304
        $transformer = app(TransactionGroupTransformer::class);
305
        $transformer->setParameters($this->parameters);
306
        $resource = new Item($selectedGroup, $transformer, 'transactions');
307
308
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
309
    }
310
311
312
    /**
313
     * Update a transaction.
314
     *
315
     * @param TransactionUpdateRequest $request
316
     * @param TransactionGroup         $transactionGroup
317
     *
318
     * @return JsonResponse
319
     */
320
    public function update(TransactionUpdateRequest $request, TransactionGroup $transactionGroup): JsonResponse
321
    {
322
        Log::debug('Now in update routine.');
323
        $data             = $request->getAll();
324
        $transactionGroup = $this->groupRepository->update($transactionGroup, $data);
325
        $manager          = $this->getManager();
326
327
        event(new UpdatedTransactionGroup($transactionGroup));
328
329
        /** @var User $admin */
330
        $admin = auth()->user();
331
        // use new group collector:
332
        /** @var GroupCollectorInterface $collector */
333
        $collector = app(GroupCollectorInterface::class);
334
        $collector
335
            ->setUser($admin)
336
            // filter on transaction group.
337
            ->setTransactionGroup($transactionGroup)
338
            // all info needed for the API:
339
            ->withAPIInformation();
340
341
        $selectedGroup = $collector->getGroups()->first();
342
        if (null === $selectedGroup) {
343
            throw new NotFoundHttpException(); // @codeCoverageIgnore
344
        }
345
        /** @var TransactionGroupTransformer $transformer */
346
        $transformer = app(TransactionGroupTransformer::class);
347
        $transformer->setParameters($this->parameters);
348
        $resource = new Item($selectedGroup, $transformer, 'transactions');
349
350
        return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
351
352
    }
353
}
354