Services.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. namespace Swagger\Client\Base;
  3. use Checks;
  4. use CurlOptions;
  5. use GuzzleHttp\Client;
  6. use Silex\Application;
  7. use Swagger\Client\ApiServices;
  8. use Swagger\Client\Base\Api\ClientApi;
  9. use Swagger\Client\Base\Api\TenancyApi;
  10. use Swagger\Client\Base\Api\ValidationApi;
  11. use Swagger\Client\Base\Model\ClientCRUD;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Throwable;
  15. use Utils;
  16. class Services extends ApiServices
  17. {
  18. public function getTenancyId(Application $app)
  19. {
  20. $app->get('/api/v1/base/tenancy', function (Application $app, Request $request) {
  21. $ch = new Checks();
  22. $codeResponse = 400;
  23. try {
  24. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  25. $apiInstance = new TenancyApi(
  26. new Client(),
  27. $config
  28. );
  29. try {
  30. $id = $ch->integer($request, "id");
  31. $result = $apiInstance->getById($id);
  32. if ($result) {
  33. $content = $result->__toString();
  34. $content = (new CurlOptions())->clearNull($content);
  35. $codeResponse = 200;
  36. } else {
  37. $content = "No se encontro el id = " . $id;
  38. }
  39. } catch (Throwable $e) {
  40. $content = $e->getCode() . " - " . $e->getMessage();
  41. }
  42. } catch (Throwable $t) {
  43. $content = $t->getCode() . " - " . $t->getMessage();
  44. }
  45. return Utils::returnJSON($content, $codeResponse);
  46. });
  47. }
  48. public function getTenancies(Application $app)
  49. {
  50. $app->get('/api/v1/base/tenancies', function (Application $app, Request $request) {
  51. $codeResponse = 400;
  52. try {
  53. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  54. $apiInstance = new TenancyApi(
  55. new Client(),
  56. $config
  57. );
  58. try {
  59. $content = "[";
  60. $result = $apiInstance->getList();
  61. foreach ($result as $v) {
  62. // se eliminan los null
  63. $content .= $v->__toString() . ",";
  64. }
  65. if (strlen($content) > 1) {
  66. $content = substr($content, 0, strlen($content) - 1);
  67. }
  68. $content .= "]";
  69. $content = (new CurlOptions())->clearNull($content);
  70. $codeResponse = 200;
  71. } catch (Throwable $e) {
  72. $content = $e->getCode() . " - " . $e->getMessage();
  73. }
  74. } catch (Throwable $t) {
  75. $content = $t->getCode() . " - " . $t->getMessage();
  76. }
  77. return Utils::returnJSON($content, $codeResponse);
  78. });
  79. }
  80. public function getClientId(Application $app)
  81. {
  82. $app->get('/api/v1/base/client', function (Application $app, Request $request) {
  83. $ch = new Checks();
  84. $codeResponse = 400;
  85. try {
  86. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  87. $apiInstance = new ClientApi(
  88. new Client(),
  89. $config
  90. );
  91. try {
  92. $id = $ch->integer($request, "id");
  93. $apiInstance->setDisabledTenancy(true);
  94. $result = $apiInstance->getById($id);
  95. $apiInstance->setDisabledTenancy(false);
  96. if ($result) {
  97. $content = $result->__toString();
  98. $content = (new CurlOptions())->clearNull($content);
  99. $codeResponse = 200;
  100. } else {
  101. $content = "No se encontro el id = " . $id;
  102. }
  103. } catch (Throwable $e) {
  104. $content = $e->getCode() . " - " . $e->getMessage();
  105. }
  106. } catch (Throwable $t) {
  107. $content = $t->getCode() . " - " . $t->getMessage();
  108. }
  109. return Utils::returnJSON($content, $codeResponse);
  110. });
  111. }
  112. public function getClients(Application $app)
  113. {
  114. $app->get('/api/v1/base/clients', function (Application $app, Request $request) {
  115. $ch = new Checks();
  116. $codeResponse = 400;
  117. try {
  118. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  119. $apiInstance = new ClientApi(
  120. new Client(),
  121. $config
  122. );
  123. try {
  124. $page = $ch->integer($request, "page", false);
  125. $limit = $ch->integer($request, "limit", false);
  126. $id = $ch->integer($request, "id", false);
  127. $name = $ch->string($request, "name", false);
  128. $externalId = $ch->string($request, "externalId", false);
  129. $company = $ch->string($request, "company", false);
  130. $address = $ch->string($request, "address", false);
  131. $email = $ch->string($request, "email", false);
  132. $currentState = $ch->string($request, "currentState", false);
  133. $tenancyId = $ch->integer($request, "tenancyId", false);
  134. $qb_criteria = $ch->integer($request, "qb-criteria", false, null);
  135. $content = "[";
  136. $result = $apiInstance->getList($id, $name, $externalId, $company, $address, $email, $currentState, $tenancyId, $qb_criteria, $limit, $page);
  137. foreach ($result as $v) {
  138. // se eliminan los null
  139. $content .= $v->__toString() . ",";
  140. }
  141. if (strlen($content) > 1) {
  142. $content = substr($content, 0, strlen($content) - 1);
  143. }
  144. $content .= "]";
  145. $content = (new CurlOptions())->clearNull($content);
  146. $codeResponse = 200;
  147. } catch (Throwable $e) {
  148. $content = $e->getCode() . " - " . $e->getMessage();
  149. }
  150. } catch (Throwable $t) {
  151. $content = $t->getCode() . " - " . $t->getMessage();
  152. }
  153. return Utils::returnJSON($content, $codeResponse);
  154. });
  155. }
  156. public function postClient(Application $app)
  157. {
  158. $app->post('/api/v1/base/client', function (Application $app, Request $request) {
  159. $codeResponse = 400;
  160. try {
  161. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  162. $apiInstance = new ClientApi(
  163. new Client(),
  164. $config
  165. );
  166. $apiValidation = new ValidationApi(
  167. new Client(),
  168. $config
  169. );
  170. $data = json_decode(file_get_contents('php://input'), true);
  171. $utils = new Utils();
  172. $instance = $utils->loadData(new ClientCRUD(), $data);
  173. $errors = $instance->listInvalidProperties();
  174. if (count($errors) === 0) {
  175. // chequeo la tenencia
  176. $apiTenancy = new TenancyApi(
  177. new Client(),
  178. $config
  179. );
  180. // verifico si existe la tenencia
  181. if (!is_null($instance->getTenancy()) && !is_null($apiTenancy->getById($instance->getTenancy()))) {
  182. // busco si el externalid ya esta cargado
  183. $result = $apiValidation->existsClient($instance->getExternalId(), $instance->getTenancy());
  184. $name = "";
  185. if (!is_null($result)) {
  186. $name = $result->getId() . " - " . $result->getName();
  187. }
  188. if ($name == "") {
  189. $apiInstance->create($instance);
  190. // busco el id del cliente, buscandolo por externalid
  191. $result = $apiValidation->existsClient($instance->getExternalId(), $instance->getTenancy());
  192. if ($result) {
  193. $content = $result->__toString();
  194. $codeResponse = 200;
  195. } else {
  196. $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente.";
  197. }
  198. } else {
  199. $codeResponse = 409;
  200. $content = "El externalId ya existe y pertenece al cliente " . $name . ".";
  201. }
  202. } else {
  203. $content = "El id de tenencia es incorrecto.";
  204. }
  205. } else {
  206. $content = json_encode($errors);
  207. }
  208. } catch (Throwable $t) {
  209. $content = $t->getCode() . " - " . $t->getMessage();
  210. }
  211. return Utils::returnJSON($content, $codeResponse);
  212. });
  213. }
  214. public function putClient(Application $app)
  215. {
  216. $app->put('/api/v1/base/client', function (Application $app, Request $request) {
  217. $codeResponse = 400;
  218. try {
  219. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  220. $apiInstance = new ClientApi(
  221. new Client(),
  222. $config
  223. );
  224. $apiValidation = new ValidationApi(
  225. new Client(),
  226. $config
  227. );
  228. $data = json_decode(file_get_contents('php://input'), true);
  229. $utils = new Utils();
  230. $instance = $utils->loadData(new ClientCRUD(), $data);
  231. $errors = $instance->listInvalidProperties();
  232. if (count($errors) === 0) {
  233. // chequeo la tenencia
  234. $apiTenancy = new TenancyApi(
  235. new Client(),
  236. $config
  237. );
  238. // verifico si existe la tenencia
  239. if (!is_null($instance->getTenancy()) && !is_null($apiTenancy->getById($instance->getTenancy()))) {
  240. // busco si el externalid ya esta cargado
  241. $result = $apiValidation->existsClient($instance->getExternalId(), $instance->getTenancy());
  242. $name = "";
  243. if (!is_null($result) && $result->getId() != $instance->getId()) {
  244. $name = $result->getId() . " - " . $result->getName();
  245. }
  246. if ($name == "") {
  247. $apiInstance->update($instance->getId(), $instance);
  248. // busco el id del cliente, buscandolo por externalid
  249. $result = $apiValidation->validClient($instance->getId());
  250. if ($result) {
  251. $content = $result->__toString();
  252. $codeResponse = 200;
  253. } else {
  254. $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente.";
  255. }
  256. } else {
  257. $codeResponse = 409;
  258. $content = "El externalId ya existe y pertenece al cliente " . $name . ".";
  259. }
  260. } else {
  261. $content = "El id de tenencia es incorrecto.";
  262. }
  263. } else {
  264. $content = json_encode($errors);
  265. }
  266. } catch (Throwable $t) {
  267. $content = $t->getCode() . " - " . $t->getMessage();
  268. }
  269. return Utils::returnJSON($content, $codeResponse);
  270. });
  271. }
  272. public function deleteClient(Application $app)
  273. {
  274. $app->delete('/api/v1/base/client', function (Application $app, Request $request) {
  275. $codeResponse = 400;
  276. $ch = new Checks();
  277. try {
  278. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  279. $apiInstance = new ClientApi(
  280. new Client(),
  281. $config
  282. );
  283. $id = $ch->integer($request, "id", false);
  284. $apiInstance->setDisabledTenancy(true);
  285. $result = $apiInstance->getById($id);
  286. $apiInstance->setDisabledTenancy(false);
  287. if (!is_null($result)) {
  288. $apiInstance->delete($id);
  289. $content = $result->__toString();
  290. $codeResponse = 200;
  291. } else {
  292. $content = "No se encontro el id = " . $id;
  293. }
  294. } catch (Throwable $t) {
  295. $content = $t->getCode() . " - " . $t->getMessage();
  296. }
  297. return Utils::returnJSON($content, $codeResponse);
  298. });
  299. }
  300. public function getClientDisable(Application $app)
  301. {
  302. $app->get('/api/v1/base/client/disable', function (Application $app, Request $request) {
  303. $codeResponse = 400;
  304. try {
  305. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  306. $apiInstance = new ClientApi(
  307. new Client(),
  308. $config
  309. );
  310. $ch = new Checks();
  311. $id = $ch->integer($request, "id");
  312. $workflow = "administrative_state";
  313. $transition = "suspend";
  314. $apiInstance->setDisabledTenancy(true);
  315. $result = $apiInstance->getById($id);
  316. $apiInstance->setDisabledTenancy(false);
  317. if (!is_null($result)) {
  318. $apiInstance->apply($id, $workflow, $transition);
  319. $apiInstance->setDisabledTenancy(true);
  320. $result = $apiInstance->getById($id);
  321. $apiInstance->setDisabledTenancy(false);
  322. if ($result) {
  323. $content = $result->__toString();
  324. $codeResponse = 200;
  325. } else {
  326. $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente.";
  327. }
  328. } else {
  329. $content = "No se encontro el cliente de id = " . $id;
  330. }
  331. } catch (Throwable $t) {
  332. $content = $t->getCode() . " - " . $t->getMessage();
  333. }
  334. return Utils::returnJSON($content, $codeResponse);
  335. });
  336. }
  337. public function getClientActive(Application $app)
  338. {
  339. $app->get('/api/v1/base/client/active', function (Application $app, Request $request) {
  340. $codeResponse = 400;
  341. try {
  342. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  343. $apiInstance = new ClientApi(
  344. new Client(),
  345. $config
  346. );
  347. $ch = new Checks();
  348. $id = $ch->integer($request, "id");
  349. $workflow = "administrative_state";
  350. $transition = "active";
  351. $apiInstance->setDisabledTenancy(true);
  352. $result = $apiInstance->getById($id);
  353. $apiInstance->setDisabledTenancy(false);
  354. if (!is_null($result)) {
  355. $apiInstance->apply($id, $workflow, $transition);
  356. $apiInstance->setDisabledTenancy(true);
  357. $result = $apiInstance->getById($id);
  358. $apiInstance->setDisabledTenancy(false);
  359. if ($result) {
  360. $content = $result->__toString();
  361. $codeResponse = 200;
  362. } else {
  363. $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente.";
  364. }
  365. } else {
  366. $content = "No se encontro el cliente de id = " . $id;
  367. }
  368. } catch (Throwable $t) {
  369. $content = $t->getCode() . " - " . $t->getMessage();
  370. }
  371. return Utils::returnJSON($content, $codeResponse);
  372. });
  373. }
  374. }