From 0ebebaae5f30a27e4e0eb752e40aa434a560e9b9 Mon Sep 17 00:00:00 2001 From: nignanthomas Date: Thu, 14 Nov 2019 10:10:47 +0200 Subject: [PATCH] chore(notification-timestamp): Add timestamp field to notifications - create migration file to add timestamp to notifications tables - add moment to models to save the current time to the notification [Finishes #169749860] --- src/controllers/commentsController.js | 2 +- .../20191113182004-addTimeNotification.js | 15 +++++++++++ src/database/models/bookingnotifications.js | 4 +++ src/database/models/notifications.js | 4 +++ ...20191028170529-notificationsTableSeeder.js | 25 +++++++++++-------- src/services/bookingNotifServices.js | 3 +++ src/services/notifServices.js | 3 +++ src/utils/db/queries/notificationQueries.js | 2 +- 8 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 src/database/migrations/20191113182004-addTimeNotification.js diff --git a/src/controllers/commentsController.js b/src/controllers/commentsController.js index c35f9bfb..8d21f6e2 100644 --- a/src/controllers/commentsController.js +++ b/src/controllers/commentsController.js @@ -26,7 +26,7 @@ export default class CommentsController { const notification = await notifBuilder( request, userNotiified, - `A comment has been made on request ${requestId}. Click here to view: ${APP_URL_BACKEND}/api/v1/requests/${requestId}.` + `A comment has been made on request ${requestId}. Click here to view: ${APP_URL_BACKEND}/api/v1/requests/${requestId}.`, ); await notifSaver(notification); const { diff --git a/src/database/migrations/20191113182004-addTimeNotification.js b/src/database/migrations/20191113182004-addTimeNotification.js new file mode 100644 index 00000000..1a53885b --- /dev/null +++ b/src/database/migrations/20191113182004-addTimeNotification.js @@ -0,0 +1,15 @@ +module.exports = { + up: (queryInterface, Sequelize) => queryInterface.sequelize.transaction(t => Promise.all([ + queryInterface.addColumn('notifications', 'timestamp', { + type: Sequelize.TIME, + }, { transaction: t }), + queryInterface.addColumn('bookingNotifications', 'timestamp', { + type: Sequelize.TIME, + }, { transaction: t }), + ])), + + down: queryInterface => queryInterface.sequelize.transaction(t => Promise.all([ + queryInterface.removeColumn('notifications', 'timestamp', { transaction: t }), + queryInterface.removeColumn('bookingNotifications', 'timestamp', { transaction: t }), + ])), +}; \ No newline at end of file diff --git a/src/database/models/bookingnotifications.js b/src/database/models/bookingnotifications.js index 63541895..14e0c646 100644 --- a/src/database/models/bookingnotifications.js +++ b/src/database/models/bookingnotifications.js @@ -1,3 +1,4 @@ + 'use strict'; module.exports = (sequelize, DataTypes) => { const bookingNotifications = sequelize.define('bookingNotifications', { @@ -8,6 +9,9 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.BOOLEAN, defaultValue: false, }, + timestamp: { + type: DataTypes.TIME, + }, }, { tableName: 'bookingNotifications' }); diff --git a/src/database/models/notifications.js b/src/database/models/notifications.js index 3d160356..de35eca8 100644 --- a/src/database/models/notifications.js +++ b/src/database/models/notifications.js @@ -1,9 +1,13 @@ + 'use strict'; module.exports = (sequelize, DataTypes) => { const notifications = sequelize.define('notifications', { requestId: DataTypes.INTEGER, userNotified: DataTypes.INTEGER, activity: DataTypes.STRING, + timestamp: { + type: DataTypes.TIME, + }, isRead: { type: DataTypes.BOOLEAN, defaultValue: false, diff --git a/src/database/seeders/20191028170529-notificationsTableSeeder.js b/src/database/seeders/20191028170529-notificationsTableSeeder.js index 18b288b1..282e6d7d 100644 --- a/src/database/seeders/20191028170529-notificationsTableSeeder.js +++ b/src/database/seeders/20191028170529-notificationsTableSeeder.js @@ -6,42 +6,47 @@ module.exports = { { requestId: 2, userNotified: 8, - activity: 'created', + activity: 'A request has been created. Click here to view: https://caret-bn-backend-staging.herokuapp.com/api/v1/requests/2.', isRead: false, createdAt: new Date(), - updatedAt: new Date() + updatedAt: new Date(), + timestamp: new Date() }, { requestId: 2, userNotified: 3, - activity: 'rejected', + activity: 'A request has been rejected. Click here to view: https://caret-bn-backend-staging.herokuapp.com/api/v1/requests/2.', isRead: true, createdAt: new Date(), - updatedAt: new Date() + updatedAt: new Date(), + timestamp: new Date() }, { requestId: 2, userNotified: 3, - activity: 'approved', + activity: 'A request has been approved. Click here to view: https://caret-bn-backend-staging.herokuapp.com/api/v1/requests/2.', isRead: false, createdAt: new Date(), - updatedAt: new Date() + updatedAt: new Date(), + timestamp: new Date() }, { requestId: 2, userNotified: 3, - activity: 'approved', + activity: 'A request has been approved. Click here to view: https://caret-bn-backend-staging.herokuapp.com/api/v1/requests/2.', isRead: false, createdAt: new Date(), - updatedAt: new Date() + updatedAt: new Date(), + timestamp: new Date() }, { requestId: 2, userNotified: 3, - activity: 'approved', + activity: 'A request has been approved. Click here to view: https://caret-bn-backend-staging.herokuapp.com/api/v1/requests/2.', isRead: false, createdAt: new Date(), - updatedAt: new Date() + updatedAt: new Date(), + timestamp: new Date() }, ]) diff --git a/src/services/bookingNotifServices.js b/src/services/bookingNotifServices.js index 46e9c4a9..217be42c 100644 --- a/src/services/bookingNotifServices.js +++ b/src/services/bookingNotifServices.js @@ -1,3 +1,4 @@ +import moment from 'moment'; import models from '../database/models'; export default class bookingNotifServices { @@ -22,10 +23,12 @@ export default class bookingNotifServices { } static async bookingNotifBuilder(booking, userNotified, activity) { + const timestamp = moment().format('HH:mm:ss'); const notification = await models.bookingNotifications.build({ bookingId: booking.id, userNotified, activity, + timestamp, }); return notification; } diff --git a/src/services/notifServices.js b/src/services/notifServices.js index f6afdbf0..211d2e80 100644 --- a/src/services/notifServices.js +++ b/src/services/notifServices.js @@ -1,4 +1,5 @@ /* eslint-disable require-jsdoc */ +import moment from 'moment'; import models from '../database/models'; export default class notifServices { @@ -43,10 +44,12 @@ export default class notifServices { } static async notifBuilder(request, userNotified, activity) { + const timestamp = moment().format('HH:mm:ss'); const notification = await models.notifications.build({ requestId: request.id, userNotified, activity, + timestamp, }); return notification; } diff --git a/src/utils/db/queries/notificationQueries.js b/src/utils/db/queries/notificationQueries.js index ecaaf287..e096a99f 100644 --- a/src/utils/db/queries/notificationQueries.js +++ b/src/utils/db/queries/notificationQueries.js @@ -8,7 +8,7 @@ export const userNotidicationQuery = userId => { userNotified: userId }, attributes: [ - 'id', 'activity', 'isRead', 'createdAt', 'updatedAt' + 'id', 'activity', 'isRead', 'createdAt', 'timestamp', 'updatedAt' ], include: [ {