Skip to content

Commit 1d6d238

Browse files
committed
changed newExam.date string to date datatype
1 parent 0d5d171 commit 1d6d238

File tree

5 files changed

+78
-63
lines changed

5 files changed

+78
-63
lines changed

controller/exam.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@ import {
33
deleteExamById,
44
examList,
55
updateExamById,
6-
} from '#services/exam';
7-
import { logger } from '#util';
6+
} from "#services/exam";
7+
import { logger } from "#util";
88

99
async function addExam(req, res) {
10-
const { date, startTime, duration, infrastructure, supervisor, course } =
11-
req.body;
10+
const {
11+
date, startTime, duration, infrastructure, supervisor, course,
12+
} = req.body;
1213
try {
1314
const exam = await createExam(
1415
date,
1516
startTime,
1617
duration,
1718
supervisor,
1819
infrastructure,
19-
course
20+
course,
2021
);
2122
res.json({ res: `added exam ${exam.id}`, id: exam.id });
2223
} catch (error) {
23-
logger.error('Error while inserting', error);
24+
logger.error("Error while inserting", error);
2425
res.status(500);
25-
res.json({ err: 'Error while inserting in DB' });
26+
res.json({ err: "Error while inserting in DB" });
2627
}
2728
}
2829

@@ -33,9 +34,9 @@ async function updateExam(req, res) {
3334
await updateExamById(id, data);
3435
res.json({ res: `updated exam with id ${id}` });
3536
} catch (error) {
36-
logger.error('Error while updating', error);
37+
logger.error("Error while updating", error);
3738
res.status(500);
38-
res.json({ err: 'Error while updaing in DB' });
39+
res.json({ err: "Error while updaing in DB" });
3940
}
4041
}
4142

@@ -51,8 +52,8 @@ async function deleteExam(req, res) {
5152
await deleteExamById(id);
5253
res.json({ res: `Deleted exam with ID ${id}` });
5354
} catch (error) {
54-
logger.error('Error while deleting', error);
55-
res.status(500).json({ error: 'Error while deleting from DB' });
55+
logger.error("Error while deleting", error);
56+
res.status(500).json({ error: "Error while deleting from DB" });
5657
}
5758
}
5859

models/exam.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@ const examSchema = {
44
date: { type: Date, required: true },
55
startTime: { type: Date, required: true },
66
duration: { type: Number, required: true },
7-
supervisor: { type: connector.Schema.Types.ObjectId, ref: "Faculty", required: "true" },
8-
infrastructure: { type: connector.Schema.Types.ObjectId, ref: "Infrastructure", required: "true" },
9-
course: { type: connector.Schema.Types.ObjectId, ref: "Course", required: "true" },
7+
supervisor: {
8+
type: connector.Schema.Types.ObjectId,
9+
ref: "Faculty",
10+
required: "true",
11+
},
12+
infrastructure: {
13+
type: connector.Schema.Types.ObjectId,
14+
ref: "Infrastructure",
15+
required: "true",
16+
},
17+
course: {
18+
type: connector.Schema.Types.ObjectId,
19+
ref: "Course",
20+
required: "true",
21+
},
1022
};
1123

1224
const Exam = connector.model("Exam", examSchema);
@@ -23,12 +35,16 @@ async function read(filter, limit = 1) {
2335
}
2436

2537
async function update(filter, updateObject, options = { multi: true }) {
26-
const updateResult = await Exam.updateMany(filter, { $set: updateObject }, options);
38+
const updateResult = await Exam.updateMany(
39+
filter,
40+
{ $set: updateObject },
41+
options,
42+
);
2743
return updateResult.acknowledged;
2844
}
2945

3046
async function remove(filter) {
31-
const deleteResult = await Exam.deleteMany(filter).exec();
47+
const deleteResult = await Exam.deleteMany(filter);
3248
return deleteResult.acknowledged;
3349
}
3450

routes/exam.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import express from 'express';
2-
import examController from '#controller/exam';
1+
import express from "express";
2+
import examController from "#controller/exam";
33

44
const router = express.Router();
5-
router.post('/add', examController.addExam);
6-
router.get('/list', examController.getExam);
7-
router.post('/update/:id', examController.updateExam);
8-
router.delete('/delete/:id', examController.deleteExam);
5+
router.post("/add", examController.addExam);
6+
router.get("/list", examController.getExam);
7+
router.post("/update/:id", examController.updateExam);
8+
router.delete("/delete/:id", examController.deleteExam);
99

1010
export default router;

services/exam.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import Exam from '#models/exam';
2-
import databaseError from '#error/database';
1+
import Exam from "#models/exam";
2+
import databaseError from "#error/database";
33

