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

154 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-05-03 21:20:58 +00:00
import e from "express";
import { bool, number } from "yup";
import { getIO } from "../libs/socket";
import Queue from "../models/Queue";
import ListUserParamiterService from "../services/UserServices/ListUserParamiterService";
import createOrUpdateOnlineUserService from "../services/UserServices/CreateOrUpdateOnlineUserService";
import { splitDateTime } from "../helpers/SplitDateTime";
import format from 'date-fns/format';
import ptBR from 'date-fns/locale/pt-BR';
import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService";
import { check } from "prettier";
let whoIsOnline_monitor: any;
// const listUserId:any[] = [{'id':8, status: 'offline'},{'id':3, status: 'offline'},{'id':5, status: 'offline'}]
let listUserId: any[] = []
let count = 0
let countTest = 0
let uuid: any = 0
let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR })))
let checked: boolean = false
let count2 = 0
const setOfflineAllUsers = async () => {
const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate, status: 'online' })
console.log(' onlineUsers: ', onlineUsers)
if (onlineUsers.length > 0) {
onlineUsers.forEach(async user => {
await createOrUpdateOnlineUserService({ userId: user.userId, status: 'offline' })
});
}
}
const monitor = async () => {
const test = require('./../libs/socket');
console.log(' TEST OBJ', test.ob)
//test del
if(countTest > 5){
countTest = 0
}
if (countTest == 0) {
//console.log('NEW USERS QUERY EMITER!')
listUserId = await ListUserParamiterService({ profile: 'user' })
2022-05-03 21:20:58 +00:00
//console.log('----------- listUserId: ', listUserId)
2022-05-03 21:20:58 +00:00
}
2022-05-03 21:20:58 +00:00
countTest++
//
if(listUserId.length == 0){
return;
}
listUserId.forEach((u) => {
const io = getIO();
io.emit("isOnline", { action: "online", userId: u.id });
});
if(test.ob){
if (count > 1) {
count = 0
}
if (count == 0) {
// uuid = test.uuid
uuid = test.ob.uuid
}
if (count == 1) {
if (uuid) {
if (uuid == test.ob.uuid) {
console.log('ALL USERS CLIENTS OFFLINE 1...........')
await setOfflineAllUsers()
test.ob.listOnline = []
test.ob.listOnline2 = []
test.ob.uuid = undefined
checked = true
}
else {
console.log('*usersOnline: ', test.ob.listOnline)
checked = false
count2 = 0
}
}
else {
console.log('ALL USERS CLIENTS OFFLINE 2...........')
if(!checked){
await setOfflineAllUsers()
checked = true
}
}
}
2022-05-03 21:20:58 +00:00
console.log(' | count: ',count, ' | test.ob.listOnline: ', test.ob.listOnline)
count++
}
else {
console.log('ALL USERS CLIENTS OFFLINE 3...........')
if(!checked && count2>10){
await setOfflineAllUsers()
checked = true
}
if(count2 < 12){
count2++
}
console.log('>>>>>>>>>>>>>>>>>>> count2: ', count2)
}
2022-05-03 21:20:58 +00:00
};
export const startWhoIsOnlinegMonitor = async (mileseconds: number) => {
2022-05-03 21:20:58 +00:00
whoIsOnline_monitor = setInterval(monitor, mileseconds)
}
export const stopWhoIsOnlineMonitor = async () => {
2022-05-03 21:20:58 +00:00
clearInterval(whoIsOnline_monitor)
}