-
Notifications
You must be signed in to change notification settings - Fork 84
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 #62 from DOOMSDAY101/feat/19-implement-backend-for…
…-notification-settings Feat/19 Implement Backend for Notification Settings
- Loading branch information
Showing
7 changed files
with
146 additions
and
2,187 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 |
---|---|---|
|
@@ -5,4 +5,6 @@ build/ | |
tmp/ | ||
temp/ | ||
.env | ||
env | ||
env | ||
src/entity | ||
src/ormconfig.ts |
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,23 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import jwt from "jsonwebtoken"; | ||
|
||
interface CustomRequest extends Request { | ||
user?: any; | ||
} | ||
export const authenticateJWT = (req: Request, res: Response, next: NextFunction) => { | ||
const authHeader = req.headers['authorization']; | ||
|
||
if (authHeader) { | ||
const token = authHeader.split(' ')[1]; | ||
|
||
jwt.verify(token, process.env.AUTH_SECRET, (err, user) => { | ||
if (err) { | ||
return res.status(400).json({ message: "JWT token is invalid.", status: 400 }); | ||
} | ||
(req as CustomRequest).user = user; next(); | ||
}); | ||
} | ||
else { | ||
return res.status(401).json({ message: "JWT token is missing or invalid.", status: 401 }); | ||
} | ||
} |
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,78 @@ | ||
import { authenticateJWT } from '../middleware/auth' | ||
import { Router } from "express"; | ||
import { NotificationSetting } from '../models/notification' | ||
import { Request, Response } from "express"; | ||
|
||
// TO validate all required fields in post /api/notification-settings | ||
interface NotificationSettings { | ||
user_id: number; | ||
email_notifications: boolean; | ||
push_notifications: boolean; | ||
sms_notifications: boolean; | ||
} | ||
|
||
const requiredFields: (keyof NotificationSettings)[] = [ | ||
'user_id', | ||
'email_notifications', | ||
'push_notifications', | ||
'sms_notifications' | ||
]; | ||
const notificationRouter = Router(); | ||
const validateFields = (body: Partial<NotificationSettings>) => { | ||
const missingFields = requiredFields.filter(field => body[field] === undefined); | ||
|
||
if (missingFields.length > 0) { | ||
return { | ||
valid: false, | ||
message: `Missing required fields: ${missingFields.join(', ')}` | ||
}; | ||
} | ||
|
||
return { valid: true }; | ||
}; | ||
|
||
// Create notification setting for a user | ||
const CreateNotification = async (req: Request, res: Response) => { | ||
try { | ||
const validation = validateFields(req.body); | ||
|
||
if (!validation.valid) { | ||
return res.status(400).json({ status: "error", code: 400, message: validation.message }); | ||
} | ||
const { user_id } = req.body; | ||
|
||
// Check if a notification setting already exists for this user_id | ||
const existingSetting = await NotificationSetting.findOne({ where: { user_id } }); | ||
|
||
const newSetting = NotificationSetting.create(req.body); | ||
const result = await NotificationSetting.save(newSetting); | ||
res.status(200).json({ status: "success", code: 200, data: result }); | ||
|
||
if (existingSetting) { | ||
return res.status(409).json({ status: "error", code: 409, message: "Notification settings for this user already exist." }); | ||
} | ||
|
||
} catch (error) { | ||
console.log(error) | ||
res.status(500).json({ status: "error", code: 500, message: "Error creating user notification" }); | ||
} | ||
} | ||
|
||
// Get notification setting | ||
const GetNotification = async (req: Request, res: Response) => { | ||
try { | ||
const settings = await NotificationSetting.findOne({ where: { user_id: String(req.params.user_id) } }); | ||
if (settings === null) { | ||
return res.status(404).json({ status: "Not found", message: "The user with the requested id cannot be found" }) | ||
} | ||
res.status(200).json({ status: "success", code: 200, data: settings }); | ||
|
||
} catch (error) { | ||
res.status(500).json({ status: "error", code: 500, message: error.message }); | ||
} | ||
} | ||
|
||
notificationRouter.post('/notification-settings', authenticateJWT, CreateNotification); | ||
notificationRouter.get("/notification-settings/:user_id", authenticateJWT, GetNotification); | ||
|
||
export { notificationRouter }; |
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
Oops, something went wrong.