From b7c5e4ebc316ea7aea3b15e76c049680ba1c3667 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Fri, 16 May 2025 19:14:20 -0400 Subject: [PATCH] Opcion de enviar update a los servicios externos --- app/resources/routes/api/external.php | 1 + app/src/Controller/API/External.php | 7 +++++++ app/src/Service/External.php | 5 ++++- app/src/Service/Venta/MediosPago/Toku.php | 10 +++++++--- app/src/Service/Venta/MediosPago/Toku/Invoice.php | 3 ++- .../Service/Venta/MediosPago/Toku/Subscription.php | 3 +++ app/src/Service/Worker/CheckExternal.php | 7 ++++++- cli/setup/settings/commands.php | 4 ++-- cli/src/Command/ExternalServices.php | 12 ++++++++++++ 9 files changed, 44 insertions(+), 8 deletions(-) diff --git a/app/resources/routes/api/external.php b/app/resources/routes/api/external.php index b9a35ca..e9b7ae9 100644 --- a/app/resources/routes/api/external.php +++ b/app/resources/routes/api/external.php @@ -11,5 +11,6 @@ $app->group('/external', function($app) { } $app->group('/services', function($app) { $app->get('/check[/]', [External::class, 'check']); + $app->get('/update[/]', [External::class, 'update']); }); }); diff --git a/app/src/Controller/API/External.php b/app/src/Controller/API/External.php index 7aa426a..86a9733 100644 --- a/app/src/Controller/API/External.php +++ b/app/src/Controller/API/External.php @@ -17,4 +17,11 @@ class External extends Ideal\Controller } return $response->withStatus(409); } + public function update(ServerRequestInterface $request, ResponseInterface $response, Service\External $externalService): ResponseInterface + { + if ($externalService->check(true)) { + return $response->withStatus(204); + } + return $response->withStatus(409); + } } diff --git a/app/src/Service/External.php b/app/src/Service/External.php index cba9773..3a43a28 100644 --- a/app/src/Service/External.php +++ b/app/src/Service/External.php @@ -24,7 +24,7 @@ class External extends Ideal\Service return $this; } - public function check(): bool + public function check(bool $update = false): bool { $errors = []; foreach ($this->externalServices as $externalService) { @@ -36,6 +36,9 @@ class External extends Ideal\Service 'service' => get_class($externalService), 'action' => 'check', ]; + if ($update) { + $queueData['args'] = ['update' => true]; + } if (!$this->queueService->enqueue($queueData)) { $errors []= get_class($externalService); } diff --git a/app/src/Service/Venta/MediosPago/Toku.php b/app/src/Service/Venta/MediosPago/Toku.php index b1cb3bc..ad4e4a0 100644 --- a/app/src/Service/Venta/MediosPago/Toku.php +++ b/app/src/Service/Venta/MediosPago/Toku.php @@ -214,7 +214,9 @@ class Toku extends Ideal\Service 'telefono' => $propietario->datos?->telefono ?? '' ]; $this->customer->edit($customer['toku_id'], $editData); - } catch (EmptyResponse) {} + } catch (EmptyResponse $exception) { + $this->logger->warning($exception); + } foreach ($cuotas as $cuota) { try { $invoice = $this->invoice->getById($cuota->id); @@ -222,7 +224,8 @@ class Toku extends Ideal\Service 'customer' => $customer['toku_id'], 'product_id' => $subscription->venta->id, 'subscription' => $subscription->toku_id, - 'cuota' => $cuota + 'cuota' => $cuota, + 'venta' => $subscription->venta ]; try { $this->invoice->edit($invoice['toku_id'], $editData); @@ -232,7 +235,8 @@ class Toku extends Ideal\Service 'customer' => $customer['toku_id'], 'product_id' => $subscription->venta->id, 'subscription' => $subscription->toku_id, - 'cuota' => $cuota + 'cuota' => $cuota, + 'venta' => $subscription->venta ]; try { $this->invoice->add($invoiceData); diff --git a/app/src/Service/Venta/MediosPago/Toku/Invoice.php b/app/src/Service/Venta/MediosPago/Toku/Invoice.php index 10cd77e..f0c5fb2 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Invoice.php +++ b/app/src/Service/Venta/MediosPago/Toku/Invoice.php @@ -17,7 +17,8 @@ class Invoice extends AbstractEndPoint { public function __construct(ClientInterface $client, protected Repository\Venta\MediosPago\Toku\Invoice $invoiceRepository, - protected Pago $pagoService, protected UF $ufService) + protected Pago $pagoService, + protected UF $ufService) { parent::__construct($client); } diff --git a/app/src/Service/Venta/MediosPago/Toku/Subscription.php b/app/src/Service/Venta/MediosPago/Toku/Subscription.php index 8b46a75..2589a7c 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Subscription.php +++ b/app/src/Service/Venta/MediosPago/Toku/Subscription.php @@ -59,6 +59,9 @@ class Subscription extends AbstractEndPoint $existingSubscriptions = []; try { $existingSubscriptions = $this->subscriptionRepsitory->fetchByVentas($ids); + array_walk($existingSubscriptions, function(&$subscription) { + $subscription->venta = $this->ventaService->getById($subscription->venta->id); + }); } catch (EmptyResult) {} if (count($existingSubscriptions) === 0) { $missingVentas = $ventas; diff --git a/app/src/Service/Worker/CheckExternal.php b/app/src/Service/Worker/CheckExternal.php index 23c1cb3..f667334 100644 --- a/app/src/Service/Worker/CheckExternal.php +++ b/app/src/Service/Worker/CheckExternal.php @@ -37,7 +37,12 @@ class CheckExternal extends Ideal\Service implements Service\Worker if (!isset($this->queueService)) { $this->queueService = $this->container->get(Service\Queue::class); } - $queues = $service->{$method}(); + if (isset($configuration['args'])) { + $args = $configuration['args']; + $queues = call_user_func_array([$service, $method], $args); + } else { + $queues = call_user_func([$service, $method]); + } foreach ($queues as $queue) { $this->queueService->enqueue($queue); } diff --git a/cli/setup/settings/commands.php b/cli/setup/settings/commands.php index 0345604..a7aa8e1 100644 --- a/cli/setup/settings/commands.php +++ b/cli/setup/settings/commands.php @@ -2,6 +2,7 @@ return [ 'commands' => function() { return [ + 'loop' => Incoviba\Command\BaseLoop::class, 'comunas' => Incoviba\Command\Comunas::class, 'contabilidad:cartolas:update' => Incoviba\Command\Contabilidad\Cartolas\Update::class, 'money:ipc' => Incoviba\Command\Money\IPC::class, @@ -14,8 +15,7 @@ return [ 'ventas:cuotas:pendientes' => Incoviba\Command\Ventas\Cuotas\Pendientes::class, 'ventas:cuotas:vencer' => Incoviba\Command\Ventas\Cuotas\PorVencer::class, 'queue' => Incoviba\Command\Queue::class, - 'loop' => Incoviba\Command\BaseLoop::class, - 'external:services' => Incoviba\Command\ExternalServices::class + 'external:services' => Incoviba\Command\ExternalServices::class, ]; } ]; diff --git a/cli/src/Command/ExternalServices.php b/cli/src/Command/ExternalServices.php index 30bcec2..249b810 100644 --- a/cli/src/Command/ExternalServices.php +++ b/cli/src/Command/ExternalServices.php @@ -10,9 +10,21 @@ use Incoviba\Common\Alias; )] class ExternalServices extends Alias\Command { + protected function configure() + { + $this->addOption('update', 'u', Console\Input\InputOption::VALUE_NONE, 'Update'); + } + public function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int { + $update = $input->getOption('update'); + $url = '/api/external/services/check'; + + if ($update) { + $url = '/api/external/services/update'; + } + $output->writeln("GET {$url}"); $response = $this->client->get($url); $output->writeln("Response Code: {$response->getStatusCode()}");