feat(weeztix): auto company from OAuth, remove company UI

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
This commit is contained in:
2026-04-05 10:56:29 +02:00
parent 977e09d8ac
commit 6561bda30d
7 changed files with 60 additions and 156 deletions

View File

@@ -13,38 +13,10 @@ use RuntimeException;
class WeeztixApiController extends Controller
{
public function companies(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);
}
try {
$companies = (new WeeztixService($config))->getCompanies();
return response()->json(['companies' => $companies]);
} catch (RuntimeException) {
return response()->json([
'message' => __('Kon bedrijven niet laden. Vernieuw de verbinding indien nodig.'),
], 422);
}
}
public function coupons(Request $request): JsonResponse
{
$request->validate([
'page_id' => ['required', 'integer', 'exists:preregistration_pages,id'],
'company_guid' => ['required', 'string', 'max:255'],
]);
$page = PreregistrationPage::query()->findOrFail($request->integer('page_id'));
@@ -57,9 +29,12 @@ class WeeztixApiController extends Controller
], 422);
}
$companyGuid = $request->string('company_guid')->toString();
$previousGuid = $config->company_guid;
$config->setAttribute('company_guid', $companyGuid);
$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();
@@ -70,8 +45,6 @@ class WeeztixApiController extends Controller
return response()->json([
'message' => __('Kon kortingsbonnen niet laden.'),
], 422);
} finally {
$config->setAttribute('company_guid', $previousGuid);
}
}

View File

@@ -100,6 +100,10 @@ class WeeztixOAuthController extends Controller
try {
$service = new WeeztixService($config);
$service->exchangeAuthorizationCode($request->string('code')->toString());
$config = $config->fresh();
if ($config !== null) {
(new WeeztixService($config))->ensureCompanyStoredFromWeeztix();
}
} catch (RuntimeException $e) {
Log::error('Weeztix OAuth callback failed', [
'page_id' => $page->id,

View File

@@ -44,8 +44,6 @@ class UpdateWeeztixConfigRequest extends FormRequest
'string',
'max:2048',
],
'company_guid' => ['nullable', 'string', 'max:255'],
'company_name' => ['nullable', 'string', 'max:255'],
'coupon_guid' => ['nullable', 'string', 'max:255'],
'coupon_name' => ['nullable', 'string', 'max:255'],
'code_prefix' => ['nullable', 'string', 'max:32'],

View File

@@ -13,6 +13,7 @@ use Illuminate\Support\Facades\Log;
use InvalidArgumentException;
use LogicException;
use RuntimeException;
use Throwable;
final class WeeztixService
{
@@ -84,6 +85,42 @@ final class WeeztixService
return $this->normalizeCompaniesFromProfile($json);
}
/**
* Persist company_guid when still empty after OAuth (single company per page; chosen in Weeztix login).
* Uses the first company returned from the user profile when several are present.
*/
public function ensureCompanyStoredFromWeeztix(): void
{
$this->config->refresh();
if (is_string($this->config->company_guid) && $this->config->company_guid !== '') {
return;
}
try {
$companies = $this->getCompanies();
if ($companies === []) {
Log::warning('Weeztix: geen bedrijf uit profiel voor automatische koppeling.', [
'weeztix_config_id' => $this->config->id,
]);
return;
}
$row = $companies[0];
$this->config->update([
'company_guid' => $row['guid'],
'company_name' => $row['name'],
]);
$this->config->refresh();
} catch (Throwable $e) {
Log::warning('Weeztix: automatisch bedrijf vastleggen mislukt', [
'weeztix_config_id' => $this->config->id,
'message' => $e->getMessage(),
]);
}
}
/**
* Exchange OAuth authorization code for tokens (admin callback).
*/