Skip to content

Commit 754dac9

Browse files
committed
fixed problem with activity endpoint
1 parent 6192efd commit 754dac9

File tree

6 files changed

+107
-104
lines changed

6 files changed

+107
-104
lines changed

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import tutorialRouter from "#routes/tutorial";
1717
import assignmentRouter from "#routes/assignment";
1818
import timetableRouter from "#routes/timetable";
1919
import courseworkRouter from "#routes/coursework";
20+
import activityRouter from "#routes/activity";
2021
import moduleRouter from "#routes/module";
2122
import { identifyUser } from "#middleware/identifyUser";
2223
import departmentRouter from "#routes/department";
@@ -46,6 +47,7 @@ app.use("/department", departmentRouter);
4647
app.use("/practical", practicalRouter);
4748
app.use("/organization", organizationRouter);
4849
app.use("/student", studentRouter);
50+
app.use("/activity", activityRouter);
4951
app.use("/tutorial", tutorialRouter);
5052
app.use("/assignment", assignmentRouter);
5153
app.use("/timetable", timetableRouter);

controller/activity.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
22
createActivity,deleteActivityById, activityList ,updateActivityById,
33
}from "#services/activity";
4-
import { assignmentList } from "#services/assignment";
5-
import {logger} from "util" ;
4+
import {logger} from "#util" ;
65

