Skip to content

Commit 7273dae

Browse files
authored
Merge pull request #139 from marc101101/feature/102_trim_response_data
Feature/102 trim response data
2 parents 3044c9d + 3a5320d commit 7273dae

6 files changed

Lines changed: 115 additions & 46 deletions

File tree

server/service/CategoriesService.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var Categories = require('../utils/database').Category;
44
var knex = require('../utils/database').knex;
55
var Errors = require('../utils/errors');
6+
var Trimmer = require('../utils/trim');
67
/**
78
* get all courses of a category
89
* get all courses of a category
@@ -20,7 +21,7 @@ exports.categoriesCategory_idCoursesGET = function(category_id) {
2021
reject(Errors.notFound("GET ID " +category_id, "Category"));
2122
}
2223
let courses = category.related("courses").toJSON();
23-
resolve(courses);
24+
resolve(Trimmer.courses(courses));
2425
}))
2526
.catch((error) => {
2627
reject(error);
@@ -64,7 +65,7 @@ exports.categoriesGET = function() {
6465
Categories
6566
.fetchAll()
6667
.then((categories) => {
67-
resolve(categories.map(item => item.attributes));
68+
resolve(Trimmer.categories(categories.toJSON()));
6869
})
6970
.catch((error) => {
7071
reject(error);

server/service/CoursesService.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var knex = require('../utils/database').knex;
88
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
99
var Errors = require('../utils/errors');
1010
var moment = require('moment');
11+
var Trimmer = require('../utils/trim');
1112

1213
/**
1314
* apply to participate in specific course
@@ -45,7 +46,7 @@ exports.coursesCourse_idApplyPOST = function (course_id, req) {
4546
new Applications(generateApplicationFor(user_id, course_id, areSlotsLefToParticipate))
4647
.save()
4748
.then((application) => {
48-
resolve(application);
49+
resolve(application.toJSON());
4950
})
5051
.catch((error) => {
5152
reject(error);
@@ -106,7 +107,7 @@ exports.coursesCourse_idFeedbackPOST = function (course_id, data) {
106107
new CourseFeedback({...data, kurs_id: course_id})
107108
.save()
108109
.then((feedback) => {
109-
resolve(feedback.attributes);
110+
resolve(feedback.toJSON());
110111
})
111112
.catch((error) => {
112113
console.log(error);
@@ -133,7 +134,7 @@ exports.coursesCourse_idGET = function (course_id) {
133134
if (!course) {
134135
reject(Errors.notFound("GET ID " + course_id, "COURSE"));
135136
}
136-
resolve(course.toJSON());
137+
resolve(Trimmer.course(course.toJSON()));
137138
})
138139
.catch((error) => {
139140
reject(error);
@@ -161,11 +162,11 @@ exports.coursesCourse_idSignoffPOST = function (course_id, req) {
161162
.save({ANM_STAT_ID: 3}, {
162163
patch: true
163164
})
164-
.then(applicationModel => {
165-
if (!applicationModel) {
165+
.then(application => {
166+
if (!application) {
166167
reject(Errors.notFound("course with ID ", course_id));
167168
}
168-
resolve(applicationModel);
169+
resolve(application.toJSON());
169170
})
170171
.catch(err => {
171172
reject(Errors.notFound("course with ID ", course_id));
@@ -200,7 +201,7 @@ exports.coursesGET = function (query) {
200201
Courses
201202
.fetchAll({withRelated: ["location", "teacher"]})
202203
.then((courses) => {
203-
resolve(courses.toJSON());
204+
resolve(Trimmer.courses(courses.toJSON()));
204205
})
205206
.catch((error) => {
206207
reject(error);
@@ -222,7 +223,7 @@ exports.coursesHighlightsGET = function () {
222223
.where({kurs_highlight: 1})
223224
.fetchAll({withRelated: ["location", "teacher"]})
224225
.then((courses) => {
225-
resolve(courses.toJSON());
226+
resolve(Trimmer.courses(courses.toJSON()));
226227
})
227228
.catch((error) => {
228229
reject(error);
@@ -239,10 +240,12 @@ exports.coursesLastminuteGET = function() {
239240
})
240241
.fetchAll({withRelated: ["applications", "location", "teacher"]})
241242
.then((courses) => {
242-
resolve(courses
243-
.filter(item => item.related('applications').toJSON().length < item.attributes.KURS_TEIL_MAX)
244-
.filter(item => item.attributes.KURS_KURSSTAT_ID === 3)
245-
.map(item => item.toJSON())
243+
resolve(Trimmer.courses(
244+
courses
245+
.filter(item => item.related('applications').toJSON().length < item.attributes.KURS_TEIL_MAX)
246+
.filter(item => item.attributes.KURS_KURSSTAT_ID === 3)
247+
.map(item => item.toJSON())
248+
)
246249
)
247250
})
248251
.catch((error) => {

server/service/UserService.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
88
var bcrypt = require('bcryptjs');
99
var config = require('../config'); // get config file
1010
var Errors = require('../utils/errors');
11+
var Trimmer = require('../utils/trim');
1112

1213
/**
1314
* get me
@@ -25,7 +26,7 @@ exports.userMeGET = function (id) {
2526
if (!user) {
2627
reject(Errors.notFound("GET ID " + id, "USER"))
2728
}
28-
resolve(user);
29+
resolve(Trimmer.user(user.toJSON()));
2930
})
3031
.catch((error) => {
3132
reject(error);
@@ -41,11 +42,11 @@ exports.userMeCoursesGET = function (user_id) {
4142
ANM_STAT_ID: 1 || 2
4243
})
4344
.fetchAll({
44-
withRelated: ["course.location", "course.teacher"]
45+
withRelated: ["course.location"]
4546
})
4647
.then((applications) => {
4748
let courses = applications.map(item => item.related('course').toJSON());
48-
resolve(courses);
49+
resolve(Trimmer.courses(courses));
4950
})
5051
.catch((error) => {
5152
reject(error);
@@ -63,7 +64,7 @@ exports.userMeCoursesGET = function (user_id) {
6364
**/
6465
exports.userPOST = function (data) {
6566
return new Promise(function (resolve, reject) {
66-
User.where({teil_email: data.teil_email})
67+
User.where({TEIL_EMAIL : data.TEIL_EMAIL})
6768
.fetch()
6869
.then((model) => {
6970
if (!model) {
@@ -75,8 +76,11 @@ exports.userPOST = function (data) {
7576
}, config.secret, {
7677
expiresIn: 86400 // expires in 24 hours
7778
});
79+
// for some reason, bookshelf returns TEIL_ID just as id after saving
80+
let trimmedUser = Trimmer.user(user.toJSON());
81+
trimmedUser.TEIL_ID = user.attributes.id;
7882
resolve({
79-
user: user,
83+
user: trimmedUser,
8084
token: token
8185
});
8286
})
@@ -108,11 +112,11 @@ exports.userPUT = function (id, data) {
108112
.save(data, {
109113
patch: true
110114
})
111-
.then(userModel => {
112-
if (!userModel) {
115+
.then(user => {
116+
if (!user) {
113117
reject(Errors.notFound("PUT ID " + id, "USER"));
114118
}
115-
resolve(userModel);
119+
resolve(Trimmer.user(user.toJSON()));
116120
})
117121
.catch(err => {
118122
reject(err);

server/test/helpers/sampleData.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,15 +619,15 @@ if (process.env.NODE_ENV === 'test') {
619619

620620
exports.user = () => {
621621
return {
622-
teil_vorname: "John",
623-
teil_nachname: "Doe",
624-
teil_email: "johndoe@vhslq.de",
625-
teil_notizen: "john's notes",
626-
teil_passwort: "hunter22",
627-
eingegeben_von_user: 0,
628-
eingegeben_am_datum: "2018-01-01",
629-
eingegeben_am_zeit: "00:00:00",
630-
datenhistory: "USER_IDENTIFIER"
622+
TEIL_VORNAME: "John",
623+
TEIL_NACHNAME: "Doe",
624+
TEIL_EMAIL: "johndoe@vhslq.de",
625+
TEIL_NOTIZEN: "john's notes",
626+
TEIL_PASSWORT: "hunter22",
627+
EINGEGEBEN_VON_USER: 0,
628+
EINGEGEBEN_AM_DATUM: "2018-01-01",
629+
EINGEGEBEN_AM_ZEIT: "00:00:00",
630+
DATENHISTORY: "USER_IDENTIFIER"
631631
}
632632
}
633633
}

server/test/user.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ describe('User', () => {
4949
.end((err, res) => {
5050
res.should.have.status(200);
5151
res.body.should.be.a('object');
52-
res.body.user.teil_vorname.should.equal(sampleUser.teil_vorname);
53-
res.body.user.teil_nachname.should.equal(sampleUser.teil_nachname);
54-
res.body.user.teil_email.should.equal(sampleUser.teil_email);
55-
userID = res.body.user.id;
52+
res.body.user.TEIL_VORNAME.should.equal(sampleUser.TEIL_VORNAME);
53+
res.body.user.TEIL_NACHNAME.should.equal(sampleUser.TEIL_NACHNAME);
54+
res.body.user.TEIL_EMAIL.should.equal(sampleUser.TEIL_EMAIL);
55+
userID = res.body.user.TEIL_ID;
5656
authToken = res.body.token;
5757
done();
5858
})
@@ -73,11 +73,11 @@ describe('User', () => {
7373
chai.request(server)
7474
.put('/v1/user/me')
7575
.set('authorization', 'Bearer ' + authToken)
76-
.send({teil_nachname: "Although"})
76+
.send({TEIL_NACHNAME: "Although"})
7777
.end((err, res) => {
7878
res.should.have.status(200);
7979
res.body.should.be.a('object');
80-
res.body.teil_nachname.should.equal("Although");
80+
res.body.TEIL_NACHNAME.should.equal("Although");
8181
chai.request(server)
8282
.get('/v1/user/me')
8383
.set('authorization', 'Bearer ' + authToken)
@@ -92,6 +92,7 @@ describe('User', () => {
9292
});
9393
it("it should get John Doe's Courses", (done) => {
9494
dbHelper.User.addCoursesToSampleUser(userID).then(() => {
95+
9596
chai.request(server)
9697
.get('/v1/user/me/courses')
9798
.set('authorization', 'Bearer ' + authToken)
@@ -107,15 +108,15 @@ describe('User', () => {
107108
})
108109
it("it should try to create a user with an already used emai and fail", (done) => {
109110
let user = {
110-
teil_vorname: "Peter",
111-
teil_nachname: "Lustig",
112-
teil_email: "johndoe@vhslq.de",
113-
teil_notizen: "Peter's notes",
114-
teil_passwort: "hunter22",
115-
eingegeben_von_user: 0,
116-
eingegeben_am_datum: "2018-01-01",
117-
eingegeben_am_zeit: "00:00:00",
118-
datenhistory: "Peters's data history"
111+
TEIL_VORNAME: "Peter",
112+
TEIL_NACHNAME: "Lustig",
113+
TEIL_EMAIL: "johndoe@vhslq.de",
114+
TEIL_NOTIZEN: "Peter's notes",
115+
TEIL_PASSWORT: "hunter22",
116+
EINGEGEBEN_VON_USER: 0,
117+
EINGEGEBEN_AM_DATUM: "2018-01-01",
118+
EINGEGEBEN_AM_ZEIT: "00:00:00",
119+
DATENHISTORY: "Peters's data history"
119120
}
120121
chai.request(server)
121122
.post('/v1/user')

server/utils/trim.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
let courseFields = ["KURS_ID", "KURS_NAME", "KURS_BESCHREIBUNG", "location", "teacher"];
3+
let categoryFields = ["RUB_ID", "RUB_NAME", "RUB_TITEL", "RUB_TEXT"];
4+
let userFields = ["TEIL_ID", "TEIL_EMAIL", "TEIL_VORNAME", "TEIL_NACHNAME"];
5+
let locationFields = ["ORT_ID", "ORT_NAME", "ORT_STRASSE", "ORT_PLZ", "ORT_ORTSNAME"];
6+
7+
// for testing purposes also add field DATENHISTORY to each object
8+
if (process.env.NODE_ENV === 'test') {
9+
courseFields.push("DATENHISTORY");
10+
categoryFields.push("DATENHISTORY");
11+
userFields.push("DATENHISTORY");
12+
locationFields.push("DATENHISTORY");
13+
}
14+
15+
let trimObject = (object, array) => {
16+
return array
17+
.map(item => object[item] ? {[item]: object[item]} : null)
18+
.filter(item => item)
19+
.reduce((old, current) => {
20+
return {...old, ...current}
21+
}, {});
22+
};
23+
24+
let trimCourse = (course) => {
25+
let newCourse = trimObject(course, courseFields);
26+
newCourse.teacher = newCourse.teacher ? trimUser(newCourse.teacher) : {};
27+
newCourse.location = newCourse.location ? trimLocation(newCourse.location) : {};
28+
return newCourse;
29+
}
30+
let trimCourses = (courses) => {
31+
return courses.map(course => trimCourse(course));
32+
}
33+
let trimCategory = (category) => {
34+
let newCategory = trimObject(category, categoryFields);
35+
newCategory.courses = newCategory.courses ? trimCourses(newCategory.courses) : {};
36+
return newCategory;
37+
}
38+
39+
let trimCategories = (categories) => {
40+
return categories.map(category => trimCategory(category));
41+
}
42+
43+
let trimUser = (user) => {
44+
return trimObject(user, userFields);
45+
}
46+
let trimUsers = (users) => {
47+
return users.map(user => trimUser(user));
48+
}
49+
let trimLocation = (location) => {
50+
return trimObject(location, locationFields);
51+
}
52+
53+
exports.course = trimCourse;
54+
exports.courses = trimCourses;
55+
56+
exports.category = trimCategory;
57+
exports.categories = trimCategories;
58+
59+
exports.user = trimUser;
60+
exports.users = trimUsers;

0 commit comments

Comments
 (0)