44
export async function createExam(
55
date,
66
startTime,
77
duration,
88
supervisor,
99
infrastructure,
10-
course
10+
course,
1111
) {
1212
const newExam = await Exam.create({
1313
date,
@@ -17,18 +17,18 @@ export async function createExam(
1717
infrastructure,
1818
course,
1919
});
20-
if (newExam.date === date) {
20+
if (Date(newExam.date) === Date(date)) {
2121
return newExam;
2222
}
23-
throw new databaseError.DataEntryError('exam');
23+
throw new databaseError.DataEntryError("exam");
2424
}
2525

2626
export async function updateExamById(id, data) {
2727
const updated = await Exam.update({ _id: id }, data);
2828
if (updated) {
2929
return updated;
3030
}
31-
throw new databaseError.DataEntryError('exam');
31+
throw new databaseError.DataEntryError("exam");
3232
}
3333

3434
export async function examList(filter) {
@@ -41,5 +41,5 @@ export async function deleteExamById(examId) {
4141
if (deleted) {
4242
return deleted;
4343
}
44-
throw new databaseError.DataDeleteError('exam');
44+
throw new databaseError.DataDeleteError("exam");
4545
}

test/routes/exam.test.js

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
import { jest } from '@jest/globals'; // eslint-disable-line import/no-extraneous-dependencies
2-
import request from 'supertest';
3-
import app from '#app';
4-
import connector from '#models/databaseUtil';
5-
import examModel from '#models/exam';
1+
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
2+
import request from "supertest";
3+
import app from "#app";
4+
import connector from "#models/databaseUtil";
5+
import examModel from "#models/exam";
66

7-
jest.mock('#util');
7+
jest.mock("#util");
88

99
let server;
1010
let agent;
1111

1212
beforeAll((done) => {
1313
server = app.listen(null, () => {
1414
agent = request.agent(server);
15-
connector.set('debug', false);
15+
connector.set("debug", false);
1616
done();
1717
});
1818
});
1919

2020
function cleanUp(callback) {
2121
examModel
2222
.remove({
23-
id: '3200594e2b7b532006c073aa',
23+
supervisor: "60a0e7e9a09c3f001c834e07",
2424
})
2525
.then(() => {
2626
connector.disconnect((DBerr) => {
27-
if (DBerr) console.log('Database disconnect error: ', DBerr);
27+
if (DBerr) console.log("Database disconnect error: ", DBerr);
2828
server.close((serverErr) => {
2929
if (serverErr) console.log(serverErr);
3030
callback();
@@ -37,54 +37,52 @@ afterAll((done) => {
3737
cleanUp(done);
3838
});
3939

40-
describe('exam API', () => {
41-
it('should create exam', async () => {
42-
const response = await agent.post('/exam/add').send({
43-
date: '2023-08-20T14:10:30Z',
44-
startTime: '2023-05-18T14:10:30Z',
40+
describe("exam API", () => {
41+
it("should create exam", async () => {
42+
const response = await agent.post("/exam/add").send({
43+
date: "2023-06-18T14:11:30Z",
44+
startTime: "2023-06-18T14:11:30Z",
4545
duration: 5,
46-
supervisor: '4500594e2b7b532006c073cv',
47-
infrastructure: '8500594e2b7b532006c073uj',
48-
course: '2500594e2b7b532006c073bb',
46+
supervisor: ["5f8778b54b553439ac49a03a"],
47+
infrastructure: ["5f8778b54b553439ac49a03a"],
48+
course: ["5f8778b54b553439ac49a03a"],
4949
});
50-
expect(response.headers['content-type']).toMatch(/json/);
50+
expect(response.headers["content-type"]).toMatch(/json/);
5151
expect(response.status).toBe(200);
5252
expect(response.body.res).toMatch(/added exam/);
5353
});
5454

55-
describe('after adding exam', () => {
55+
describe("after adding exam", () => {
5656
let id;
5757
beforeEach(async () => {
58-
id = await agent.post('/exam/add').send({
59-
date: '2023-08-20T14:10:30Z',
60-
startTime: '2023-05-18T14:10:30Z',
58+
id = await agent.post("/exam/add").send({
59+
date: "2023-06-18T14:11:30Z",
60+
startTime: "2023-06-18T14:11:30Z",
6161
duration: 5,
62-
supervisor: '4500594e2b7b532006c073cv',
63-
infrastructure: '8500594e2b7b532006c073uj',
64-
course: '2500594e2b7b532006c073bb',
62+
supervisor: "64453a62c8f2146f2f34c73a",
63+
infrastructure: "64453a62c8f2146f2f34c73a",
64+
course: "64453a62c8f2146f2f34c73a",
6565
});
6666
id = JSON.parse(id.res.text).id;
6767
});
6868

6969
afterEach(async () => {
70-
await examModel.remove({
71-
id: '3200594e2b7b532006c073aa',
72-
});
70+
await examModel.remove({ supervisor: "64453a62c8f2146f2f34c73a" });
7371
});
7472

75-
it('should read exam', async () => {
73+
it("should read exam", async () => {
7674
const response = await agent
77-
.get('/exam/list')
78-
.send({ supervisor: '4500594e2b7b532006c073cv' });
75+
.get("/exam/list")
76+
.send({ supervisor: "64453a62c8f2146f2f34c73a" });
7977
expect(response.status).toBe(200);
8078
expect(response.body.res).toBeDefined();
8179
});
8280

83-
it('should update exam', async () => {
81+
it("should update exam", async () => {
8482
const response = await agent
8583
.post(`/exam/update/${id}`)
86-
.send({ duration: 5 }, { duration: 10 });
87-
expect(response.headers['content-type']).toMatch(/json/);
84+
.send({ duration: 10 });
85+
expect(response.headers["content-type"]).toMatch(/json/);
8886
expect(response.status).toBe(200);
8987
expect(response.body.res).toMatch(/updated exam/);
9088
});

0 commit comments

Comments
 (0)