150 lines
5.2 KiB
PHP
150 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\IntegrationHub\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Modules\IntegrationHub\Models\IhWebhookDelivery;
|
|
use Modules\IntegrationHub\Models\IhWebhookEndpoint;
|
|
use Modules\IntegrationHub\Services\WebhookDispatcherService;
|
|
|
|
class WebhookController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
if (! auth()->user()->can('integrationhub.manage_webhooks')) {
|
|
abort(403);
|
|
}
|
|
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
$endpoints = IhWebhookEndpoint::forBusiness($business_id)->latest('id')->paginate(20);
|
|
$events = app(WebhookDispatcherService::class)->availableEvents();
|
|
|
|
return view('integrationhub::webhooks.index', compact('endpoints', 'events'));
|
|
}
|
|
|
|
public function create(Request $request)
|
|
{
|
|
if (! auth()->user()->can('integrationhub.manage_webhooks')) {
|
|
abort(403);
|
|
}
|
|
|
|
$events = app(WebhookDispatcherService::class)->availableEvents();
|
|
|
|
return view('integrationhub::webhooks.create', compact('events'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
if (! auth()->user()->can('integrationhub.manage_webhooks')) {
|
|
abort(403);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:191',
|
|
'url' => 'required|url|max:2048',
|
|
'secret' => 'nullable|string|max:191',
|
|
'events' => 'nullable|array',
|
|
'events.*' => 'string',
|
|
'is_active' => 'nullable|boolean',
|
|
]);
|
|
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
|
|
IhWebhookEndpoint::create([
|
|
'business_id' => $business_id,
|
|
'name' => $validated['name'],
|
|
'url' => $validated['url'],
|
|
'secret' => $validated['secret'] ?? null,
|
|
'events' => $validated['events'] ?? ['*'],
|
|
'is_active' => $request->boolean('is_active', true),
|
|
]);
|
|
|
|
return redirect()
|
|
->action([self::class, 'index'])
|
|
->with('status', ['success' => 1, 'msg' => __('integrationhub::lang.webhook_created')]);
|
|
}
|
|
|
|
public function edit(Request $request, $id)
|
|
{
|
|
if (! auth()->user()->can('integrationhub.manage_webhooks')) {
|
|
abort(403);
|
|
}
|
|
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
$endpoint = IhWebhookEndpoint::forBusiness($business_id)->findOrFail($id);
|
|
$events = app(WebhookDispatcherService::class)->availableEvents();
|
|
$deliveries = IhWebhookDelivery::where('endpoint_id', $endpoint->id)->latest('id')->limit(20)->get();
|
|
|
|
return view('integrationhub::webhooks.edit', compact('endpoint', 'events', 'deliveries'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
if (! auth()->user()->can('integrationhub.manage_webhooks')) {
|
|
abort(403);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:191',
|
|
'url' => 'required|url|max:2048',
|
|
'secret' => 'nullable|string|max:191',
|
|
'events' => 'nullable|array',
|
|
'events.*' => 'string',
|
|
'is_active' => 'nullable|boolean',
|
|
]);
|
|
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
$endpoint = IhWebhookEndpoint::forBusiness($business_id)->findOrFail($id);
|
|
|
|
$endpoint->update([
|
|
'name' => $validated['name'],
|
|
'url' => $validated['url'],
|
|
'secret' => $validated['secret'] ?? null,
|
|
'events' => $validated['events'] ?? ['*'],
|
|
'is_active' => $request->boolean('is_active', true),
|
|
]);
|
|
|
|
return redirect()
|
|
->action([self::class, 'index'])
|
|
->with('status', ['success' => 1, 'msg' => __('integrationhub::lang.webhook_updated')]);
|
|
}
|
|
|
|
public function destroy(Request $request, $id)
|
|
{
|
|
if (! auth()->user()->can('integrationhub.manage_webhooks')) {
|
|
abort(403);
|
|
}
|
|
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
$endpoint = IhWebhookEndpoint::forBusiness($business_id)->findOrFail($id);
|
|
$endpoint->delete();
|
|
|
|
return redirect()
|
|
->action([self::class, 'index'])
|
|
->with('status', ['success' => 1, 'msg' => __('integrationhub::lang.webhook_deleted')]);
|
|
}
|
|
|
|
public function test(Request $request, $id, WebhookDispatcherService $dispatcher)
|
|
{
|
|
if (! auth()->user()->can('integrationhub.manage_webhooks')) {
|
|
abort(403);
|
|
}
|
|
|
|
$business_id = (int) $request->session()->get('user.business_id');
|
|
$endpoint = IhWebhookEndpoint::forBusiness($business_id)->findOrFail($id);
|
|
|
|
$ok = $dispatcher->deliver($endpoint, 'integration.test', [
|
|
'message' => 'IntegrationHub test payload',
|
|
'triggered_by' => auth()->id(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => $ok,
|
|
'msg' => $ok
|
|
? __('integrationhub::lang.webhook_test_success')
|
|
: __('integrationhub::lang.webhook_test_failed'),
|
|
]);
|
|
}
|
|
}
|