Skip to content

Commit

Permalink
Merge pull request #925 from OneCommunityGlobal/Jordy_add_notificatio…
Browse files Browse the repository at this point in the history
…nController_unit_tests

Jordy add notification controller unit tests
  • Loading branch information
one-community authored Jun 15, 2024
2 parents 8937006 + a522ca6 commit 0e0b44f
Show file tree
Hide file tree
Showing 9 changed files with 435 additions and 28 deletions.
15 changes: 15 additions & 0 deletions requirements/notificationController/creatUserNotification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Check mark: ✅
Cross Mark: ❌


# Create User Notification

## Negative Cases

1. ✅ Returns error 403 if requestor role is not Admin or Owner
2. ✅ Returns error 400 if message and recipient are missing from request
3. ✅ Returns error 500 if there is an internal error while fetching unread notifications.

## Positive Cases

1. ✅ Returns status 200 when notification is successfully created with sender, recipient and message
14 changes: 14 additions & 0 deletions requirements/notificationController/deleteUserNotification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Check mark: ✅
Cross Mark: ❌


# Delete User Notification

## Negative Cases

1. ✅ Returns error 403 if requestor role is not Admin or Owner.
2. ✅ Returns error 500 if there is an internal error while deleting notification.

## Positive Cases

1. ✅ Returns status 200 when notification is successfully deleted.
14 changes: 14 additions & 0 deletions requirements/notificationController/getSentNotifications.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Check mark: ✅
Cross Mark: ❌


# GET Sent Notifications

## Positive Cases

1. ✅ Returns status 200 Successful Data Retrieval

## Negative Cases

1. ✅ Returns error 403 if requestor role is not Admin or Owner
2. ✅ Returns error 500 if there is an internal error while fetching notifications.
15 changes: 15 additions & 0 deletions requirements/notificationController/getUnreadUserNotifications.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Check mark: ✅
Cross Mark: ❌


# GET Unread User Notifications

## Negative Cases

1. ✅ Returns error 403 if userId does not match requestorId.
2. ✅ Returns error 400 if the userId is missing from the request.
3. ✅ Returns error 500 if there is an internal error while fetching unread notifications.

## Positive Cases

1. ✅ Returns status 200 with notification data when a valid userId is provided by an Administrator or Owner querying another user's notifications.
15 changes: 15 additions & 0 deletions requirements/notificationController/getUserNotifications.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Check mark: ✅
Cross Mark: ❌


# GET User Notifications

## Negative Cases

1. ✅ Returns error 403 if userId does not match requestorId.
2. ✅ Returns error 400 if the userId is missing from the request.
3. ✅ Returns error 500 if there is an internal error while fetching notifications.

## Positive Cases

1. ✅ Returns status 200 with notification data when a valid userId is provided by an Administrator or Owner querying another user's notifications.
14 changes: 14 additions & 0 deletions requirements/notificationController/markNotificationAsRead.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Check mark: ✅
Cross Mark: ❌


# Mark Notification as Read

## Negative Cases

1. ✅ Returns error 400 if recipientId is missing.
2. ✅ Returns error 500 if there is an internal error while reading notification.

## Positive Cases

1. ✅ Returns status 200 when notification is successfully read.
60 changes: 33 additions & 27 deletions src/controllers/notificationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const LOGGER = require('../startup/logger');

/**
* API endpoint for notifications service.
* @param {} Notification
* @returns
* @param {} Notification
* @returns
*/

