Ajustes para configurar ticket com bot

pull/20/head
adriano 2023-08-09 11:03:28 -03:00
parent 1cda5bc25e
commit d670d879e5
4 changed files with 90 additions and 70 deletions

View File

@ -57,7 +57,7 @@ const AutoCloseTickets = async () => {
} }
} }
} catch (error) { } 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);
} }
}; };

View File

@ -1,69 +1,88 @@
const fsPromises = require("fs/promises"); const fsPromises = require("fs/promises");
import dir from 'path'; import dir from "path";
import fs from 'fs'; import fs from "fs";
import os from 'os'; 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) => { 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`); const botInfoFile = dir.join(os.tmpdir(), `botInfo.json`);
console.log('The bot botInfoFile: ', botInfoFile) console.log("The bot botInfoFile: ", botInfoFile);
try { try {
if (fs.existsSync(botInfoFile)) { if (fs.existsSync(botInfoFile)) {
console.log("botInfo.json file exists");
console.log('botInfo.json file exists'); const botInfo = fs.readFileSync(botInfoFile, {
encoding: "utf8",
const botInfo = fs.readFileSync(botInfoFile, {encoding:'utf8', flag:'r'}); flag: "r"
});
return JSON.parse(botInfo)
return JSON.parse(botInfo);
} else { } else {
console.log('botInfo.json file not found!'); console.log("botInfo.json file not found!");
} }
} catch (error) { } 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({
const { users, count, hasMore } = await ListUsersService({ searchParam: `${botName}`, pageNumber: 1 }); searchParam: `${botName}`,
let botIsOnQueue = false pageNumber: 1
let userIdBot = null });
let queueId = null let botIsOnQueue = false;
let userIdBot = null;
let queueId = null;
if (users.length > 0) { if (users.length > 0) {
try { try {
console.log(
console.log('----------------- bot queue id: ', Object(users)[0]["queues"][0].id) "----------------- bot queue id: ",
Object(users)[0]["queues"][0].id
);
queueId = Object(users)[0]["queues"][0].id; queueId = Object(users)[0]["queues"][0].id;
userIdBot = Object(users)[0].id userIdBot = Object(users)[0].id;
botIsOnQueue = true botIsOnQueue = true;
fs.writeFileSync(botInfoFile, JSON.stringify({ userIdBot: userIdBot, botQueueId: queueId, isOnQueue: botIsOnQueue }), "utf8");
fs.writeFileSync(
botInfoFile,
JSON.stringify({
userIdBot: userIdBot,
botQueueId: queueId,
isOnQueue: botIsOnQueue
}),
"utf8"
);
} catch (err) { } 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) fs.writeFileSync(
botInfoFile,
JSON.stringify({ isOnQueue: false, botQueueId: 0, userIdBot: 0 }),
"utf8"
);
} }
} return { userIdBot: userIdBot, botQueueId: queueId, isOnQueue: botIsOnQueue };
else { };
console.log('Usuário botqueue não existe!')
fs.writeFileSync(botInfoFile, JSON.stringify({ isOnQueue: false, botQueueId: 0, userIdBot: 0 }), "utf8");
}
return { userIdBot: userIdBot, botQueueId: queueId, isOnQueue: botIsOnQueue }
}
export default _botIsOnQueue; export default _botIsOnQueue;

View File

@ -3,51 +3,52 @@ import UpdateTicketService from "../services/TicketServices/UpdateTicketService"
import BotIsOnQueue from "./BotIsOnQueue"; import BotIsOnQueue from "./BotIsOnQueue";
const fsPromises = require("fs/promises"); const fsPromises = require("fs/promises");
const fs = require('fs') const fs = require("fs");
let timer: any let timer: any;
const CloseBotTickets = async () => { const CloseBotTickets = async () => {
try { 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,
console.log('tickets: ', tickets) status: "open",
userId: botInfo.userIdBot
for (let i = 0; i < tickets.length; i++) {
await UpdateTicketService({
ticketData: { 'status': 'closed', 'userId': botInfo.userIdBot, 'statusChatEnd': 'FINALIZADO' },
ticketId: tickets[i].ticket_id
}); });
} console.log("tickets: ", tickets);
for (let i = 0; i < tickets.length; i++) {
await UpdateTicketService({
ticketData: {
status: "closed",
userId: botInfo.userIdBot,
statusChatEnd: "FINALIZADO"
},
ticketId: tickets[i].ticket_id
});
}
} catch (error) { } 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 () => { const schedule = async () => {
try { try {
clearInterval(timer); clearInterval(timer);
await CloseBotTickets() await CloseBotTickets();
} catch (error) { } catch (error) {
console.log('error on schedule: ', error) console.log("error on schedule: ", error);
} } finally {
finally {
timer = setInterval(schedule, 60000); timer = setInterval(schedule, 60000);
} }
} };
timer = setInterval(schedule, 60000); timer = setInterval(schedule, 60000);

View File

@ -105,9 +105,9 @@ const isWeekend = async () => {
// obj.set = true; // obj.set = true;
// obj.msg = weekend.message; // obj.msg = weekend.message;
} }
}
return obj; return obj;
}
}; };
async function isOutBusinessTime() { async function isOutBusinessTime() {