Skip to content

Commit

Permalink
Merge 4f3f924 into cc96e8b
Browse files Browse the repository at this point in the history
  • Loading branch information
nignanthomas committed Jan 13, 2020
2 parents cc96e8b + 4f3f924 commit c7170b5
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/controllers/accommodationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export default class AccommodationController {
const newBooking = await models.booking.create(bookingData);

await notifSender(
req,
'Accommodation Booked',
newBooking,
accommodation[0].owner,
Expand Down Expand Up @@ -280,7 +281,7 @@ export default class AccommodationController {
await bookingHelper.updateAccomodation(req, remainingSpace);
}

await notifSender(subject, booking, booking.userId, APP_URL_BACKEND, activity, 'booking', host);
await notifSender(req, subject, booking, booking.userId, APP_URL_BACKEND, activity, 'booking', host);
return responseHelper(res, responseMessage, null, 200);
}
return responseHelper(res, strings.accommodations.error.BOOKING_NOT_FOUND, null, 404);
Expand Down
1 change: 1 addition & 0 deletions src/controllers/chatController.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,5 @@ const viewPrivateChats = async (req, res) => {
EMPTY_HISTORY : CHAT_HISTORY, privateChats);
};


export default { chat, viewChats , viewPrivateChats};
2 changes: 1 addition & 1 deletion src/controllers/commentsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class CommentsController {
userNotiified,
`A comment has been made on request ${requestId}. Click here to view: ${APP_URL_BACKEND}/api/v1/requests/${requestId}.`,
);
await notifSaver(notification);
await notifSaver(req, notification);
const {
deleted, userId,
...data
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/destinationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class destinationController {

const { lineManager } = user.payload;

await notifSender('Request Created', request, lineManager, APP_URL_BACKEND, 'created', 'request', host);
await notifSender(req, 'Request Created', request, lineManager, APP_URL_BACKEND, 'created', 'request', host);

return Utilities.responseHelper(
res,
Expand Down
5 changes: 2 additions & 3 deletions src/controllers/requestController.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export default class requestController {
statusId, activity, subject, responseMessage
} = req;
const { host } = req.body;

const requestToProcess = await findOneRequest({ id });
const { userId } = requestToProcess;
const user = await findUser({ id: userId });
Expand All @@ -95,7 +94,7 @@ export default class requestController {

request = request[1][0].dataValues;

await notifSender(subject, request, request.userId, APP_URL_BACKEND, activity, 'request', host);
await notifSender(req, subject, request, request.userId, APP_URL_BACKEND, activity, 'request', host);
return responseHelper(res, responseMessage, null, 200);
}
return responseHelper(res, strings.requests.NOT_FOUND, null, 404);
Expand Down Expand Up @@ -123,7 +122,7 @@ export default class requestController {

const request = await allSearch({ id });
const requestData = request[0].dataValues;
await notifSender('Request Edited', requestData, lineManager, APP_URL_BACKEND, 'edited', 'request');
await notifSender(req, 'Request Edited', requestData, lineManager, APP_URL_BACKEND, 'edited', 'request');
return responseUtil(res, 200, strings.request.success.SUCCESS_UPDATE_REQUEST, request);
} catch (error) {
return res.status(500).json({ error: 'Something wrong' });
Expand Down
6 changes: 4 additions & 2 deletions src/helpers/notifSender.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const transporter = nodemailer.createTransport({
}
});

const notifSender = async (subject, object, userId, APP_URL_BACKEND, activity, table, host) => {
const notifSender = async (
req, subject, object, userId, APP_URL_BACKEND, activity, table, host
) => {
try {
const user = await findUser({ id: userId });
let URL;
Expand All @@ -33,7 +35,7 @@ const notifSender = async (subject, object, userId, APP_URL_BACKEND, activity, t
if (user.appNotif) {
const notification = await notifBuilder(table, object.id, userId, activityMessage);
try {
await notifSaver(notification);
await notifSaver(req, notification);
} catch (error) {
return error;
}
Expand Down
14 changes: 14 additions & 0 deletions src/helpers/socketNotif.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const socketNotif = (receiverId, data, connectedClients, io, entity) => {
console.log('receiverId ===> ', receiverId);
if (!receiverId) {
console.log('all');
io.emit('notification', data);
} else if (connectedClients[receiverId.toString()]) {
connectedClients[receiverId.toString()].forEach(element => {
console.log('oneUser');
io.to(element).emit('notification', data);
});
}
};

export default socketNotif;
3 changes: 3 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
socket.on("connection_error", message => {
console.log(message);
});
socket.on("notification", notifData => {
console.log(notifData);
});
socket.on("disconnect", err => {
console.log("Server disconnected, please reload!");
socket.disconnect();
Expand Down
27 changes: 25 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import errorhandler from 'errorhandler';
import cors from 'cors';
import allRoutes from './routes';
import chatController from './controllers/chatController';
import decodeToken from './middlewares/auth/decodeToken';

// Create global app object
dotenv.config();
Expand Down Expand Up @@ -50,6 +51,28 @@ io.on('connect', socket => {
});
});

// eslint-disable-next-line no-console
http.listen(port, () => console.log(`Barefoot Nomad is runnig server on port ${port}...`));

const connectedClients = {};
io.use((socket, next) => {
const { token } = socket.handshake.query;
const userData = decodeToken(token);
if (userData) {
const clientKey = Number.parseInt(userData.payload.id, 10);
connectedClients[clientKey] = connectedClients[clientKey] || [];
connectedClients[clientKey].push(socket.id);
socket.emit('test', 'teststshbfjrfkjbhfbhjrebfhbfjhgerbf');
}
next();
});

app.use((req, res, next) => {
req.io = io;
req.connectedClients = connectedClients;
next();
});

app.use('/', allRoutes);

app.use('*', (req, res) => {
Expand All @@ -58,7 +81,7 @@ app.use('*', (req, res) => {
message: 'Sorry this route does not exist !',
});
});
// eslint-disable-next-line no-console
http.listen(port, () => console.log(`Barefoot Nomad is runnig server on port ${port}...`));

export { io };

export default app;
10 changes: 10 additions & 0 deletions src/middlewares/auth/decodeToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import jwt from 'jsonwebtoken';

export default token => {
try {
const verifiedUser = jwt.verify(token, process.env.JWT_SECRET);
return verifiedUser;
} catch (error) {
return error;
}
};
4 changes: 3 additions & 1 deletion src/services/notifServices.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable require-jsdoc */
import moment from 'moment';
import models from '../database/models';
import socketNotif from '../helpers/socketNotif';

export default class notifServices {
static async notifSaver(notif) {
static async notifSaver(req, notif) {
const notification = await notif.save();
socketNotif(notification.userNotified, notification, req.connectedClients, req.io, notification.entity);
return notification;
}

Expand Down

0 comments on commit c7170b5

Please sign in to comment.