Skip to content

add seen notification server support #20

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

Merged
merged 1 commit into from
Mar 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/swagger_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ paths:
read:
type: boolean
description: read flag
seen:
type: boolean
description: seen flag
contents:
type: object
description: the event message in JSON format
Expand Down Expand Up @@ -152,6 +155,42 @@ paths:
description: "Internal server error."
schema:
$ref: "#/definitions/Error"
/notifications/{id}/seen:
put:
description:
mark notification(s) as seen, id can be single id or '-' separated ids
security:
- jwt: []
parameters:
- in: path
name: id
description: notification id
required: true
type: integer
format: int64
responses:
200:
description: OK, the notification(s) are marked as seen
400:
description: "Invalid input"
schema:
$ref: "#/definitions/Error"
401:
description: "authentication failed"
schema:
$ref: "#/definitions/Error"
403:
description: "Action not allowed."
schema:
$ref: "#/definitions/Error"
404:
description: "Notification is not found"
schema:
$ref: "#/definitions/Error"
500:
description: "Internal server error."
schema:
$ref: "#/definitions/Error"
/notificationsettings:
get:
description:
Expand Down
2 changes: 2 additions & 0 deletions migrations/v1.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE public."Notifications"
ADD COLUMN seen boolean;
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function startKafkaConsumer(handlers) {
version: notification.version || null,
contents: _.extend({}, messageJSON, notification.contents),
read: false,
seen: false,
}))))
// commit offset
.then(() => consumer.commitOffset({ topic, partition, offset: m.offset }))
Expand Down
11 changes: 11 additions & 0 deletions src/controllers/NotificationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ function* markAllRead(req, res) {
res.end();
}

/**
* Mark a notification as seen.
* @param req the request
* @param res the response
*/
function* markAsSeen(req, res) {
yield NotificationService.markAsSeen(req.params.id, req.user.userId);
res.end();
}

/**
* Get notification settings.
* @param req the request
Expand All @@ -58,6 +68,7 @@ module.exports = {
listNotifications,
markAsRead,
markAllRead,
markAsSeen,
getSettings,
updateSettings,
};
1 change: 1 addition & 0 deletions src/models/Notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = (sequelize, DataTypes) => sequelize.define('Notification', {
type: { type: DataTypes.STRING, allowNull: false },
contents: { type: DataTypes.JSONB, allowNull: false },
read: { type: DataTypes.BOOLEAN, allowNull: false },
seen: { type: DataTypes.BOOLEAN, allowNull: true },
version: { type: DataTypes.SMALLINT, allowNull: true },
}, {});

Expand Down
6 changes: 6 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ module.exports = {
method: 'markAllRead',
},
},
'/notifications/:id/seen': {
put: {
controller: 'NotificationController',
method: 'markAsSeen',
},
},
'/notificationsettings': {
get: {
controller: 'NotificationController',
Expand Down
31 changes: 31 additions & 0 deletions src/services/NotificationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,36 @@ markAllRead.schema = {
userId: Joi.number().required(),
};

/**
* Mark notification(s) as seen.
* @param {Number} id the notification id or '-' separated ids
* @param {Number} userId the user id
*/
function* markAsSeen(id, userId) {
const ids = _.map(id.split('-'), (str) => {
const idInt = Number(str);
if (!_.isInteger(idInt)) {
throw new errors.BadRequestError(`Notification id should be integer: ${str}`);
}
return idInt;
});
const entities = yield models.Notification.findAll({ where: { id: { $in: ids }, seen: { $not: true } } });
if (!entities || entities.length === 0) {
throw new errors.NotFoundError(`Cannot find un-seen Notification where id = ${id}`);
}
_.each(entities, (entity) => {
if (Number(entity.userId) !== userId) {
throw new errors.ForbiddenError(`Cannot access Notification where id = ${entity.id}`);
}
});
yield models.Notification.update({ seen: true }, { where: { id: { $in: ids }, seen: { $not: true } } });
}

markAsSeen.schema = {
id: Joi.string().required(),
userId: Joi.number().required(),
};

updateSettings.schema = {
data: Joi.array().min(1).items(Joi.object().keys({
topic: Joi.string().required(),
Expand All @@ -188,6 +218,7 @@ module.exports = {
listNotifications,
markAsRead,
markAllRead,
markAsSeen,
getSettings,
updateSettings,
};