1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00

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
*/
// @todo: Double check if this should be in admin module
namespace App\Http\Controllers\Gateways;
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
{
public function __invoke(Request $request)
class GoCardlessOAuthWebhookController extends Controller
{
public function __construct(
protected CompanyRepository $company_repository,
) {
}
public function __invoke(WebhookRequest $request)
{
foreach ($request->events as $event) {
match ($event['details']['cause']) {
'app_disconnected' => info($event),
default => nlog('Not acting on this event type: ' . $event['details']['cause']),
};
nlog($event['action']);
$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);
}
}
}
}