Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| GenerateTransactionsAction | |
100.00% |
17 / 17 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Action\SuperAdmin; |
| 6 | |
| 7 | use App\Domain\Auth\Data\UserAuthData; |
| 8 | use App\Domain\Exception\BadRequestException; |
| 9 | use App\Domain\Exception\ForbiddenException; |
| 10 | use App\Domain\SuperAdmin\Service\SuperAdminService; |
| 11 | use App\Renderer\JsonRenderer; |
| 12 | use App\Support\Row; |
| 13 | use Psr\Http\Message\ResponseInterface; |
| 14 | use Psr\Http\Message\ServerRequestInterface; |
| 15 | |
| 16 | /** |
| 17 | * Generate transaction history for an existing account. |
| 18 | */ |
| 19 | final readonly class GenerateTransactionsAction |
| 20 | { |
| 21 | public function __construct( |
| 22 | private JsonRenderer $renderer, |
| 23 | private SuperAdminService $service, |
| 24 | ) {} |
| 25 | |
| 26 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
| 27 | { |
| 28 | // Verify super_admin role |
| 29 | $user = $request->getAttribute('user'); |
| 30 | if (!$user instanceof UserAuthData || $user->role !== 'super_admin') { |
| 31 | throw new ForbiddenException('Super admin access required'); |
| 32 | } |
| 33 | |
| 34 | $data = (array)$request->getParsedBody(); |
| 35 | |
| 36 | // Validate required field |
| 37 | $accountId = Row::nullableInt($data, 'accountId'); |
| 38 | if ($accountId === null || $accountId <= 0) { |
| 39 | throw new BadRequestException('Account ID is required'); |
| 40 | } |
| 41 | |
| 42 | $result = $this->service->generateTransactions( |
| 43 | accountId: $accountId, |
| 44 | monthsBack: Row::nullableInt($data, 'monthsBack') ?? 6, |
| 45 | ); |
| 46 | |
| 47 | return $this->renderer->json($response, [ |
| 48 | 'success' => true, |
| 49 | 'message' => 'Transactions generated successfully', |
| 50 | 'data' => $result, |
| 51 | ]); |
| 52 | } |
| 53 | } |