-
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.
- Loading branch information
1 parent
512caa3
commit 73795b8
Showing
5 changed files
with
211 additions
and
0 deletions.
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,96 @@ | ||
import { Request, Response } from 'express'; | ||
import Notification from '../database/models/notification'; | ||
import User from '../database/models/user'; | ||
import { sendInternalErrorResponse, validateFields } from '../validations'; | ||
import logger from '../logs/config'; | ||
import EventEmitter from 'node:events'; | ||
import Role from '../database/models/role'; | ||
|
||
Role.afterCreate((role, options) => { | ||
try { | ||
console.log('Role was created successfully', role); | ||
// Any additional logic you want to execute after a Role is created | ||
} catch (error) { | ||
console.error('Error occurred after creating Role:', error); | ||
} | ||
}); | ||
|
||
// const eventEmitter = new EventEmitter(); | ||
// const getNotifications = async (req:Request, res:Response) => { | ||
// const { userId } = req.params; | ||
// const userNotifications = await User.findOne() | ||
|
||
// const notifications = await Notification.findAll() | ||
// } | ||
// const getAllnotifications = async (req: Request, res: Response) => { | ||
// try { | ||
// const notifications = await Notification.findAll(); | ||
// if (!notifications) { | ||
// res.status(404).json({ ok: false, message: 'Notifications could not be found' }); | ||
// return; | ||
// } | ||
// res.status(200).json({ ok: true, data: notifications }); | ||
// } catch (error) { | ||
// res.status(404).json({ ok: false, message: 'Notifications could not be found' }); | ||
// } | ||
// }; | ||
// const createNotifications = async (req: Request, res: Response) => { | ||
// try { | ||
// const { message, isRead } = req.body; | ||
// const missingFields = validateFields(req, ['message', 'isRead']); | ||
// if (missingFields.length !== 0) { | ||
// logger.error(`Missing required fields: ${missingFields.join(', ')}`); | ||
// res.status(400).json({ | ||
// ok: false, | ||
// message: `Missing required fields: ${missingFields.join(', ')}.`, | ||
// }); | ||
// return; | ||
// } | ||
// const notification = await Notification.create({ message, isRead }); | ||
|
||
// if (!notification) { | ||
// throw new Error('Notification could not be created successfully'); | ||
// } | ||
|
||
// res.status(201).json({ | ||
// ok: true, | ||
// data: notification, | ||
// }); | ||
// } catch (error) { | ||
// logger.error('Notification could not be created', error); | ||
// sendInternalErrorResponse(res, error); | ||
// return; | ||
// } | ||
// }; | ||
|
||
// // const eventEmitter.emit | ||
|
||
// //CREATE AN EVENT EMMITER IN YOUR PRODUCT CONTROLLERS WHICH USES HOOKS [AFTERUPDATE, AFTERCREATE]. | ||
// //THEN THE EVEN GET CAPTURED IN YOUR NOTIFICATION CONTROLLERS, | ||
// //ONCE CAPTURED, THE CONTROLLER CREATEA NOTIFICATION AND SENDS IT VIA EMAIL, AND THEN RETURN A RESPONSE THAT INCLUDES THAT NOT. | ||
// //THEN THERE WILL BE ONE GET ROUTE TO WORK ON FRONTEND | ||
// //ALSO ANOTHER DELETE ROUTE TO DELETE THE NOTIFICATIONS FROM THE DATABASE | ||
|
||
// // Role.afterCreate(async product => { | ||
// // const { id, name } = product; | ||
// // //HERE, FIND THE PRODUCTS ID AND NAME TO ASSOCIATE WITH THE NOTIFICATION SENT | ||
// // const notification = await Notification.create({ | ||
// // message: `Product ${name} created successfully`, | ||
// // isRead: false, | ||
// // }); | ||
// // }); | ||
// // Role.afterUpdate(async product => { | ||
// // const { id, name } = product; | ||
// // const notification = await Notification.create({ | ||
// // message: `Product ${name} was updated successfully!`, | ||
// // isRead: false, | ||
// // }); | ||
// // }); | ||
|
||
// // Role.afterDestroy(async product => { | ||
// // const { id, name } = product; | ||
// // const notification = await Notification.create({ | ||
// // message: `Product named ${name} was deleted successfully!`, | ||
// // isRead: false, | ||
// // }); | ||
// // }); |
31 changes: 31 additions & 0 deletions
31
src/database/migrations/20240502174714-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,31 @@ | ||
'use strict'; | ||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.createTable('Notifications', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER, | ||
}, | ||
message: { | ||
type: Sequelize.STRING, | ||
}, | ||
read: { | ||
type: Sequelize.BOOLEAN, | ||
}, | ||
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,45 @@ | ||
import { Model, Optional, DataTypes, UUIDV4 } from 'sequelize'; | ||
import sequelize from './index'; | ||
import User from './user'; | ||
export interface NotificationAttributes { | ||
id: 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 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, | ||
}, | ||
}, | ||
{ | ||
sequelize: sequelize, | ||
timestamps: true, | ||
} | ||
); | ||
User.hasMany(Notification); | ||
Notification.belongsTo(User); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { sendEmail } from './send-email'; | ||
let product: any; | ||
interface Product { | ||
id: string; | ||
name: string; | ||
} | ||
interface User { | ||
name: string; | ||
email: string; | ||
} | ||
|
||
const sendNotifications = async (product: Product, user: User) => { | ||
sendEmail('added_product_notification', user); | ||
|
||
return { | ||
message: `Your product,${product.name} was added successfully`, | ||
productId: product.id, | ||
link: 'https://dummy productlink.com', | ||
}; | ||
}; |