The Statement Section of the AP Investment Platform API documentation provides detailed information on managing user financial statements within the platform. This section is divided into the following key areas:
The Statement Section allows users to access and manage their financial statements, including transaction history and account balances. This section provides endpoints to generate financial statements for specific periods and retrieve a list of available statements. Once the page is accessed, the /api:E5rHZ0xs4/statements/availability endpoint is called to check the availability of statements for the user. The endpoint runs the App\Http\Controllers\StatementsController@userHasStatement method to retrieve statement availability data.
public function userHasStatement(StatementService $statementService)
{
$ttl = 60 * 60 * 24;
$accounts = \Cache::remember('accounts-that-have-statements', $ttl, function () use ($statementService) {
return $statementService->getAvailableStatements();
});
$accountExists = collect(array_keys($accounts))->contains(Auth::user()->email);
return RespondWithSuccess::make()->handle([
'has_statement' => $accountExists
]);
}
{
"has_statement": true
}
The Generate Statement feature allows users to create a financial statement for their account. The /api:E5rHZ0xs4/statements/request endpoint is called to generate a statement for the user. The endpoint runs the App\Http\Controllers\StatementsController@requestStatement method to create a new statement.
public function requestStatement(Request $request, StatementService $statementService): JsonResponse
{
$validated = $request->validate([
'from_date' => 'required|date_format:d-m-Y',
'to_date' => 'required|date_format:d-m-Y|after:from_date',
]);
$user = $request->user();
$statementRequest = StatementRequest::query()->create([
'email' => $user->email,
'users_id' => $user->id,
'client_name' => $user->full_name,
'from_date' => $validated['from_date'],
'to_date' => $validated['to_date'],
'status' => 'pending'
]);
GenerateStatement::dispatch($statementRequest, $statementService);
return response()->json([
'message' => 'Statement generation request received',
'request_id' => $statementRequest->id
]);
}
The requestStatement method validates the request parameters, creates a new statement request in the database, and dispatches a job to generate the statement asynchronously. The method returns a JSON response with a success message and the request ID.
{
"message": "Statement generation request received",
"request_id": "9e638ce1-52d4-44ee-93f8-258da3f97b8f"
}
The request_id is used to poll the status of the statement generation process. Polling is done by calling the /api:E5rHZ0xs4/statements/{request_id}/status endpoint, which runs the App\Http\Controllers\StatementsController@checkStatus method to check the status of the statement request.
The Statements List feature allows users to view a list of available financial statements. The /api:E5rHZ0xs4/statements?page=1 endpoint is called to retrieve a list of statements for the user. The endpoint runs the App\Http\Controllers\StatementsController@listStatements method to fetch the user's statements.
public function listStatements(Request $request): LengthAwarePaginator
{
$user = $request->user();
return StatementRequest::query()
->where('users_id', $user->id)
->orderBy('created_at', 'desc')
->paginate();
}
The listStatements method fetches the user's statements from the database and returns a paginated list of statements. Users can view details such as the statement ID, generation date, period, and status of each statement. The list is paginated to improve performance and provide a better user experience. The endpoint returns a JSON response with the paginated list of statements.
The Statement list has two actions available for each statement: View and Download. Users can view the details of a statement by clicking the View button, which opens a modal displaying the statement details. The Download button allows users to download the statement as a PDF file for offline access.
The Path hit is /api:E5rHZ0xs4/statements/{request_id}/download which runs the App\Http\Controllers\StatementsController@downloadStatement method to retrieve the statement.
public function downloadStatement(StatementRequest $statementRequest): StreamedResponse|JsonResponse|ResponseFactory|Application|Response
{
if ($statementRequest->status !== 'completed') {
return response()->json([
'message' => 'Statement is not ready yet',
'status' => $statementRequest->status,
'error_message' => $statementRequest->error_message
], 400);
}
if (!Storage::disk('do-private')->exists($statementRequest->result_path)) {
$statementRequest->update([
'status' => 'failed',
'error_message' => 'Statement file not found in storage'
]);
return response()->json([
'message' => 'Statement file not found. Please try generating the statement again.',
'error' => 'file_not_found'
], 404);
}
$fileSize = Storage::disk('do-private')->size($statementRequest->result_path);
if ($fileSize < 1000) { // Less than 1KB is suspicious for a PDF
Storage::disk('do-private')->delete($statementRequest->result_path);
$statementRequest->update([
'status' => 'failed',
'error_message' => 'Generated statement appears to be blank or corrupted'
]);
return response()->json([
'message' => 'The generated statement appears to be blank or corrupted. Please try generating the statement again.',
'error' => 'blank_statement'
], 500);
}
try {
$fileContents = Storage::disk('do-private')->get($statementRequest->result_path);
if (!str_starts_with($fileContents, '%PDF-')) {
Storage::disk('do-private')->delete($statementRequest->result_path);
$statementRequest->update([
'status' => 'failed',
'error_message' => 'Invalid PDF content'
]);
return response()->json([
'message' => 'The generated file is not a valid PDF. Please try generating the statement again.',
'error' => 'invalid_pdf'
], 500);
}
return response($fileContents, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="statement_' . $statementRequest->id . '.pdf"',
'Content-Length' => $fileSize,
]);
} catch (\Exception $e) {
\Log::emergency('Error downloading statement', [
'statement_id' => $statementRequest->id,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
return response()->json([
'message' => 'An error occurred while downloading the statement: ' . $e->getMessage(),
'error' => 'download_error'
], 500);
}
}
The downloadStatement method checks the status of the statement request and the availability of the statement file in the storage. If the statement is ready and the file exists, the method retrieves the file contents and returns a streamed response with the PDF file. If the file is not found or is empty, the method updates the statement status and returns an error response. If the file is not a valid PDF, the method logs an error and returns an error response. The method handles exceptions and logs any errors that occur during the download process.