-
-
Notifications
You must be signed in to change notification settings - Fork 39
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 #408 from tcet-opensource/321-crud-endpoints-testc…
…ases-apidoc-for-notification [Mega-Feat]:- Added crud, endpoints, testcases and apidoc for notification model
- Loading branch information
Showing
7 changed files
with
367 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
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,65 @@ | ||
import { | ||
createNotification, | ||
deleteNotificationById, | ||
listNotifications, | ||
updateNotificationById, | ||
} from "#services/notification"; | ||
import { logger } from "#util"; | ||
|
||
async function addNotification(req, res) { | ||
const { data, title, type, from, filter } = req.body; | ||
try { | ||
const newNotification = await createNotification({ | ||
data, | ||
title, | ||
type, | ||
from, | ||
filter, | ||
}); | ||
res.json({ | ||
res: `Added notification with ID: ${newNotification.id}`, | ||
id: newNotification.id, | ||
}); | ||
} catch (error) { | ||
logger.error("Error while inserting", error); | ||
res.status(500); | ||
res.json({ error: "Error while inserting in DB" }); | ||
} | ||
} | ||
|
||
async function updateNotification(req, res) { | ||
const { id } = req.params; | ||
const { ...data } = req.body; | ||
try { | ||
await updateNotificationById(id, data); | ||
res.json({ res: `Updated notification with ID: ${id}` }); | ||
} catch (error) { | ||
logger.error("Error while updating", error); | ||
res.status(500); | ||
res.json({ error: "Error while updating in DB" }); | ||
} | ||
} | ||
|
||
async function getNotifications(req, res) { | ||
const filter = req.query; | ||
const notificationList = await listNotifications(filter); | ||
res.json({ res: notificationList }); | ||
} | ||
|
||
async function deleteNotification(req, res) { | ||
const { id } = req.params; | ||
try { | ||
await deleteNotificationById(id); | ||
res.json({ res: `Deleted notification with ID: ${id}` }); | ||
} catch (error) { | ||
logger.error("Error while deleting", error); | ||
res.status(500).json({ error: "Error while deleting from DB" }); | ||
} | ||
} | ||
|
||
export default { | ||
addNotification, | ||
deleteNotification, | ||
getNotifications, | ||
updateNotification, | ||
}; |
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,65 @@ | ||
import connector from "#models/databaseUtil"; | ||
|
||
const notificationSchema = { | ||
data: { | ||
type: String, | ||
required: true, | ||
}, | ||
title: { | ||
type: String, | ||
required: true, | ||
}, | ||
from: { | ||
type: connector.Schema.Types.ObjectId, | ||
ref: "Faculty", // Reference to the Faculty model | ||
required: true, | ||
}, | ||
type: { | ||
type: String, | ||
enum: ["Student", "Faculty"], | ||
required: true, | ||
}, | ||
filter: [ | ||
{ | ||
type: connector.Schema.Types.ObjectId, | ||
ref: "User", // You might have a User model for storing IDs | ||
}, | ||
], | ||
}; | ||
|
||
const Notification = connector.model("Notification", notificationSchema); | ||
|
||
// CRUD Operations | ||
|
||
async function create(notificationData) { | ||
const{ | ||
data,title,from,type,filter, | ||
} =notificationData; | ||
const notification=new Notification({ | ||
data,title,from,type,filter, | ||
}); | ||
const notificationDOC=await notification.save(); | ||
return notificationDOC; | ||
} | ||
|
||
async function read(filter, limit = 1) { | ||
const notificationDoc = await Notification.find(filter).limit(limit); | ||
return notificationDoc; | ||
} | ||
|
||
async function update(filter, updateObject, options = { multi: true }) { | ||
const updateResult = await Notification.updateMany(filter, { $set: updateObject }, options); | ||
return updateResult.acknowledged; | ||
} | ||
|
||
async function remove(filter) { | ||
const deleteResult = await Notification.deleteMany(filter); | ||
return deleteResult.acknowledged; | ||
} | ||
|
||
export default { | ||
create, | ||
read, | ||
update, | ||
remove, | ||
}; |
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,18 @@ | ||
import express from "express"; | ||
import notificationController from "#controller/notification"; | ||
|
||
const router = express.Router(); | ||
|
||
// Create a new Notification | ||
router.post("/add", notificationController.addNotification); | ||
|
||
// List Notification entities with optional filters | ||
router.get("/list", notificationController.getNotifications); | ||
|
||
// Update Notification entities based on filters and update data | ||
router.post("/update/:id", notificationController.updateNotification); | ||
|
||
// Delete Notification entities based on ID | ||
router.delete("/delete/:id", notificationController.deleteNotification); | ||
|
||
export default router; |
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 the Notification model | ||
import Notification from "#models/notification"; | ||
import databaseError from "#error/database"; | ||
|
||
// Service function to create a new Notification entity | ||
export async function createNotification({ data, title, type, from, filter }) { | ||
try { | ||
const newNotification = await Notification.create({ | ||
data, | ||
title, | ||
type, | ||
from, | ||
filter, | ||
}); | ||
return newNotification; | ||
} catch (error) { | ||
throw new databaseError.DataEntryError("notification"); | ||
} | ||
} | ||
|
||
// Service function to update a Notification entity by ID | ||
export async function updateNotificationById(id, data) { | ||
try { | ||
const updated = await Notification.update({ _id: id }, data); | ||
if (updated) { | ||
return updated; | ||
} | ||
throw new databaseError.DataEntryError("notification"); | ||
} catch (error) { | ||
throw new databaseError.DataEntryError("notification"); | ||
} | ||
} | ||
|
||
// Service function to retrieve a list of Notification entities based on filters | ||
export async function listNotifications(filter) { | ||
try { | ||
const notificationList = await Notification.read(filter); | ||
return notificationList; | ||
} catch (error) { | ||
throw new databaseError.DataEntryError("notification"); | ||
} | ||
} | ||
|
||
// Service function to delete a Notification entity by ID | ||
export async function deleteNotificationById(notificationId) { | ||
try { | ||
const deleted = await Notification.deleteOne({ _id: notificationId }); | ||
if (deleted.deletedCount > 0) { | ||
return deleted; | ||
} | ||
throw new databaseError.DataDeleteError("notification"); | ||
} catch (error) { | ||
throw new databaseError.DataDeleteError("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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies | ||
import notificationModel from "#models/notification"; | ||
import connector from "#models/databaseUtil"; // Import your Express app instance | ||
|
||
jest.mock("#util"); | ||
const { agent } = global; | ||
|
||
function cleanUp(callback) { | ||
notificationModel | ||
.remove({ | ||
data: "Sample Notification", | ||
title: "Test Title", | ||
from: "64fc3c8bde9fa947ea1f412f", | ||
type: "Student", | ||
filter: ["64fc3c8bde9fa947ea1f412f"], | ||
}) | ||
.then(() => { | ||
connector.disconnect((DBerr) => { | ||
if (DBerr) console.log("database disconnect error: ", DBerr); | ||
callback(); | ||
}); | ||
}); | ||
} | ||
|
||
afterAll((done) => { | ||
cleanUp(done); | ||
}); | ||
|
||
describe("Notification API", () => { | ||
it("should create a new notification", async () => { | ||
const response = await agent.post("/notification/add").send({ | ||
data: "Sample Notification", | ||
title: "Test Title", | ||
from: "64fc3c8bde9fa947ea1f412f", // Use a valid Faculty ID | ||
type: "Student", | ||
filter: ["64fc3c8bde9fa947ea1f412f"], // Use a valid User ID | ||
}); | ||
expect(response.status).toBe(200); | ||
expect(response.body.res).toMatch(/Added notification/); | ||
const notificationId = JSON.parse(response.res.text).id; | ||
await notificationModel.remove({ _id: notificationId }); | ||
}); | ||
|
||
describe("after adding notification", () => { | ||
let notificationId; | ||
beforeEach(async () => { | ||
const id = await agent.post("/notification/add").send({ | ||
data: "Sample Notification", | ||
title: "Test Title", | ||
from: "64fc3c8bde9fa947ea1f412f", | ||
type: "Student", | ||
filter: ["64fc3c8bde9fa947ea1f412f"], | ||
}); | ||
notificationId = JSON.parse(id.res.text).id; | ||
}); | ||
afterEach(async () => { | ||
await notificationModel.remove({ | ||
data: "Sample Notification", | ||
title: "Test Title", | ||
from: "64fc3c8bde9fa947ea1f412f", | ||
type: "Student", | ||
filter: ["64fc3c8bde9fa947ea1f412f"], | ||
}); | ||
}); | ||
|
||
it("should update a notification entity", async () => { | ||
const response = await agent | ||
.post(`/notification/update/${notificationId}`) | ||
.send({ | ||
data: "Updated Notification Data", | ||
title: "Updated Title", | ||
from: "64fc3c8bde9fa947ea1f412f", | ||
type: "Faculty", | ||
filter: ["64fc3c8bde9fa947ea1f412f"], | ||
}); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body.res).toMatch(/Updated notification/); | ||
}); | ||
|
||
it("should list notification entities", async () => { | ||
const response = await agent.get("/notification/list").send({ | ||
data: "Sample Notification", | ||
title: "Test Title", | ||
from: "64fc3c8bde9fa947ea1f412f", | ||
type: "Student", | ||
filter: ["64fc3c8bde9fa947ea1f412f"], | ||
}); | ||
expect(response.status).toBe(200); | ||
expect(response.body.res).toBeDefined(); | ||
}); | ||
}); | ||
}); |