const logger = require('../logging');

const addQuestion = require('../helpers').addQuestion;
const endQuestion = require('../helpers').endQuestion;

const issue = (socket) => {
  socket.on('issue', (data) => {
    logger.debug('issue data', data);
    if (data.action === 'open') {
      logger.info('received issue open');
      addQuestion(data)
      .then((question) => {
        logger.debug('add question works', { question: question.description });
        socket.broadcast.emit('issue', question);
      }).catch((err) => {
        logger.error('add question error on socket', { err });
      });
    }
    if (data.action === 'close') {
      logger.info('received issue close');
      socket.broadcast.emit('issue', data);
    }
  });
};

module.exports = issue;

@@ -141,16 +141,71 @@
 uestion;
+%0Aconst getUserById = require('../helpers').getUserById;
 %0A%0Aconst 
@@ -267,39 +267,101 @@
     
-logger.debug('issue data', data
+const payload = data.data;%0A    logger.debug('issue payload', %7B payload, action: data.action %7D
 );%0A 
@@ -397,50 +397,8 @@
 ) %7B%0A
-      logger.info('received issue open');%0A
     
@@ -471,26 +471,44 @@
 ug('
-add question works
+Added new question. Broadcasting ...
 ', %7B
@@ -593,16 +593,37 @@
 stion);%0A
+        return null;%0A
       %7D)
@@ -662,19 +662,26 @@
 .error('
-a
+A
 dd
+ing new
  questio
@@ -686,23 +686,15 @@
 ion 
-error on socket
+failed.
 ', %7B
@@ -712,145 +712,891 @@
     
-%7D);%0A    %7D%0A    if (data.action === 'close') %7B%0A      logger.info('received issue close');%0A      socket.broadcast.emit('issue', data);%0A    %7D
+  return null;%0A      %7D);%0A      return null;%0A    %7D else if (data.action === 'close') %7B%0A      if (!data.user) %7B%0A        socket.emit('issue', %7B%0A          error: 'User id required to be able to close an ongoing issue.',%0A        %7D);%0A        return null;%0A      %7D%0A      logger.info('Closing issue.', %7B issue: data._id, user: data.user %7D);%0A      getUserById(data.user).then((user) =%3E %7B%0A        logger.debug('Fetched user profile', %7B user: user.name %7D);%0A        endQuestion(data._id, user)%0A        .catch((err) =%3E %7B%0A          logger.error('closing issue failed', %7B err %7D);%0A        %7D).then((d) =%3E %7B%0A          logger.info('closed question', %7B question: d._id %7D);%0A        %7D);%0A        socket.broadcast.emit('issue', data);%0A        return null;%0A      %7D).catch((err) =%3E %7B%0A        logger.error('getting user failed', %7B err %7D);%0A        return null;%0A      %7D);%0A      return null;%0A    %7D%0A    return null;
 %0A  %7D

Make closing questions work. Also refine some logging.