get('/api/v1/base/tenancy', function (Application $app, Request $request) { $ch = new Checks(); $codeResponse = 400; try { $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration()); $apiInstance = new TenancyApi( new Client(), $config ); try { $id = $ch->integer($request, "id"); $result = $apiInstance->getById($id); if ($result) { $content = $result->__toString(); $content = (new CurlOptions())->clearNull($content); $codeResponse = 200; } else { $content = "No se encontro el id = " . $id; } } catch (Throwable $e) { $content = $e->getCode() . " - " . $e->getMessage(); } } catch (Throwable $t) { $content = $t->getCode() . " - " . $t->getMessage(); } return Utils::returnJSON($content, $codeResponse); }); } public function getTenancies(Application $app) { $app->get('/api/v1/base/tenancies', function (Application $app, Request $request) { $codeResponse = 400; try { $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration()); $apiInstance = new TenancyApi( new Client(), $config ); try { $content = "["; $result = $apiInstance->getList(); foreach ($result as $v) { // se eliminan los null $content .= $v->__toString() . ","; } if (strlen($content) > 1) { $content = substr($content, 0, strlen($content) - 1); } $content .= "]"; $content = (new CurlOptions())->clearNull($content); $codeResponse = 200; } catch (Throwable $e) { $content = $e->getCode() . " - " . $e->getMessage(); } } catch (Throwable $t) { $content = $t->getCode() . " - " . $t->getMessage(); } return Utils::returnJSON($content, $codeResponse); }); } public function getClientId(Application $app) { $app->get('/api/v1/base/client', function (Application $app, Request $request) { $ch = new Checks(); $codeResponse = 400; try { $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration()); $apiInstance = new ClientApi( new Client(), $config ); try { $id = $ch->integer($request, "id"); $apiInstance->setDisabledTenancy(true); $result = $apiInstance->getById($id); $apiInstance->setDisabledTenancy(false); if ($result) { $content = $result->__toString(); $content = (new CurlOptions())->clearNull($content); $codeResponse = 200; } else { $content = "No se encontro el id = " . $id; } } catch (Throwable $e) { $content = $e->getCode() . " - " . $e->getMessage(); } } catch (Throwable $t) { $content = $t->getCode() . " - " . $t->getMessage(); } return Utils::returnJSON($content, $codeResponse); }); } public function getClients(Application $app) { $app->get('/api/v1/base/clients', function (Application $app, Request $request) { $ch = new Checks(); $codeResponse = 400; try { $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration()); $apiInstance = new ClientApi( new Client(), $config ); try { $page = $ch->integer($request, "page", false); $limit = $ch->integer($request, "limit", false); $id = $ch->integer($request, "id", false); $name = $ch->string($request, "name", false); $externalId = $ch->string($request, "externalId", false); $company = $ch->string($request, "company", false); $address = $ch->string($request, "address", false); $email = $ch->string($request, "email", false); $currentState = $ch->string($request, "currentState", false); $tenancyId = $ch->integer($request, "tenancyId", false); $qb_criteria = $ch->integer($request, "qb-criteria", false, null); $content = "["; $result = $apiInstance->getList($id, $name, $externalId, $company, $address, $email, $currentState, $tenancyId, $qb_criteria, $limit, $page); foreach ($result as $v) { // se eliminan los null $content .= $v->__toString() . ","; } if (strlen($content) > 1) { $content = substr($content, 0, strlen($content) - 1); } $content .= "]"; $content = (new CurlOptions())->clearNull($content); $codeResponse = 200; } catch (Throwable $e) { $content = $e->getCode() . " - " . $e->getMessage(); } } catch (Throwable $t) { $content = $t->getCode() . " - " . $t->getMessage(); } return Utils::returnJSON($content, $codeResponse); }); } public function postClient(Application $app) { $app->post('/api/v1/base/client', function (Application $app, Request $request) { $codeResponse = 400; try { $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration()); $apiInstance = new ClientApi( new Client(), $config ); $apiValidation = new ValidationApi( new Client(), $config ); $data = json_decode(file_get_contents('php://input'), true); $utils = new Utils(); $instance = $utils->loadData(new ClientCRUD(), $data); $errors = $instance->listInvalidProperties(); if (count($errors) === 0) { // chequeo la tenencia $apiTenancy = new TenancyApi( new Client(), $config ); // verifico si existe la tenencia if (!is_null($instance->getTenancy()) && !is_null($apiTenancy->getById($instance->getTenancy()))) { // busco si el externalid ya esta cargado $result = $apiValidation->existsClient($instance->getExternalId(), $instance->getTenancy()); $name = ""; if (!is_null($result)) { $name = $result->getId() . " - " . $result->getName(); } if ($name == "") { $apiInstance->create($instance); // busco el id del cliente, buscandolo por externalid $result = $apiValidation->existsClient($instance->getExternalId(), $instance->getTenancy()); if ($result) { $content = $result->__toString(); $codeResponse = 200; } else { $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente."; } } else { $codeResponse = 409; $content = "El externalId ya existe y pertenece al cliente " . $name . "."; } } else { $content = "El id de tenencia es incorrecto."; } } else { $content = json_encode($errors); } } catch (Throwable $t) { $content = $t->getCode() . " - " . $t->getMessage(); } return Utils::returnJSON($content, $codeResponse); }); } public function putClient(Application $app) { $app->put('/api/v1/base/client', function (Application $app, Request $request) { $codeResponse = 400; try { $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration()); $apiInstance = new ClientApi( new Client(), $config ); $apiValidation = new ValidationApi( new Client(), $config ); $data = json_decode(file_get_contents('php://input'), true); $utils = new Utils(); $instance = $utils->loadData(new ClientCRUD(), $data); $errors = $instance->listInvalidProperties(); if (count($errors) === 0) { // chequeo la tenencia $apiTenancy = new TenancyApi( new Client(), $config ); // verifico si existe la tenencia if (!is_null($instance->getTenancy()) && !is_null($apiTenancy->getById($instance->getTenancy()))) { // busco si el externalid ya esta cargado $result = $apiValidation->existsClient($instance->getExternalId(), $instance->getTenancy()); $name = ""; if (!is_null($result) && $result->getId() != $instance->getId()) { $name = $result->getId() . " - " . $result->getName(); } if ($name == "") { $apiInstance->update($instance->getId(), $instance); // busco el id del cliente, buscandolo por externalid $result = $apiValidation->validClient($instance->getId()); if ($result) { $content = $result->__toString(); $codeResponse = 200; } else { $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente."; } } else { $codeResponse = 409; $content = "El externalId ya existe y pertenece al cliente " . $name . "."; } } else { $content = "El id de tenencia es incorrecto."; } } else { $content = json_encode($errors); } } catch (Throwable $t) { $content = $t->getCode() . " - " . $t->getMessage(); } return Utils::returnJSON($content, $codeResponse); }); } public function deleteClient(Application $app) { $app->delete('/api/v1/base/client', function (Application $app, Request $request) { $codeResponse = 400; $ch = new Checks(); try { $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration()); $apiInstance = new ClientApi( new Client(), $config ); $id = $ch->integer($request, "id", false); $apiInstance->setDisabledTenancy(true); $result = $apiInstance->getById($id); $apiInstance->setDisabledTenancy(false); if (!is_null($result)) { $apiInstance->delete($id); $content = $result->__toString(); $codeResponse = 200; } else { $content = "No se encontro el id = " . $id; } } catch (Throwable $t) { $content = $t->getCode() . " - " . $t->getMessage(); } return Utils::returnJSON($content, $codeResponse); }); } public function getClientDisable(Application $app) { $app->get('/api/v1/base/client/disable', function (Application $app, Request $request) { $codeResponse = 400; try { $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration()); $apiInstance = new ClientApi( new Client(), $config ); $ch = new Checks(); $id = $ch->integer($request, "id"); $workflow = "administrative_state"; $transition = "suspend"; $apiInstance->setDisabledTenancy(true); $result = $apiInstance->getById($id); $apiInstance->setDisabledTenancy(false); if (!is_null($result)) { $apiInstance->apply($id, $workflow, $transition); $apiInstance->setDisabledTenancy(true); $result = $apiInstance->getById($id); $apiInstance->setDisabledTenancy(false); if ($result) { $content = $result->__toString(); $codeResponse = 200; } else { $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente."; } } else { $content = "No se encontro el cliente de id = " . $id; } } catch (Throwable $t) { $content = $t->getCode() . " - " . $t->getMessage(); } return Utils::returnJSON($content, $codeResponse); }); } public function getClientActive(Application $app) { $app->get('/api/v1/base/client/active', function (Application $app, Request $request) { $codeResponse = 400; try { $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration()); $apiInstance = new ClientApi( new Client(), $config ); $ch = new Checks(); $id = $ch->integer($request, "id"); $workflow = "administrative_state"; $transition = "active"; $apiInstance->setDisabledTenancy(true); $result = $apiInstance->getById($id); $apiInstance->setDisabledTenancy(false); if (!is_null($result)) { $apiInstance->apply($id, $workflow, $transition); $apiInstance->setDisabledTenancy(true); $result = $apiInstance->getById($id); $apiInstance->setDisabledTenancy(false); if ($result) { $content = $result->__toString(); $codeResponse = 200; } else { $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente."; } } else { $content = "No se encontro el cliente de id = " . $id; } } catch (Throwable $t) { $content = $t->getCode() . " - " . $t->getMessage(); } return Utils::returnJSON($content, $codeResponse); }); } }