Initialer Laravel Commit für BetiX
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

This commit is contained in:
2026-04-04 18:01:50 +02:00
commit 0280278978
374 changed files with 65210 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
<?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');
}
}
}