akaunting 3.0 (the last dance)

This commit is contained in:
Burak Civan
2022-06-01 10:15:55 +03:00
parent cead09f6d4
commit d9c0764572
3812 changed files with 126831 additions and 102949 deletions

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\InvitationCreated;
use App\Models\Auth\UserInvitation;
use Illuminate\Support\Str;
class CreateInvitation extends Job
{
protected $invitation;
protected $user;
protected $company;
public function __construct($user, $company)
{
$this->user = $user;
$this->company = $company;
}
public function handle(): UserInvitation
{
\DB::transaction(function () {
if ($this->user->hasPendingInvitation($this->company->id)) {
$pending_invitation = $this->user->getPendingInvitation($this->company->id);
$this->dispatch(new DeleteInvitation($pending_invitation));
}
$this->invitation = UserInvitation::create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'token' => (string) Str::uuid(),
]);
});
event(new InvitationCreated($this->invitation));
return $this->invitation;
}
}

View File

@@ -3,6 +3,8 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\RoleCreated;
use App\Events\Auth\RoleCreating;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\HasSource;
use App\Interfaces\Job\ShouldCreate;
@@ -12,6 +14,8 @@ class CreateRole extends Job implements HasOwner, HasSource, ShouldCreate
{
public function handle(): Role
{
event(new RoleCreating($this->request));
\DB::transaction(function () {
$this->model = Role::create($this->request->input());
@@ -20,6 +24,8 @@ class CreateRole extends Job implements HasOwner, HasSource, ShouldCreate
}
});
event(new RoleCreated($this->model, $this->request));
return $this->model;
}
}

View File

@@ -3,13 +3,14 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\UserCreated;
use App\Events\Auth\UserCreating;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\HasSource;
use App\Interfaces\Job\ShouldCreate;
use App\Events\Auth\UserCreated;
use App\Events\Auth\UserCreating;
use App\Models\Auth\User;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
class CreateUser extends Job implements HasOwner, HasSource, ShouldCreate
{
@@ -18,6 +19,10 @@ class CreateUser extends Job implements HasOwner, HasSource, ShouldCreate
event(new UserCreating($this->request));
\DB::transaction(function () {
if (! app()->runningInConsole() && ! request()->isInstall()) {
$this->request->merge(['password' => Str::random(40)]);
}
$this->model = User::create($this->request->input());
// Upload picture
@@ -64,6 +69,12 @@ class CreateUser extends Job implements HasOwner, HasSource, ShouldCreate
'user' => $this->model->id,
'company' => $company->id,
]);
if (app()->runningInConsole() || request()->isInstall()) {
continue;
}
$this->dispatch(new CreateInvitation($this->model, $company));
}
});

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldDelete;
class DeleteInvitation extends Job implements ShouldDelete
{
public function handle(): bool
{
\DB::transaction(function () {
$this->model->delete();
});
return true;
}
}

View File

@@ -3,18 +3,24 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\RoleDeleted;
use App\Events\Auth\RoleDeleting;
use App\Interfaces\Job\ShouldDelete;
class DeleteRole extends Job implements ShouldDelete
{
public function handle(): bool
{
event(new RoleDeleting($this->model));
\DB::transaction(function () {
$this->model->delete();
$this->model->flushCache();
});
event(new RoleDeleted($this->model));
return true;
}
}

View File

@@ -3,6 +3,8 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\UserDeleted;
use App\Events\Auth\UserDeleting;
use App\Interfaces\Job\ShouldDelete;
class DeleteUser extends Job implements ShouldDelete
@@ -11,12 +13,16 @@ class DeleteUser extends Job implements ShouldDelete
{
$this->authorize();
event(new UserDeleting($this->model));
\DB::transaction(function () {
$this->model->delete();
$this->model->flushCache();
});
event(new UserDeleted($this->model));
return true;
}

View File

@@ -3,6 +3,8 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\RoleUpdated;
use App\Events\Auth\RoleUpdating;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Auth\Role;
@@ -10,6 +12,8 @@ class UpdateRole extends Job implements ShouldUpdate
{
public function handle(): Role
{
event(new RoleUpdating($this->model, $this->request));
\DB::transaction(function () {
$this->model->update($this->request->all());
@@ -18,6 +22,8 @@ class UpdateRole extends Job implements ShouldUpdate
}
});
event(new RoleUpdated($this->model, $this->request));
return $this->model;
}
}

View File

@@ -3,10 +3,12 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldUpdate;
use App\Events\Auth\UserUpdated;
use App\Events\Auth\UserUpdating;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Auth\User;
use App\Models\Common\Company;
use Illuminate\Support\Facades\Artisan;
class UpdateUser extends Job implements ShouldUpdate
{
@@ -16,6 +18,7 @@ class UpdateUser extends Job implements ShouldUpdate
// Do not reset password if not entered/changed
if (empty($this->request['password'])) {
unset($this->request['current_password']);
unset($this->request['password']);
unset($this->request['password_confirmation']);
}
@@ -38,7 +41,7 @@ class UpdateUser extends Job implements ShouldUpdate
if ($this->request->has('companies')) {
if (app()->runningInConsole() || request()->isInstall()) {
$this->model->companies()->sync($this->request->get('companies'));
$sync = $this->model->companies()->sync($this->request->get('companies'));
} else {
$user = user();
@@ -47,7 +50,7 @@ class UpdateUser extends Job implements ShouldUpdate
});
if ($companies->isNotEmpty()) {
$this->model->companies()->sync($companies->toArray());
$sync = $this->model->companies()->sync($companies->toArray());
}
}
}
@@ -55,6 +58,31 @@ class UpdateUser extends Job implements ShouldUpdate
if ($this->model->contact) {
$this->model->contact->update($this->request->input());
}
if (isset($sync) && !empty($sync['attached'])) {
foreach ($sync['attached'] as $id) {
$company = Company::find($id);
Artisan::call('user:seed', [
'user' => $this->model->id,
'company' => $company->id,
]);
$this->dispatch(new CreateInvitation($this->model, $company));
}
}
if (isset($sync) && !empty($sync['detached'])) {
foreach ($sync['detached'] as $id) {
$company = Company::find($id);
if ($this->model->hasPendingInvitation($company->id)) {
$pending_invitation = $this->model->getPendingInvitation($company->id);
$this->dispatch(new DeleteInvitation($pending_invitation));
}
}
}
});
event(new UserUpdated($this->model, $this->request));