7-
async function addActivity(res,req) {
6+
async function addActivity(req,res) {
87
const{
8+
activityBlueprint,
99
startTime,
1010
duration,
1111
course,
@@ -17,7 +17,7 @@ async function addActivity(res,req) {
1717
}=req.body;
1818
try{
1919
const newActivity = await createActivity(activityBlueprint,startTime,duration,course,faculty,type,task,group,students);
20-
res.json ({res: `added user ${newActivity.id}`});
20+
res.json ({res: `added activity ${newActivity.id}`, id: newActivity.id});
2121
} catch (error){
2222
logger.error ("Error while inserting",error);
2323
res.status(500);
@@ -26,8 +26,9 @@ async function addActivity(res,req) {
2626
}
2727

2828
async function updateActivity(req,res){
29+
const { id }=req.params;
2930
const {
30-
id, ...data
31+
...data
3132
}=req.body;
3233
try {
3334
await updateActivityById(id,data);
@@ -47,11 +48,11 @@ async function getActivity(req,res){
4748

4849

4950
async function deleteActivity(res,req){
50-
const { activityId }=req.params;
51+
const { id }=req.params;
5152
try{
52-
await deleteActivityById(activityId);
53+
await deleteActivityById(id);
5354

54-
res.json({res:`Deleted activity with ID ${activityId}`});
55+
res.json({res:`Deleted activity with ID ${id}`});
5556
}catch(error){
5657
logger.error ("Error while deleting",error);
5758
res.status(500).json({error:"Error while deleting from DB"});

models/activity.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ const Activity = connector.model("Activity", activitySchema);
2727
//add a activity to the database
2828
async function create(activityData){
2929
const {
30-
startTime,duration,course,faculty,type,task,group,students,
30+
activityBlueprint, startTime,duration,course,faculty,type,task,group,students,
3131
}=activityData;
3232
const activity= new Activity({
33-
startTime,duration,course,faculty,type,task,group,students,
33+
activityBlueprint, startTime,duration,course,faculty,type,task,group,students,
3434
});
3535
const activityDoc =await activity.save();
3636
return activityDoc;
3737
}
3838

3939
//Retrieve activity based on a given filter and limit
4040
async function read(filter,limit=1){
41-
const activity = await Activity.find (filter).limit(limit);
41+
const activityDoc = await Activity.find (filter).limit(limit);
4242
return activityDoc ;
4343
}
4444

routes/activity.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import express from "express";
22
import activityController from "#controller/activity";
33

44
const router=express.Router();
5-
router.post ("/add",activityController.addActivity);
5+
router.post("/add",activityController.addActivity);
66
router.get("/list",activityController.getActivity);
7-
router.post("/update",activityController.updateActivity);
8-
router.post("/delete",activityController.deleteActivity);
7+
router.post("/update/:id",activityController.updateActivity);
8+
router.delete("/delete/:id",activityController.deleteActivity);
99

1010
export default router;

services/activity.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Activity from "#models/activity" ;
22
import databaseError from "#error/database";
33

4-
export async function createActivity (startTime,duration,course,faculty,type,task,group,students){
4+
export async function createActivity (activityBlueprint,startTime,duration,course,faculty,type,task,group,students){
55
const newActivity = await Activity.create({
6-
startTime,duration,course,faculty,task,type,group,students,
6+
activityBlueprint,startTime,duration,course,faculty,task,type,group,students,
77
});
8-
if (newActivity.title===title){
8+
if (newActivity){
99
return newActivity;
1010
}
1111
throw new databaseError.DataEntryError("actvity");
@@ -24,7 +24,7 @@ export async function activityList(filter){
2424
return activitylist;
2525
}
2626

27-
export async function deleteActivityById(activityId){
27+
export async function deleteActivityById(id){
2828
const deleted = await Activity.remove({_id:id},data);
2929
if(deleted){
3030
return deleted;

test/routes/activity.test.js

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {jest} from "@jest/globals" ; //eslint-disable-line-import/no-extraneous-dependencies
1+
import { jest } from "@jest/globals"; //eslint-disable-line-import/no-extraneous-dependencies
22
import request from "supertest";
33
import app from "#app";//Update this import based on your app's
44
import connector from "#models/databaseUtil"; //Update this import
@@ -10,108 +10,108 @@ let server;
1010
let agent;
1111

1212
beforeAll((done) => {
13-
server = app.listen(null,() => {
14-
agent = request.agent(server);
15-
connector.set("debug",false);
16-
done();
17-
});
13+
server = app.listen(null, () => {
14+
agent = request.agent(server);
15+
connector.set("debug", false);
16+
done();
17+
});
1818
});
1919

20-
function cleanUp(callback){
21-
activityModel
20+
function cleanUp(callback) {
21+
activityModel
2222
.remove({
23-
startTime:"11:45 AM",
24-
duration:2,
25-
course: "64fc3c8bde9fa947ea1f412f",
26-
faculty: "64fc3c8bde9fa947ea1f412f",
27-
type:"LECTURE",
28-
task:"Practical",
29-
group: "64fc3c8bde9fa947ea1f412f",
30-
students:"xyz",
23+
startTime: "2023-06-18T14:11:30Z",
24+
duration: 2,
25+
course: "64fc3c8bde9fa947ea1f412f",
26+
faculty: "64fc3c8bde9fa947ea1f412f",
27+
type: "LECTURE",
28+
task: ["64fc3c8bde9fa947ea1f412f"],
29+
group: "64fc3c8bde9fa947ea1f412f",
30+
students: ["64fc3c8bde9fa947ea1f412f"]
3131
})
32-
.then(()=>{
33-
connector.disconnect ((DBerr)=>{
34-
if (DBerr) console.log("Database disconnect error : ",DBerr);
35-
server.close((serverErr)=> {
36-
if (serverErr) console.log(serverErr);
37-
callback();
38-
});
32+
.then(() => {
33+
connector.disconnect((DBerr) => {
34+
if (DBerr) console.log("Database disconnect error : ", DBerr);
35+
server.close((serverErr) => {
36+
if (serverErr) console.log(serverErr);
37+
callback();
3938
});
39+
});
4040
});
4141
}
4242

4343

44-
afterAll((done) => {
45-
cleanUp(done);
46-
});
47-
48-
describe("Activity API", () => {
49-
it("should create activity",async () => {
50-
const response = await agent.post ("/activity/add").send({
51-
startTime:"11:45 AM",
52-
duration:2,
53-
course: "64fc3c8bde9fa947ea1f412f",
54-
faculty: "64fc3c8bde9fa947ea1f412f",
55-
type:"LECTURE",
56-
task:"Practical",
57-
group: "64fc3c8bde9fa947ea1f412f",
58-
students:"xyz",
59-
});
44+
afterAll((done) => {
45+
cleanUp(done);
46+
});
6047

61-
expect(response.status).toBe(200);
62-
expect(response.body.res).toMatch(/added activity/);
63-
});
48+
describe("Activity API", () => {
49+
it("should create activity", async () => {
50+
const response = await agent.post("/activity/add").send({
51+
activityBlueprint: "5f8778b54b553439ac49a03a",
52+
startTime: "2023-06-18T14:11:30Z",
53+
duration: 2,
54+
course: "5f8778b54b553439ac49a03a",
55+
faculty: "5f8778b54b553439ac49a03a",
56+
type: "LECTURE",
57+
task: ["5f8778b54b553439ac49a03a"],
58+
group: "5f8778b54b553439ac49a03a",
59+
students: ["5f8778b54b553439ac49a03a"]
60+
});
61+
62+
expect(response.status).toBe(200);
63+
expect(response.body.res).toMatch(/added activity/);
64+
});
6465

65-
describe("after adding activity",()=>{
66-
beforeEach(async () => {
67-
await agent.post("/activity/add").send({
68-
startTime:"11:45 AM",
69-
duration:2,
70-
course: "64fc3c8bde9fa947ea1f412f",
66+
describe("after adding activity", () => {
67+
let id;
68+
beforeEach(async () => {
69+
id = await agent.post("/activity/add").send({
70+
activityBlueprint: "64fc3c8bde9fa947ea1f412f",
71+
startTime: "2023-06-18T14:11:30Z",
72+
duration: 2,
73+
course: "64fc3c8bde9fa947ea1f412f",
7174
faculty: "64fc3c8bde9fa947ea1f412f",
72-
type:"LECTURE",
73-
task:"Practical",
75+
type: "LECTURE",
76+
task: ["64fc3c8bde9fa947ea1f412f"],
7477
group: "64fc3c8bde9fa947ea1f412f",
75-
students:"xyz",
76-
});
77-
});
78+
students: ["64fc3c8bde9fa947ea1f412f"]
79+
});
80+
id = JSON.parse(id.res.text).id;
81+
});
7882

79-
afterEach(async () => {
80-
await activityModel.remove({
81-
startTime:"11:45 AM",
82-
duration:2,
83-
course: "64fc3c8bde9fa947ea1f412f",
83+
afterEach(async () => {
84+
await activityModel.remove({
85+
activityBlueprint: "64fc3c8bde9fa947ea1f412f",
86+
startTime: "2023-06-18T14:11:30Z",
87+
duration: 2,
88+
course: "64fc3c8bde9fa947ea1f412f",
8489
faculty: "64fc3c8bde9fa947ea1f412f",
85-
type:"LECTURE",
86-
task:"Practical",
90+
type: "LECTURE",
91+
task: ["64fc3c8bde9fa947ea1f412f"],
8792
group: "64fc3c8bde9fa947ea1f412f",
88-
students:"xyz",
89-
});
90-
});
91-
92-
it("should read activity", async () => {
93-
const response = await agent
94-
.post("/activity/list")
95-
.send({startTime:"11:45 AM"});
96-
expect(response.status).toBe(200);
97-
expect(response.body.res).toBeDefined();
98-
});
93+
students: ["64fc3c8bde9fa947ea1f412f"]
94+
});
95+
});
9996

100-
it("should update activity",async () => {
101-
const response =await agent
102-
.post("/activity/update")
103-
.send({
104-
startTime:"11:45 AM",
105-
duration:2,
106-
course: "64fc3c8bde9fa947ea1f412f",
107-
faculty: "64fc3c8bde9fa947ea1f412f",
108-
type:"LECTURE",
109-
task:"Practical",
110-
group: "64fc3c8bde9fa947ea1f412f",
111-
students:"xyz",});
97+
it("should read activity", async () => {
98+
const response = await agent
99+
.get("/activity/list")
100+
.send({ startTime: "2023-06-18T14:11:30Z" });
101+
expect(response.status).toBe(200);
102+
expect(response.body.res).toBeDefined();
103+
});
112104

113-
expect (response.status).toBe(200);
114-
expect(response.body.res).toMatch(/updated activity/);
115-
});
105+
it("should update activity", async () => {
106+
console.log(id)
107+
const response = await agent
108+
.post(`/activity/update/${id}`)
109+
.send({
110+
duration: 5,
116111
});
112+
113+
expect(response.status).toBe(200);
114+
expect(response.body.res).toMatch(/updated activity/);
117115
});
116+
});
117+
});

0 commit comments

Comments
 (0)