diff --git a/backend/src/helpers/AutoCloseTickets.ts b/backend/src/helpers/AutoCloseTickets.ts index eaa7508..d2fc937 100644 --- a/backend/src/helpers/AutoCloseTickets.ts +++ b/backend/src/helpers/AutoCloseTickets.ts @@ -57,7 +57,7 @@ const AutoCloseTickets = async () => { } } } catch (error) { - console.log("There was an error on try close the bot tickets: ", error); + console.log("There was an error on try close the tickets: ", error); } }; diff --git a/backend/src/helpers/BotIsOnQueue.ts b/backend/src/helpers/BotIsOnQueue.ts index 4a531f9..62fbebb 100644 --- a/backend/src/helpers/BotIsOnQueue.ts +++ b/backend/src/helpers/BotIsOnQueue.ts @@ -1,69 +1,88 @@ const fsPromises = require("fs/promises"); -import dir from 'path'; -import fs from 'fs'; -import os from 'os'; +import dir from "path"; +import fs from "fs"; +import os from "os"; -import ListUsersService from "../services/UserServices/ListUsersService" +import ListUsersService from "../services/UserServices/ListUsersService"; +import { isHoliday, isOutBusinessTime, isWeekend } from "./TicketConfig"; const _botIsOnQueue = async (botName: string) => { + // MESSAGE TO HOLIDAY + const holiday: any = await isHoliday(); + // MESSAGES TO SATURDAY OR SUNDAY + const weekend: any = await isWeekend(); + // MESSAGE TO BUSINESS TIME + const businessTime: any = await isOutBusinessTime(); + + if (holiday?.set || weekend?.set || businessTime?.set) { + return { isOnQueue: false }; + } const botInfoFile = dir.join(os.tmpdir(), `botInfo.json`); - console.log('The bot botInfoFile: ', botInfoFile) - + console.log("The bot botInfoFile: ", botInfoFile); try { - if (fs.existsSync(botInfoFile)) { + console.log("botInfo.json file exists"); - console.log('botInfo.json file exists'); - - const botInfo = fs.readFileSync(botInfoFile, {encoding:'utf8', flag:'r'}); - - return JSON.parse(botInfo) - + const botInfo = fs.readFileSync(botInfoFile, { + encoding: "utf8", + flag: "r" + }); + + return JSON.parse(botInfo); } else { - console.log('botInfo.json file not found!'); - } - - + console.log("botInfo.json file not found!"); + } } catch (error) { - console.log('There was an error on try to read the botInfo.json file: ',error) - } - + console.log( + "There was an error on try to read the botInfo.json file: ", + error + ); + } - const { users, count, hasMore } = await ListUsersService({ searchParam: `${botName}`, pageNumber: 1 }); - let botIsOnQueue = false - let userIdBot = null - let queueId = null + const { users, count, hasMore } = await ListUsersService({ + searchParam: `${botName}`, + pageNumber: 1 + }); + let botIsOnQueue = false; + let userIdBot = null; + let queueId = null; if (users.length > 0) { - try { - - console.log('----------------- bot queue id: ', Object(users)[0]["queues"][0].id) + console.log( + "----------------- bot queue id: ", + Object(users)[0]["queues"][0].id + ); queueId = Object(users)[0]["queues"][0].id; - userIdBot = Object(users)[0].id - botIsOnQueue = true - - fs.writeFileSync(botInfoFile, JSON.stringify({ userIdBot: userIdBot, botQueueId: queueId, isOnQueue: botIsOnQueue }), "utf8"); + userIdBot = Object(users)[0].id; + botIsOnQueue = true; + fs.writeFileSync( + botInfoFile, + JSON.stringify({ + userIdBot: userIdBot, + botQueueId: queueId, + isOnQueue: botIsOnQueue + }), + "utf8" + ); } catch (err) { + console.log("O usuário botqueue não está em nenhuma fila err: ", err); + } + } else { + console.log("Usuário botqueue não existe!"); - console.log('O usuário botqueue não está em nenhuma fila err: ', err) - - } - - } - else { - console.log('Usuário botqueue não existe!') - - fs.writeFileSync(botInfoFile, JSON.stringify({ isOnQueue: false, botQueueId: 0, userIdBot: 0 }), "utf8"); - + fs.writeFileSync( + botInfoFile, + JSON.stringify({ isOnQueue: false, botQueueId: 0, userIdBot: 0 }), + "utf8" + ); } - return { userIdBot: userIdBot, botQueueId: queueId, isOnQueue: botIsOnQueue } + return { userIdBot: userIdBot, botQueueId: queueId, isOnQueue: botIsOnQueue }; +}; -} - -export default _botIsOnQueue; \ No newline at end of file +export default _botIsOnQueue; diff --git a/backend/src/helpers/CloseBotTickets.ts b/backend/src/helpers/CloseBotTickets.ts index 43cbe81..b04f160 100644 --- a/backend/src/helpers/CloseBotTickets.ts +++ b/backend/src/helpers/CloseBotTickets.ts @@ -3,52 +3,53 @@ import UpdateTicketService from "../services/TicketServices/UpdateTicketService" import BotIsOnQueue from "./BotIsOnQueue"; const fsPromises = require("fs/promises"); -const fs = require('fs') +const fs = require("fs"); -let timer: any +let timer: any; const CloseBotTickets = async () => { - try { + const botInfo = await BotIsOnQueue("botqueue"); - const botInfo = await BotIsOnQueue('botqueue') + if (!botInfo.isOnQueue) return; - if (!botInfo.userIdBot) return + if (!botInfo.userIdBot) return; - let tickets: any = await ListTicketTimeLife({ timeseconds: 60, status: 'open', userId: botInfo.userIdBot }) + let tickets: any = await ListTicketTimeLife({ + timeseconds: 60, + status: "open", + userId: botInfo.userIdBot + }); - console.log('tickets: ', tickets) + console.log("tickets: ", tickets); for (let i = 0; i < tickets.length; i++) { - await UpdateTicketService({ - ticketData: { 'status': 'closed', 'userId': botInfo.userIdBot, 'statusChatEnd': 'FINALIZADO' }, + ticketData: { + status: "closed", + userId: botInfo.userIdBot, + statusChatEnd: "FINALIZADO" + }, ticketId: tickets[i].ticket_id }); - } - } catch (error) { - console.log('There was an error on try close the bot tickets: ', error) + console.log("There was an error on try close the bot tickets: ", error); } - -} +}; const schedule = async () => { - try { clearInterval(timer); - await CloseBotTickets() - + await CloseBotTickets(); } catch (error) { - console.log('error on schedule: ', error) - } - finally { + console.log("error on schedule: ", error); + } finally { timer = setInterval(schedule, 60000); } -} +}; timer = setInterval(schedule, 60000); -export default schedule; \ No newline at end of file +export default schedule; diff --git a/backend/src/helpers/TicketConfig.ts b/backend/src/helpers/TicketConfig.ts index 830646a..69826a3 100644 --- a/backend/src/helpers/TicketConfig.ts +++ b/backend/src/helpers/TicketConfig.ts @@ -104,10 +104,10 @@ const isWeekend = async () => { else{ // obj.set = true; // obj.msg = weekend.message; - } + } + } return obj; - } }; async function isOutBusinessTime() {