-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from atlp-rwanda/187354274-fit-notifications
Creating notifications
- Loading branch information
Showing
8 changed files
with
214 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Request, Response } from 'express'; | ||
import Notification from '../database/models/notification'; | ||
import { sendInternalErrorResponse } from '../validations'; | ||
import logger from '../logs/config'; | ||
|
||
export const getNotifications = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
const { userId } = req.params; | ||
const notifications = await Notification.findAll({ where: { userId } }); | ||
if (notifications.length === 0) { | ||
logger.error('No notifications were found'); | ||
res.status(404).json({ | ||
ok: false, | ||
message: 'No notifications that were found', | ||
}); | ||
return; | ||
} | ||
res.status(200).json({ ok: true, data: notifications }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
return; | ||
} | ||
}; | ||
|
||
export const markNotificationAsRead = async (req: Request, res: Response): Promise<void> => { | ||
const { id } = req.params; | ||
const { isRead } = req.body; | ||
try { | ||
const notification = await Notification.findByPk(id); | ||
if (!notification) { | ||
res.status(401).json({ | ||
ok: false, | ||
errorMessage: 'No such notification that were found! Try again', | ||
}); | ||
return; | ||
} | ||
notification.isRead = isRead; | ||
await notification.save(); | ||
res.status(200).json({ ok: true, message: 'Notification were updated successfully' }); | ||
} catch (error) { | ||
logger.error(error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; |
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
46 changes: 46 additions & 0 deletions
46
src/database/migrations/20240507222727-create-notification.js
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,46 @@ | ||
'use strict'; | ||
|
||
const { UUIDV4 } = require('sequelize'); | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.createTable('Notifications', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
defaultValue: Sequelize.UUIDV4, | ||
}, | ||
message: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
isRead: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
}, | ||
userId: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
}); | ||
}, | ||
async down(queryInterface, Sequelize) { | ||
await queryInterface.dropTable('Notifications'); | ||
}, | ||
}; |
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,55 @@ | ||
import { Model, Optional, DataTypes, UUIDV4 } from 'sequelize'; | ||
import sequelize from './index'; | ||
|
||
export interface NotificationAttributes { | ||
id: string; | ||
userId: string; | ||
message: string; | ||
isRead: boolean; | ||
} | ||
|
||
export interface NotificationCreationAttributes extends Optional<NotificationAttributes, 'id'> {} | ||
|
||
class Notification extends Model<NotificationAttributes, NotificationCreationAttributes> { | ||
public id!: string; | ||
public message!: string; | ||
public userId!: string; | ||
public isRead!: boolean; | ||
public readonly createdAt: Date | undefined; | ||
public readonly updatedAt: Date | undefined; | ||
} | ||
Notification.init( | ||
{ | ||
id: { | ||
type: DataTypes.UUID, | ||
defaultValue: UUIDV4, | ||
primaryKey: true, | ||
allowNull: false, | ||
unique: true, | ||
}, | ||
message: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
isRead: { | ||
type: DataTypes.BOOLEAN, | ||
allowNull: false, | ||
}, | ||
userId: { | ||
type: DataTypes.UUID, | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
}, | ||
}, | ||
{ | ||
sequelize: sequelize, | ||
timestamps: true, | ||
} | ||
); | ||
|
||
export default 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
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,9 @@ | ||
import express from 'express'; | ||
import { getNotifications, markNotificationAsRead } from '../controllers/notificationsController'; | ||
|
||
const route = express.Router(); | ||
|
||
route.get('/:userId', getNotifications); | ||
route.patch('/:id', markNotificationAsRead); | ||
|
||
export default route; |