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

Merge pull request #8216 from LarsK1/patch-3

Add Webhook for archiving / restoring
This commit is contained in:
David Bomba 2023-01-31 19:24:26 +11:00 committed by GitHub
commit 9350ac3376
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 210 additions and 27 deletions

View File

@ -80,6 +80,45 @@ class Webhook extends BaseModel
const EVENT_UPDATE_PAYMENT = 31;
const EVENT_ARCHIVE_PAYMENT = 32;
const EVENT_ARCHIVE_INVOICE = 33;
const EVENT_ARCHIVE_QUOTE = 34;
const EVENT_ARCHIVE_CREDIT = 35;
const EVENT_ARCHIVE_TASK = 36;
const EVENT_ARCHIVE_CLIENT = 37;
const EVENT_ARCHIVE_PROJECT = 38;
const EVENT_ARCHIVE_EXPENSE = 39;
const EVENT_RESTORE_PAYMENT = 40;
const EVENT_RESTORE_INVOICE = 41;
const EVENT_RESTORE_QUOTE = 42;
const EVENT_RESTORE_CREDIT = 43;
const EVENT_RESTORE_TASK = 44;
const EVENT_RESTORE_CLIENT = 45;
const EVENT_RESTORE_PROJECT = 46;
const EVENT_RESTORE_EXPENSE = 47;
const EVENT_ARCHIVE_VENDOR = 48;
const EVENT_RESTORE_VENDOR = 49;
public static $valid_events = [
self::EVENT_CREATE_CLIENT,
@ -112,7 +151,26 @@ class Webhook extends BaseModel
self::EVENT_UPDATE_CREDIT,
self::EVENT_DELETE_CREDIT,
self::EVENT_PROJECT_DELETE,
self::EVENT_UPDATE_PAYMENT
self::EVENT_UPDATE_PAYMENT,
self::EVENT_ARCHIVE_EXPENSE,
self::EVENT_ARCHIVE_PROJECT,
self::EVENT_ARCHIVE_CLIENT,
self::EVENT_ARCHIVE_TASK,
self::EVENT_ARCHIVE_CREDIT,
self::EVENT_ARCHIVE_QUOTE,
self::EVENT_ARCHIVE_INVOICE,
self::EVENT_ARCHIVE_PAYMENT,
self::EVENT_ARCHIVE_VENDOR,
self::EVENT_RESTORE_EXPENSE,
self::EVENT_RESTORE_PROJECT,
self::EVENT_RESTORE_CLIENT,
self::EVENT_RESTORE_TASK,
self::EVENT_RESTORE_CREDIT,
self::EVENT_RESTORE_QUOTE,
self::EVENT_RESTORE_INVOICE,
self::EVENT_RESTORE_PAYMENT,
self::EVENT_RESTORE_VENDOR
];
protected $fillable = [

View File

@ -89,4 +89,5 @@ class ClientObserver
{
//
}
}

View File

@ -91,4 +91,10 @@ class CreditObserver
{
//
}
/**
* Handle the client "archive" event.
*
* @param Credit $credit
* @return void
*/
}

View File

@ -89,4 +89,10 @@ class ExpenseObserver
{
//
}
/**
* Handle the expense "archive" event.
*
* @param Expense $expense
* @return void
*/
}

View File

@ -92,4 +92,10 @@ class ProjectObserver
{
//
}
/**
* Handle the product "archived" event.
*
* @param Project $project
* @return void
*/
}

View File

@ -12,13 +12,20 @@
namespace App\Repositories;
use App\Jobs\Product\UpdateOrCreateProduct;
use App\Jobs\Util\WebhookHandler;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\Credit;
use App\Models\Expense;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Project;
use App\Models\Quote;
use App\Models\RecurringInvoice;
use App\Models\Task;
use App\Models\Vendor;
use App\Models\Webhook;
use App\Utils\Helpers;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
@ -48,7 +55,7 @@ class BaseRepository
/**
* @param $entity
*/
public function archive($entity)
public function archive($entity): void
{
if ($entity->trashed()) {
return;
@ -60,6 +67,7 @@ class BaseRepository
if (class_exists($className)) {
event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
$this->handleWebhook($entity, false);
}
}
@ -86,6 +94,104 @@ class BaseRepository
if (class_exists($className)) {
event(new $className($entity, $fromDeleted, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
$this->handleWebhook($entity, true);
}
}
private function handleWebhook($entity, $restore)
{
switch(true) {
case $entity instanceof Invoice:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_INVOICE;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_INVOICE;}
break;
case $entity instanceof Quote:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_QUOTE;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_QUOTE;
}
break;
case $entity instanceof Credit:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_CREDIT;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_CREDIT;}
break;
case $entity instanceof Payment:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_PAYMENT;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_PAYMENT;}
break;
case $entity instanceof Task:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_TASK;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_TASK;}
break;
case $entity instanceof Project:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_PROJECT;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_PROJECT;}
break;
case $entity instanceof Client:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_CLIENT;
}
else{
$webhookEvent = Webhook::EVENT_ARCHIVE_CLIENT;
}
break;
case $entity instanceof Expense:
if ($restore){
$webhookEvent= Webhook::EVENT_RESTORE_EXPENSE;
}
else{
$webhookEvent = Webhook::EVENT_ARCHIVE_EXPENSE;
}
break;
case $entity instanceof Vendor:
if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_VENDOR;
}
else {
$webhookEvent = Webhook::EVENT_ARCHIVE_VENDOR;
}
break;
}
if (isset($webhookEvent)){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', $webhookEvent)
->exists();
if ($subscriptions) {
switch(true){
case $webhookEvent == Webhook::EVENT_RESTORE_PAYMENT:
case $webhookEvent == Webhook::EVENT_ARCHIVE_PAYMENT:
WebhookHandler::dispatch($webhookEvent, $entity, $entity->company, 'invoices,client')->delay(now()->addSeconds(2));
break;
case $webhookEvent == Webhook::EVENT_RESTORE_EXPENSE:
case $webhookEvent == Webhook::EVENT_ARCHIVE_EXPENSE:
case $webhookEvent == Webhook::EVENT_ARCHIVE_CREDIT:
case $webhookEvent == Webhook::EVENT_RESTORE_CREDIT:
case $webhookEvent == Webhook::EVENT_RESTORE_CLIENT:
case $webhookEvent == Webhook::EVENT_ARCHIVE_CLIENT:
WebhookHandler::dispatch($webhookEvent, $entity, $entity->company)->delay(now()->addSeconds(2));
break;
default:
WebhookHandler::dispatch($webhookEvent, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}
}
}
}