123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- <?php
- namespace Swagger\Client\Base;
- use Checks;
- use CurlOptions;
- use GuzzleHttp\Client;
- use Silex\Application;
- use Swagger\Client\ApiServices;
- use Swagger\Client\Base\Api\ClientApi;
- use Swagger\Client\Base\Api\TenancyApi;
- use Swagger\Client\Base\Api\ValidationApi;
- use Swagger\Client\Base\Model\ClientCRUD;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Throwable;
- use Utils;
- class Services extends ApiServices
- {
- public function getTenancyId(Application $app)
- {
- $app->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);
- });
- }
- }
|