Rutas API Contratos

This commit is contained in:
Juan Pablo Vial
2025-03-03 11:26:45 -03:00
parent 8ea13c3efd
commit 4aa88d5164
4 changed files with 118 additions and 13 deletions

View File

@ -17,7 +17,7 @@ class Brokers
return $this->withJson($response, compact('brokers'));
}
public function show(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService, int $broker_rut): ResponseInterface
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService, int $broker_rut): ResponseInterface
{
try {
$broker = $brokerService->get($broker_rut);

View File

@ -1,6 +1,8 @@
<?php
namespace Incoviba\Controller\API\Proyectos\Brokers;
use DateTimeImmutable;
use DateMalformedStringException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Controller\API\withJson;
@ -17,6 +19,21 @@ class Contracts
return $this->withJson($response, compact('contracts'));
}
public function getByBroker(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker\Contract $contractService,
Service\Proyecto\Broker $brokerService, int $broker_rut): ResponseInterface
{
$output = [
'broker_rut' => $broker_rut,
'contracts' => []
];
try {
$broker = $brokerService->get($broker_rut);
$output['contracts'] = $contractService->getByBroker($broker->rut);
} catch (ServiceAction\Read $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker\Contract $contractService, int $contract_id): ResponseInterface
{
$output = [
@ -82,6 +99,31 @@ class Contracts
return $this->withJson($response, $output);
}
public function inactive(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker\Contract $contractService, int $contract_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'contract_id' => $contract_id,
'input' => $input,
'contract' => null,
'success' => false
];
try {
$contract = $contractService->getById($contract_id);
$date = new DateTimeImmutable();
if (!empty($input['date'])) {
try {
$date = new DateTimeImmutable($input['date']);
} catch (DateMalformedStringException) {}
}
$output['contract'] = $contractService->inactive($contract, $date);
$output['success'] = true;
} catch (ServiceAction\Read | ServiceAction\Update $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
public function delete(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker\Contract $contractService, int $contract_id): ResponseInterface
{
$output = [