diff --git a/app/resources/routes/api/proyectos/brokers.php b/app/resources/routes/api/proyectos/brokers.php index 48b5da2..b8cb395 100644 --- a/app/resources/routes/api/proyectos/brokers.php +++ b/app/resources/routes/api/proyectos/brokers.php @@ -1,12 +1,27 @@ 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']); }); diff --git a/app/src/Controller/API/Proyectos/Brokers.php b/app/src/Controller/API/Proyectos/Brokers.php index f9633ca..667c77b 100644 --- a/app/src/Controller/API/Proyectos/Brokers.php +++ b/app/src/Controller/API/Proyectos/Brokers.php @@ -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); diff --git a/app/src/Controller/API/Proyectos/Brokers/Contracts.php b/app/src/Controller/API/Proyectos/Brokers/Contracts.php index 248fa11..5e4beb3 100644 --- a/app/src/Controller/API/Proyectos/Brokers/Contracts.php +++ b/app/src/Controller/API/Proyectos/Brokers/Contracts.php @@ -1,6 +1,8 @@ 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 = [ diff --git a/app/src/Service/Proyecto/Broker/Contract.php b/app/src/Service/Proyecto/Broker/Contract.php index 3d34307..24f6bbc 100644 --- a/app/src/Service/Proyecto/Broker/Contract.php +++ b/app/src/Service/Proyecto/Broker/Contract.php @@ -1,7 +1,9 @@ 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; + } }