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)); + } + } } }