|
@@ -7,36 +7,48 @@ use Libs\ResponseLib;
|
|
|
use Models\CompanyModel;
|
|
use Models\CompanyModel;
|
|
|
use Models\UserModel;
|
|
use Models\UserModel;
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
+use Respect\Validation\Validator as val;
|
|
|
|
|
+use Respect\Validation\Exceptions\ValidationException;
|
|
|
|
|
|
|
|
class CompanyWithUserController
|
|
class CompanyWithUserController
|
|
|
{
|
|
{
|
|
|
|
|
+ private CompanyModel $companyModel;
|
|
|
|
|
+ private UserModel $userModel;
|
|
|
|
|
+
|
|
|
|
|
+ public function __construct()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->companyModel = new CompanyModel();
|
|
|
|
|
+ $this->userModel = new UserModel();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function __invoke(ServerRequestInterface $request)
|
|
public function __invoke(ServerRequestInterface $request)
|
|
|
{
|
|
{
|
|
|
$body = json_decode((string)$request->getBody(), true) ?? [];
|
|
$body = json_decode((string)$request->getBody(), true) ?? [];
|
|
|
|
|
|
|
|
- $required = [
|
|
|
|
|
- 'company_name',
|
|
|
|
|
- 'username','email','password','phone','address','city','state','zip','country',
|
|
|
|
|
- 'kyc','birthdate','cpf'
|
|
|
|
|
- ];
|
|
|
|
|
- foreach ($required as $field) {
|
|
|
|
|
- if (!isset($body[$field]) || $body[$field] === '') {
|
|
|
|
|
- return ResponseLib::sendFail("Missing field: $field", [], "E_VALIDATE")->withStatus(400);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if (!filter_var($body['email'], FILTER_VALIDATE_EMAIL)) {
|
|
|
|
|
- return ResponseLib::sendFail("Invalid email format", [], "E_VALIDATE")->withStatus(400);
|
|
|
|
|
- }
|
|
|
|
|
- if (strlen($body['password']) < 8) {
|
|
|
|
|
- return ResponseLib::sendFail("Password must be at least 8 characters", [], "E_VALIDATE")->withStatus(400);
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ val::key('company_name', val::stringType()->notEmpty()->length(1, 255))
|
|
|
|
|
+ ->key('username', val::stringType()->notEmpty()->length(1, 100))
|
|
|
|
|
+ ->key('email', val::email())
|
|
|
|
|
+ ->key('password', val::stringType()->length(8, null))
|
|
|
|
|
+ ->key('phone', val::stringType()->notEmpty()->length(1, 50))
|
|
|
|
|
+ ->key('address', val::stringType()->notEmpty()->length(1, 255))
|
|
|
|
|
+ ->key('city', val::stringType()->notEmpty()->length(1, 100))
|
|
|
|
|
+ ->key('state', val::stringType()->notEmpty()->length(1, 100))
|
|
|
|
|
+ ->key('zip', val::stringType()->notEmpty()->length(1, 20))
|
|
|
|
|
+ ->key('country', val::stringType()->notEmpty()->length(1, 100))
|
|
|
|
|
+ ->key('kyc', val::intType())
|
|
|
|
|
+ ->key('birthdate', val::intType())
|
|
|
|
|
+ ->key('cpf', val::stringType()->notEmpty()->length(1, 50))
|
|
|
|
|
+ ->assert($body);
|
|
|
|
|
+ } catch (ValidationException $e) {
|
|
|
|
|
+ return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
$pdo = $GLOBALS['pdo'];
|
|
$pdo = $GLOBALS['pdo'];
|
|
|
$pdo->beginTransaction();
|
|
$pdo->beginTransaction();
|
|
|
|
|
|
|
|
- $companyModel = new CompanyModel();
|
|
|
|
|
- $companyId = $companyModel->createCompany($body['company_name'], 'a');
|
|
|
|
|
|
|
+ $companyId = $this->companyModel->createCompany($body['company_name'], 'a');
|
|
|
$roleId = 1;
|
|
$roleId = 1;
|
|
|
$chk = $pdo->prepare('SELECT 1 FROM "role" WHERE role_id = :rid');
|
|
$chk = $pdo->prepare('SELECT 1 FROM "role" WHERE role_id = :rid');
|
|
|
$chk->execute(['rid' => $roleId]);
|
|
$chk->execute(['rid' => $roleId]);
|
|
@@ -45,7 +57,6 @@ class CompanyWithUserController
|
|
|
return ResponseLib::sendFail('Default role_id 1 not found', [], 'E_DATABASE')->withStatus(500);
|
|
return ResponseLib::sendFail('Default role_id 1 not found', [], 'E_DATABASE')->withStatus(500);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $userModel = new UserModel();
|
|
|
|
|
$userPayload = [
|
|
$userPayload = [
|
|
|
'username' => $body['username'],
|
|
'username' => $body['username'],
|
|
|
'email' => $body['email'],
|
|
'email' => $body['email'],
|
|
@@ -62,7 +73,7 @@ class CompanyWithUserController
|
|
|
'company_id' => $companyId,
|
|
'company_id' => $companyId,
|
|
|
'role_id' => $roleId
|
|
'role_id' => $roleId
|
|
|
];
|
|
];
|
|
|
- $userData = $userModel->createUser($userPayload);
|
|
|
|
|
|
|
+ $userData = $this->userModel->createUser($userPayload);
|
|
|
if (!$userData) {
|
|
if (!$userData) {
|
|
|
$pdo->rollBack();
|
|
$pdo->rollBack();
|
|
|
return ResponseLib::sendFail("Email already exists or creation failed", [], "E_VALIDATE")->withStatus(400);
|
|
return ResponseLib::sendFail("Email already exists or creation failed", [], "E_VALIDATE")->withStatus(400);
|
|
@@ -88,7 +99,7 @@ class CompanyWithUserController
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$stmt = $pdo->prepare('SELECT chain_id FROM "chain" WHERE chain_name = :name');
|
|
$stmt = $pdo->prepare('SELECT chain_id FROM "chain" WHERE chain_name = :name');
|
|
|
- $stmt->execute(['name' => 'primalchain']);
|
|
|
|
|
|
|
+ $stmt->execute(['name' => 'polygon']);
|
|
|
$chainId = $stmt->fetchColumn();
|
|
$chainId = $stmt->fetchColumn();
|
|
|
if (!$chainId) {
|
|
if (!$chainId) {
|
|
|
$pdo->rollBack();
|
|
$pdo->rollBack();
|
|
@@ -121,5 +132,4 @@ class CompanyWithUserController
|
|
|
return ResponseLib::sendFail($e->getMessage(), [], 'E_DATABASE')->withStatus(500);
|
|
return ResponseLib::sendFail($e->getMessage(), [], 'E_DATABASE')->withStatus(500);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
|
|
+}
|