Deduplication

This commit is contained in:
Lars Kusch 2023-01-31 08:59:02 +01:00
parent d5cf303000
commit 95727d6ec2
2 changed files with 84 additions and 134 deletions

View File

@ -112,6 +112,10 @@ class Webhook extends BaseModel
const EVENT_RESTORE_EXPENSE = 47; const EVENT_RESTORE_EXPENSE = 47;
const EVENT_ARCHIVE_VENDOR = 48;
const EVENT_RESTORE_VENDOR = 49;
@ -156,6 +160,7 @@ class Webhook extends BaseModel
self::EVENT_ARCHIVE_QUOTE, self::EVENT_ARCHIVE_QUOTE,
self::EVENT_ARCHIVE_INVOICE, self::EVENT_ARCHIVE_INVOICE,
self::EVENT_ARCHIVE_PAYMENT, self::EVENT_ARCHIVE_PAYMENT,
self::EVENT_ARCHIVE_VENDOR,
self::EVENT_RESTORE_EXPENSE, self::EVENT_RESTORE_EXPENSE,
self::EVENT_RESTORE_PROJECT, self::EVENT_RESTORE_PROJECT,
self::EVENT_RESTORE_CLIENT, self::EVENT_RESTORE_CLIENT,
@ -163,7 +168,8 @@ class Webhook extends BaseModel
self::EVENT_RESTORE_CREDIT, self::EVENT_RESTORE_CREDIT,
self::EVENT_RESTORE_QUOTE, self::EVENT_RESTORE_QUOTE,
self::EVENT_RESTORE_INVOICE, self::EVENT_RESTORE_INVOICE,
self::EVENT_RESTORE_PAYMENT self::EVENT_RESTORE_PAYMENT,
self::EVENT_RESTORE_VENDOR
]; ];

View File

@ -24,6 +24,7 @@ use App\Models\Project;
use App\Models\Quote; use App\Models\Quote;
use App\Models\RecurringInvoice; use App\Models\RecurringInvoice;
use App\Models\Task; use App\Models\Task;
use App\Models\Vendor;
use App\Models\Webhook; use App\Models\Webhook;
use App\Utils\Helpers; use App\Utils\Helpers;
use App\Utils\Ninja; use App\Utils\Ninja;
@ -66,76 +67,7 @@ class BaseRepository
if (class_exists($className)) { if (class_exists($className)) {
event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
if ($entity instanceof Invoice){ $this->handleWebhook($entity, false);
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_INVOICE)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_INVOICE, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}}
elseif ($entity instanceof Quote){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_QUOTE)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_QUOTE, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}}
elseif ($entity instanceof Credit){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_CREDIT)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CREDIT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}
}
elseif ($entity instanceof Client){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_CLIENT)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CLIENT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}
}
elseif ($entity instanceof Expense){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_EXPENSE)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_EXPENSE, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}
}
elseif ($entity instanceof Project){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_PROJECT)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PROJECT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}
}
elseif ($entity instanceof Task){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_TASK)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_TASK, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}
}
elseif ($entity instanceof Payment){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_PAYMENT)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PAYMENT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}
}
} }
} }
@ -162,75 +94,87 @@ class BaseRepository
if (class_exists($className)) { if (class_exists($className)) {
event(new $className($entity, $fromDeleted, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); event(new $className($entity, $fromDeleted, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
if ($entity instanceof Invoice){ $this->handleWebhook($entity, true);
$subscriptions = Webhook::where('company_id', $entity->company_id) }
->where('event_id', Webhook::EVENT_RESTORE_INVOICE) }
->exists(); private function handleWebhook($entity, $restore)
{
if ($subscriptions) { switch(true) {
WebhookHandler::dispatch(Webhook::EVENT_RESTORE_INVOICE, $entity, $entity->company, 'client')->delay(now()->addSeconds(2)); case $entity instanceof Invoice:
}} if ($restore){
elseif ($entity instanceof Quote){ $webhookEvent = Webhook::EVENT_RESTORE_INVOICE;
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_RESTORE_QUOTE)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_RESTORE_QUOTE, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
}}
elseif ($entity instanceof Credit){
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_RESTORE_CREDIT)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_RESTORE_CREDIT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
} }
} else {
elseif ($entity instanceof Client){ $webhookEvent = Webhook::EVENT_ARCHIVE_INVOICE;}
$subscriptions = Webhook::where('company_id', $entity->company_id) break;
->where('event_id', Webhook::EVENT_RESTORE_CLIENT) case $entity instanceof Quote:
->exists(); if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_QUOTE;
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_RESTORE_CLIENT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
} }
} else {
elseif ($entity instanceof Expense){ $webhookEvent = Webhook::EVENT_ARCHIVE_QUOTE;
$subscriptions = Webhook::where('company_id', $entity->company_id)
->where('event_id', Webhook::EVENT_RESTORE_EXPENSE)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_RESTORE_EXPENSE, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
} }
} break;
elseif ($entity instanceof Project){ case $entity instanceof Credit:
$subscriptions = Webhook::where('company_id', $entity->company_id) if ($restore){
->where('event_id', Webhook::EVENT_RESTORE_PROJECT) $webhookEvent = Webhook::EVENT_RESTORE_CREDIT;
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_RESTORE_PROJECT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
} }
} else {
elseif ($entity instanceof Task){ $webhookEvent = Webhook::EVENT_ARCHIVE_CREDIT;}
$subscriptions = Webhook::where('company_id', $entity->company_id) break;
->where('event_id', Webhook::EVENT_RESTORE_TASK) case $entity instanceof Payment:
->exists(); if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_PAYMENT;
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_RESTORE_TASK, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
} }
} else {
elseif ($entity instanceof Payment){ $webhookEvent = Webhook::EVENT_ARCHIVE_PAYMENT;}
$subscriptions = Webhook::where('company_id', $entity->company_id) break;
->where('event_id', Webhook::EVENT_RESTORE_PAYMENT) case $entity instanceof Task:
->exists(); if ($restore){
$webhookEvent = Webhook::EVENT_RESTORE_TASK;
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_RESTORE_PAYMENT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
} }
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) {
WebhookHandler::dispatch($webhookEvent, $entity, $entity->company, 'client')->delay(now()->addSeconds(2));
} }
} }
} }