-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourseBought.js
65 lines (58 loc) · 3.03 KB
/
courseBought.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var functions = require("./util.js");
const service = "courseBought";
class CourseBought {
constructor(store) {
this.store = store;
this.service = service;
this.courseBought = this.store.getTable("CourseBought");
this.courses = this.store.getTable("Course");
this.exercise = this.store.getTable("Exercise");
this.review = this.store.getTable("Review");
}
async manageGet(queryString) {}
async managePost(body) {
let idUser = this.store.getIdUser(body.idSession);
if (idUser != undefined) {
if (body.method == "buyCourse" && body.data.idCourse != undefined && body.data.idCourse != "") {
if (
this.courseBought.filter((c) => c.idUser == idUser && c.idCourse == body.data.idCourse).length > 0
) {
return { code: 201, response: '{"message": "Course already bought"}' };
}
this.courseBought.push({ idUser: idUser, idCourse: body.data.idCourse, isArchived: false });
this.store.saveData("CourseBought", this.courseBought);
let course = this.courses.filter((c) => c.idCourse == body.data.idCourse)[0];
course = JSON.parse(JSON.stringify(course));
course.isBought = true;
course.isArchived = false;
let exercise = this.exercise.filter((e) => e.idCourse == course.idCourse);
let school = this.store.getTable("School").filter((s) => s.idSchool == course.idSchool)[0];
let trainer = this.store.getTable("User").filter((u) => u.idUser == school.idTrainer)[0];
let trainerLevel = this.store.getTable("Level").filter((ul) => ul.idUser == trainer.idUser);
let review = this.store.getTable("Review").filter((r) => r.idCourse == course.idCourse);
return {
code: 200,
response: {
Course: [course],
Exercise: exercise,
School: [school],
User: [trainer],
Level: trainerLevel,
Review: review,
},
};
} else if (body.method == "archiveCourse" && body.data.idCourse != undefined) {
let myCourse = this.courseBought.filter((c) => c.idUser == idUser && c.idCourse == body.data.idCourse);
if (myCourse.length == 0) {
return { code: 401, response: '{"message": "Course not bought"}' };
}
myCourse = myCourse[0];
myCourse.isArchived = !myCourse.isArchived;
this.store.saveData("CourseBought", this.courseBought);
return { code: 200, response: { value: myCourse.isArchived } };
}
}
return { code: 400, response: '{"message": "idSession is not correct"}' };
}
}
module.exports = CourseBought;