delete email templates after module uninstall

#1kxwd3z
This commit is contained in:
Denis Duliçi 2021-11-19 10:54:18 +03:00
parent 7c81eba203
commit 1d82157d7a
2 changed files with 35 additions and 1 deletions

View File

@ -6,7 +6,9 @@ use App\Events\Module\Uninstalled as Event;
use App\Exceptions\Common\LastDashboard; use App\Exceptions\Common\LastDashboard;
use App\Jobs\Common\DeleteDashboard; use App\Jobs\Common\DeleteDashboard;
use App\Jobs\Common\DeleteReport; use App\Jobs\Common\DeleteReport;
use App\Jobs\Setting\DeleteEmailTemplate;
use App\Models\Common\Dashboard; use App\Models\Common\Dashboard;
use App\Models\Common\EmailTemplate;
use App\Models\Common\Report; use App\Models\Common\Report;
use App\Traits\Jobs; use App\Traits\Jobs;
use Throwable; use Throwable;
@ -24,6 +26,7 @@ class FinishUninstallation
public function handle(Event $event) public function handle(Event $event)
{ {
$this->deleteDashboards($event->alias); $this->deleteDashboards($event->alias);
$this->deleteEmailTemplates($event->alias);
$this->deleteReports($event->alias); $this->deleteReports($event->alias);
} }
@ -48,6 +51,23 @@ class FinishUninstallation
}); });
} }
/**
* Delete any email template created by the module.
*
* @param string $alias
* @return void
*/
protected function deleteEmailTemplates($alias)
{
EmailTemplate::moduleAlias($alias)->get()->each(function ($template) {
try {
$this->dispatch(new DeleteEmailTemplate($template));
} catch (Throwable $e) {
report($e);
}
});
}
/** /**
* Delete any report created by the module. * Delete any report created by the module.
* *

View File

@ -16,7 +16,7 @@ class EmailTemplate extends Model
protected $fillable = ['company_id', 'alias', 'class', 'name', 'subject', 'body', 'params', 'created_from', 'created_by']; protected $fillable = ['company_id', 'alias', 'class', 'name', 'subject', 'body', 'params', 'created_from', 'created_by'];
/** /**
* Scope to only include contacts of a given type. * Scope to only include email templates of a given alias.
* *
* @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $query
* @param mixed $alias * @param mixed $alias
@ -26,4 +26,18 @@ class EmailTemplate extends Model
{ {
return $query->where('alias', $alias); return $query->where('alias', $alias);
} }
/**
* Scope to only include email templates of a given module alias (class).
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $alias
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeModuleAlias($query, $alias)
{
$class = ($alias == 'core') ? 'App\\\\' : 'Modules\\\\' . Str::studly($alias) . '\\\\';
return $query->where('class', 'like', $class . '%');
}
} }