1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Observers/ProjectObserver.php

116 lines
2.8 KiB
PHP
Raw Normal View History

2022-04-14 13:38:29 +02:00
<?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)
2022-04-14 13:38:29 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Observers;
use App\Jobs\Util\WebhookHandler;
use App\Models\Project;
use App\Models\Webhook;
class ProjectObserver
{
public $afterCommit = true;
2022-04-14 13:38:29 +02:00
/**
* Handle the product "created" event.
*
* @param Project $project
* @return void
*/
public function created(Project $project)
{
$subscriptions = Webhook::where('company_id', $project->company_id)
->where('event_id', Webhook::EVENT_PROJECT_CREATE)
->exists();
2023-02-16 02:36:09 +01:00
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch(Webhook::EVENT_PROJECT_CREATE, $project, $project->company, 'client')->delay(0);
2023-02-16 02:36:09 +01:00
}
2022-04-14 13:38:29 +02:00
}
/**
* Handle the product "updated" event.
*
* @param Project $project
* @return void
*/
public function updated(Project $project)
{
2023-01-31 13:53:54 +01:00
$event = Webhook::EVENT_PROJECT_UPDATE;
2023-02-16 02:36:09 +01:00
if ($project->getOriginal('deleted_at') && !$project->deleted_at) {
2023-01-31 13:53:54 +01:00
$event = Webhook::EVENT_RESTORE_PROJECT;
2023-02-16 02:36:09 +01:00
}
2023-01-31 13:53:54 +01:00
2023-02-16 02:36:09 +01:00
if ($project->is_deleted) {
$event = Webhook::EVENT_PROJECT_DELETE;
}
2023-01-31 13:53:54 +01:00
2022-04-14 13:38:29 +02:00
$subscriptions = Webhook::where('company_id', $project->company_id)
2023-01-31 13:53:54 +01:00
->where('event_id', $event)
->exists();
2023-02-16 02:36:09 +01:00
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch($event, $project, $project->company, 'client')->delay(0);
2023-02-16 02:36:09 +01:00
}
2022-04-14 13:38:29 +02:00
}
/**
* Handle the product "deleted" event.
*
* @param Project $project
* @return void
*/
public function deleted(Project $project)
{
2023-02-16 02:36:09 +01:00
if ($project->is_deleted) {
2023-01-31 13:53:54 +01:00
return;
2023-02-16 02:36:09 +01:00
}
2023-01-31 13:53:54 +01:00
2022-12-05 02:30:58 +01:00
$subscriptions = Webhook::where('company_id', $project->company_id)
2023-01-31 13:53:54 +01:00
->where('event_id', Webhook::EVENT_ARCHIVE_PROJECT)
2022-12-05 02:30:58 +01:00
->exists();
2023-02-16 02:36:09 +01:00
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PROJECT, $project, $project->company, 'client')->delay(0);
2023-02-16 02:36:09 +01:00
}
2022-04-14 13:38:29 +02:00
}
/**
* Handle the product "restored" event.
*
* @param Project $project
* @return void
*/
public function restored(Project $project)
{
//
}
/**
* Handle the product "force deleted" event.
*
* @param Project $project
* @return void
*/
public function forceDeleted(Project $project)
{
//
}
2023-01-29 14:43:57 +01:00
/**
* Handle the product "archived" event.
*
* @param Project $project
* @return void
*/
2022-04-14 13:38:29 +02:00
}