Skip to content

Commit 6b37435

Browse files
committed
added endpoints,apidoc and testcases
1 parent 77f826f commit 6b37435

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed

_apidoc.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,87 @@
665665
* @apiSuccess {String[]} module.cognitiveLevels Array of cognitive levels of
666666
* attainment as per Bloom's Taxanomy (L1-L6).
667667
*/
668+
// ------------------------------------------------------------------------------------------
669+
// Attendance.
670+
// ------------------------------------------------------------------------------------------
671+
672+
/**
673+
* @api {post} /attendance/add Add Attendance
674+
* @apiName AddAttendance
675+
* @apiGroup Attendance
676+
* @apiDescription Add a new attendance.
677+
*
678+
* @apiBody {String} student Student name.
679+
* @apiBody {String} course Course name.
680+
* @apiBody {Number} monthlyAttended Monthly attendance of student.
681+
* @apiBody {Number} monthlyOccured Monthly occured.
682+
* @apiBody {Number} cumulativeAttended sum of attendance of student.
683+
* @apiBody {Number} cumulativeOccured sum of occured.
684+
*
685+
* @apiSuccess {String} res Response message.
686+
* @apiError (Error 500) UserNotFound The of the User was not found
687+
*
688+
* @apiSuccessExample Success-Response:
689+
* HTTP/1.1 200 OK
690+
* {
691+
* "res": "added attendance Example Attendance"
692+
* }
693+
*
694+
* @apiErrorExample Error-Response:
695+
* HTTP/1.1 500 Internal Server Error
696+
* {
697+
* "err": "Error while inserting in DB"
698+
* }
699+
*/
700+
701+
/**
702+
* @api {delete} /attendance/delete/:attendanceId To delete Attendance
703+
* @apiName DeleteAttendance
704+
* @apiGroup Attendance
705+
*
706+
* @apiParam {String} attendanceId The ID of the attendance document to delete.
707+
*
708+
* @apiSuccess {String} res Success message indicating the deletion.
709+
*
710+
* @apiError (Error 500) err Error message if there was an error during the deletion.
711+
*
712+
* */
713+
714+
/**
715+
* @api {post} /attendance/update update attendance details
716+
* @apiName UpdateAttendance
717+
* @apiGroup Attendance
718+
* @apiDescription update Existing attendance
719+
*
720+
* @apiBody {String} [student] Student name.
721+
* @apiBody {String} [course] Course name.
722+
* @apiBody {Number} [monthlyAttended] Monthly attendance of student.
723+
* @apiBody {Number} [monthlyOccured] Monthly occured.
724+
* @apiBody {Number} [cumulativeAttended] sum of attendance of student.
725+
* @apiBody {Number} [cumulativeOccured] sum of occured.
726+
*
727+
* @apiSuccess {String} res Attendance updated.
728+
* @apiError (Error 500) err Error in updating database
729+
*
730+
*/
731+
732+
/**
733+
* @api {get} attendance/list Get Attendance List
734+
* @apiName GetAttendance
735+
* @apiGroup Attendance
736+
*
737+
* @apiBody {String} [student] Student name.
738+
* @apiBody {String} [course] Course name.
739+
* @apiBody {Number} [monthlyAttended] Monthly attendance of student.
740+
* @apiBody {Number} [monthlyOccured] Monthly occured.
741+
* @apiBody {Number} [cumulativeAttended] sum of attendance of student.
742+
* @apiBody {Number} [cumulativeOccured] sum of occured.
743+
*
744+
* @apiSuccess {attendance[]} res Array of Filtered attendance Doc.
745+
* @apiSuccess {String} attendance._id ID of document given by database.
746+
* @apiSuccess {String} attendance.student Name of student.
747+
* @apiSuccess {String} attendance.course Name of course.
748+
* @apiSuccess {Number} attendance.monthlyAttended Monthly attendance of student.
749+
* @apiSuccess {Number} attendance.cumulativeAttended sum of attendance of student.
750+
* @apiSuccess {Number} attendance.cumulativeOccured sum of occured.
751+
*/

controller/attendance.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {
2+
addNewAttendance, deleteAttendanceById, updateAttendanceById, getAttendances,
3+
} from "#services/attendance";
4+
import { logger } from "#util";
5+
6+
async function addAttendance(req, res) {
7+
const {
8+
student, course, monthlyAttended, monthlyOccured, cumulativeAttended, cumulativeOccured,
9+
} = req.body;
10+
try {
11+
// eslint-disable-next-line max-len
12+
const attendance = await addNewAttendance( student, course, monthlyAttended, monthlyOccured, cumulativeAttended, cumulativeOccured);
13+
res.json({ res: `added attendance ${attendance.student}`, id: attendance.id });
14+
} catch (error) {
15+
logger.error("Error while inserting", error);
16+
res.status(500);
17+
res.json({ err: "Error while inserting in DB" });
18+
}
19+
}
20+
async function deleteAttendance(req, res) {
21+
const { id } = req.params;
22+
try {
23+
await deleteAttendanceById(id);
24+
res.json({ res: "Attendance deleted successfully" });
25+
} catch (error) {
26+
logger.error("Error while deleting", error);
27+
res.status(500);
28+
res.json({ err: "Error while deleting from DB" });
29+
}
30+
}
31+
32+
async function updateAttendance(req, res) {
33+
const { id } = req.params;
34+
const {
35+
...data
36+
} = req.body;
37+
38+
try {
39+
await updateAttendanceById(id, data);
40+
res.json({ res: `${id} attendance updated` });
41+
} catch (error) {
42+
logger.error("Error while inserting", error);
43+
res.status(500);
44+
res.json({ err: "Error while inserting in DB" });
45+
}
46+
}
47+
48+
async function showAttendance(req, res) {
49+
try {
50+
const attendance = await getAttendances(req.query);
51+
return res.json({ res: attendance });
52+
} catch (error) {
53+
logger.error("Error while fetching", error);
54+
res.status(500);
55+
return res.json({ err: "Error while fetching the data" });
56+
}
57+
}
58+
59+
export default {
60+
addAttendance, updateAttendance, deleteAttendance, showAttendance,
61+
};
62+

