Files
asan-service/server/WebSocket/controllers/GPS_EventsController.js
T
2024-09-30 12:55:01 +03:30

49 lines
1.0 KiB
JavaScript

const Notification = require('../../models/GPS.Notif')
module.exports.getAllNotifs = async socket => {
try {
console.log('4');
const notif = await Notification.find({ userID: socket.userId, read: false }).sort({ updated_at: -1 })
console.log(notif);
socket.emit(`gps:allNotifs`, notif)
} catch (e) {
console.log(e)
}
}
module.exports.notifyGPS = async (type, userId) => {
try {
const io = global._io
const gps = io.of('/gps')
const data = { type }
if (type === 'updateGPSInbox') {
const gpsInbox = await Notification.find({ userID: userId, read: false }).sort({ updated_at: -1 })
data.gpsInbox = gpsInbox
}
gps.to(userId.toString()).emit(`gps:notify`, data)
} catch (e) {
console.log(e)
}
}
module.exports.fetchNotifications = async (socket) => {
try {
console.log('5');
const notif = await Notification.find({ userID: socket.userId, read: false }).sort({ updated_at: -1 })
console.log(notif);
socket.emit('gps:notifications', notif)
} catch (e) {
console.log(e)
}
}