1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Http/Controllers/WebhookController.php

518 lines
19 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
2023-03-17 08:27:26 +01:00
use App\Models\Webhook;
use Illuminate\Support\Str;
use Illuminate\Http\Response;
2020-07-06 13:22:36 +02:00
use App\Factory\WebhookFactory;
use App\Filters\WebhookFilters;
2023-03-17 08:27:26 +01:00
use App\Utils\Traits\MakesHash;
use App\Jobs\Util\WebhookSingle;
use App\Repositories\BaseRepository;
use App\Transformers\WebhookTransformer;
2020-07-06 13:22:36 +02:00
use App\Http\Requests\Webhook\EditWebhookRequest;
use App\Http\Requests\Webhook\ShowWebhookRequest;
2023-03-17 08:27:26 +01:00
use App\Http\Requests\Webhook\RetryWebhookRequest;
2020-07-06 13:22:36 +02:00
use App\Http\Requests\Webhook\StoreWebhookRequest;
2023-03-17 08:27:26 +01:00
use App\Http\Requests\Webhook\CreateWebhookRequest;
2020-07-06 13:22:36 +02:00
use App\Http\Requests\Webhook\UpdateWebhookRequest;
2023-03-17 08:27:26 +01:00
use App\Http\Requests\Webhook\DestroyWebhookRequest;
2020-07-06 13:22:36 +02:00
class WebhookController extends BaseController
{
use MakesHash;
2020-07-06 13:22:36 +02:00
protected $entity_type = Webhook::class;
2020-07-06 13:22:36 +02:00
protected $entity_transformer = WebhookTransformer::class;
public $base_repo;
public function __construct(BaseRepository $base_repo)
{
parent::__construct();
$this->base_repo = $base_repo;
}
/**
2020-10-28 11:10:49 +01:00
* @OA\Get(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks",
* operationId="getWebhooks",
* tags={"webhooks"},
* summary="Gets a list of Webhooks",
* description="Lists Webhooks, search and filters allow fine grained lists to be generated.
*
2020-07-06 13:22:36 +02:00
* Query parameters can be added to performed more fine grained filtering of the Webhooks, these are handled by the WebhookFilters class which defines the methods available",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="A list of Webhooks",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2020-10-28 11:10:49 +01:00
* @param WebhookFilters $filters
* @return Response|mixed
*/
2020-07-06 13:22:36 +02:00
public function index(WebhookFilters $filters)
{
2020-07-06 13:22:36 +02:00
$webhooks = Webhook::filter($filters);
2020-07-06 13:22:36 +02:00
return $this->listResponse($webhooks);
}
/**
* Display the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param ShowWebhookRequest $request
* @param Webhook $webhook
* @return Response
*
*
* @OA\Get(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks/{id}",
* operationId="showWebhook",
* tags={"webhooks"},
* summary="Shows a Webhook",
* description="Displays a Webhook by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
2020-07-06 13:22:36 +02:00
* description="The Webhook Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="Returns the Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function show(ShowWebhookRequest $request, Webhook $webhook)
{
2020-07-06 13:22:36 +02:00
return $this->itemResponse($webhook);
}
/**
* Show the form for editing the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param EditWebhookRequest $request
* @param Webhook $webhook
* @return Response
*
*
* @OA\Get(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks/{id}/edit",
* operationId="editWebhook",
* tags={"webhooks"},
* summary="Shows a Webhook for editting",
* description="Displays a Webhook by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
2020-07-06 13:22:36 +02:00
* description="The Webhook Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="Returns the Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function edit(EditWebhookRequest $request, Webhook $webhook)
{
2020-07-06 13:22:36 +02:00
return $this->itemResponse($webhook);
}
/**
* Update the specified resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param UpdateWebhookRequest $request
* @param Webhook $webhook
* @return Response
*
*
*
* @OA\Put(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks/{id}",
* operationId="updateWebhook",
* tags={"webhooks"},
* summary="Updates a Webhook",
* description="Handles the updating of a Webhook by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
2020-07-06 13:22:36 +02:00
* description="The Webhook Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="Returns the Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function update(UpdateWebhookRequest $request, Webhook $webhook)
{
2020-07-06 13:22:36 +02:00
if ($request->entityIsDeleted($webhook)) {
return $request->disallowUpdate();
}
2020-07-06 13:22:36 +02:00
$webhook->fill($request->all());
$webhook->save();
2020-07-06 13:22:36 +02:00
return $this->itemResponse($webhook);
}
/**
* Show the form for creating a new resource.
*
2020-10-28 11:10:49 +01:00
* @param CreateWebhookRequest $request
* @return Response
*
*
*
* @OA\Get(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks/create",
* operationId="getWebhooksCreate",
* tags={"webhooks"},
* summary="Gets a new blank Webhook object",
* description="Returns a blank object with default values",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="A blank Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function create(CreateWebhookRequest $request)
{
2020-07-06 13:22:36 +02:00
$webhook = WebhookFactory::create(auth()->user()->company()->id, auth()->user()->id);
$webhook->fill($request->all());
$webhook->save();
2020-07-06 13:22:36 +02:00
return $this->itemResponse($webhook);
}
/**
* Store a newly created resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param StoreWebhookRequest $request
* @return Response
*
*
*
* @OA\Post(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks",
* operationId="storeWebhook",
* tags={"webhooks"},
* summary="Adds a Webhook",
* description="Adds an Webhook to a company",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="Returns the saved Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function store(StoreWebhookRequest $request)
{
2020-07-06 13:22:36 +02:00
$event_id = $request->input('event_id');
$target_url = $request->input('target_url');
if (! in_array($event_id, Webhook::$valid_events)) {
return response()->json('Invalid event', 400);
2020-07-06 13:22:36 +02:00
}
$webhook = new Webhook;
$webhook->company_id = auth()->user()->company()->id;
$webhook->user_id = auth()->user()->id;
$webhook->event_id = $event_id;
$webhook->target_url = $target_url;
2021-04-08 23:57:01 +02:00
$webhook->fill($request->all());
2020-07-06 13:22:36 +02:00
$webhook->save();
if (! $webhook->id) {
2021-01-25 01:57:49 +01:00
return response()->json(ctrans('texts.create_webhook_failure'), 400);
2020-07-06 13:22:36 +02:00
}
return $this->itemResponse($webhook);
}
/**
* Remove the specified resource from storage.
*
2020-10-28 11:10:49 +01:00
* @param DestroyWebhookRequest $request
* @param Webhook $webhook
* @return Response
*
*
2020-10-28 11:10:49 +01:00
* @throws \Exception
* @OA\Delete(
2020-11-06 05:43:10 +01:00
* path="/api/v1/webhooks/{id}",
2020-07-06 13:22:36 +02:00
* operationId="deleteWebhook",
* tags={"Webhooks"},
* summary="Deletes a Webhook",
* description="Handles the deletion of a Webhook by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
2020-07-06 13:22:36 +02:00
* description="The Webhook Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns a HTTP status",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function destroy(DestroyWebhookRequest $request, Webhook $webhook)
{
//may not need these destroy routes as we are using actions to 'archive/delete'
2020-07-06 13:22:36 +02:00
$webhook->delete();
2020-07-06 13:22:36 +02:00
return $this->itemResponse($webhook);
}
/**
* Perform bulk actions on the list view.
*
2020-10-28 11:10:49 +01:00
* @return Response
*
*
* @OA\Post(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks/bulk",
* operationId="bulkWebhooks",
* tags={"webhooks"},
* summary="Performs bulk actions on an array of Webhooks",
* description="",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\RequestBody(
* description="User credentials",
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="array",
* @OA\Items(
* type="integer",
* description="Array of hashed IDs to be bulk 'actioned",
* example="[0,1,2,3]",
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="The Webhook User response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function bulk()
{
$action = request()->input('action');
$ids = request()->input('ids');
2020-07-06 13:22:36 +02:00
$webhooks = Webhook::withTrashed()->find($this->transformKeys($ids));
2020-07-06 13:22:36 +02:00
$webhooks->each(function ($webhook, $key) use ($action) {
if (auth()->user()->can('edit', $webhook)) {
$this->base_repo->{$action}($webhook);
}
});
2020-07-06 13:22:36 +02:00
return $this->listResponse(Webhook::withTrashed()->whereIn('id', $this->transformKeys($ids)));
}
2023-03-17 08:27:26 +01:00
public function retry(RetryWebhookRequest $request, Webhook $webhook)
{
match($request->entity) {
'invoice' => $includes ='client',
'payment' => $includes ='invoices,client',
'project' => $includes ='client',
'purchase_order' => $includes ='vendor',
'quote' => $includes ='client',
default => $includes = ''
};
$class = 'App\Models\\'.ucfirst(Str::camel($request->entity));
$entity = $class::withTrashed()->where('id', $this->decodePrimaryKey($request->entity_id))->company()->first();
if(!$entity){
return response()->json(['message' => ctrans('texts.record_not_found')], 400);
}
WebhookSingle::dispatchSync($webhook->id, $entity, auth()->user()->company()->db, $includes);
return $this->itemResponse($webhook);
}
}