-
Notifications
You must be signed in to change notification settings - Fork 129
Notifications #2021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Notifications #2021
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
87b60c2
Validate registry organization program status
a26f7dc
Use title-case registry organization statuses
jdalphond-mitre 90c36c5
Update registry org status assertion casing
8a4d47f
Rebasing on dev after 2.8.6 merge
jdalphond-mitre cf1e2aa
First pass at notifications
f582b1a
Fixing merge conflicts
0b8ac2e
Fixing missing stubs for unit tests
bfff0cc
Added the body stuff
f637978
Iterating on feedback
78414a2
Notification unit test changes
927a84c
Merge branch '2.9.0-release' into dr_notifications
david-rocca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,6 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "type": "object", | ||
| "properties": { "notifications": { "type": "array", "items": { "$ref": "notification.json" } } }, | ||
| "required": ["notifications"] | ||
| } |
This file contains hidden or 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,12 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "type": "object", | ||
| "properties": { | ||
| "UUID": { "type": "string" }, | ||
| "body": { "type": "string" }, | ||
| "links": { "type": "object", "properties": { "org_shortname": { "type": "string" }, "user_shortname": { "type": "string" } } }, | ||
| "type": { "enum": ["PRIVATE_MESSAGE", "PUBLIC_MESSAGE"] }, | ||
| "created_at": { "type": "string", "format": "date-time" } | ||
| }, | ||
| "required": ["UUID", "body", "links", "type", "created_at"] | ||
| } |
This file contains hidden or 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,59 @@ | ||
| const router = require('express').Router() | ||
| const { body, param } = require('express-validator') | ||
| const mw = require('../../middleware/middleware') | ||
| const controller = require('./notification.controller') | ||
|
|
||
| function parseError (req, res, next) { | ||
| const { validationResult } = require('express-validator') | ||
| const errors = validationResult(req) | ||
| if (!errors.isEmpty()) return res.status(400).json({ error: 'BAD_INPUT', message: 'Parameters were invalid', details: errors.array() }) | ||
| next() | ||
| } | ||
|
|
||
| router.get('/notification', | ||
| /* | ||
| #swagger.tags = ['Notification'] | ||
| #swagger.operationId = 'getNotifications' | ||
| #swagger.summary = 'Retrieves notifications for the authenticated user' | ||
| #swagger.responses[200] = { description: 'Notifications ordered newest first', content: { 'application/json': { schema: { $ref: '../schemas/notification/list-notifications-response.json' } } } } | ||
| */ | ||
| mw.validateUser, | ||
| controller.getNotifications | ||
| ) | ||
|
|
||
| router.delete('/notification/:uuid', | ||
| /* | ||
| #swagger.tags = ['Notification'] | ||
| #swagger.operationId = 'dismissNotification' | ||
| #swagger.summary = 'Dismisses one notification for the authenticated user' | ||
| #swagger.responses[204] = { description: 'Notification dismissed' } | ||
| #swagger.responses[404] = { description: 'Notification was not found for the authenticated user' } | ||
| */ | ||
| mw.validateUser, | ||
|
david-rocca marked this conversation as resolved.
Dismissed
|
||
| param('uuid').isUUID(4), | ||
| parseError, | ||
| (req, res, next) => { req.ctx.params = { uuid: req.params.uuid }; next() }, | ||
| controller.dismissNotification | ||
| ) | ||
|
|
||
| router.post('/notification/target/:user_uuid', | ||
| /* | ||
| #swagger.tags = ['Notification'] | ||
| #swagger.operationId = 'createNotificationForUser' | ||
| #swagger.summary = 'Creates a notification for an existing user (Secretariat only)' | ||
| #swagger.responses[201] = { description: 'Notification created', content: { 'application/json': { schema: { $ref: '../schemas/notification/notification.json' } } } } | ||
| */ | ||
| mw.validateUser, | ||
|
david-rocca marked this conversation as resolved.
Dismissed
|
||
| mw.onlySecretariat, | ||
| param('user_uuid').isUUID(4), | ||
| body('body').isString().trim().notEmpty(), | ||
| body('type').isIn(['PRIVATE_MESSAGE', 'PUBLIC_MESSAGE']), | ||
| body('links').optional().isObject(), | ||
| body('links.org_shortname').optional().isString(), | ||
| body('links.user_shortname').optional().isString(), | ||
| parseError, | ||
| (req, res, next) => { req.ctx.params = { user_uuid: req.params.user_uuid }; req.ctx.body = req.body; next() }, | ||
| controller.createNotificationForUser | ||
| ) | ||
|
|
||
| module.exports = router | ||
45 changes: 45 additions & 0 deletions
45
src/controller/notification.controller/notification.controller.js
This file contains hidden or 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 @@ | ||
| const authContext = require('../../utils/authContext') | ||
|
|
||
| async function getNotifications (req, res, next) { | ||
| try { | ||
| const notificationRepo = req.ctx.repositories.getNotificationRepository() | ||
| const userRepo = req.ctx.repositories.getBaseUserRepository() | ||
| const orgRepo = req.ctx.repositories.getBaseOrgRepository() | ||
| const userUUID = await authContext.getRequesterUserUUID(req, userRepo, orgRepo) | ||
| return res.status(200).json({ notifications: await notificationRepo.getNotifications(userUUID) }) | ||
| } catch (err) { | ||
| next(err) | ||
| } | ||
| } | ||
|
|
||
| async function dismissNotification (req, res, next) { | ||
| try { | ||
| const notificationRepo = req.ctx.repositories.getNotificationRepository() | ||
| const userRepo = req.ctx.repositories.getBaseUserRepository() | ||
| const orgRepo = req.ctx.repositories.getBaseOrgRepository() | ||
| const userUUID = await authContext.getRequesterUserUUID(req, userRepo, orgRepo) | ||
| const dismissed = await notificationRepo.dismissNotification(userUUID, req.ctx.params.uuid) | ||
| if (!dismissed) return res.status(404).json({ error: 'NOTIFICATION_DNE', message: 'Notification does not exist.' }) | ||
| return res.status(204).send() | ||
| } catch (err) { | ||
| next(err) | ||
| } | ||
| } | ||
|
|
||
| async function createNotificationForUser (req, res, next) { | ||
| try { | ||
| const notificationRepo = req.ctx.repositories.getNotificationRepository() | ||
| const userRepo = req.ctx.repositories.getBaseUserRepository() | ||
| const recipient = await userRepo.findUserByUUID(req.ctx.params.user_uuid) | ||
| if (!recipient) return res.status(404).json({ error: 'USER_DNE', message: 'User does not exist.' }) | ||
| if (!notificationRepo.validateNotification(req.ctx.body)) { | ||
| return res.status(400).json({ error: 'BAD_INPUT', message: 'Notification body, links, or type is invalid.' }) | ||
| } | ||
| const notification = await notificationRepo.addNotification(recipient.UUID, req.ctx.body) | ||
| return res.status(201).json(notification) | ||
| } catch (err) { | ||
| next(err) | ||
| } | ||
| } | ||
|
|
||
| module.exports = { getNotifications, dismissNotification, createNotificationForUser } |
This file contains hidden or 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,21 @@ | ||
| const mongoose = require('mongoose') | ||
|
|
||
| const NotificationSchema = new mongoose.Schema({ | ||
| user_uuid: { type: String, required: true, unique: true, index: true }, | ||
| notifications: [{ | ||
| _id: false, | ||
| UUID: { type: String, required: true }, | ||
| body: { type: String, required: true }, | ||
| links: { | ||
| _id: false, | ||
| org_shortname: { type: String, default: '' }, | ||
| user_shortname: { type: String, default: '' } | ||
| }, | ||
| type: { type: String, enum: ['PRIVATE_MESSAGE', 'PUBLIC_MESSAGE'], required: true }, | ||
| created_at: { type: Date, required: true } | ||
| }] | ||
| }, { collection: 'Notification' }) | ||
|
|
||
| NotificationSchema.index({ user_uuid: 1, 'notifications.created_at': -1 }) | ||
|
|
||
| module.exports = mongoose.model('Notification', NotificationSchema) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,61 @@ | ||
| const uuid = require('uuid') | ||
| const NotificationModel = require('../model/notification') | ||
| const BaseRepository = require('./baseRepository') | ||
|
|
||
| class NotificationRepository extends BaseRepository { | ||
| constructor () { | ||
| super(NotificationModel) | ||
| } | ||
|
|
||
| validateNotification (notification) { | ||
| return notification && | ||
| typeof notification.body === 'string' && notification.body.trim().length > 0 && | ||
| ['PRIVATE_MESSAGE', 'PUBLIC_MESSAGE'].includes(notification.type) && | ||
| (!notification.links || (typeof notification.links === 'object' && !Array.isArray(notification.links) && | ||
| (!notification.links.org_shortname || typeof notification.links.org_shortname === 'string') && | ||
| (!notification.links.user_shortname || typeof notification.links.user_shortname === 'string'))) | ||
| } | ||
|
|
||
| createNotification (notification) { | ||
| return { | ||
| UUID: uuid.v4(), | ||
| body: notification.body, | ||
| links: { | ||
| org_shortname: notification.links?.org_shortname || '', | ||
| user_shortname: notification.links?.user_shortname || '' | ||
| }, | ||
| type: notification.type, | ||
| created_at: new Date() | ||
| } | ||
| } | ||
|
|
||
| async addNotification (userUUID, notification, options = {}) { | ||
| const createdNotification = this.createNotification(notification) | ||
| await NotificationModel.updateOne( | ||
| { user_uuid: userUUID }, | ||
| { $push: { notifications: createdNotification } }, | ||
| { upsert: true, ...options } | ||
| ) | ||
| return createdNotification | ||
| } | ||
|
|
||
| async addNotifications (userUUIDs, notification, options = {}) { | ||
| return Promise.all([...new Set(userUUIDs)].map(userUUID => this.addNotification(userUUID, notification, options))) | ||
| } | ||
|
|
||
| async getNotifications (userUUID, options = {}) { | ||
| const result = await NotificationModel.findOne({ user_uuid: userUUID }, null, options).lean() | ||
| return (result?.notifications || []).sort((a, b) => new Date(b.created_at) - new Date(a.created_at)) | ||
| } | ||
|
|
||
| async dismissNotification (userUUID, notificationUUID, options = {}) { | ||
| const result = await NotificationModel.updateOne( | ||
| { user_uuid: userUUID, 'notifications.UUID': notificationUUID }, | ||
| { $pull: { notifications: { UUID: notificationUUID } } }, | ||
| options | ||
| ) | ||
| return result.modifiedCount === 1 | ||
| } | ||
| } | ||
|
|
||
| module.exports = NotificationRepository |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.