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

@ -1,12 +1,27 @@
<?php
use Incoviba\Controller\API\Proyectos\Brokers;
use Incoviba\Controller\API\Proyectos\Brokers\Contracts;
$app->group('/brokers', function($app) {
$app->post('/add[/]', Brokers::class . ':add');
$app->post('/edit[/]', Brokers::class . ':edit');
$app->group('/contracts', function($app) {
$app->post('/add[/]', [Contracts::class, 'add']);
$app->get('[/]', Contracts::class);
});
$app->group('/contract/{contract_id}', function($app) {
$app->post('/edit[/]', [Contracts::class, 'edit']);
$app->post('/inactive[/]', [Contracts::class, 'inactive']);
$app->delete('[/]', [Contracts::class, 'delete']);
$app->get('[/]', [Contracts::class, 'get']);
});
$app->post('/add[/]', [Brokers::class, 'add']);
$app->post('/edit[/]', [Brokers::class, 'edit']);
$app->get('[/]', Brokers::class);
});
$app->group('/broker/{broker_rut}', function($app) {
$app->delete('[/]', Brokers::class . ':delete');
$app->get('[/]', Brokers::class . ':show');
$app->group('/contracts', function($app) {
$app->post('/add[/]', [Contracts::class, 'add']);
$app->get('[/]', [Contracts::class, 'getByBroker']);
});
$app->delete('[/]', [Brokers::class, 'delete']);
$app->get('[/]', [Brokers::class, 'get']);
});

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 = [

View File

@ -1,7 +1,9 @@
<?php
namespace Incoviba\Service\Proyecto\Broker;
use Incoviba\Exception\ServiceAction\Update;
use DateTimeInterface;
use DateTimeImmutable;
use DateMalformedStringException;
use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
@ -9,10 +11,12 @@ use Incoviba\Common\Implement;
use Incoviba\Exception\ServiceAction;
use Incoviba\Model;
use Incoviba\Repository;
class Contract extends Ideal\Service
{
public function __construct(LoggerInterface $logger,
protected Repository\Proyecto\Broker\Contract $contractRepository)
protected Repository\Proyecto\Broker\Contract $contractRepository,
protected Repository\Proyecto\Broker\Contract\State $stateRepository)
{
parent::__construct($logger);
}
@ -20,11 +24,19 @@ class Contract extends Ideal\Service
public function getAll(): array
{
try {
return $this->contractRepository->fetchAll();
return array_map([$this, 'process'], $this->contractRepository->fetchAll());
} catch (Implement\Exception\EmptyResult) {
return [];
}
}
public function getByBroker(int $broker_rut): array
{
try {
return array_map([$this, 'broker'], $this->contractRepository->fetchByBroker($broker_rut));
} catch (Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Read(__CLASS__, $exception);
}
}
/**
* @throws ServiceAction\Read
@ -32,7 +44,7 @@ class Contract extends Ideal\Service
public function getById(int $id): Model\Proyecto\Broker\Contract
{
try {
return $this->contractRepository->fetchById($id);
return $this->process($this->contractRepository->fetchById($id));
} catch (Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Read(__CLASS__, $exception);
}
@ -44,26 +56,36 @@ class Contract extends Ideal\Service
public function add(array $data): Model\Proyecto\Broker\Contract
{
try {
return $this->contractRepository->fetchActiveByProjectAndBroker($data['proyecto_id'], $data['broker_rut']);
return $this->process($this->contractRepository->fetchActiveByProjectAndBroker($data['proyecto_id'], $data['broker_rut']));
} catch (Implement\Exception\EmptyResult) {}
try {
$filteredData = $this->contractRepository->filterData($data);
$contract = $this->contractRepository->create($filteredData);
return $this->contractRepository->save($contract);
$contract = $this->contractRepository->save($contract);
$type = Model\Proyecto\Broker\Contract\State\Type::ACTIVE;
$date = new DateTimeImmutable();
if (isset($data['date'])) {
try {
$date = new DateTimeImmutable($data['date']);
} catch (DateMalformedStringException) {}
}
$state = $this->stateRepository->create(['contract_id' => $contract->id, 'date' => $date, 'type' => $type]);
$this->stateRepository->save($state);
return $this->process($contract);
} catch (Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Create(__CLASS__, $exception);
}
}
/**
* @throws Update
* @throws ServiceAction\Update
*/
public function edit(Model\Proyecto\Broker\Contract $contract, array $data): Model\Proyecto\Broker\Contract
{
try {
$filteredData = $this->contractRepository->filterData($data);
return $this->contractRepository->edit($contract, $filteredData);
return $this->process($this->contractRepository->edit($contract, $filteredData));
} catch (PDOException | Implement\Exception\EmptyResult) {
throw new ServiceAction\Update(__CLASS__);
}
@ -82,4 +104,30 @@ class Contract extends Ideal\Service
throw new ServiceAction\Delete(__CLASS__, $exception);
}
}
/**
* @throws ServiceAction\Update
*/
public function inactive(Model\Proyecto\Broker\Contract $contract, DateTimeInterface $date = new DateTimeImmutable()): Model\Proyecto\Broker\Contract
{
try {
$type = Model\Proyecto\Broker\Contract\State\Type::INACTIVE;
$state = $this->stateRepository->create(['contract_id' => $contract->id, 'date' => $date, 'type' => $type]);
$this->stateRepository->save($state);
return $this->process($contract);
} catch (PDOException | Implement\Exception\EmptyResult $exception) {
throw new ServiceAction\Update(__CLASS__, $exception);
}
}
protected function process(Model\Proyecto\Broker\Contract $contract): Model\Proyecto\Broker\Contract
{
$contract->addFactory('states', (new Implement\Repository\Factory())
->setCallable([$this->stateRepository, 'fetchByContract'])
->setArgs(['contract_id' => $contract->id]));
$contract->addFactory('currentState', (new Implement\Repository\Factory())
->setCallable([$this->stateRepository, 'fetchActiveByContract'])
->setArgs(['contract_id' => $contract->id]));
return $contract;
}
}