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) {
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");
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)
fs.writeFileSync(
botInfoFile,
JSON.stringify({ isOnQueue: false, botQueueId: 0, userIdBot: 0 }),
"utf8"
);
}
}
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 }
}
return { userIdBot: userIdBot, botQueueId: queueId, isOnQueue: botIsOnQueue };
};
export default _botIsOnQueue;

View File

@ -3,51 +3,52 @@ 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 })
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
let tickets: any = await ListTicketTimeLife({
timeseconds: 60,
status: "open",
userId: botInfo.userIdBot
});
}
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) {
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);

View File

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