Skip to content

Commit

Permalink
Merge pull request #67 from andela/feature/168407551/mark-notificatio…
Browse files Browse the repository at this point in the history
…n-as-read

#168407551 add mark as read
  • Loading branch information
Nkemjiks committed Sep 12, 2019
2 parents b79f658 + f4acae3 commit 9e22021
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 99 deletions.
132 changes: 66 additions & 66 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions src/controllers/NotificationController.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import helpers from '../helpers';
import services from '../services';

const { notificationServices: { getUserNotification } } = services;
const { notificationServices: { getUserNotification, markAsRead: markAsReadService } } = services;


const { responseMessage } = helpers;

const getNotification = async (req, res) => {
export const getNotification = async (req, res) => {
const { id } = req.user;
try {
const userNotificationObject = await getUserNotification(id);
responseMessage(res, 200, { notifications: userNotificationObject });
return responseMessage(res, 200, { notifications: userNotificationObject });
} catch (error) {
responseMessage(res, 500, { error: error.message });
return responseMessage(res, 500, { error: error.message });
}
};

export default getNotification;
export const markAsRead = async (req, res) => {
const { id: userId } = req.user;
const id = req.params.id || null;
try {
await markAsReadService(id, userId);
return responseMessage(res, 200, { message: 'Notification was mark as read successfully' });
} catch (error) {
return responseMessage(res, 500, { error: 'Something went wrong' });
}
};
1 change: 1 addition & 0 deletions src/controllers/novelController.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const createNovel = async (request, response) => {
configObjectId: 0,
entityId: createdNovel.id,
followeeId: request.user.id,
actorId: request.user.id,
isSingle: false,
response
};
Expand Down
3 changes: 2 additions & 1 deletion src/database/models/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default (sequelize, DataTypes) => {
defaultValue: DataTypes.UUIDV4,
},
notificationObjectId: DataTypes.UUID,
notifierId: DataTypes.UUID
notifierId: DataTypes.UUID,
isRead: DataTypes.BOOLEAN,
}, {});
Notification.associate = (models) => {
Notification.belongsTo(models.User, {
Expand Down
17 changes: 17 additions & 0 deletions src/database/seeders/20190724090406-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: null,
isVerified: true,
isSubscribed: true,
allowEmailNotification: true,
roleId: '2c4dfb3f-1798-43d4-8eb6-1c125994a263',
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -28,6 +29,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: null,
isVerified: true,
isSubscribed: true,
allowEmailNotification: true,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -43,6 +45,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: null,
isVerified: true,
isSubscribed: true,
allowEmailNotification: true,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -58,6 +61,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: null,
isVerified: true,
isSubscribed: true,
allowEmailNotification: true,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -73,6 +77,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: null,
isVerified: true,
isSubscribed: false,
allowEmailNotification: true,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -87,6 +92,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: '2347032123404',
isVerified: true,
isSubscribed: false,
allowEmailNotification: true,
roleId: 'b79c4eed-60c6-42fb-9040-f0822d8414fa',
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -101,6 +107,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
avatarUrl: null,
phoneNo: null,
isVerified: true,
allowEmailNotification: false,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
isSubscribed: true,
createdAt: new Date(),
Expand All @@ -117,6 +124,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: null,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
isVerified: true,
allowEmailNotification: true,
isSubscribed: true,
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -133,6 +141,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
isVerified: true,
isSubscribed: true,
allowEmailNotification: true,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -145,6 +154,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
roleId: superAdminRoleId,
isVerified: true,
isSubscribed: false,
allowEmailNotification: true,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -159,6 +169,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: '2347032123909',
isVerified: true,
isSubscribed: true,
allowEmailNotification: true,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -176,6 +187,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
isVerified: true,
isSubscribed: true,
allowEmailNotification: true,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -191,6 +203,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
isVerified: true,
isSubscribed: true,
allowEmailNotification: true,
createdAt: new Date(),
updatedAt: new Date()
},
Expand All @@ -206,6 +219,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
isVerified: true,
isSubscribed: true,
allowEmailNotification: true,
createdAt: new Date(),
updatedAt: new Date()
}, {
Expand All @@ -219,6 +233,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: null,
isVerified: true,
isSubscribed: false,
allowEmailNotification: true,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -233,6 +248,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: null,
isVerified: true,
isSubscribed: true,
allowEmailNotification: true,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
createdAt: new Date(),
updatedAt: new Date()
Expand All @@ -247,6 +263,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: null,
isVerified: false,
isSubscribed: true,
allowEmailNotification: true,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
createdAt: new Date(),
updatedAt: new Date()
Expand Down
9 changes: 9 additions & 0 deletions src/database/seeders/20190911120324-notificationObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const up = queryInterface => queryInterface.bulkInsert('NotificationObjects', [{
id: 'aeab6e66-b8e9-4045-803a-92eced0c56db',
entityId: 'a00bbeaf-b91e-4986-a3fd-485457b00436',
actorId: '11fb0350-5b46-4ace-9a5b-e3b788167915',
entityTypeId: 0,
createdAt: new Date(),
updatedAt: new Date()
}], {});
export const down = queryInterface => queryInterface.bulkDelete('NotificationObjects', null, {});
17 changes: 17 additions & 0 deletions src/database/seeders/20190911124604-notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const up = queryInterface => queryInterface.bulkInsert('Notifications', [{
id: 'f83238c9-258f-4c38-a548-5c939a7fdf22',
notificationObjectId: 'aeab6e66-b8e9-4045-803a-92eced0c56db',
notifierId: '122a0d86-8b78-4bb8-b28f-8e5f7811c456',
isRead: false,
createdAt: new Date(),
updatedAt: new Date()
},
{
id: 'f48e79ba-3363-4305-aba2-1c720f505815',
notificationObjectId: 'aeab6e66-b8e9-4045-803a-92eced0c56db',
notifierId: 'ce87299b-0dfa-44ed-bb53-45d434647eb2',
isRead: false,
createdAt: new Date(),
updatedAt: new Date()
}], {});
export const down = queryInterface => queryInterface.bulkDelete('Notifications', null, {});
8 changes: 7 additions & 1 deletion src/helpers/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ const isValidUUIDParam = paramName => param(paramName)
.isUUID()
.withMessage('invalid request');

const isValidUUIDOptional = field => check(field)
.trim()
.optional()
.isUUID()
.withMessage(`${field} must be an UUID`);
/**
* @param {String} field
* @returns {Object} - Express-validator
Expand Down Expand Up @@ -262,5 +267,6 @@ export default {
isValidPasswords,
isValidReadInput,
isBooleanNotOptional,
isValidPublishedStatus
isValidPublishedStatus,
isValidUUIDOptional,
};
4 changes: 3 additions & 1 deletion src/middlewares/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import highlightValidator from './highlightValidator';
import verifyToken from './verifyToken';
import authorizeUser from './authorizeUser';
import reportValidator from './reportValidator';
import notificationValidator from './notification';


export default {
Expand All @@ -16,5 +17,6 @@ export default {
highlightValidator,
verifyToken,
authorizeUser,
reportValidator
reportValidator,
notificationValidator,
};
17 changes: 17 additions & 0 deletions src/middlewares/notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import validator from '../helpers/validator';
import errorHandler from './errorHandler';

const {
isValidUUIDOptional
} = validator;

const { validatorError } = errorHandler;

const notificationValidator = {
markAsRead: [
isValidUUIDOptional('id'),
validatorError
]
};

export default notificationValidator;
5 changes: 3 additions & 2 deletions src/routes/notification.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import express from 'express';
import middlewares from '../middlewares';
import getNotification from '../controllers/NotificationController';
import { getNotification, markAsRead } from '../controllers/NotificationController';

const { verifyToken } = middlewares;
const { verifyToken, notificationValidator } = middlewares;

const notification = express.Router();

notification.get('/notifications', verifyToken, getNotification);
notification.patch('/notification/:id?', verifyToken, notificationValidator.markAsRead, markAsRead);

export default notification;
Loading

0 comments on commit 9e22021

Please sign in to comment.