Skip to content

Commit

Permalink
chore(notification): update notification response messages
Browse files Browse the repository at this point in the history
- change default sequelize response to user friendly reponse messages

[Maintains #168424047]
  • Loading branch information
jsbuddy committed Sep 11, 2019
1 parent 8432c04 commit 62d78b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/controllers/notificationController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import response from '../utils/response';
import messages from '../utils/messages';
import '../config/env';
import {
markAsRead,
Expand All @@ -15,13 +16,15 @@ import {
* @param {Object} req - express req object
* @param {Object} res - express res object
* @param {Function} method - method to be called
* @param {String} message - success message to show to the users
* @returns {void}
*/
const handleMethodById = async (req, res, method) => {
const handleMethodById = async (req, res, method, message) => {
try {
const { id } = req.decoded;
const data = await method(id);
response(res, 200, 'success', data);
const payload = message ? { message } : data;
response(res, 200, 'success', payload);
} catch (error) {
response(res, 500, 'error', { message: error.message });
}
Expand All @@ -41,15 +44,19 @@ export const getAllNotifications = (req, res) => handleMethodById(req, res, find
* @param {Object} res - server response
* @returns {Object} - custom response
*/
export const handleOptInEmail = (req, res) => handleMethodById(req, res, optInEmail);
export const handleOptInEmail = (req, res) => {
return handleMethodById(req, res, optInEmail, messages.optinEmailNotification);
};

/**
* notification controller
* @param {Object} req - server request
* @param {Object} res - server response
* @returns {Object} - custom response
*/
export const handleOptOutEmail = (req, res) => handleMethodById(req, res, optOutEmail);
export const handleOptOutEmail = (req, res) => {
return handleMethodById(req, res, optOutEmail, messages.optoutEmailNotification);
};

/**
* notification controller
Expand All @@ -62,7 +69,7 @@ export const handleMarkAsRead = async (req, res) => {
const { id } = req.params;
const { id: userId } = req.decoded;
const notification = await findOneNotification(id);
if (notification.receiver !== userId) return response(res, 403, 'error', { message: 'Forbidden' });
if (notification.receiver !== userId) return response(res, 403, 'error', { message: messages.forbidden });
const update = await markAsRead(id, { isRead: true, readDate: Date.now() });
response(res, 200, 'success', update);
} catch (e) {
Expand Down Expand Up @@ -92,4 +99,6 @@ export const handleMarkAllAsRead = async (req, res) => {
* @param {Object} res - server response
* @returns {Object} - custom response
*/
export const clearNotifications = (req, res) => handleMethodById(req, res, deleteAllNotifications);
export const clearNotifications = (req, res) => {
return handleMethodById(req, res, deleteAllNotifications, messages.notificationsCleared);
};
3 changes: 3 additions & 0 deletions src/utils/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const messages = {
updatePassword: 'Password updated successfully',
confirmation: 'Please confirm this action by passing confirmation as true as query parameter in your request',
unAuthorized: 'You are not authorized to create accommodation',
optinEmailNotification: 'You will now receive email notifications',
optoutEmailNotification: 'You will no longer receive email notifications',
notificationsCleared: 'Notifications cleared!',
};

export default messages;

0 comments on commit 62d78b8

Please sign in to comment.