Files
voltcerto/app/Http/Middleware/ApiResponse.php
Luis Medon e488ae3342 Add feature tests, unit tests, and configuration for Laravel application
- Created ExampleTest and RoutesTest for basic application response and route testing.
- Implemented IsolatedDatabaseTestCase for isolated database testing.
- Added unit tests for various models and services including Contract, CPE, and LeadDetail.
- Developed request validation tests for CreateClientRequest, UpdateClientRequest, and ContractRequest.
- Introduced Vite configuration for asset management in the Laravel application.
2026-10-29 11:30:06 +00:00

46 lines
1.7 KiB
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ApiResponse
{
private const FINANCIAL_FIELDS = [
'contract_value', 'commission_value', 'profit_value', 'chargeback_value', 'chargeback_date',
'paid', 'chargeback', 'contract_snapshot', 'payments', 'installments',
'totalPvp', 'totalCommission', 'totalChargeback', 'totalMargin', 'totalReceived', 'totalPending',
'commission', 'profit', 'pvp', 'net', 'commission_rate', 'amount', 'profit_margin', 'total_installments', 'paid_installments',
'total_vendas', 'total_lucro', 'total_comissao', 'vendasMensais', 'vendasPorTipo',
];
public function handle(Request $request, Closure $next)
{
$request->headers->set('Accept', 'application/json');
$response = $next($request);
if ($response instanceof JsonResponse && $request->is('api/contracts*', 'api/clients*', 'api/dashboard', 'api/analytics', 'api/leads*')
&& $request->user() && !$request->user()->can('view_contract_summary')) {
$response->setData($this->withoutFinancials($response->getData(true)));
}
$response->headers->set('Cache-Control', 'no-store, private');
return $response;
}
private function withoutFinancials($data)
{
if (!is_array($data)) {
return $data;
}
foreach ($data as $key => $value) {
if (in_array($key, self::FINANCIAL_FIELDS, true)) {
unset($data[$key]);
} else {
$data[$key] = $this->withoutFinancials($value);
}
}
return $data;
}
}