const { isUser } = require('../middlewares/ws_authentication')
const userEventsController = require('../controllers/user_EventsController')

module.exports.userNamespace = () => {
  const io = global._io
  const user = io.of('/user')

  // users middlewares
  user.use(isUser)

  // attach events
  user.on('connection', socket => {
    socket.join(socket.userId)
    userEventsController.getAllNotifs(socket)

    /// /////////////////////////////////////////////////////// handle connection error
    socket.on('connect_error', err => {
      console.log(err.message)
    })

    /// ////////////////////////////////////////////////////// events
    socket.on('user:fetchAnnouncements', data => userEventsController.fetchAnnouncements(socket))
    socket.on('user:updateAnnouncement', data => userEventsController.updateAnnouncement(socket, data))
  })
}
