Skip to content

Commit cd837dc

Browse files
Merge pull request #360 from tcet-opensource/354-attendance-endpoints-apidoc-testcases
added endpoints,apidoc and testcases
2 parents ae5b099 + ba54cf8 commit cd837dc

File tree

6 files changed

+270
-0
lines changed

6 files changed

+270
-0
lines changed

_apidoc.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,39 @@
672672
* attainment as per Bloom's Taxanomy (L1-L6).
673673
*/
674674

675+
// ------------------------------------------------------------------------------------------
676+
// Attendance.
677+
// ------------------------------------------------------------------------------------------
678+
679+
/**
680+
* @api {post} /attendance/add Add Attendance
681+
* @apiName AddAttendance
682+
* @apiGroup Attendance
683+
* @apiDescription Add a new attendance.
684+
*
685+
* @apiBody {String} student Student name.
686+
* @apiBody {String} course Course name.
687+
* @apiBody {Number} monthlyAttended Monthly attendance of student.
688+
* @apiBody {Number} monthlyOccured Monthly occured.
689+
* @apiBody {Number} cumulativeAttended sum of attendance of student.
690+
* @apiBody {Number} cumulativeOccured sum of occured.
691+
*
692+
* @apiSuccess {String} res Response message.
693+
* @apiError (Error 500) UserNotFound The of the User was not found
694+
*
695+
* @apiSuccessExample Success-Response:
696+
* HTTP/1.1 200 OK
697+
* {
698+
* "res": "added attendance Example Attendance"
699+
* }
700+
*
701+
* @apiErrorExample Error-Response:
702+
* HTTP/1.1 500 Internal Server Error
703+
* {
704+
* "err": "Error while inserting in DB"
705+
* }
706+
*/
707+
675708
// ------------------------------------------------------------------------------------------
676709
// Exam.
677710
// ------------------------------------------------------------------------------------------
@@ -883,6 +916,20 @@
883916
* "err": "Error while inserting in DB"
884917
* }
885918
*/
919+
920+
/**
921+
* @api {delete} /attendance/delete/:attendanceId To delete Attendance
922+
* @apiName DeleteAttendance
923+
* @apiGroup Attendance
924+
*
925+
* @apiParam {String} attendanceId The ID of the attendance document to delete.
926+
*
927+
* @apiSuccess {String} res Success message indicating the deletion.
928+
*
929+
* @apiError (Error 500) err Error message if there was an error during the deletion.
930+
*
931+
* */
932+
886933
// ------------------------------------------------------------------------------------------
887934
// Practical.
888935
// ------------------------------------------------------------------------------------------
@@ -1037,6 +1084,24 @@
10371084
*
10381085
* */
10391086

1087+
/**
1088+
* @api {post} /attendance/update update attendance details
1089+
* @apiName UpdateAttendance
1090+
* @apiGroup Attendance
1091+
* @apiDescription update Existing attendance
1092+
*
1093+
* @apiBody {String} [student] Student name.
1094+
* @apiBody {String} [course] Course name.
1095+
* @apiBody {Number} [monthlyAttended] Monthly attendance of student.
1096+
* @apiBody {Number} [monthlyOccured] Monthly occured.
1097+
* @apiBody {Number} [cumulativeAttended] sum of attendance of student.
1098+
* @apiBody {Number} [cumulativeOccured] sum of occured.
1099+
*
1100+
* @apiSuccess {String} res Attendance updated.
1101+
* @apiError (Error 500) err Error in updating database
1102+
*
1103+
*/
1104+
10401105
/**
10411106
* @api {post} /paper/update/:id Update Paper
10421107
* @apiName UpdatePaper
@@ -1075,6 +1140,27 @@
10751140
*
10761141
*/
10771142

1143+
/**
1144+
* @api {get} attendance/list Get Attendance List
1145+
* @apiName GetAttendance
1146+
* @apiGroup Attendance
1147+
*
1148+
* @apiBody {String} [student] Student name.
1149+
* @apiBody {String} [course] Course name.
1150+
* @apiBody {Number} [monthlyAttended] Monthly attendance of student.
1151+
* @apiBody {Number} [monthlyOccured] Monthly occured.
1152+
* @apiBody {Number} [cumulativeAttended] sum of attendance of student.
1153+
* @apiBody {Number} [cumulativeOccured] sum of occured.
1154+
*
1155+
* @apiSuccess {attendance[]} res Array of Filtered attendance Doc.
1156+
* @apiSuccess {String} attendance._id ID of document given by database.
1157+
* @apiSuccess {String} attendance.student Name of student.
1158+
* @apiSuccess {String} attendance.course Name of course.
1159+
* @apiSuccess {Number} attendance.monthlyAttended Monthly attendance of student.
1160+
* @apiSuccess {Number} attendance.cumulativeAttended sum of attendance of student.
1161+
* @apiSuccess {Number} attendance.cumulativeOccured sum of occured.
1162+
*/
1163+
10781164
/**
10791165
* @api {post} /exam/update/:id Update Exam Details
10801166
* @apiName UpdateExam
@@ -1585,6 +1671,7 @@
15851671
* @apiSuccess {String} res Topic updated.
15861672
* @apiError (Error 500) DatabaseError Error in updating the database.
15871673
*/
1674+
15881675
/**
15891676
* @api {post} /faculty/update/:id Update Faculty
15901677
* @apiName UpdateFaculty

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import moduleRouter from "#routes/module";
2323
import facultyRouter from "#routes/faculty";
2424
import { identifyUser } from "#middleware/identifyUser";
2525
import departmentRouter from "#routes/department";
26+
import attendanceRouter from "#routes/attendance";
2627
import examRouter from "#routes/exam";
2728
import paperRouter from "#routes/paper";
2829
import groupRouter from "#routes/group";
@@ -60,6 +61,7 @@ app.use("/timetable", timetableRouter);
6061
app.use("/department", departmentRouter);
6162
app.use("/coursework", courseworkRouter);
6263
app.use("/module", moduleRouter);
64+
app.use("/attendance", attendanceRouter);
6365
app.use("/exam", examRouter);
6466
app.use("/paper", paperRouter);
6567
app.use("/group", groupRouter);

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

0 commit comments

Comments
 (0)