131 lines
4.7 KiB
PHP
131 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Concerns\ProxiesBackend;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\BackendHttpClient;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class PromoAdminController extends Controller
|
|
{
|
|
use ProxiesBackend;
|
|
|
|
public function __construct(private readonly BackendHttpClient $client)
|
|
{
|
|
}
|
|
|
|
private function assertAdmin(): void
|
|
{
|
|
if (!Auth::check() || strtolower((string) Auth::user()->role) !== 'admin') {
|
|
abort(403, 'Nur für Admins');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show simple admin page to manage promos (data from upstream)
|
|
*/
|
|
public function index(Request $request): Response
|
|
{
|
|
$this->assertAdmin();
|
|
|
|
$promos = [];
|
|
try {
|
|
$res = $this->client->get($request, '/admin/promos', ['per_page' => 20], retry: true);
|
|
if ($res->successful()) {
|
|
$j = $res->json() ?: [];
|
|
$promos = $j['data'] ?? $j['promos'] ?? $j;
|
|
}
|
|
} catch (\Throwable $e) {
|
|
// show empty list with error message on page via flash if desired
|
|
}
|
|
|
|
return Inertia::render('Admin/Promos', [
|
|
'promos' => $promos,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Create a new promo via upstream
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$this->assertAdmin();
|
|
|
|
$data = $this->validateData($request);
|
|
$data['code'] = strtoupper(trim($data['code']));
|
|
$data['is_active'] = $data['is_active'] ?? true;
|
|
|
|
try {
|
|
$res = $this->client->post($request, '/admin/promos', $data);
|
|
if ($res->successful()) {
|
|
return back()->with('success', 'Promo erstellt.');
|
|
}
|
|
if ($res->clientError()) {
|
|
$msg = data_get($res->json(), 'message', 'Ungültige Eingabe');
|
|
return back()->withErrors(['promo' => $msg]);
|
|
}
|
|
if ($res->serverError()) {
|
|
return back()->withErrors(['promo' => 'Service temporär nicht verfügbar']);
|
|
}
|
|
return back()->withErrors(['promo' => 'API Server nicht erreichbar']);
|
|
} catch (\Throwable $e) {
|
|
return back()->withErrors(['promo' => 'API Server nicht erreichbar']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update an existing promo via upstream
|
|
*/
|
|
public function update(Request $request, int $id)
|
|
{
|
|
$this->assertAdmin();
|
|
|
|
$data = $this->validateData($request, $id);
|
|
if (isset($data['code'])) {
|
|
$data['code'] = strtoupper(trim($data['code']));
|
|
}
|
|
|
|
try {
|
|
$res = $this->client->patch($request, "/admin/promos/{$id}", $data);
|
|
if ($res->successful()) {
|
|
return back()->with('success', 'Promo aktualisiert.');
|
|
}
|
|
if ($res->clientError()) {
|
|
$msg = data_get($res->json(), 'message', 'Ungültige Eingabe');
|
|
return back()->withErrors(['promo' => $msg]);
|
|
}
|
|
if ($res->serverError()) {
|
|
return back()->withErrors(['promo' => 'Service temporär nicht verfügbar']);
|
|
}
|
|
return back()->withErrors(['promo' => 'API Server nicht erreichbar']);
|
|
} catch (\Throwable $e) {
|
|
return back()->withErrors(['promo' => 'API Server nicht erreichbar']);
|
|
}
|
|
}
|
|
|
|
private function validateData(Request $request, ?int $ignoreId = null): array
|
|
{
|
|
$isUpdate = $request->isMethod('patch') || $request->isMethod('put');
|
|
|
|
$rules = [
|
|
'code' => [($isUpdate ? 'sometimes' : 'required'), 'string', 'max:64'],
|
|
'description' => [($isUpdate ? 'sometimes' : 'nullable'), 'nullable', 'string', 'max:255'],
|
|
'bonus_amount' => [($isUpdate ? 'sometimes' : 'required'), 'numeric', 'min:0'],
|
|
'wager_multiplier' => [($isUpdate ? 'sometimes' : 'required'), 'integer', 'min:0', 'max:1000'],
|
|
'per_user_limit' => [($isUpdate ? 'sometimes' : 'required'), 'integer', 'min:1', 'max:1000'],
|
|
'global_limit' => [($isUpdate ? 'sometimes' : 'nullable'), 'nullable', 'integer', 'min:1', 'max:1000000'],
|
|
'starts_at' => [($isUpdate ? 'sometimes' : 'nullable'), 'nullable', 'date'],
|
|
'ends_at' => [($isUpdate ? 'sometimes' : 'nullable'), 'nullable', 'date', 'after_or_equal:starts_at'],
|
|
'min_deposit' => [($isUpdate ? 'sometimes' : 'nullable'), 'nullable', 'numeric', 'min:0'],
|
|
'bonus_expires_days' => [($isUpdate ? 'sometimes' : 'nullable'), 'nullable', 'integer', 'min:1', 'max:365'],
|
|
'is_active' => [($isUpdate ? 'sometimes' : 'nullable'), 'boolean'],
|
|
];
|
|
|
|
return $request->validate($rules);
|
|
}
|
|
}
|