Skip to content

Commit 2103cf6

Browse files
authored
Merge pull request #90 from marc101101/feature/last_minute_courses
Feature/last minute courses
2 parents 2741949 + 606135e commit 2103cf6

7 files changed

Lines changed: 252 additions & 31 deletions

File tree

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@
4141
"cookie-parser": "^1.4.3",
4242
"core-js": "^2.4.1",
4343
"cors": "^2.8.4",
44-
"dateformat": "^3.0.3",
4544
"express": "^4.16.3",
4645
"http-server": "^0.10.0",
4746
"js-yaml": "^3.3.0",
4847
"jsonwebtoken": "^8.3.0",
4948
"knex": "^0.13.0",
5049
"mariasql": "^0.2.6",
51-
"moment": "^2.18.1",
50+
"moment": "^2.22.2",
5251
"nodemon": "^1.11.0",
5352
"rxjs": "^6.1.0",
5453
"rxjs-compat": "^6.0.0-rc.0",

server/api/swagger.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,34 @@ paths:
276276
security:
277277
- PublicKey: []
278278
x-swagger-router-controller: "Courses"
279+
/courses/lastminute:
280+
get:
281+
tags:
282+
- "Courses"
283+
summary: "get last minute courses"
284+
description: "get last minute courses"
285+
operationId: "coursesLastminuteGET"
286+
parameters: []
287+
responses:
288+
200:
289+
description: "OK"
290+
schema:
291+
type: "array"
292+
items:
293+
$ref: "#/definitions/Course"
294+
401:
295+
description: Invalid authentication credentials.
296+
403:
297+
description: Permission 'xxx' denied on file 'yyy'.
298+
404:
299+
description: Permission 'xxx' denied on file 'yyy'.
300+
409:
301+
description: Resource 'xxx' already exists.
302+
500:
303+
description: Internal Server Error.
304+
security:
305+
- PublicKey: []
306+
x-swagger-router-controller: "Courses"
279307
/courses/{course_id}/apply:
280308
post:
281309
tags:

server/controllers/Courses.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,13 @@ module.exports.coursesHighlightsGET = function coursesHighlightsGET (req, res, n
7373
utils.writeJson(res, response);
7474
});
7575
};
76+
77+
module.exports.coursesLastminuteGET = function coursesLastminuteGET (req, res, next) {
78+
Courses.coursesLastminuteGET()
79+
.then(function (response) {
80+
utils.writeJson(res, response);
81+
})
82+
.catch(function (response) {
83+
utils.writeJson(res, response);
84+
});
85+
};

server/service/CoursesService.js

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ var Applications = require('../utils/database').Application;
55

66
var knex = require('../utils/database').knex;
77
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
8-
var dateFormat = require('dateformat');
98
var Errors = require('../utils/errors');
9+
var moment = require('moment');
1010

