projeto-hit/backend/src/helpers/BotIsOnQueue.ts

70 lines
1.7 KiB
TypeScript

const fsPromises = require("fs/promises");
import dir from 'path';
import fs from 'fs';
import os from 'os';
import ListUsersService from "../services/UserServices/ListUsersService"
const _botIsOnQueue = async (botName: string) => {
const botInfoFile = dir.join(os.tmpdir(), `botInfo.json`);
console.log('The bot botInfoFile: ', botInfoFile)
try {
if (fs.existsSync(botInfoFile)) {
console.log('botInfo.json file exists');
const botInfo = fs.readFileSync(botInfoFile, {encoding:'utf8', flag:'r'});
return JSON.parse(botInfo)
} else {
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('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ')
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)
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");
} 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!')
fs.writeFileSync(botInfoFile, JSON.stringify({ isOnQueue: false, botQueueId: 0, userIdBot: 0 }), "utf8");
}
return { userIdBot: userIdBot, botQueueId: queueId, isOnQueue: botIsOnQueue }
}
export default _botIsOnQueue;