WS finial test
This commit is contained in:
@@ -1,19 +1,48 @@
|
||||
const Notification = require('../../models/GPS.Notif')
|
||||
|
||||
module.exports.getAllNotifs = async socket => {
|
||||
try {
|
||||
console.log('4');
|
||||
|
||||
module.exports.notifyGPS = async (type, gps_id) => {
|
||||
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:gps_id, seen: false }).sort({updated_at: -1})
|
||||
const gpsInbox = await Notification.find({ userID: userId, read: false }).sort({ updated_at: -1 })
|
||||
data.gpsInbox = gpsInbox
|
||||
}
|
||||
|
||||
gps.to(gps_id.toString()).emit(`GPS:notify`, data)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,16 +69,26 @@ module.exports.isAgent = async (socket, next) => {
|
||||
|
||||
module.exports.isGPS = async (socket, next) => {
|
||||
try {
|
||||
console.log('1');
|
||||
|
||||
const token = socket.handshake?.auth?.token
|
||||
if (!token) throw new Error('unauthenticated')
|
||||
|
||||
if (!token) {
|
||||
console.log('unauthorized 1');
|
||||
throw new Error('unauthorized')
|
||||
}
|
||||
// verifies secret and checks if the token is expired
|
||||
const decodedToken = await jwt.verify(token.replace(/^Bearer\s/, ''), authentication.secretKey)
|
||||
if (!decodedToken) throw new Error('unauthorized')
|
||||
if (!decodedToken) {
|
||||
console.log('unauthorized 2');
|
||||
throw new Error('unauthorized')
|
||||
}
|
||||
|
||||
// find user
|
||||
const user = await UserGPS.findById(decodedToken._id)
|
||||
if (!user || !user.active) throw new Error('unauthorized')
|
||||
if (!user || !user.active) {
|
||||
console.log('unauthorized 3');
|
||||
throw new Error('unauthorized')
|
||||
}
|
||||
|
||||
socket.userId = decodedToken._id.toString()
|
||||
return next()
|
||||
|
||||
@@ -4,13 +4,15 @@ const gpsEventsController = require('../controllers/GPS_EventsController')
|
||||
module.exports.gpsNamespace = () => {
|
||||
const io = global._io
|
||||
const gps = io.of('/gps')
|
||||
console.log('2');
|
||||
|
||||
// gpss middlewares
|
||||
gps.use(isGPS)
|
||||
console.log('3');
|
||||
|
||||
// attach events
|
||||
gps.on('connection', socket => {
|
||||
socket.join(socket.gpsId)
|
||||
socket.join(socket.userId)
|
||||
gpsEventsController.getAllNotifs(socket)
|
||||
|
||||
/// /////////////////////////////////////////////////////// handle connection error
|
||||
@@ -19,7 +21,6 @@ module.exports.gpsNamespace = () => {
|
||||
})
|
||||
|
||||
/// ////////////////////////////////////////////////////// events
|
||||
socket.on('gps:fetchAnnouncements', data => gpsEventsController.fetchAnnouncements(socket))
|
||||
socket.on('gps:updateAnnouncement', data => gpsEventsController.updateAnnouncement(socket, data))
|
||||
socket.on('gps:fetchNotifications', data => gpsEventsController.fetchNotifications(socket))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user