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

214 lines
5.4 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";
import { addHours, addMinutes, addSeconds, intervalToDuration, add } from "date-fns";
import { date } from "faker";
import sumOnlineTimeNow from '../helpers/SumOlineTimeNow'
import UserOnlineTime from "../models/UserOnlineTime";
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 timeInterval = 5
let lstOnline: any = []
let deleted: boolean = false;
2022-05-16 04:03:16 +00:00
const emitterOnline = (user: any, status: string, showOnlineTime: boolean = true) => {
if (!user.id || !user.updatedAt || !user.onlineTime) return
let newOnlinetime = sumOnlineTimeNow(user)
let onlineTime: any = {
userId: user.id,
status: status,
onlineTime: newOnlinetime,
}
if (!showOnlineTime) {
onlineTime = {
userId: user.id,
status: status
}
}
2022-05-03 21:20:58 +00:00
const io = getIO();
io.emit("onlineStatus", {
action: "update",
userOnlineTime: onlineTime
});
}
const monitor = async () => {
2022-05-16 04:03:16 +00:00
try {
2022-05-16 04:03:16 +00:00
const usersSocket = require('./../libs/socket');
2022-05-16 04:03:16 +00:00
if (usersSocket.ob) {
if (count > 1) {
count = 0
}
2022-05-16 04:03:16 +00:00
if (count == 0) {
uuid = usersSocket.ob.uuid
}
if (count == 1) {
if (uuid) {
if (uuid == usersSocket.ob.uuid) {
console.log('ALL USERS CLIENTS OFFLINE 1...........')
usersSocket.ob.listOnline = []
usersSocket.ob.listOnlineAux = []
usersSocket.ob.uuid = undefined
}
2022-05-16 04:03:16 +00:00
}
2022-05-16 04:03:16 +00:00
} else {
usersSocket.ob.listOnline.forEach(async (el: any) => {
const indexAux = lstOnline.findIndex((e: any) => e.id == el.id)
if (indexAux == -1) {
console.log(' entrou indexAux: ', indexAux)
const userOnline = await createOrUpdateOnlineUserService({ userId: el.id, status: 'online' })
if (userOnline) {
el.onlineTime = userOnline.onlineTime
el.updatedAt = userOnline.updatedAt,
console.log(' * CREATED OR UPDATED USER TO ONLINE: ', userOnline.userId)
lstOnline.push({ 'id': el.id, 'status': 'online' })
}
}
2022-05-16 04:03:16 +00:00
if (el.status == 'waiting...') {
emitterOnline(el, el.status, false)
}
else {
emitterOnline(el, el.status)
}
});
let difference = lstOnline.filter((x: any) => !usersSocket.ob.listOnline.map((e: any) => e.id).includes(x.id));
difference.forEach(async (e: any) => {
const index = lstOnline.findIndex((x: any) => x.id == e.id)
if (index != -1) {
lstOnline.splice(index, 1)
const userOnline = await createOrUpdateOnlineUserService({ userId: e.id, status: 'offline' })
}
})
}
count++
}
2022-05-16 04:03:16 +00:00
if (countTest > 5) {
countTest = 0
}
2022-05-16 04:03:16 +00:00
if (countTest == 0) {
try {
2022-06-29 00:34:43 +00:00
console.log(' Carregando usuárion no listUserId...')
2022-05-16 04:03:16 +00:00
listUserId = await ListUserParamiterService({ profile: 'user' })
} catch (error) {
console.log('There was an erro on ListUserParamiterService: ', error)
return
}
}
countTest = 1
listUserId.forEach((u) => {
const io = getIO();
io.emit("isOnline", { action: "online", userId: u.id });
});
} catch (error) {
console.log('>>> There was an error no WhoIsOnlineMonitor.ts: ',error)
stopWhoIsOnlineMonitor()
startWhoIsOnlineMonitor()
}
2022-05-03 21:20:58 +00:00
};
export const startWhoIsOnlineMonitor = async (mileseconds?: number) => {
listUserId = []
count = 0
countTest = 0
uuid = 0
if (mileseconds) {
timeInterval = mileseconds
}
2022-05-03 21:20:58 +00:00
whoIsOnline_monitor = setInterval(monitor, timeInterval)
2022-05-03 21:20:58 +00:00
}
export const stopWhoIsOnlineMonitor = async () => {
2022-05-03 21:20:58 +00:00
clearInterval(whoIsOnline_monitor)
}