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\HasOwner;
|
2021-09-07 10:33:34 +03:00
|
|
|
use App\Interfaces\Job\HasSource;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldCreate;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Models\Auth\User;
|
2020-06-05 16:18:46 +03:00
|
|
|
use App\Models\Auth\Role;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Models\Common\Contact;
|
2021-04-20 21:44:20 +03:00
|
|
|
use Illuminate\Support\Str;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-07 10:33:34 +03:00
|
|
|
class CreateContact extends Job implements HasOwner, HasSource, ShouldCreate
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2021-09-06 11:53:57 +03:00
|
|
|
public function handle(): Contact
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
|
|
|
if ($this->request->get('create_user', 'false') === 'true') {
|
|
|
|
$this->createUser();
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model = Contact::create($this->request->all());
|
2021-04-20 21:44:20 +03:00
|
|
|
|
|
|
|
// Upload logo
|
|
|
|
if ($this->request->file('logo')) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$media = $this->getMedia($this->request->file('logo'), Str::plural($this->model->type));
|
2021-06-17 10:59:07 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->attachMedia($media, 'logo');
|
2021-04-20 21:44:20 +03:00
|
|
|
}
|
2020-06-26 13:40:19 +03:00
|
|
|
});
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
return $this->model;
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function createUser(): void
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
// Check if user exist
|
|
|
|
if ($user = User::where('email', $this->request['email'])->first()) {
|
|
|
|
$message = trans('messages.error.customer', ['name' => $user->name]);
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->request->all();
|
|
|
|
$data['locale'] = setting('default.locale', 'en-GB');
|
|
|
|
|
2020-06-05 16:18:46 +03:00
|
|
|
$customer_role = Role::all()->filter(function ($role) {
|
|
|
|
return $role->hasPermission('read-client-portal');
|
|
|
|
})->first();
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$user = User::create($data);
|
2020-06-05 16:18:46 +03:00
|
|
|
$user->roles()->attach($customer_role);
|
2020-12-25 12:08:15 +03:00
|
|
|
$user->companies()->attach($data['company_id']);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
$this->request['user_id'] = $user->id;
|
|
|
|
}
|
|
|
|
}
|