-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
584 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* eslint-disable import/no-cycle */ | ||
import models from '../models'; | ||
import { HelperMethods } from '../utils'; | ||
/** | ||
* Class representing the Notification controller | ||
* @class Notification | ||
* @description notification controller | ||
*/ | ||
class Notification { | ||
/** | ||
* Test real-tim in-app notification | ||
* Route: POST: api/v1/ | ||
* @param {object} req - HTTP Request object | ||
* @param {object} res - HTTP Response object | ||
* @return {res} res - HTTP Response object | ||
* @memberof Notification | ||
*/ | ||
static async notify(req, res) { | ||
const { id } = req.params; | ||
const { Message } = models; | ||
let messageCount; | ||
try { | ||
const messages = await Message.findAll({ | ||
where: { | ||
lineManager: id, | ||
isRead: false, | ||
} | ||
}); | ||
messageCount = messages.length; | ||
} catch (error) { | ||
return HelperMethods.clientError(res); | ||
} | ||
const notify = ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Barefoot Nomad</title> | ||
<script src="/socket.io/socket.io.js"></script> | ||
</head> | ||
<body> | ||
<h1>Barefoot Nomad</h1> | ||
<h2 id="message"></h2> | ||
<script> | ||
const socket = io(); | ||
let countMessage = ${messageCount}; | ||
const messageDisplay = messageCount => { | ||
const message = document.getElementById('message'); | ||
message.textContent = 'Messages:' + messageCount; | ||
}; | ||
messageDisplay(${messageCount}); | ||
const showDesktopNotification = (message, body, icon, sound, timeout) => { | ||
if (!timeout) { | ||
timeout = 4000; | ||
} | ||
const Notification = window.Notification || window.mozNotification | ||
|| window.webkitNotification; | ||
Notification.requestPermission(permission => {}); | ||
const requestNotificationPermissions = () => { | ||
if (Notification.permission !== 'denied') { | ||
Notification.requestPermission(permission => {}); | ||
} | ||
} | ||
requestNotificationPermissions(); | ||
const instance = new Notification(message, { | ||
body, | ||
icon, | ||
sound | ||
}); | ||
if (sound) { | ||
instance.sound; | ||
} | ||
setTimeout(instance.close.bind(instance), timeout); | ||
return false; | ||
}; | ||
const sendNodeNotification = (title, message, icon) => { | ||
socket.emit('new_notification', { | ||
message, | ||
title, | ||
icon | ||
}); | ||
}; | ||
const setNotification = data => { | ||
showDesktopNotification('Barefoot Nomad', data, '/index.png'); | ||
sendNodeNotification( | ||
'Barefoot Nomad', | ||
'Browser Notification..!', | ||
'/index.png' | ||
); | ||
}; | ||
socket.on('${id}', data => { | ||
countMessage += 1; | ||
messageDisplay(countMessage); | ||
setNotification(data); | ||
}); | ||
socket.on('show_notification', data => { | ||
showDesktopNotification(data.title, data.message, data.icon); | ||
}); | ||
</script> | ||
</body> | ||
</html>`; | ||
res.send(notify); | ||
} | ||
} | ||
export default Notification; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
/* eslint-disable import/no-cycle */ | ||
import UserController from './UserController'; | ||
import NotificationController from './NotificationController'; | ||
import RequestController from './RequestController'; | ||
|
||
export { UserController, RequestController }; | ||
export { UserController, RequestController, NotificationController }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Messages', { | ||
id: { | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
defaultValue: Sequelize.UUIDV4 | ||
}, | ||
userId: { | ||
type: Sequelize.UUID, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
} | ||
}, | ||
lineManager: { | ||
type: Sequelize.UUID, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
} | ||
}, | ||
type: { | ||
type: Sequelize.ENUM, | ||
values: ['approval', 'rejection', 'edition', 'creation'], | ||
}, | ||
message: { | ||
type: Sequelize.STRING, | ||
}, | ||
isRead: { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: false | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
}), | ||
|
||
down: queryInterface => queryInterface.dropTable('Messages') | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Message = sequelize.define('Message', { | ||
id: { | ||
type: DataTypes.UUID, | ||
primaryKey: true, | ||
defaultValue: DataTypes.UUIDV4, | ||
}, | ||
lineManager: { | ||
type: DataTypes.UUID, | ||
primaryKey: true, | ||
defaultValue: DataTypes.UUIDV4, | ||
}, | ||
type: { | ||
type: DataTypes.ENUM(['approval', 'rejection', 'edition', 'creation']) | ||
}, | ||
message: { | ||
type: DataTypes.STRING, | ||
}, | ||
isRead: { | ||
type: DataTypes.BOOLEAN, | ||
defaultValue: false | ||
} | ||
}, | ||
{ | ||
classMethods: { | ||
associate: models => { | ||
Message.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
onDelete: 'CASCADE', | ||
}); | ||
} | ||
} | ||
}); | ||
return Message; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { NotificationController } from '../controllers'; | ||
|
||
const notificationRoute = app => { | ||
app.get( | ||
'/api/v1/managerNotification/:id', | ||
NotificationController.notify | ||
); | ||
}; | ||
export default notificationRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.