2023-08-08 19:09:31 +00:00
import { Sequelize } from "sequelize" ;
2022-11-19 22:34:39 +00:00
const dbConfig = require ( "../../config/database" ) ;
2023-08-08 19:09:31 +00:00
const sequelize = new Sequelize ( dbConfig ) ;
const { QueryTypes } = require ( "sequelize" ) ;
2022-11-19 22:34:39 +00:00
import { splitDateTime } from "../../helpers/SplitDateTime" ;
2023-08-08 19:09:31 +00:00
import format from "date-fns/format" ;
2023-09-29 17:10:47 +00:00
import ptBR from "date-fns/locale/es" ;
2023-08-08 19:09:31 +00:00
import BotIsOnQueue from "../../helpers/BotIsOnQueue" ;
interface Request {
timeseconds : string | number ;
status : string ;
userId? : string | number ;
2022-11-19 22:34:39 +00:00
}
2023-08-08 19:09:31 +00:00
const ListTicketTimeLife = async ( {
timeseconds ,
status ,
userId
} : Request ) : Promise < any [ ] > = > {
let tickets = [ ] ;
let currentDate = format ( new Date ( ) , "yyyy-MM-dd HH:mm:ss" , { locale : ptBR } ) ;
// console.log('------------------> currentDate: ', currentDate)
if ( userId ) {
// CONSULTANDO FILAS PELO ID DO USUARIO
tickets = await sequelize . query (
` select user.id as user_id, user.name as user_name, t.id as ticket_id from Tickets as t inner join Users as user on
t . userId = user . id and user . name = 'botqueue' and t . status = '${status}' and ( TIMESTAMPDIFF ( SECOND , t . updatedAt , '${currentDate}' ) ) >= $ { timeseconds } ; ` ,
{ type : QueryTypes . SELECT }
) ;
} else {
const botInfo = await BotIsOnQueue ( "botqueue" ) ;
if ( botInfo . isOnQueue ) {
// CONSULTANDO FILAS PELO USUARIO
tickets = await sequelize . query (
` select id as ticket_id from Tickets where status=' ${ status } ' and userId != ${ botInfo . userIdBot } and (TIMESTAMPDIFF(SECOND, updatedAt, ' ${ currentDate } ')) >= ${ timeseconds } ; ` ,
{ type : QueryTypes . SELECT }
) ;
2022-11-19 22:34:39 +00:00
} else {
2023-08-08 19:09:31 +00:00
// CONSULTANDO FILAS PELO USUARIO
tickets = await sequelize . query (
` select id as ticket_id from Tickets where status=' ${ status } ' and (TIMESTAMPDIFF(SECOND, updatedAt, ' ${ currentDate } ')) >= ${ timeseconds } ; ` ,
{ type : QueryTypes . SELECT }
) ;
2022-11-19 22:34:39 +00:00
}
2023-08-08 19:09:31 +00:00
}
2022-11-19 22:34:39 +00:00
2023-08-08 19:09:31 +00:00
return tickets ;
2022-11-19 22:34:39 +00:00
} ;
export default ListTicketTimeLife ;