From 8fce15750b9f222fdb318368790f24a050daa005 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Fri, 27 Jan 2023 14:25:33 +0100 Subject: [PATCH 1/8] Update Webhook.php --- app/Models/Webhook.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/app/Models/Webhook.php b/app/Models/Webhook.php index 4c51d6af90ce..ef16150a3479 100644 --- a/app/Models/Webhook.php +++ b/app/Models/Webhook.php @@ -79,6 +79,25 @@ class Webhook extends BaseModel const EVENT_PROJECT_DELETE = 30; 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; + + + public static $valid_events = [ @@ -112,7 +131,16 @@ 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::VENT_ARCHIVE_INVOICE, + self::EVENT_ARCHIVE_PAYMENT + ]; protected $fillable = [ From 949722ff57e0ad25cb87edbea9439496fb793cd0 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Sun, 29 Jan 2023 14:43:57 +0100 Subject: [PATCH 2/8] Minor fixes and addition functionality --- app/Models/Webhook.php | 26 +++++++++++++------------- app/Observers/ClientObserver.php | 11 +++++++++++ app/Observers/CreditObserver.php | 16 ++++++++++++++++ app/Observers/ExpenseObserver.php | 16 ++++++++++++++++ app/Observers/InvoiceObserver.php | 14 ++++++++++++++ app/Observers/PaymentObserver.php | 10 ++++++++++ app/Observers/ProjectObserver.php | 17 +++++++++++++++++ app/Observers/QuoteObserver.php | 11 +++++++++++ app/Observers/TaskObserver.php | 10 ++++++++++ 9 files changed, 118 insertions(+), 13 deletions(-) diff --git a/app/Models/Webhook.php b/app/Models/Webhook.php index ef16150a3479..43de857cd669 100644 --- a/app/Models/Webhook.php +++ b/app/Models/Webhook.php @@ -79,25 +79,25 @@ class Webhook extends BaseModel const EVENT_PROJECT_DELETE = 30; 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; - - - + + + public static $valid_events = [ @@ -138,9 +138,9 @@ class Webhook extends BaseModel self::EVENT_ARCHIVE_TASK, self::EVENT_ARCHIVE_CREDIT, self::EVENT_ARCHIVE_QUOTE, - self::VENT_ARCHIVE_INVOICE, + self::EVENT_ARCHIVE_INVOICE, self::EVENT_ARCHIVE_PAYMENT - + ]; protected $fillable = [ diff --git a/app/Observers/ClientObserver.php b/app/Observers/ClientObserver.php index 273b2e6c002c..97f3bcf99c53 100644 --- a/app/Observers/ClientObserver.php +++ b/app/Observers/ClientObserver.php @@ -89,4 +89,15 @@ class ClientObserver { // } + + public function archived(Client $client) + { + $subscriptions = Webhook::where('company_id', $client->company->id) + ->where('event_id', Webhook::EVENT_ARCHIVE_CLIENT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CLIENT, $client, $client->company)->delay(now()->addSeconds(2)); + } + } } diff --git a/app/Observers/CreditObserver.php b/app/Observers/CreditObserver.php index a60e5f02eb50..4a2552725b33 100644 --- a/app/Observers/CreditObserver.php +++ b/app/Observers/CreditObserver.php @@ -91,4 +91,20 @@ class CreditObserver { // } + /** + * Handle the client "archive" event. + * + * @param Credit $credit + * @return void + */ + public function archived(Credit $credit) + { + $subscriptions = Webhook::where('company_id', $credit->company->id) + ->where('event_id', Webhook::EVENT_ARCHIVE_CREDIT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(2)); + } + } } diff --git a/app/Observers/ExpenseObserver.php b/app/Observers/ExpenseObserver.php index 2c71e5fc42da..2edfabd76827 100644 --- a/app/Observers/ExpenseObserver.php +++ b/app/Observers/ExpenseObserver.php @@ -89,4 +89,20 @@ class ExpenseObserver { // } + /** + * Handle the expense "archive" event. + * + * @param Expense $expense + * @return void + */ + public function archived(Expense $expense) + { + $subscriptions = Webhook::where('company_id', $expense->company->id) + ->where('event_id', Webhook::EVENT_ARCHIVE_EXPENSE) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(2)); + } + } } diff --git a/app/Observers/InvoiceObserver.php b/app/Observers/InvoiceObserver.php index 249aa23866f8..1e50b9d0e26b 100644 --- a/app/Observers/InvoiceObserver.php +++ b/app/Observers/InvoiceObserver.php @@ -93,4 +93,18 @@ class InvoiceObserver { // } + + /** + * @param Invoice $invoice + * @return void + */ + public function archived(Invoice $invoice){ + $subscriptions = Webhook::where('company_id', $invoice->company_id) + ->where('event_id', Webhook::EVENT_ARCHIVE_INVOICE) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(2)); + } + } } diff --git a/app/Observers/PaymentObserver.php b/app/Observers/PaymentObserver.php index 3de2d0955953..f29cfeb24050 100644 --- a/app/Observers/PaymentObserver.php +++ b/app/Observers/PaymentObserver.php @@ -89,4 +89,14 @@ class PaymentObserver { // } + public function archived(Payment $payment) + { + $subscriptions = Webhook::where('company_id', $payment->company->id) + ->where('event_id', Webhook::EVENT_ARCHIVE_PAYMENT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PAYMENT, $payment, $payment->company, 'invoices,client')->delay(now()->addSeconds(20)); + } + } } diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php index 74972a56ef73..b2acc2661537 100644 --- a/app/Observers/ProjectObserver.php +++ b/app/Observers/ProjectObserver.php @@ -92,4 +92,21 @@ class ProjectObserver { // } + /** + * Handle the product "archived" event. + * + * @param Project $project + * @return void + */ + public function archived(Project $project) + { + $subscriptions = Webhook::where('company_id', $project->company_id) + ->where('event_id', Webhook::EVENT_ARCHIVE_PROJECT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PROJECT, $project, $project->company, 'client')->delay(now()->addSeconds(2)); + + } + } } diff --git a/app/Observers/QuoteObserver.php b/app/Observers/QuoteObserver.php index 1c6adccf6225..823bf5e54063 100644 --- a/app/Observers/QuoteObserver.php +++ b/app/Observers/QuoteObserver.php @@ -93,4 +93,15 @@ class QuoteObserver { // } + public function archived(Quote $quote) + { + $subscriptions = Webhook::where('company_id', $quote->company->id) + ->where('event_id', Webhook::EVENT_ARCHIVE_QUOTE) + ->exists(); + + if ($subscriptions) { + $quote->load('client'); + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(2)); + } + } } diff --git a/app/Observers/TaskObserver.php b/app/Observers/TaskObserver.php index 5bfe4b43ef69..1527acf30eec 100644 --- a/app/Observers/TaskObserver.php +++ b/app/Observers/TaskObserver.php @@ -89,4 +89,14 @@ class TaskObserver { // } + public function archived(Task $task) + { + $subscriptions = Webhook::where('company_id', $task->company->id) + ->where('event_id', Webhook::EVENT_ARCHIVE_TASK) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_TASK, $task, $task->company)->delay(now()->addSeconds(2)); + } + } } From 0f05065279b7f3d55bdbbf219d88644b259f634b Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 30 Jan 2023 08:02:02 +0100 Subject: [PATCH 3/8] Move invoice archive to Base Repository --- app/Observers/InvoiceObserver.php | 14 ------- app/Repositories/BaseRepository.php | 59 +++++++++++++++++------------ 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/app/Observers/InvoiceObserver.php b/app/Observers/InvoiceObserver.php index 1e50b9d0e26b..249aa23866f8 100644 --- a/app/Observers/InvoiceObserver.php +++ b/app/Observers/InvoiceObserver.php @@ -93,18 +93,4 @@ class InvoiceObserver { // } - - /** - * @param Invoice $invoice - * @return void - */ - public function archived(Invoice $invoice){ - $subscriptions = Webhook::where('company_id', $invoice->company_id) - ->where('event_id', Webhook::EVENT_ARCHIVE_INVOICE) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_INVOICE, $invoice, $invoice->company, 'client')->delay(now()->addSeconds(2)); - } - } } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 46a5df9d8412..191b4ffd6f5c 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -12,6 +12,7 @@ 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; @@ -19,6 +20,7 @@ use App\Models\Credit; use App\Models\Invoice; use App\Models\Quote; use App\Models\RecurringInvoice; +use App\Models\Webhook; use App\Utils\Helpers; use App\Utils\Ninja; use App\Utils\Traits\MakesHash; @@ -60,6 +62,13 @@ class BaseRepository if (class_exists($className)) { event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + $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)); + } } } @@ -115,7 +124,7 @@ class BaseRepository { if (is_array($invitation) && ! array_key_exists('key', $invitation)) return false; - + $invitation_class = sprintf('App\\Models\\%sInvitation', $resource); $invitation = $invitation_class::with('company')->where('key', $invitation['key'])->first(); @@ -132,15 +141,15 @@ class BaseRepository case ($model instanceof Invoice): return 'invoice_id'; case ($model instanceof Quote): - return 'quote_id'; + return 'quote_id'; case ($model instanceof Credit): - return 'credit_id'; + return 'credit_id'; } } /** * Alternative save used for Invoices, Recurring Invoices, Quotes & Credits. - * + * * @param $data * @param $model * @return mixed @@ -149,10 +158,10 @@ class BaseRepository protected function alternativeSave($data, $model) { //$start = microtime(true); //forces the client_id if it doesn't exist - if(array_key_exists('client_id', $data)) + if(array_key_exists('client_id', $data)) $model->client_id = $data['client_id']; - $client = Client::with('group_settings')->where('id', $model->client_id)->withTrashed()->firstOrFail(); + $client = Client::with('group_settings')->where('id', $model->client_id)->withTrashed()->firstOrFail(); $state = []; @@ -171,12 +180,12 @@ class BaseRepository $tmp_data = $data; //preserves the $data array /* We need to unset some variable as we sometimes unguard the model */ - if (isset($tmp_data['invitations'])) + if (isset($tmp_data['invitations'])) unset($tmp_data['invitations']); - - if (isset($tmp_data['client_contacts'])) + + if (isset($tmp_data['client_contacts'])) unset($tmp_data['client_contacts']); - + $model->fill($tmp_data); $model->custom_surcharge_tax1 = $client->company->custom_surcharge_taxes1; @@ -188,7 +197,7 @@ class BaseRepository $this->new_model = true; if(is_array($model->line_items) && !($model instanceof RecurringInvoice)) - { + { $model->line_items = (collect($model->line_items))->map(function ($item) use($model,$client) { $item->notes = Helpers::processReservedKeywords($item->notes, $client); @@ -207,10 +216,10 @@ class BaseRepository $model->service()->setReminder()->save(); /* Save any documents */ - if (array_key_exists('documents', $data)) + if (array_key_exists('documents', $data)) $this->saveDocuments($data['documents'], $model); - if (array_key_exists('file', $data)) + if (array_key_exists('file', $data)) $this->saveDocuments($data['file'], $model); /* If invitations are present we need to filter existing invitations with the new ones */ @@ -222,9 +231,9 @@ class BaseRepository $invitation_class = sprintf('App\\Models\\%sInvitation', $resource); $invitation = $invitation_class::where('key', $invitation)->first(); - if ($invitation) + if ($invitation) $invitation->delete(); - + }); foreach ($data['invitations'] as $invitation) { @@ -232,7 +241,7 @@ class BaseRepository //if no invitations are present - create one. if (! $this->getInvitation($invitation, $resource)) { - if (isset($invitation['id'])) + if (isset($invitation['id'])) unset($invitation['id']); //make sure we are creating an invite for a contact who belongs to the client only! @@ -267,7 +276,7 @@ class BaseRepository } /* If no invitations have been created, this is our fail safe to maintain state*/ - if ($model->invitations()->count() == 0) + if ($model->invitations()->count() == 0) $model->service()->createInvitations(); /* Recalculate invoice amounts */ @@ -284,7 +293,7 @@ class BaseRepository $model->partial = min($model->amount, $model->balance); /* Update product details if necessary - if we are inside a transaction - do nothing */ - if ($model->company->update_products && $model->id && \DB::transactionLevel() == 0) + if ($model->company->update_products && $model->id && \DB::transactionLevel() == 0) UpdateOrCreateProduct::dispatch($model->line_items, $model, $model->company); /* Perform model specific tasks */ @@ -298,11 +307,11 @@ class BaseRepository } - if (! $model->design_id) + if (! $model->design_id) $model->design_id = $this->decodePrimaryKey($client->getSetting('invoice_design_id')); //links tasks and expenses back to the invoice, but only if we are not in the middle of a transaction. - if (\DB::transactionLevel() == 0) + if (\DB::transactionLevel() == 0) $model->service()->linkEntities()->save(); if($this->new_model) @@ -316,14 +325,14 @@ class BaseRepository $model = $model->calc()->getCredit(); - if (! $model->design_id) + if (! $model->design_id) $model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id')); if(array_key_exists('invoice_id', $data) && $data['invoice_id']) $model->invoice_id = $data['invoice_id']; if($this->new_model) - event('eloquent.created: App\Models\Credit', $model); + event('eloquent.created: App\Models\Credit', $model); else event('eloquent.updated: App\Models\Credit', $model); @@ -336,7 +345,7 @@ class BaseRepository if ($model instanceof Quote) { - if (! $model->design_id) + if (! $model->design_id) $model->design_id = $this->decodePrimaryKey($client->getSetting('quote_design_id')); $model = $model->calc()->getQuote(); @@ -350,9 +359,9 @@ class BaseRepository if ($model instanceof RecurringInvoice) { - if (! $model->design_id) + if (! $model->design_id) $model->design_id = $this->decodePrimaryKey($client->getSetting('invoice_design_id')); - + $model = $model->calc()->getRecurringInvoice(); From 6984aa35e36e16c231233461aeee780551379209 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 30 Jan 2023 08:21:47 +0100 Subject: [PATCH 4/8] Move observers into rigth place --- app/Observers/ClientObserver.php | 10 ---------- app/Observers/CreditObserver.php | 10 ---------- app/Observers/ExpenseObserver.php | 10 ---------- app/Observers/PaymentObserver.php | 10 ---------- app/Observers/ProjectObserver.php | 11 ----------- app/Observers/QuoteObserver.php | 11 ----------- app/Observers/TaskObserver.php | 10 ---------- 7 files changed, 72 deletions(-) diff --git a/app/Observers/ClientObserver.php b/app/Observers/ClientObserver.php index 97f3bcf99c53..9abc305878b8 100644 --- a/app/Observers/ClientObserver.php +++ b/app/Observers/ClientObserver.php @@ -90,14 +90,4 @@ class ClientObserver // } - public function archived(Client $client) - { - $subscriptions = Webhook::where('company_id', $client->company->id) - ->where('event_id', Webhook::EVENT_ARCHIVE_CLIENT) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CLIENT, $client, $client->company)->delay(now()->addSeconds(2)); - } - } } diff --git a/app/Observers/CreditObserver.php b/app/Observers/CreditObserver.php index 4a2552725b33..b34b2f7f3aa1 100644 --- a/app/Observers/CreditObserver.php +++ b/app/Observers/CreditObserver.php @@ -97,14 +97,4 @@ class CreditObserver * @param Credit $credit * @return void */ - public function archived(Credit $credit) - { - $subscriptions = Webhook::where('company_id', $credit->company->id) - ->where('event_id', Webhook::EVENT_ARCHIVE_CREDIT) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CREDIT, $credit, $credit->company)->delay(now()->addSeconds(2)); - } - } } diff --git a/app/Observers/ExpenseObserver.php b/app/Observers/ExpenseObserver.php index 2edfabd76827..686c938870cf 100644 --- a/app/Observers/ExpenseObserver.php +++ b/app/Observers/ExpenseObserver.php @@ -95,14 +95,4 @@ class ExpenseObserver * @param Expense $expense * @return void */ - public function archived(Expense $expense) - { - $subscriptions = Webhook::where('company_id', $expense->company->id) - ->where('event_id', Webhook::EVENT_ARCHIVE_EXPENSE) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_EXPENSE, $expense, $expense->company)->delay(now()->addSeconds(2)); - } - } } diff --git a/app/Observers/PaymentObserver.php b/app/Observers/PaymentObserver.php index f29cfeb24050..3de2d0955953 100644 --- a/app/Observers/PaymentObserver.php +++ b/app/Observers/PaymentObserver.php @@ -89,14 +89,4 @@ class PaymentObserver { // } - public function archived(Payment $payment) - { - $subscriptions = Webhook::where('company_id', $payment->company->id) - ->where('event_id', Webhook::EVENT_ARCHIVE_PAYMENT) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PAYMENT, $payment, $payment->company, 'invoices,client')->delay(now()->addSeconds(20)); - } - } } diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php index b2acc2661537..8602104635ca 100644 --- a/app/Observers/ProjectObserver.php +++ b/app/Observers/ProjectObserver.php @@ -98,15 +98,4 @@ class ProjectObserver * @param Project $project * @return void */ - public function archived(Project $project) - { - $subscriptions = Webhook::where('company_id', $project->company_id) - ->where('event_id', Webhook::EVENT_ARCHIVE_PROJECT) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PROJECT, $project, $project->company, 'client')->delay(now()->addSeconds(2)); - - } - } } diff --git a/app/Observers/QuoteObserver.php b/app/Observers/QuoteObserver.php index 823bf5e54063..1c6adccf6225 100644 --- a/app/Observers/QuoteObserver.php +++ b/app/Observers/QuoteObserver.php @@ -93,15 +93,4 @@ class QuoteObserver { // } - public function archived(Quote $quote) - { - $subscriptions = Webhook::where('company_id', $quote->company->id) - ->where('event_id', Webhook::EVENT_ARCHIVE_QUOTE) - ->exists(); - - if ($subscriptions) { - $quote->load('client'); - WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_QUOTE, $quote, $quote->company, 'client')->delay(now()->addSeconds(2)); - } - } } diff --git a/app/Observers/TaskObserver.php b/app/Observers/TaskObserver.php index 1527acf30eec..5bfe4b43ef69 100644 --- a/app/Observers/TaskObserver.php +++ b/app/Observers/TaskObserver.php @@ -89,14 +89,4 @@ class TaskObserver { // } - public function archived(Task $task) - { - $subscriptions = Webhook::where('company_id', $task->company->id) - ->where('event_id', Webhook::EVENT_ARCHIVE_TASK) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_TASK, $task, $task->company)->delay(now()->addSeconds(2)); - } - } } From 0e4dd61684b743b5ab4014701392a37b158a73ad Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 30 Jan 2023 08:22:19 +0100 Subject: [PATCH 5/8] Add observers into BaseRepository --- app/Repositories/BaseRepository.php | 77 +++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 191b4ffd6f5c..81c4dac1a3d8 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -17,9 +17,13 @@ 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\Webhook; use App\Utils\Helpers; use App\Utils\Ninja; @@ -50,7 +54,7 @@ class BaseRepository /** * @param $entity */ - public function archive($entity) + public function archive($entity): void { if ($entity->trashed()) { return; @@ -62,12 +66,75 @@ class BaseRepository if (class_exists($className)) { event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - $subscriptions = Webhook::where('company_id', $entity->company_id) - ->where('event_id', Webhook::EVENT_ARCHIVE_INVOICE) - ->exists(); + if ($entity instanceof Invoice){ + $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_INVOICE, $entity, $entity->company, 'client')->delay(now()->addSeconds(2)); + 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)); + } } } } From d5cf303000d93ed5428a7747df1ea992b1d4358e Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 30 Jan 2023 08:26:32 +0100 Subject: [PATCH 6/8] Add webhooks for restore --- app/Models/Webhook.php | 26 ++++++++++- app/Repositories/BaseRepository.php | 70 +++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/app/Models/Webhook.php b/app/Models/Webhook.php index 43de857cd669..c0ea84a4cb72 100644 --- a/app/Models/Webhook.php +++ b/app/Models/Webhook.php @@ -96,6 +96,22 @@ class Webhook extends BaseModel 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; + @@ -139,7 +155,15 @@ class Webhook extends BaseModel self::EVENT_ARCHIVE_CREDIT, self::EVENT_ARCHIVE_QUOTE, self::EVENT_ARCHIVE_INVOICE, - self::EVENT_ARCHIVE_PAYMENT + self::EVENT_ARCHIVE_PAYMENT, + 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 ]; diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 81c4dac1a3d8..3199ea89260a 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -162,6 +162,76 @@ class BaseRepository if (class_exists($className)) { event(new $className($entity, $fromDeleted, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + if ($entity instanceof Invoice){ + $subscriptions = Webhook::where('company_id', $entity->company_id) + ->where('event_id', Webhook::EVENT_RESTORE_INVOICE) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_RESTORE_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_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)); + } + } + elseif ($entity instanceof Client){ + $subscriptions = Webhook::where('company_id', $entity->company_id) + ->where('event_id', Webhook::EVENT_RESTORE_CLIENT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_RESTORE_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_RESTORE_EXPENSE) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_RESTORE_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_RESTORE_PROJECT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_RESTORE_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_RESTORE_TASK) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_RESTORE_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_RESTORE_PAYMENT) + ->exists(); + + if ($subscriptions) { + WebhookHandler::dispatch(Webhook::EVENT_RESTORE_PAYMENT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2)); + } + } } } From 95727d6ec2ca86e80796be70642744084f5960a1 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Tue, 31 Jan 2023 08:59:02 +0100 Subject: [PATCH 7/8] Deduplication --- app/Models/Webhook.php | 8 +- app/Repositories/BaseRepository.php | 210 ++++++++++------------------ 2 files changed, 84 insertions(+), 134 deletions(-) diff --git a/app/Models/Webhook.php b/app/Models/Webhook.php index c0ea84a4cb72..8639ad192246 100644 --- a/app/Models/Webhook.php +++ b/app/Models/Webhook.php @@ -112,6 +112,10 @@ class Webhook extends BaseModel 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_INVOICE, self::EVENT_ARCHIVE_PAYMENT, + self::EVENT_ARCHIVE_VENDOR, self::EVENT_RESTORE_EXPENSE, self::EVENT_RESTORE_PROJECT, self::EVENT_RESTORE_CLIENT, @@ -163,7 +168,8 @@ class Webhook extends BaseModel self::EVENT_RESTORE_CREDIT, self::EVENT_RESTORE_QUOTE, self::EVENT_RESTORE_INVOICE, - self::EVENT_RESTORE_PAYMENT + self::EVENT_RESTORE_PAYMENT, + self::EVENT_RESTORE_VENDOR ]; diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 3199ea89260a..120cd0a3b673 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -24,6 +24,7 @@ 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; @@ -66,76 +67,7 @@ class BaseRepository if (class_exists($className)) { event(new $className($entity, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - if ($entity instanceof Invoice){ - $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)); - } - } + $this->handleWebhook($entity, false); } } @@ -162,75 +94,87 @@ class BaseRepository if (class_exists($className)) { event(new $className($entity, $fromDeleted, $entity->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - if ($entity instanceof Invoice){ - $subscriptions = Webhook::where('company_id', $entity->company_id) - ->where('event_id', Webhook::EVENT_RESTORE_INVOICE) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_RESTORE_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_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)); + $this->handleWebhook($entity, true); + } + } + private function handleWebhook($entity, $restore) + { + switch(true) { + case $entity instanceof Invoice: + if ($restore){ + $webhookEvent = Webhook::EVENT_RESTORE_INVOICE; } - } - elseif ($entity instanceof Client){ - $subscriptions = Webhook::where('company_id', $entity->company_id) - ->where('event_id', Webhook::EVENT_RESTORE_CLIENT) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_RESTORE_CLIENT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2)); + else { + $webhookEvent = Webhook::EVENT_ARCHIVE_INVOICE;} + break; + case $entity instanceof Quote: + if ($restore){ + $webhookEvent = Webhook::EVENT_RESTORE_QUOTE; } - } - elseif ($entity instanceof Expense){ - $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)); + else { + $webhookEvent = Webhook::EVENT_ARCHIVE_QUOTE; } - } - elseif ($entity instanceof Project){ - $subscriptions = Webhook::where('company_id', $entity->company_id) - ->where('event_id', Webhook::EVENT_RESTORE_PROJECT) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_RESTORE_PROJECT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2)); + break; + case $entity instanceof Credit: + if ($restore){ + $webhookEvent = Webhook::EVENT_RESTORE_CREDIT; } - } - elseif ($entity instanceof Task){ - $subscriptions = Webhook::where('company_id', $entity->company_id) - ->where('event_id', Webhook::EVENT_RESTORE_TASK) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_RESTORE_TASK, $entity, $entity->company, 'client')->delay(now()->addSeconds(2)); + else { + $webhookEvent = Webhook::EVENT_ARCHIVE_CREDIT;} + break; + case $entity instanceof Payment: + if ($restore){ + $webhookEvent = Webhook::EVENT_RESTORE_PAYMENT; } - } - elseif ($entity instanceof Payment){ - $subscriptions = Webhook::where('company_id', $entity->company_id) - ->where('event_id', Webhook::EVENT_RESTORE_PAYMENT) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_RESTORE_PAYMENT, $entity, $entity->company, 'client')->delay(now()->addSeconds(2)); + 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) { + WebhookHandler::dispatch($webhookEvent, $entity, $entity->company, 'client')->delay(now()->addSeconds(2)); } } } From bb361f78dd67cdcba49fc52ca87a39d0010acf1e Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Tue, 31 Jan 2023 09:05:58 +0100 Subject: [PATCH 8/8] Handle different cases --- app/Repositories/BaseRepository.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 120cd0a3b673..e6915595f701 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -174,7 +174,23 @@ class BaseRepository ->exists(); if ($subscriptions) { - WebhookHandler::dispatch($webhookEvent, $entity, $entity->company, 'client')->delay(now()->addSeconds(2)); + 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)); + } + } } }