Browse Source

Implementei metodo de listar chamadas por evento

1.9
parent
commit
73a18ef4fa
  1. 92
      app/Repositories/Bilhetes.php
  2. 71
      app/controllers/CallController.php
  3. 5
      app/core/Repository.php
  4. 11
      app/traits/AuthToken.php

92
app/Repositories/Bilhetes.php

@ -11,38 +11,108 @@ class Bilhetes extends Repository
public static function getBilhetes(array $data): array
{
$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,
a.uniqueid AS uniqueid, a.userfield AS nome_audio,
a.data_bilhete AS data, a.fora_horario AS fora_horario,
a.org_id
a.id_bilhetes AS id, a.calldate AS data_hora, SPLIT_PART(a.clid, ' ', 1) AS entrada,
a.src, a.dst, a.billsec AS tempo_conversacao,
a.duration AS tempo_atendimento, a.accountcode AS id_transfer, a.uniqueid AS uniqueid,
a.userfield AS nome_audio, a.fora_horario AS fora_horario,
a.org_id
FROM pbx_bilhetes a
WHERE a.lastapp <> 'Transferred Call' ";
// mountig the 'WHERE' statement
foreach ($data as $k => $v) {
if (!$v) {
continue;
}
if (in_array($k, ['src', 'dst', 'entry'])) {
$query .= " AND $k LIKE '%:$k%'";
if (in_array($k, ['src', 'dst'])) {
$query .= " AND $k LIKE :$k";
continue;
}
if ($k === 'entrada') {
$query .= " AND a.clid LIKE :$k";
continue;
}
if ($k === 'data_i') {
$query .= " AND calldate >= :$k";
continue;
}
if ($k === 'data_f') {
$query .= " AND calldate <= :$k";
continue;
}
if ($k === 'i_date') {
$query .= " AND $k = :$k";
}
$query .= " ORDER BY calldate";
return self::query($query, $data, 'all');
}
public static function getEventosBilhetes(array $data): array
{
$query = "SELECT
a.id_bilhetes AS id,
a.src,
a.dst,
a.uniqueid,
SPLIT_PART(a.clid, ' ', 1) AS entrada,
c.id AS fila_id,
b.fila,
d.nome,
d.apelido AS login,
SUBSTRING(b.agente,7,4) AS matricula,
b.evento AS evento,
b.param1 AS param1,
b.param2 AS param2,
b.param3 AS param3,
b.param4 AS param4
FROM pbx_bilhetes a
INNER JOIN pbx_eventos_dacs b ON a.uniqueid = b.uid2
INNER JOIN pbx_dacs c ON c.nome = b.fila
INNER JOIN pbx_usuarios d ON d.matricula = SUBSTRING(b.agente,7,4)
WHERE evento IN ('ABANDON','COMPLETEAGENT','COMPLETECALLER','CONNECT','ENTERQUEUE',
'EXITWITHTIMEOUT', 'TRANSBORDANDO', 'TRANSBORDADO','TRANSFER', 'TRANSFERORIG',
'COMPLETACALLER', 'COMPLETAAGENT', 'ANSWERED', 'BUSYS', 'NOANSWERS') ";
foreach ($data as $k => $v) {
if (!$v) {
continue;
}
if (in_array($k, ['src', 'dst', 'fila', 'evento'])) {
$query .= " AND $k LIKE :$k";
continue;
}
if ($k === 'entrada') {
$query .= " AND a.clid LIKE :$k";
continue;
}
if ($k === 'data_i') {
$query .= " AND data_bilhete >= :$k";
continue;
}
if ($k === 'f_date') {
if ($k === 'data_f') {
$query .= " AND data_bilhete <= :$k";
continue;
}
if ($k === 'org_id') {
$query .= " AND a.org_id = :$k";
continue;
}
$query .= " AND $k = :$k";
}
$query .= " ORDER BY data_bilhete";
return self::query($query, $data, 'all');
}
}

71
app/controllers/CallController.php

@ -30,7 +30,7 @@ class CallController
function listarBilhetes(Request $request, Response $response, array $args)
{
self::$logger->debug('Org id do token: ' . print_r($request->getAttribute('orgs'), true), true);
//self::$logger->debug('Org id do token: ' . print_r($request->getAttribute('orgs'), true), true);
try {
$validator = new Validator();
$validator->mapFieldsRules([
@ -38,9 +38,9 @@ class CallController
'uniqueid' => [],
'src' => [],
'dst' => [],
'i_date' => ['date'],
'f_date' => ['date'],
'entry' => [],
'data_i' => ['date'],
'data_f' => ['date'],
'entrada' => [],
'org_id' => ['integer', 'required']
]);
@ -52,17 +52,10 @@ class CallController
return $response->withStatus(422);
}
self::$logger->debug('Passed in bilhetes validation', true);
self::$logger->debug('Passed in bilhetes validation with body: ' . print_r($body, true), true);
$result = Bilhetes::getBilhetes($body);
$response->getBody()->write(json_encode(['status' => true, 'data' => $result]));
//this block returns an error message if the result of the query is empty
/*if (!$result) {
$response->getBody()->write(json_encode(['status' => false, 'data' => ['message' => 'Nenhum resultado encontrado!']]));
} else {
$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);
@ -73,42 +66,30 @@ class CallController
public function listarEventos(Request $request, Response $response, array $args)
{
try {
$body = json_decode($request->getBody()->getContents(), true);
//$dados = $this->validateData($request, true);
$query = "SELECT
a.id_bilhetes AS id,
a.uniqueid,
c.id AS fila_id,
b.fila,
d.nome,
d.apelido AS login,
SUBSTRING(b.agente,7,4) AS matricula,
b.evento AS evento,
b.param1 AS param1,
b.param2 AS param2,
b.param3 AS param3,
b.param4 AS param4
FROM pbx_bilhetes a
INNER JOIN pbx_eventos_dacs b ON a.uniqueid = b.uid2
INNER JOIN pbx_dacs c ON c.nome = b.fila
INNER JOIN pbx_usuarios d ON d.matricula = SUBSTRING(b.agente,7,4)
WHERE evento IN ('ABANDON','COMPLETEAGENT','COMPLETECALLER','CONNECT','ENTERQUEUE',
'EXITWITHTIMEOUT', 'TRANSBORDANDO', 'TRANSBORDADO','TRANSFER', 'TRANSFERORIG',
'COMPLETACALLER', 'COMPLETAAGENT', 'ANSWERED', 'BUSYS', 'NOANSWERS') ";
$validator = new Validator();
$validator->mapFieldsRules([
'id' => ['integer', ['min', 1]],
'fila' => [],
'entrada' => [],
'uniqueid' => [],
'src' => [],
'dst' => [],
'data_i' => ['date'],
'data_f' => ['date'],
'org_id' => ['integer', 'required']
]);
$dados['org_id'] = $body['org_id'];
$data = Bilhetes::query($query, $dados);
foreach ($dados as $k => $v) {
if ($v) {
$query .= " AND $k = :$k";
}
$body = json_decode($request->getBody()->getContents(), true);
$validator = $validator->withData($body);
if (!$validator->validate()) {
$response->getBody()
->write(json_encode($validator->errors()));
return $response->withStatus(422);
}
if (!$data) {
$response->getBody()->write(json_encode(['status' => false, 'data' => ['message' => 'Nenhum resultado encontrado!']]));
} else {
$response->getBody()->write(json_encode(['status' => true, 'data' => $data]));
}
self::$logger->debug('Passed in bilhetes validation with body: ' . print_r($body, true), true);
$result = Bilhetes::getEventosBilhetes($body);
$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()]]));
}

5
app/core/Repository.php

@ -32,6 +32,11 @@ abstract class Repository
$stmt = Connection::getInstance()->prepare($query);
self::$logger->debug("Biding values for $query\nwith params: " . print_r($params, true), true);
foreach ($params as $key => $value) {
// menaging LIKE statement in query
if (in_array($key, ['src', 'dst', 'entrada', 'fila', 'evento'])) {
$value = "%$value%";
}
$stmt->bindValue(":$key", $value, (is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR));
}
$stmt->execute();

11
app/traits/AuthToken.php

@ -32,17 +32,6 @@ trait AuthToken
$tokenEmpresas = Usuario::verifyTokenByOrg($bearerToken, $body['org_id']);
self::$logger->debug('tokenEmpresas in AuthToken: ' . print_r($tokenEmpresas, true), true);
//arrumar essa parte
/* $tokenEmpresas = Token::query(
"SELECT pu.id as user_id, t.id as token_id, pou.id_organizacao, t.token, t.expired_at
FROM pbx_usuarios pu
INNER JOIN pbx_tokens t ON t.id_usuario = pu.id
INNER JOIN pbx_organizacao_usuarios pou ON pou.id_usuario = pu.id
WHERE t.token = :token AND id_organizacao = :org_id;",
["token" => $bearerToken, 'org_id' => $body['org_id']]
); */
if (empty($tokenEmpresas)) {
throw new Exception('Requisicao nao autorizada, por favor verifique o token e a permissao de acesso!');
}

Loading…
Cancel
Save