Files
BetiX/app/Http/Controllers/PromoController.php
Dolo 0280278978
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled
Initialer Laravel Commit für BetiX
2026-04-04 18:01:50 +02:00

52 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Concerns\ProxiesBackend;
use App\Services\BackendHttpClient;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PromoController extends Controller
{
use ProxiesBackend;
public function __construct(private readonly BackendHttpClient $client)
{
}
/**
* Apply a promo code for the authenticated user via external API.
* Request: { code: string }
*/
public function apply(Request $request)
{
$data = Validator::make($request->all(), [
'code' => ['required','string','max:64'],
])->validate();
$code = strtoupper(trim($data['code']));
try {
$res = $this->client->post($request, '/promos/apply', [ 'code' => $code ]);
if ($res->successful()) {
$body = $res->json() ?: [];
// Backward compatibility: ensure a message key exists
if (!isset($body['message'])) {
$body['message'] = 'Promo applied successfully.';
}
// PromoControllerTest expects { success: true, message: '...' }
return response()->json([
'success' => true,
'message' => $body['message']
], 200);
}
if ($res->clientError()) return $this->mapClientError($res);
if ($res->serverError()) return $this->mapServiceUnavailable($res);
return $this->mapBadGateway();
} catch (\Throwable $e) {
return $this->mapBadGateway('API server not reachable');
}
}
}