From b8a0aa47bfc578c581747540b4cbff8229eb10f1 Mon Sep 17 00:00:00 2001 From: Benjamin Nussbaum Date: Mon, 31 Mar 2025 21:45:28 +0200 Subject: [PATCH] feat: update plugin data via webhook --- .../views/livewire/plugins/receipt.blade.php | 74 +++++++++++-------- routes/api.php | 21 ++++++ tests/Feature/PluginWebhookTest.php | 69 +++++++++++++++++ 3 files changed, 135 insertions(+), 29 deletions(-) create mode 100644 tests/Feature/PluginWebhookTest.php diff --git a/resources/views/livewire/plugins/receipt.blade.php b/resources/views/livewire/plugins/receipt.blade.php index 4b98611..f3acbda 100644 --- a/resources/views/livewire/plugins/receipt.blade.php +++ b/resources/views/livewire/plugins/receipt.blade.php @@ -313,41 +313,57 @@ HTML;
- + - +
-
- - - - - -
+ @if($data_strategy === 'polling') +
+ + + + + +
-
- - - - -
+
+ + + + +
-
- -
+
+ +
+ @else +
+ + +
+
+

Send JSON payload with key merge_variables to the webhook URL. The payload will be merged with the plugin data.

+
+ @endif
diff --git a/routes/api.php b/routes/api.php index f8942d0..94562c9 100644 --- a/routes/api.php +++ b/routes/api.php @@ -194,3 +194,24 @@ Route::post('/display/update', function (Request $request) { }) ->name('display.update') ->middleware('auth:sanctum', 'ability:update-screen'); + +Route::post('custom_plugins/{plugin_uuid}', function (string $plugin_uuid) { + $plugin = \App\Models\Plugin::where('uuid', $plugin_uuid)->firstOrFail(); + + // Check if plugin uses webhook strategy + if ($plugin->data_strategy !== 'webhook') { + return response()->json(['error' => 'Plugin does not use webhook strategy'], 400); + } + + $request = request(); + if (! $request->has('merge_variables')) { + return response()->json(['error' => 'Request must contain merge_variables key'], 400); + } + + $plugin->update([ + 'data_payload' => $request->input('merge_variables'), + 'data_payload_updated_at' => now(), + ]); + + return response()->json(['message' => 'Data updated successfully']); +})->name('api.custom_plugins.webhook'); diff --git a/tests/Feature/PluginWebhookTest.php b/tests/Feature/PluginWebhookTest.php new file mode 100644 index 0000000..70fa53a --- /dev/null +++ b/tests/Feature/PluginWebhookTest.php @@ -0,0 +1,69 @@ +create([ + 'data_strategy' => 'webhook', + 'data_payload' => ['old' => 'data'], + ]); + + // Make request to update plugin data + $response = $this->postJson("/api/custom_plugins/{$plugin->uuid}", [ + 'merge_variables' => ['new' => 'data'], + ]); + + // Assert response + $response->assertOk() + ->assertJson(['message' => 'Data updated successfully']); + + // Assert plugin was updated + $this->assertDatabaseHas('plugins', [ + 'id' => $plugin->id, + 'data_payload' => json_encode(['new' => 'data']), + ]); +}); + +test('webhook returns 400 for non-webhook strategy plugins', function () { + // Create a plugin with non-webhook strategy + $plugin = Plugin::factory()->create([ + 'data_strategy' => 'polling', + 'data_payload' => ['old' => 'data'], + ]); + + // Make request to update plugin data + $response = $this->postJson("/api/custom_plugins/{$plugin->uuid}", [ + 'merge_variables' => ['new' => 'data'], + ]); + + // Assert response + $response->assertStatus(400) + ->assertJson(['error' => 'Plugin does not use webhook strategy']); +}); + +test('webhook returns 400 when merge_variables is missing', function () { + // Create a plugin with webhook strategy + $plugin = Plugin::factory()->create([ + 'data_strategy' => 'webhook', + 'data_payload' => ['old' => 'data'], + ]); + + // Make request without merge_variables + $response = $this->postJson("/api/custom_plugins/{$plugin->uuid}", []); + + // Assert response + $response->assertStatus(400) + ->assertJson(['error' => 'Request must contain merge_variables key']); +}); + +test('webhook returns 404 for non-existent plugin', function () { + // Make request with non-existent plugin UUID + $response = $this->postJson('/api/custom_plugins/'.Str::uuid(), [ + 'merge_variables' => ['new' => 'data'], + ]); + + // Assert response + $response->assertNotFound(); +});