routes/attendance.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import express from "express";
2+
import attendanceController from "#controller/attendance";
3+
4+
const router = express.Router();
5+
router.get("/list", attendanceController.showAttendance);
6+
router.post("/add", attendanceController.addAttendance);
7+
router.delete("/delete/:id", attendanceController.deleteAttendance);
8+
router.post("/update/:id", attendanceController.updateAttendance);
9+
10+
export default router;

services/attendance.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Attendance from "#models/attendance";
2+
import databaseError from "#error/database";
3+
4+
export async function addNewAttendance( student, course, monthlyAttended, monthlyOccured, cumulativeAttended, cumulativeOccured) {
5+
const newAttendance = await Attendance.create({
6+
student,
7+
course,
8+
monthlyAttended,
9+
monthlyOccured,
10+
cumulativeAttended,
11+
cumulativeOccured,
12+
});
13+
if (newAttendance.student === student) {
14+
return newAttendance;
15+
}
16+
throw new databaseError.DataEntryError("Add Attendance");
17+
}
18+
19+
export async function getAttendances(filter) {
20+
const attendances = await Attendance.read(filter);
21+
if (attendances) {
22+
return attendances;
23+
}
24+
throw new databaseError.DataNotFoundError("Attendance");
25+
}
26+
27+
export async function deleteAttendanceById(attendanceId) {
28+
const deleted = await Attendance.remove({ _id: attendanceId });
29+
if (deleted) {
30+
return deleted;
31+
}
32+
throw new databaseError.DataDeleteError("Attendance");
33+
}
34+
35+
export async function updateAttendanceById(id, data) {
36+
const updated = await Attendance.update({ _id: id }, data);
37+
if (updated) {
38+
return updated;
39+
}
40+
throw new databaseError.DataEntryError("Attendance");
41+
}

test/routes/attendance.test.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import request from "supertest";
2+
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
3+
import app from "#app";
4+
import attendanceModel from "#models/attendance";
5+
import connector from "#models/databaseUtil";
6+
7+
jest.mock("#util");
8+
9+
let server;
10+
let agent;
11+
beforeAll((done) => {
12+
server = app.listen(null, () => {
13+
agent = request.agent(server);
14+
connector.set("debug", false);
15+
done();
16+
});
17+
});
18+
19+
function cleanUp(callback) {
20+
attendanceModel.remove({ name: "xyz" }).then(() => {
21+
connector.disconnect((DBerr) => {
22+
if (DBerr) console.log("Database dissconnnect error: ", DBerr);
23+
server.close((serverErr) => {
24+
if (serverErr) console.log(serverErr);
25+
callback();
26+
});
27+
});
28+
});
29+
}
30+
31+
afterAll((done) => {
32+
cleanUp(done);
33+
});
34+
35+
describe("checking attendance functions", () => {
36+
it("create attendance", async () => {
37+
const response = await agent.post("/attendance/add").send({
38+
name: "xyz",
39+
course: "XYZ",
40+
monthlyAttended: 123,
41+
monthlyOccured: 123,
42+
cumulativeAttended: 123,
43+
cumulativeOccured: 123,
44+
});
45+
expect(response.headers["content-type"]).toMatch(/json/);
46+
expect(response.status).toBe(200);
47+
expect(response.body.res).toMatch(/added attendance/);
48+
});
49+
let id;
50+
beforeEach(async () => {
51+
id = await agent.post("/attendance/add").send({
52+
name: "xyz",
53+
course: "XYZ",
54+
monthlyAttended: 123,
55+
monthlyOccured: 123,
56+
cumulativeAttended: 123,
57+
cumulativeOccured: 123,
58+
});
59+
id = JSON.parse(id.res.text).id;
60+
});
61+
62+
afterEach(async () => {
63+
await attendanceModel.remove({ name: "xyz" });
64+
});
65+
66+
it("read attendance", async () => {
67+
const response = await agent
68+
.get("/attendance/list")
69+
.send({ name: "xyz" });
70+
expect(response.status).toBe(200);
71+
expect(response.body.res).toBeDefined();
72+
});
73+
74+
it("update attendance", async () => {
75+
const response = await agent
76+
.post(`/attendance/update/${id}`)
77+
.send({ name: "xyz" }, { name: "123" });
78+
expect(response.headers["content-type"]).toMatch(/json/);
79+
expect(response.status).toBe(200);
80+
expect(response.body.res).toMatch(/attendance updated/);
81+
});
82+
});

0 commit comments

Comments
 (0)