Skip to content

Release to prod #21

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 13 commits into from
Mar 23, 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# TOPCODER NOTIFICATIONS SERIES - NOTIFICATIONS SERVER
# TOPCODER NOTIFICATIONS SERIES - NOTIFICATIONS SERVER


## Dependencies
- nodejs https://nodejs.org/en/ (v6+)
- Heroku Toolbelt https://toolbelt.heroku.com
- git
- PostgreSQL 9.5


## Configuration
Configuration for the notification server is at `config/default.js`.
Expand Down Expand Up @@ -125,5 +125,5 @@ In case it expires, you may get a new token in this way:
## Swagger

Swagger API definition is provided at `docs/swagger_api.yaml`,
you may check it at `http://editor.swagger.io`.
you may check it at `http://editor.swagger.io`.

10 changes: 10 additions & 0 deletions connect/events-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const EVENTS = [
type: 'notifications.connect.project.approved',
projectRoles: [PROJECT_ROLE_OWNER, PROJECT_ROLE_COPILOT, PROJECT_ROLE_MANAGER],
topcoderRoles: [ROLE_CONNECT_COPILOT, ROLE_ADMINISTRATOR],
}, {
type: 'notifications.connect.project.active',
projectRoles: [PROJECT_ROLE_OWNER, PROJECT_ROLE_COPILOT, PROJECT_ROLE_MANAGER],
topcoderRoles: [ROLE_ADMINISTRATOR],
}, {
type: 'notifications.connect.project.paused',
projectRoles: [PROJECT_ROLE_OWNER, PROJECT_ROLE_COPILOT, PROJECT_ROLE_MANAGER],
Expand Down Expand Up @@ -111,6 +115,12 @@ const EVENTS = [
projectRoles: [PROJECT_ROLE_OWNER, PROJECT_ROLE_COPILOT, PROJECT_ROLE_MANAGER, PROJECT_ROLE_MEMBER],
toTopicStarter: true,
toMentionedUsers: true,
}, {
type: 'notifications.connect.project.post.edited',
version: 2,
projectRoles: [PROJECT_ROLE_OWNER, PROJECT_ROLE_COPILOT, PROJECT_ROLE_MANAGER, PROJECT_ROLE_MEMBER],
toTopicStarter: true,
toMentionedUsers: true,
}, {
type: 'notifications.connect.project.post.mention',
},
Expand Down
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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"pg": "^7.3.0",
"sequelize": "^4.21.0",
"superagent": "^3.8.0",
"tc-core-library-js": "gondzo/tc-core-library-js.git#dev",
"tc-core-library-js": "appirio-tech/tc-core-library-js.git#v2.2",
"winston": "^2.2.0"
},
"engines": {
Expand Down
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,
};