akaunting/app/Jobs/Common/DeleteDashboard.php

52 lines
1.2 KiB
PHP
Raw Normal View History

2020-01-07 17:15:00 +03:00
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Exceptions\Common\LastDashboard;
use App\Exceptions\Common\NotUserDashboard;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\ShouldDelete;
2020-01-07 17:15:00 +03:00
use App\Traits\Users;
2021-09-06 11:53:57 +03:00
class DeleteDashboard extends Job implements ShouldDelete
2020-01-07 17:15:00 +03:00
{
use Users;
2021-09-06 11:53:57 +03:00
public function handle(): bool
2020-01-07 17:15:00 +03:00
{
$this->authorize();
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
2021-09-06 11:53:57 +03:00
$this->deleteRelationships($this->model, ['widgets']);
2020-01-07 17:15:00 +03:00
2021-09-06 11:53:57 +03:00
$this->model->delete();
2020-06-26 13:40:19 +03:00
});
2020-01-07 17:15:00 +03:00
return true;
}
/**
* Determine if this action is applicable.
*/
2021-09-06 11:53:57 +03:00
public function authorize(): void
2020-01-07 17:15:00 +03:00
{
2020-01-11 22:57:19 +03:00
// Can't delete last dashboard for any shared user
2021-09-06 11:53:57 +03:00
foreach ($this->model->users as $user) {
2020-01-11 22:57:19 +03:00
if ($user->dashboards()->enabled()->count() > 1) {
continue;
}
2020-01-07 17:15:00 +03:00
$message = trans('dashboards.error.delete_last');
throw new LastDashboard($message);
2020-01-07 17:15:00 +03:00
}
// Check if user can access dashboard
2021-09-06 11:53:57 +03:00
if ($this->isNotUserDashboard($this->model->id)) {
2020-01-07 17:15:00 +03:00
$message = trans('dashboards.error.not_user_dashboard');
throw new NotUserDashboard($message);
2020-01-07 17:15:00 +03:00
}
}
}