Store company_guid after OAuth via profile API; drop company select and companies endpoint. Coupons AJAX uses stored company only. Form request no longer accepts company fields from the browser. Made-with: Cursor
85 lines
2.4 KiB
PHP
85 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\PreregistrationPage;
|
|
use App\Services\WeeztixService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use RuntimeException;
|
|
|
|
class WeeztixApiController extends Controller
|
|
{
|
|
public function coupons(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'page_id' => ['required', 'integer', 'exists:preregistration_pages,id'],
|
|
]);
|
|
|
|
$page = PreregistrationPage::query()->findOrFail($request->integer('page_id'));
|
|
$this->authorize('update', $page);
|
|
|
|
$config = $page->weeztixConfig;
|
|
if ($config === null || ! $config->is_connected) {
|
|
return response()->json([
|
|
'message' => __('Niet verbonden met Weeztix.'),
|
|
], 422);
|
|
}
|
|
|
|
$companyGuid = $config->company_guid;
|
|
if (! is_string($companyGuid) || $companyGuid === '') {
|
|
return response()->json([
|
|
'message' => __('Geen Weeztix-bedrijf gekoppeld. Verbind opnieuw met Weeztix.'),
|
|
], 422);
|
|
}
|
|
|
|
try {
|
|
$raw = (new WeeztixService($config))->getCoupons();
|
|
$coupons = $this->normalizeCouponsPayload($raw);
|
|
|
|
return response()->json(['coupons' => $coupons]);
|
|
} catch (RuntimeException) {
|
|
return response()->json([
|
|
'message' => __('Kon kortingsbonnen niet laden.'),
|
|
], 422);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $raw
|
|
* @return list<array{guid: string, name: string}>
|
|
*/
|
|
private function normalizeCouponsPayload(array $raw): array
|
|
{
|
|
$list = $raw;
|
|
if (isset($raw['data']) && is_array($raw['data'])) {
|
|
$list = $raw['data'];
|
|
}
|
|
|
|
if (! is_array($list)) {
|
|
return [];
|
|
}
|
|
|
|
$out = [];
|
|
foreach ($list as $row) {
|
|
if (! is_array($row)) {
|
|
continue;
|
|
}
|
|
$guid = data_get($row, 'guid') ?? data_get($row, 'id');
|
|
if (! is_string($guid) || $guid === '') {
|
|
continue;
|
|
}
|
|
$name = data_get($row, 'name') ?? data_get($row, 'title') ?? $guid;
|
|
$out[] = [
|
|
'guid' => $guid,
|
|
'name' => is_string($name) ? $name : $guid,
|
|
];
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
}
|