2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldDelete;
|
2020-06-05 16:04:54 +03:00
|
|
|
use App\Jobs\Auth\DeleteUser;
|
2020-01-22 21:23:20 +03:00
|
|
|
use App\Traits\Contacts;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
class DeleteContact extends Job implements ShouldDelete
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2020-01-22 21:23:20 +03:00
|
|
|
use Contacts;
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function handle() :bool
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
$this->authorize();
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
2021-09-06 11:53:57 +03:00
|
|
|
if ($user = $this->model->user) {
|
2020-06-26 13:40:19 +03:00
|
|
|
$this->dispatch(new DeleteUser($user));
|
|
|
|
}
|
2020-06-05 16:04:54 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->delete();
|
2020-06-26 13:40:19 +03:00
|
|
|
});
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this action is applicable.
|
|
|
|
*/
|
2021-09-06 11:53:57 +03:00
|
|
|
public function authorize(): void
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
if ($relationships = $this->getRelationships()) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function getRelationships(): array
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
$rels = [
|
|
|
|
'transactions' => 'transactions',
|
|
|
|
];
|
|
|
|
|
2020-06-08 23:09:43 +03:00
|
|
|
if ($this->isCustomer()) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$rels['invoices'] = 'invoices';
|
|
|
|
} else {
|
|
|
|
$rels['bills'] = 'bills';
|
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
return $this->countRelationships($this->model, $rels);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
}
|