-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
send notification to q user or when a comment is added to a request [finishes #167727476]
- Loading branch information
Showing
7 changed files
with
185 additions
and
41 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
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,50 @@ | ||
import model from '../db/models/index'; | ||
|
||
const { Notification } = model; | ||
|
||
/** | ||
* Notifications middleware | ||
*/ | ||
export default class NotificationMiddleware { | ||
/** | ||
* sends notification to user when a comment is made on a request | ||
* @param {Object} req | ||
* @param {Object} res | ||
* @returns {JSON} responseBody | ||
*/ | ||
static async postCommentNotification(req, res) { | ||
// requestData and commentData were gotten from the ensureUserCanPost and postComment middlewares respectively | ||
const { requestData, user, commentData } = req.body; | ||
|
||
try { | ||
// send notification to User's manager if user owns the request | ||
const responseMessage = (user.id === requestData.user_id) | ||
? 'A notification was sent to your manager.' | ||
: 'A notification was sent to the request\'s owner.'; | ||
|
||
const receiver_id = (user.id === requestData.user_id) | ||
? requestData.User.manager_id | ||
: requestData.user_id; | ||
|
||
await Notification.create({ | ||
request_id: requestData.id, | ||
subject: 'New Comment On Request', | ||
notification: commentData.comment, | ||
receiver_id | ||
}); | ||
|
||
await res.status(201).json({ | ||
status: 'success', | ||
data: commentData, | ||
message: responseMessage | ||
}); | ||
} catch (error) { | ||
// sending a 201 status because although notification failed, comment was successfully posted | ||
res.status(201).json({ | ||
status: 'success', | ||
data: commentData, | ||
message: 'Could not send 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
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