const notificationController = function () {
Expand All @@ -18,15 +18,17 @@ const notificationController = function () {
const getUserNotifications = async function (req, res) {
const { userId } = req.params;
const { requestor } = req.body;
if (requestor.requestorId !== userId && (requestor.role !== 'Administrator' || requestor.role !== 'Owner')) {
res.status(403).send({ error: 'Unauthorized request' });
return;
}

if (!userId) {
res.status(400).send({ error: 'User ID is required' });
return;
}
if (
requestor.requestorId !== userId &&
(requestor.role !== 'Administrator' || requestor.role !== 'Owner')
) {
res.status(403).send({ error: 'Unauthorized request' });
return;
}

try {
const result = await notificationService.getNotifications(userId);
Expand All @@ -37,7 +39,7 @@ const notificationController = function () {
}
};

/**
/**
* This function allows the user to get unread notifications for themselves or
* allows the admin/owner user to get unread notifications for a specific user.
* @param {Object} req - The request with userID as request param.
Expand All @@ -47,15 +49,17 @@ const notificationController = function () {
const getUnreadUserNotifications = async function (req, res) {
const { userId } = req.params;
const { requestor } = req.body;
if (requestor.requestorId !== userId && (requestor.role !== 'Administrator' || requestor.role !== 'Owner')) {
res.status(403).send({ error: 'Unauthorized request' });
return;
}

if (!userId) {
res.status(400).send({ error: 'User ID is required' });
return;
}
if (
requestor.requestorId !== userId &&
(requestor.role !== 'Administrator' || requestor.role !== 'Owner')
) {
res.status(403).send({ error: 'Unauthorized request' });
return;
}

try {
const result = await notificationService.getUnreadUserNotifications(userId);
Expand All @@ -68,13 +72,13 @@ const notificationController = function () {

/**
* This function allows the admin/owner user to get all notifications that they have sent.
* @param {*} req
* @param {*} res
* @returns
* @param {*} req
* @param {*} res
* @returns
*/
const getSentNotifications = async function (req, res) {
const { requestor } = req.body;
if ((requestor.role !== 'Administrator' || requestor.role !== 'Owner')) {
if (requestor.role !== 'Administrator' && requestor.role !== 'Owner') {
res.status(403).send({ error: 'Unauthorized request' });
return;
}
Expand All @@ -88,18 +92,17 @@ const notificationController = function () {
}
};


/**
* This function allows the Administrator/Owner user to create a notification to specific user.
* @param {*} req request with a JSON payload containing the message and recipient list.
* @param {*} res
* @returns
* @param {*} res
* @returns
*/
const createUserNotification = async function (req, res) {
const { message, recipient } = req.body;
const sender = req.requestor.requestorId;

if (req.body.requestor.role !== 'Administrator' || req.body.requestor.role !== 'Owner') {
if (req.body.requestor.role !== 'Administrator' && req.body.requestor.role !== 'Owner') {
res.status(403).send({ error: 'Unauthorized request' });
return;
}
Expand All @@ -121,13 +124,13 @@ const notificationController = function () {
/**
* This function allows the Administrator/Owner user to delete a notification.
* @param {*} req request with the notification ID as a parameter.
* @param {*} res
* @returns
* @param {*} res
* @returns
*/
const deleteUserNotification = async function (req, res) {
const { requestor } = req.body;

if (requestor.role !== 'Administrator' || requestor.role !== 'Owner') {
if (requestor.role !== 'Administrator' && requestor.role !== 'Owner') {
res.status(403).send({ error: 'Unauthorized request' });
return;
}
Expand All @@ -144,8 +147,8 @@ const notificationController = function () {
/**
* This function allows the user to mark a notification as read.
* @param {*} req request with the notification ID as a parameter.
* @param {*} res
* @returns
* @param {*} res
* @returns
*/
const markNotificationAsRead = async function (req, res) {
const recipientId = req.body.requestor.requestorId;
Expand All @@ -156,7 +159,10 @@ const notificationController = function () {
}

try {
const result = await notificationService.markNotificationAsRead(req.params.notificationId, recipientId);
const result = await notificationService.markNotificationAsRead(
req.params.notificationId,
recipientId,
);
res.status(200).send(result);
} catch (err) {
LOGGER.logException(err);
Expand Down
Loading

0 comments on commit 0e0b44f

Please sign in to comment.