1111
/**
1212
* apply to participate in specific course
@@ -30,25 +30,7 @@ exports.coursesCourse_idApplyPOST = function (course_id, req) {
3030
.fetchAll()
3131
.then((applications) => {
3232
if (applicationFound(applications, user_id, course_id)) {
33-
let data = {
34-
ANM_DATUM: dateFormat(Date.now(), "yyyy-mm-dd"),
35-
ANM_TEIL_ID: user_id,
36-
ANM_KURS_ID: course_id,
37-
ANM_BEARBEITER: 0,
38-
ANM_STAT_ID: 0,
39-
ANM_ABR_DATUM: 0,
40-
ANM_ABR_RUECKLAST_DATUM: 0,
41-
ANM_TNB_GEM_DRUCKEN: 0,
42-
ANM_TEIL_ID_ZAHLER: 0,
43-
ANM_WARTEL_INFORMIEREN: 0,
44-
ANM_ABR_ABRECHNEN: 0,
45-
ANM_ABR_ABGERECHNET: 0,
46-
EINGEGEBEN_VON_USER: user_id,
47-
EINGEGEBEN_AM_DATUM: 0,
48-
EINGEGEBEN_AM_ZEIT: 0,
49-
DATENHISTORY: ""
50-
};
51-
new Applications(data)
33+
new Applications(generateApplicationFor(user_id, course_id))
5234
.save()
5335
.then((application) => {
5436
resolve(application);
@@ -91,6 +73,27 @@ function applicationFound(applications, user_id, course_id) {
9173
return applicationFound;
9274
}
9375

76+
function generateApplicationFor(user_id, course_id) {
77+
return {
78+
ANM_DATUM: moment().format('YYYY-MM-DD'),
79+
ANM_TEIL_ID: user_id,
80+
ANM_KURS_ID: course_id,
81+
ANM_BEARBEITER: 0,
82+
ANM_STAT_ID: 0,
83+
ANM_ABR_DATUM: 0,
84+
ANM_ABR_RUECKLAST_DATUM: 0,
85+
ANM_TNB_GEM_DRUCKEN: 0,
86+
ANM_TEIL_ID_ZAHLER: 0,
87+
ANM_WARTEL_INFORMIEREN: 0,
88+
ANM_ABR_ABRECHNEN: 0,
89+
ANM_ABR_ABGERECHNET: 0,
90+
EINGEGEBEN_VON_USER: user_id,
91+
EINGEGEBEN_AM_DATUM: 0,
92+
EINGEGEBEN_AM_ZEIT: 0,
93+
DATENHISTORY: ""
94+
};
95+
}
96+
9497
/**
9598
* give feedback to a course
9699
* give feedback to a course
@@ -156,7 +159,7 @@ exports.coursesCourse_idSignoffPOST = function (course_id, req) {
156159
ANM_TEIL_ID: user_id,
157160
ANM_KURS_ID: course_id
158161
})
159-
.save({ANM_ABR_ABRECHNEN: 1, ANM_ABR_DATUM: dateFormat(Date.now(), "yyyy-mm-dd")}, {
162+
.save({ANM_ABR_ABRECHNEN: 1, ANM_ABR_DATUM: moment().format('YYYY-MM-DD')}, {
160163
patch: true
161164
})
162165
.then(applicationModel => {
@@ -222,6 +225,27 @@ exports.coursesHighlightsGET = function () {
222225
});
223226
}
224227

228+
exports.coursesLastminuteGET = function() {
229+
return new Promise(function(resolve, reject) {
230+
Courses
231+
.forge()
232+
.query(function(qb) {
233+
qb.whereBetween('KURS_ANMFRIST', [moment().format('YYYY-MM-DD'), moment().add(6, 'weeks').format('YYYY-MM-DD')]);
234+
})
235+
.fetchAll({withRelated: ["applications"]})
236+
.then((courses) => {
237+
resolve(courses
238+
.filter(item => item.related('applications').toJSON().length < item.attributes.KURS_TEIL_MAX)
239+
.filter(item => item.attributes.KURS_KURSSTAT_ID === 3)
240+
.map(item => item.attributes)
241+
)
242+
})
243+
.catch((error) => {
244+
reject(error);
245+
});
246+
});
247+
}
248+
225249
if (process.env.NODE_ENV === 'test') {
226250
exports.clearDataBase = () => {
227251
console.log("Clearing all Content in Table vhslq_kurse");
@@ -256,6 +280,37 @@ if (process.env.NODE_ENV === 'test') {
256280
});
257281
}
258282

283+
exports.setupLastMinute = () => {
284+
console.log("Setting up Last Minute Content in Table vhslq_kurse")
285+
return new Promise((resolve, reject) => {
286+
let sample = require('../utils/sampleData').coursesForLastMinute();
287+
let _Courses = require('../utils/database').Courses;
288+
let courses = _Courses.forge(sample);
289+
290+
Promise.all(courses.invokeMap('save'))
291+
.then((data) => {
292+
let courseIDs = data.map(item => item.attributes.id);
293+
let fullCourse = courseIDs[2];
294+
let _Applications = require('../utils/database').Applications
295+
let applications = _Applications.forge([
296+
generateApplicationFor(1222313, fullCourse),
297+
generateApplicationFor(1222312, fullCourse)
298+
])
299+
Promise.all(applications.invokeMap('save')).then(() => {
300+
console.log("Finished Setting up Last Minute Content in Table vhslq_kurse")
301+
resolve("done");
302+
}).catch((error) => {
303+
console.log(error)
304+
reject(error);
305+
})
306+
})
307+
.catch((error) => {
308+
reject(error);
309+
})
310+
});
311+
}
312+
313+
259314
exports.setupCoursesOfCategory = (category_id) => {
260315
return new Promise((resolve, reject) => {
261316
console.log("Setting up Content for Category with ID " + category_id);

server/test/courses.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let sampleAppliactions = require('../utils/sampleData').applications();
55
let chai = require('chai');
66
let chaiHttp = require('chai-http');
77
let server = require('../../index');
8-
let dateFormat = require('dateformat');
8+
let moment = require('moment');
99

1010
let should = chai.should();
1111

@@ -131,7 +131,7 @@ describe('Courses', () => {
131131
.end((err, res) => {
132132
res.should.have.status(200);
133133
res.body.ANM_ABR_ABRECHNEN.should.equal(1);
134-
res.body.ANM_ABR_DATUM.should.equal(dateFormat(Date.now(), "yyyy-mm-dd"));
134+
res.body.ANM_ABR_DATUM.should.equal(moment().format('YYYY-MM-DD'));
135135
done();
136136
});
137137
});
@@ -147,4 +147,32 @@ describe('Courses', () => {
147147
});
148148
});
149149

150-
});
150+
});
151+
152+
describe("Courses Last Minute", () => {
153+
it("it should get an empty array of courses", (done) => {
154+
coursesService.clearDataBase().then(() => {
155+
chai.request(server)
156+
.get('/v1/courses/lastminute')
157+
.end((err, res) => {
158+
res.should.have.status(200);
159+
res.body.should.be.a('array');
160+
res.body.should.be.empty;
161+
done();
162+
})
163+
})
164+
});
165+
166+
it("it should create 4 courses but only return 1 course as valid last minute course", (done) => {
167+
coursesService.setupLastMinute().then(() => {
168+
chai.request(server)
169+
.get('/v1/courses/lastminute')
170+
.end((err, res) => {
171+
res.should.have.status(200);
172+
res.body.should.be.a('array');
173+
res.body.length.should.equal(1);
174+
done();
175+
})
176+
})
177+
});
178+
});

server/utils/database.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var knex = require('knex')({
1010
}
1111
});
1212

13+
var moment = require('moment');
1314
var bookshelf = require('bookshelf')(knex);
1415
exports.knex = knex;
1516

@@ -19,7 +20,10 @@ let course = bookshelf.Model.extend({
1920
// use function() instead of lambda big arrow here otherwise "this" is not the correct this ....
2021
category: function() {
2122
return this.belongsTo(category, "RUB_ID")
22-
}
23+
},
24+
applications: function() {
25+
return this.hasMany(application, "ANM_KURS_ID", "KURS_ID")
26+
},
2327
})
2428

2529
exports.Course = course
@@ -49,10 +53,6 @@ exports.User = bookshelf.Model.extend({
4953
// -- APPLICATIONS
5054
let application = bookshelf.Model.extend({
5155
tableName: 'vhslq_anmeldungen',
52-
// use function() instead of lambda big arrow here otherwise "this" is not the correct this ....
53-
applications: function() {
54-
return this.hasMany(application, "ANM_ID")
55-
}
5656
})
5757

5858
exports.Application = application;

0 commit comments

Comments
 (0)