diff --git a/app/models/Audita.php b/app/Repositories/Audita.php similarity index 100% rename from app/models/Audita.php rename to app/Repositories/Audita.php diff --git a/app/models/AuditaFull.php b/app/Repositories/AuditaFull.php similarity index 100% rename from app/models/AuditaFull.php rename to app/Repositories/AuditaFull.php diff --git a/app/Repositories/Bilhetes.php b/app/Repositories/Bilhetes.php new file mode 100644 index 00000000..1c47e544 --- /dev/null +++ b/app/Repositories/Bilhetes.php @@ -0,0 +1,48 @@ + 'Transferred Call' "; + + foreach ($data as $k => $v) { + if (!$v) { + continue; + } + + if (in_array($k, ['src', 'dst', 'entry'])) { + $query .= " AND $k LIKE '%:$k%'"; + continue; + } + + if ($k === 'i_date') { + $query .= " AND data_bilhete >= :$k"; + continue; + } + + if ($k === 'f_date') { + $query .= " AND data_bilhete <= :$k"; + continue; + } + $query .= " AND $k = :$k"; + } + + $query .= " ORDER BY data_bilhete"; + return self::query($query, $data); + } +} diff --git a/app/models/Meet.php b/app/Repositories/Meet.php similarity index 100% rename from app/models/Meet.php rename to app/Repositories/Meet.php diff --git a/app/models/MeetPeople.php b/app/Repositories/MeetPeople.php similarity index 100% rename from app/models/MeetPeople.php rename to app/Repositories/MeetPeople.php diff --git a/app/models/Organizacao.php b/app/Repositories/Organizacao.php similarity index 100% rename from app/models/Organizacao.php rename to app/Repositories/Organizacao.php diff --git a/app/models/OrganizacaoUsuario.php b/app/Repositories/OrganizacaoUsuario.php similarity index 100% rename from app/models/OrganizacaoUsuario.php rename to app/Repositories/OrganizacaoUsuario.php diff --git a/app/models/Parametro.php b/app/Repositories/Parametro.php similarity index 100% rename from app/models/Parametro.php rename to app/Repositories/Parametro.php diff --git a/app/models/Queue.php b/app/Repositories/Queue.php similarity index 100% rename from app/models/Queue.php rename to app/Repositories/Queue.php diff --git a/app/models/Token.php b/app/Repositories/Token.php similarity index 100% rename from app/models/Token.php rename to app/Repositories/Token.php diff --git a/app/models/Usuario.php b/app/Repositories/Usuario.php similarity index 100% rename from app/models/Usuario.php rename to app/Repositories/Usuario.php diff --git a/app/controllers/CallController.php b/app/controllers/CallController.php index 40cd2217..683a8986 100644 --- a/app/controllers/CallController.php +++ b/app/controllers/CallController.php @@ -2,16 +2,17 @@ namespace app\controllers; -use app\models\Bilhetes; -use app\traits\Validate; use Slim\Routing\RouteCollectorProxy; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; +use Valitron\Validator; use Exception; +use app\Repositories\Bilhetes; +use app\traits\Validate; + class CallController { - use Validate; static function route() { return function (RouteCollectorProxy $group) { @@ -23,10 +24,26 @@ class CallController function listarBilhetes(Request $request, Response $response, array $args) { try { + $validator = new Validator(); + $validator->mapFieldsRules([ + 'id' => ['integer', ['min', 1]], + 'uniqueid' => ['string'], + 'src' => ['string'], + 'dst' => ['string'], + 'i_date' => ['date'], + 'f_date' => ['date'], + 'entry' => ['string'] + ]); $body = json_decode($request->getBody()->getContents(), true); - $dados = $this->validateData($request, true); + $validator = $validator->withData($body); + if (!$validator->validate()) { + $response->getBody() + ->write(json_encode($validator->errors())); + return $response->withStatus(422); + } + $result = Bilhetes::getBilhetes($body); - $query = "SELECT + /*$query = "SELECT a.id_bilhetes AS id, a.calldate AS data_hora, a.src AS origem, a.dst AS destino, a.billsec AS tempo_conversacao, a.duration AS tempo_atendimento, a.accountcode AS id_transfer, @@ -36,21 +53,22 @@ class CallController FROM pbx_bilhetes a WHERE a.lastapp <> 'Transferred Call' "; - $dados['org_id'] = $body['org_id']; - foreach ($dados as $k => $v) { + + foreach ($body as $k => $v) { if ($v) { $query .= " AND $k = :$k"; } } - $data = Bilhetes::query($query, $dados); - if (!$data) { + $result = Bilhetes::query($query, $data); */ + if (!$result) { $response->getBody()->write(json_encode(['status' => false, 'data' => ['message' => 'Nenhum resultado encontrado!']])); } else { - $response->getBody()->write(json_encode(['status' => true, 'data' => $data])); + $response->getBody()->write(json_encode(['status' => true, 'data' => $result])); } } catch (Exception $e) { $response->getBody()->write(json_encode(['status' => false, 'data' => ["message" => "Nao foi possivel realizar a consulta! " . $e->getMessage()]])); + return $response->withStatus(500); } return $response; } diff --git a/app/core/Connection.php b/app/core/Connection.php index c9b06f9f..8b467a6a 100644 --- a/app/core/Connection.php +++ b/app/core/Connection.php @@ -7,7 +7,7 @@ use PDO; class Connection { private static PDO $instance; - public static function getInstance() + public static function getInstance(): PDO|NULL { $cd = []; if (empty(self::$instance)) { diff --git a/app/core/Repository.php b/app/core/Repository.php index 812f2640..f62591a9 100644 --- a/app/core/Repository.php +++ b/app/core/Repository.php @@ -200,119 +200,4 @@ abstract class Repository } return 0; } - - - // function create(array $params) - // { - // $dados = []; - // try { - // $table = $this->table; - // $query = "INSERT INTO $table ("; - // foreach ($params as $key => $value) { - // $dados[$key] = $value; - // if (array_key_last($params) == $key) { - // $query .= " $key )"; - // } else { - // $query .= " $key, "; - // } - // } - // $query .= " VALUES( "; - // foreach ($params as $key => $value) { - // if (array_key_last($params) == $key) { - // $query .= " :$key );"; - // } else { - // $query .= " :$key, "; - // } - // } - // return $this->db->create($query, $dados); - // } catch (Exception $e) { - // } - // } - - // function list(array $params = []) - // { - // try { - // $table = $this->table; - // $query = "SELECT * FROM $table"; - // $data = $this->db->read($query, $params); - // if (!$data) { - // return []; - // } - // return $data->fetchAll(); - // } catch (Exception $e) { - // } - // } - - // function update(array $params, $where) - // { - // try { - // $dados = array_filter($params); - // $table = $this->table; - // $query = "UPDATE $table SET "; - // foreach ($dados as $key => $value) { - // if (array_key_last($dados) == $key) { - // $query .= " $key = :$key"; - // } else { - // $query .= " $key = :$key, "; - // } - // } - // $query .= " WHERE 1 = 1 "; - // if (empty($where)) { - // throw new Exception("Parâmetro (where) é obrigatório! [Table: $table] where: ['column' => 'value']"); - // } - - // foreach ($where as $column => $value) { - // $query .= "AND $column = :$column"; - // $dados[$column] = $value; - // } - - // return $this->db->update($query, $dados); - // } catch (Exception $e) { - // } - // } - - // function delete(array $where): int - // { - // try { - // $table = $this->table; - // $dados = []; - // $query = "DELETE FROM $table WHERE 1 = 1"; - // if (empty($where)) { - // throw new Exception("Parâmetro (where) é obrigatório! [Table: $table] where: ['column' => 'value']"); - // } - - // foreach ($where as $column => $value) { - // $query .= "AND $column = :$column"; - // $dados[$column] = $value; - // } - - // return $this->db->delete($query, $dados); - // } catch (Exception $e) { - // } - // return 0; - // } - - // function get(array $params = null, $limit = 100) - // { - // try { - // $table = $this->table; - // $dados = []; - // $query = "SELECT * FROM $table WHERE 1=1 "; - // if (empty($params)) { - // $query .= " ORDER BY 1 LIMIT $limit"; - // } else { - // foreach ($params as $column => $value) { - // $query .= "AND $column = :$column"; - // $dados[$column] = $value; - // } - // } - - // $data = $this->db->read($query, $dados); - // if (!$data) { - // return []; - // } - // return $data->fetch(); - // } catch (Exception $e) { - // } - // } } diff --git a/app/models/Bilhetes.php b/app/models/Bilhetes.php deleted file mode 100644 index 5a230a23..00000000 --- a/app/models/Bilhetes.php +++ /dev/null @@ -1,10 +0,0 @@ -getBody()->getContents(), true); $parametro = Parametro::find(['org_id' => $body['org_id']], ['prm_max_dias_relatorio']); - $datas = []; - - $filters = [ - 'id', 'uniqueid', 'origem', 'destino', - 'fila', 'org_id', 'data_inicial', - 'data_final', 'agente', 'apelido', - 'nome', 'ramal', 'evento' - ]; + $dates = []; + + if (empty($filters)) { + $filters = [ + 'id', 'uniqueid', 'origem', 'destino', + 'fila', 'org_id', 'data_inicial', + 'data_final', 'agente', 'apelido', + 'nome', 'ramal', 'evento' + ]; + } foreach ($body as $k => $v) { if (!in_array($k, $filters)) { @@ -28,9 +29,9 @@ trait Validate } } - if ($data) { - $datas['data_inicial'] = $body['data_inicial'] ?? date('Y-m-d H:i:s'); - $datas['data_final'] = $body['data_final'] ?? date('Y-m-d H:i:s'); + if ($date) { + $dates['data_inicial'] = $body['data_inicial'] ?? date('Y-m-d H:i:s'); + $dates['data_final'] = $body['data_final'] ?? date('Y-m-d H:i:s'); if (($body['data_inicial'] && $body['data_final']) && strtotime($body['data_inicial']) > strtotime($body['data_final'])) { throw new Exception('A [data_inicial] nao pode ser maior que [data_final]!'); @@ -41,11 +42,11 @@ trait Validate } if ($parametro->prm_max_dias_relatorio) { - if (strtotime($datas['data_inicial']) > strtotime($body['data_inicial'] . "+" . $parametro->prm_max_dias_relatorio . "days")) { + if (strtotime($dates['data_inicial']) > strtotime($body['data_inicial'] . "+" . $parametro->prm_max_dias_relatorio . "days")) { throw new Exception("O periodo nao pode ultrapassar " . $parametro->prm_max_dias_relatorio . " dias!"); } } } - return $datas; + return $dates; } } diff --git a/composer.json b/composer.json index 9561ded6..cad2b746 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "slim/slim": "4.*", "slim/psr7": "^1.6", "tuupola/cors-middleware": "^1.3", - "firebase/php-jwt": "^6.3" + "firebase/php-jwt": "^6.3", + "vlucas/valitron": "^1.4" }, "config": { "optimize-autoloader": false, diff --git a/composer.lock b/composer.lock index 0ebf0709..0ee39231 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fdbb6dffe913987160bb9dd2cbde94e0", + "content-hash": "1dd4e4d801720ff263feb9d481a91eee", "packages": [ { "name": "fig/http-message-util", @@ -1081,6 +1081,65 @@ } ], "time": "2021-09-14T12:46:25+00:00" + }, + { + "name": "vlucas/valitron", + "version": "v1.4.11", + "source": { + "type": "git", + "url": "https://github.com/vlucas/valitron.git", + "reference": "fadce39f5f235755bb9794b2573af2d5bfcba85f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/vlucas/valitron/zipball/fadce39f5f235755bb9794b2573af2d5bfcba85f", + "reference": "fadce39f5f235755bb9794b2573af2d5bfcba85f", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "require-dev": { + "phpunit/phpunit": ">=4.8.35" + }, + "suggest": { + "ext-mbstring": "It can support the multiple bytes string length." + }, + "type": "library", + "autoload": { + "psr-4": { + "Valitron\\": "src/Valitron" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Vance Lucas", + "email": "vance@vancelucas.com", + "homepage": "https://www.vancelucas.com" + } + ], + "description": "Simple, elegant, stand-alone validation library with NO dependencies", + "homepage": "https://github.com/vlucas/valitron", + "keywords": [ + "valid", + "validation", + "validator" + ], + "support": { + "issues": "https://github.com/vlucas/valitron/issues", + "source": "https://github.com/vlucas/valitron/tree/v1.4.11" + }, + "funding": [ + { + "url": "https://tidelift.com/funding/github/packagist/vlucas/valitron", + "type": "tidelift" + } + ], + "time": "2022-10-14T11:54:24+00:00" } ], "packages-dev": [],