Refactor GoCardlessOAuthWebhookController for WebhookRequest

This commit is contained in:
Benjamin Beganović 2024-06-12 19:33:33 +02:00
parent 1f179aedaa
commit 19ef27b5e5

View File

@ -10,22 +10,44 @@
* @license https://www.elastic.co/licensing/elastic-license * @license https://www.elastic.co/licensing/elastic-license
*/ */
// @todo: Double check if this should be in admin module
namespace App\Http\Controllers\Gateways; namespace App\Http\Controllers\Gateways;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\Request; use App\Http\Requests\GoCardless\WebhookRequest;
use App\Models\CompanyGateway;
use App\Repositories\CompanyRepository;
use Illuminate\Support\Arr;
class GoCardlessOAuthController extends Controller class GoCardlessOAuthWebhookController extends Controller
{ {
public function __invoke(Request $request) public function __construct(
protected CompanyRepository $company_repository,
) {
}
public function __invoke(WebhookRequest $request)
{ {
foreach ($request->events as $event) { foreach ($request->events as $event) {
match ($event['details']['cause']) { nlog($event['action']);
'app_disconnected' => info($event),
default => nlog('Not acting on this event type: ' . $event['details']['cause']), $e = Arr::dot($event);
};
if ($event['action'] === 'disconnected') {
/** @var \App\Models\CompanyGateway $company_gateway */
$company_gateway = CompanyGateway::query()
->whereJsonCotains('config->account_id', $e['links.organisation'])
->firstOrFail();
$current = $company_gateway->getConfig('__current');
if ($current) {
$company_gateway->setConfig($current);
$company_gateway->save();
}
$this->company_repository->archive($company_gateway);
}
} }
} }
} }