Skip to content

Commit

Permalink
improve admin creation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Sep 29, 2021
1 parent 603656a commit 70b167c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions app/Containers/AppSection/User/Actions/CreateAdminAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
use App\Containers\AppSection\User\UI\API\Requests\CreateAdminRequest;
use App\Ship\Exceptions\CreateResourceFailedException;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Exceptions\Exception;
use Illuminate\Support\Facades\DB;
use Throwable;

class CreateAdminAction extends Action
{
/**
* @throws CreateResourceFailedException
* @throws Throwable
*/
public function run(CreateAdminRequest $request): User
{
Expand All @@ -24,10 +28,18 @@ public function run(CreateAdminRequest $request): User
'birth',
]);

$admin = app(CreateUserByCredentialsTask::class)->run($sanitizedData);
DB::beginTransaction();

app(AssignRolesToUserTask::class)->run($admin, [config('appSection-authorization.admin_role')]);
try {
$admin = app(CreateUserByCredentialsTask::class)->run($sanitizedData);
app(AssignRolesToUserTask::class)->run($admin, [config('appSection-authorization.admin_role')]);

return $admin;
DB::commit();

return $admin;
} catch (Exception $exception) {
DB::rollBack();
throw $exception;
}
}
}

0 comments on commit 70b167c

Please sign in to comment.