2022-05-03 21:20:58 +00:00
|
|
|
|
2022-05-06 22:49:45 +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";
|
2022-05-16 02:48:06 +00:00
|
|
|
import { addHours, addMinutes, addSeconds, intervalToDuration, add } from "date-fns";
|
|
|
|
import { date } from "faker";
|
|
|
|
|
|
|
|
import sumOnlineTimeNow from '../helpers/SumOlineTimeNow'
|
|
|
|
import UserOnlineTime from "../models/UserOnlineTime";
|
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
let _fifo: any
|
2022-05-06 22:49:45 +00:00
|
|
|
|
|
|
|
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
|
2022-12-27 14:25:50 +00:00
|
|
|
// let countTest = 0
|
2022-05-06 22:49:45 +00:00
|
|
|
let uuid: any = 0
|
|
|
|
let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR })))
|
2022-05-16 02:48:06 +00:00
|
|
|
let timeInterval = 5
|
|
|
|
let lstOnline: any = []
|
|
|
|
let deleted: boolean = false;
|
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
const emitterOnline = (user: any, status: string, showOnlineTime: boolean = true) => {
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-05-18 00:36:55 +00:00
|
|
|
if (!user.id || !user.updatedAt || !user.onlineTime) return
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
let newOnlinetime = sumOnlineTimeNow(user)
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
let onlineTime: any = {
|
|
|
|
userId: user.id,
|
|
|
|
status: status,
|
|
|
|
onlineTime: newOnlinetime,
|
2022-05-06 22:49:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
if (!showOnlineTime) {
|
|
|
|
onlineTime = {
|
|
|
|
userId: user.id,
|
|
|
|
status: status
|
|
|
|
}
|
2022-05-06 22:49:45 +00:00
|
|
|
}
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
// console.log('-------------> onlineTime: ', onlineTime)
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
const io = getIO();
|
|
|
|
io.emit("onlineStatus", {
|
|
|
|
action: "update",
|
|
|
|
userOnlineTime: onlineTime
|
|
|
|
});
|
|
|
|
}
|
2022-05-06 22:49:45 +00:00
|
|
|
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
const monitor = async () => {
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
try {
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
const usersSocket = require('./../libs/socket');
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2024-01-29 11:48:20 +00:00
|
|
|
if (usersSocket?.ob) {
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
if (count > 1) {
|
|
|
|
count = 0
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
if (count == 0) {
|
|
|
|
uuid = usersSocket.ob.uuid
|
|
|
|
}
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
if (count == 1) {
|
|
|
|
if (uuid) {
|
|
|
|
if (uuid == usersSocket.ob.uuid) {
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2024-01-29 11:48:20 +00:00
|
|
|
console.log('ALL USERS CLIENTS OFFLINE 1...........')
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
usersSocket.ob.listOnline = []
|
|
|
|
usersSocket.ob.listOnlineAux = []
|
|
|
|
usersSocket.ob.uuid = undefined
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
2022-12-18 16:56:45 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
usersSocket.ob.listOnline.forEach(async (el: any) => {
|
|
|
|
|
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
const indexAux = lstOnline.findIndex((e: any) => e.id == el.id)
|
2022-12-18 16:56:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (indexAux == -1) {
|
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
const userOnline = await createOrUpdateOnlineUserService({ userId: el.id, status: 'online' })
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2024-01-29 11:48:20 +00:00
|
|
|
if (userOnline) {
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
el.onlineTime = userOnline.onlineTime
|
|
|
|
el.updatedAt = userOnline.updatedAt,
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
console.log(' * CREATED OR UPDATED USER TO ONLINE: ', userOnline.userId)
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
lstOnline.push({ 'id': el.id, 'status': 'online' })
|
|
|
|
}
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-12-27 14:25:50 +00:00
|
|
|
// console.log('el: ', el)
|
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
if (el.status == 'waiting...') {
|
|
|
|
emitterOnline(el, el.status, false)
|
|
|
|
}
|
2022-12-27 14:25:50 +00:00
|
|
|
// else {
|
|
|
|
// emitterOnline(el, el.status)
|
|
|
|
// }
|
2022-12-18 16:56:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
});
|
2022-12-18 16:56:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
let difference = lstOnline.filter((x: any) => !usersSocket.ob.listOnline.map((e: any) => e.id).includes(x.id));
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
difference.forEach(async (e: any) => {
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
const index = lstOnline.findIndex((x: any) => x.id == e.id)
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
if (index != -1) {
|
2022-12-18 16:56:45 +00:00
|
|
|
|
|
|
|
lstOnline.splice(index, 1)
|
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
const userOnline = await createOrUpdateOnlineUserService({ userId: e.id, status: 'offline' })
|
2022-12-18 16:56:45 +00:00
|
|
|
|
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
}
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
})
|
2022-12-18 16:56:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
count++
|
2022-12-27 14:25:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2022-05-16 04:03:16 +00:00
|
|
|
} catch (error) {
|
2022-12-18 16:56:45 +00:00
|
|
|
console.log('>>> There was an error no WhoIsOnlineMonitor.ts: ', error)
|
|
|
|
|
|
|
|
listUserId = []
|
|
|
|
count = 0
|
2022-12-27 14:25:50 +00:00
|
|
|
// countTest = 0
|
2022-12-18 16:56:45 +00:00
|
|
|
uuid = 0
|
|
|
|
}
|
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-05-03 21:20:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
|
|
|
|
const WhoIsOnlineMonitor = async () => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
clearInterval(_fifo);
|
|
|
|
|
|
|
|
await monitor()
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log('error on WhoIsOnlineMonitor: ', error)
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
_fifo = setInterval(WhoIsOnlineMonitor, 3000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_fifo = setInterval(WhoIsOnlineMonitor, 3000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const startWhoIsOnlineMonitor = async (mileseconds?: number) => {
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
listUserId = []
|
|
|
|
count = 0
|
2022-12-27 14:25:50 +00:00
|
|
|
// countTest = 0
|
2022-05-16 02:48:06 +00:00
|
|
|
uuid = 0
|
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
clearInterval(_fifo);
|
|
|
|
_fifo = setInterval(WhoIsOnlineMonitor, 3000);
|
|
|
|
|
|
|
|
// if (mileseconds) {
|
|
|
|
// timeInterval = mileseconds
|
|
|
|
// }
|
|
|
|
}
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
const stopWhoIsOnlineMonitor = async () => {
|
|
|
|
|
|
|
|
clearInterval(_fifo);
|
|
|
|
// _fifo = setInterval(WhoIsOnlineMonitor, 3000);
|
2022-05-03 21:20:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-05-03 21:20:58 +00:00
|
|
|
|
|
|
|
|
2022-12-18 16:56:45 +00:00
|
|
|
export { WhoIsOnlineMonitor, startWhoIsOnlineMonitor, stopWhoIsOnlineMonitor }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-03 21:20:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|