mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
backend registration step 1
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\ControllersLogic;
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
||||
|
||||
use App\Classes\Modules\Accounts\Standards\Rules\CanCreateUser;
|
||||
use App\Classes\Modules\Accounts\Services\CreatesUser;
|
||||
use App\Classes\Modules\Accounts\DataTransferObjects\UserObject;
|
||||
use App\Http\Resources\UserResource;
|
||||
|
||||
use App\Classes\Modules\Companies\Standards\Rules\CanCreateCompany;
|
||||
use App\Classes\Modules\Companies\Services\CreatesCompany;
|
||||
use App\Classes\Modules\Companies\DataTransferObjects\CompanyObject;
|
||||
use App\Http\Resources\CompanyResource;
|
||||
|
||||
use App\Classes\Modules\Contacts\Standards\Rules\CanCreateContact;
|
||||
use App\Classes\Modules\Contacts\Services\CreatesContact;
|
||||
use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;
|
||||
use App\Http\Resources\ContactResource;
|
||||
|
||||
use App\Classes\Modules\CompanyEmployees\Standards\Rules\CanCreateCompanyEmployee;
|
||||
use App\Classes\Modules\CompanyEmployees\Services\CreatesCompanyEmployee;
|
||||
use App\Classes\Modules\CompanyEmployees\DataTransferObjects\CompanyEmployeeObject;
|
||||
use App\Http\Resources\CompanyEmployeeResource;
|
||||
|
||||
use ErrorException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CreateUserLogic extends AbstractControllerLogic
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function notification():array {
|
||||
return [
|
||||
'title' => 'Created User',
|
||||
'message' => 'You have successfully created a new User'
|
||||
];
|
||||
}
|
||||
|
||||
/** @var CanCreateUser */
|
||||
private $canCreateUser;
|
||||
|
||||
/** @var CreatesUser */
|
||||
private $createsUser;
|
||||
|
||||
/** @var CanCreateCompany */
|
||||
private $canCreateCompany;
|
||||
|
||||
/** @var CreatesCompany */
|
||||
private $createsCompany;
|
||||
|
||||
/** @var CanCreateContact */
|
||||
private $canCreateContact;
|
||||
|
||||
/** @var CreatesContact */
|
||||
private $createsContact;
|
||||
|
||||
/** @var CanCreateCompanyEmployee */
|
||||
private $canCreateCompanyEmployee;
|
||||
|
||||
/** @var CreatesCompanyEmployee */
|
||||
private $createsCompanyEmployee;
|
||||
|
||||
/**
|
||||
* CreateUserControllerLogic constructor.
|
||||
* @param CanCreateUser $canCreateUser
|
||||
* @param CreatesUser $createsUser
|
||||
* @param CanCreateCompany $canCreateCompany
|
||||
* @param CreatesCompany $createsCompany
|
||||
* @param CanCreateContact $canCreateContact
|
||||
* @param CreatesContact $createsContact
|
||||
* @param CanCreateCompanyEmployee $canCreateCompanyEmployee
|
||||
* @param CreatesCompanyEmployee $createsCompanyEmployee
|
||||
*/
|
||||
public function __construct(
|
||||
CanCreateUser $canCreateUser,
|
||||
CreatesUser $createsUser,
|
||||
CanCreateCompany $canCreateCompany,
|
||||
CreatesCompany $createsCompany,
|
||||
CanCreateContact $canCreateContact,
|
||||
CreatesContact $createsContact,
|
||||
CanCreateCompanyEmployee $canCreateCompanyEmployee,
|
||||
CreatesCompanyEmployee $createsCompanyEmployee
|
||||
)
|
||||
{
|
||||
$this->canCreateUser = $canCreateUser;
|
||||
$this->createsUser = $createsUser;
|
||||
$this->canCreateCompany = $canCreateCompany;
|
||||
$this->createsCompany = $createsCompany;
|
||||
$this->canCreateContact = $canCreateContact;
|
||||
$this->createsContact = $createsContact;
|
||||
$this->canCreateCompanyEmployee = $canCreateCompanyEmployee;
|
||||
$this->createsCompanyEmployee = $createsCompanyEmployee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function logic(Request $request) : JsonResponse
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$user_object = new UserObject(
|
||||
$request->input('name'),
|
||||
$request->input('email'),
|
||||
$request->input('password'),
|
||||
$request->input('password_confirmation'),
|
||||
2,
|
||||
9
|
||||
);
|
||||
|
||||
$this->canCreateUser->passes($user_object);
|
||||
$user_query = $this->createsUser->execute($user_object);
|
||||
|
||||
$company_object = new CompanyObject(
|
||||
$request->input('company_name'),
|
||||
$request->input('company_reference'),
|
||||
1
|
||||
);
|
||||
|
||||
$this->canCreateCompany->passes($company_object);
|
||||
$company_query = $this->createsCompany->execute($company_object);
|
||||
|
||||
$contact_object = new ContactObject(
|
||||
1,
|
||||
$company_query->id,
|
||||
$request->input('contact_reference'),
|
||||
$request->input('phone'),
|
||||
$request->input('contact_email'),
|
||||
$request->input('wechat_id')
|
||||
);
|
||||
|
||||
$this->canCreateContact->passes($contact_object);
|
||||
$contact_query = $this->createsContact->execute($contact_object);
|
||||
|
||||
|
||||
$company_employee_object = new CompanyEmployeeObject(
|
||||
$company_query->id,
|
||||
$user_query->id
|
||||
);
|
||||
|
||||
$this->canCreateCompanyEmployee->passes($company_employee_object);
|
||||
$company_employee_query = $this->createsCompanyEmployee->execute($user_query, $company_employee_object);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $this->resourceResponse(new UserResource($user_query));
|
||||
|
||||
} catch (\Exception $exception) {
|
||||
throw new ErrorException($exception->getMessage(), $exception->getCode());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\DataTransferObjects;
|
||||
|
||||
use App\Classes\Interfaces\DataTransferObject;
|
||||
|
||||
class UserObject implements DataTransferObject
|
||||
{
|
||||
/** @var string|null */
|
||||
private $name;
|
||||
|
||||
/** @var string|null */
|
||||
private $email;
|
||||
|
||||
/** @var string|null */
|
||||
private $password;
|
||||
|
||||
/** @var string|null */
|
||||
private $password_confirmation;
|
||||
|
||||
/** @var int */
|
||||
private $type;
|
||||
|
||||
/** @var status */
|
||||
private $status;
|
||||
|
||||
/**
|
||||
* UserObject constructor.
|
||||
* @param string|null $name
|
||||
* @param string|null $email
|
||||
* @param string|null $password
|
||||
* @param string|null $password_confirmation
|
||||
* @param int $type
|
||||
* @param int $status
|
||||
*/
|
||||
public function __construct(
|
||||
?string $name,
|
||||
?string $email,
|
||||
?string $password,
|
||||
?string $password_confirmation,
|
||||
int $type,
|
||||
int $status
|
||||
)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->email = $email;
|
||||
$this->password = $password;
|
||||
$this->password_confirmation = $password_confirmation;
|
||||
$this->type = $type;
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail(): ?string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPasswordConfirmation(): ?string
|
||||
{
|
||||
return $this->password_confirmation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getType(): int
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStatus(): int
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\Services;
|
||||
|
||||
use App\Classes\General\Eloquent\AbstractUpdateRecord;
|
||||
use App\Classes\Modules\Accounts\DataTransferObjects\UserObject;
|
||||
use App\Models\User;
|
||||
|
||||
class CreatesUser extends AbstractUpdateRecord
|
||||
{
|
||||
/**
|
||||
* @param UserObject $object
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function execute(UserObject $object) {
|
||||
$model = new User();
|
||||
$model->name = $object->getName();
|
||||
$model->email = $object->getEmail();
|
||||
$model->password = bcrypt($object->getPassword());
|
||||
$model->type = $object->getType();
|
||||
$model->status = $object->getStatus();
|
||||
|
||||
return $this->handler($model);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\Standards\Rules;
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractRule;
|
||||
use App\Classes\Modules\Accounts\DataTransferObjects\UserObject;
|
||||
use App\Classes\Modules\Accounts\Standards\Validators\UserValidation;
|
||||
|
||||
class CanCreateUser extends AbstractRule
|
||||
{
|
||||
|
||||
/** @var UserValidation */
|
||||
private $userValidation;
|
||||
|
||||
/**
|
||||
* CanCreateUser constructor.
|
||||
* @param UserValidation $userValidation
|
||||
*/
|
||||
public function __construct(UserValidation $userValidation)
|
||||
{
|
||||
$this->userValidation = $userValidation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorized(): bool
|
||||
{
|
||||
// TODO Set Authorization rules
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UserObject $object
|
||||
* @return bool
|
||||
* @throws \App\Classes\Exceptions\RequestValidationException
|
||||
*/
|
||||
protected function validators($object): bool
|
||||
{
|
||||
return $this->userValidation->validate($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UserObject $object
|
||||
* @return bool
|
||||
*/
|
||||
protected function criteria($object): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Accounts\Standards\Validators;
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractValidation;
|
||||
use App\Classes\Modules\Accounts\DataTransferObjects\UserObject;
|
||||
|
||||
class UserValidation extends AbstractValidation
|
||||
{
|
||||
/**
|
||||
* @param UserObject $object
|
||||
* @return array
|
||||
*/
|
||||
protected function data($object): array
|
||||
{
|
||||
return [
|
||||
'name' => $object->getName(),
|
||||
'email' => $object->getEmail(),
|
||||
'password' => $object->getPassword(),
|
||||
'password_confirmation' => $object->getPasswordConfirmation(),
|
||||
'type' => $object->getType(),
|
||||
'status' => $object->getStatus(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required',
|
||||
'email' => 'required|email|max:255|unique:users,email',
|
||||
'password' => 'required|min:6|confirmed',
|
||||
'type' => 'required',
|
||||
'status' => 'required'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function messages(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user