@universis/evaluations 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/.eslintignore +1 -0
  2. package/.eslintrc +34 -0
  3. package/.gitlab-ci.yml +22 -0
  4. package/LICENSE +163 -0
  5. package/README.md +38 -0
  6. package/babel.config.js +3 -0
  7. package/dist/EvaluationSchemaLoader.js +33 -0
  8. package/dist/EvaluationSchemaLoader.js.map +1 -0
  9. package/dist/EvaluationService.js +56 -0
  10. package/dist/EvaluationService.js.map +1 -0
  11. package/dist/config/models/AccessToken.json +76 -0
  12. package/dist/config/models/ClassInstructorEvaluationEvent.json +76 -0
  13. package/dist/config/models/CourseClassEvaluationEvent.json +57 -0
  14. package/dist/config/models/EvaluationAccessToken.json +101 -0
  15. package/dist/config/models/EvaluationDocument.json +72 -0
  16. package/dist/config/models/EvaluationEvent.json +50 -0
  17. package/dist/config/scope.access.json +11 -0
  18. package/dist/evaluationRouter.js +118 -0
  19. package/dist/evaluationRouter.js.map +1 -0
  20. package/dist/handlers/evaluation-key-handler.js +45 -0
  21. package/dist/handlers/evaluation-key-handler.js.map +1 -0
  22. package/dist/index.js +3 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/listeners/class-instructor-event-listener.js +27 -0
  25. package/dist/listeners/class-instructor-event-listener.js.map +1 -0
  26. package/dist/listeners/force-set-state-listener.js +47 -0
  27. package/dist/listeners/force-set-state-listener.js.map +1 -0
  28. package/dist/models/access-token-model.d.ts +37 -0
  29. package/dist/models/access-token-model.js +39 -0
  30. package/dist/models/access-token-model.js.map +1 -0
  31. package/dist/models/evaluation-access-token-model.d.ts +17 -0
  32. package/dist/models/evaluation-access-token-model.js +40 -0
  33. package/dist/models/evaluation-access-token-model.js.map +1 -0
  34. package/dist/models/evaluation-document-model.d.ts +13 -0
  35. package/dist/models/evaluation-document-model.js +150 -0
  36. package/dist/models/evaluation-document-model.js.map +1 -0
  37. package/dist/models/evaluation-event-model.d.ts +13 -0
  38. package/dist/models/evaluation-event-model.js +318 -0
  39. package/dist/models/evaluation-event-model.js.map +1 -0
  40. package/package.json +53 -0
  41. package/spec/CourseEvaluationSampleForm.json +1046 -0
  42. package/spec/EvaluationService.spec.js +9 -0
  43. package/spec/helpers/babel.js +1 -0
  44. package/spec/helpers/env.js +7 -0
  45. package/spec/helpers/reporter.js +11 -0
  46. package/spec/support/jasmine.json +13 -0
  47. package/src/EvaluationSchemaLoader.js +35 -0
  48. package/src/EvaluationService.js +57 -0
  49. package/src/config/models/AccessToken.json +76 -0
  50. package/src/config/models/ClassInstructorEvaluationEvent.json +76 -0
  51. package/src/config/models/CourseClassEvaluationEvent.json +57 -0
  52. package/src/config/models/EvaluationAccessToken.json +101 -0
  53. package/src/config/models/EvaluationDocument.json +72 -0
  54. package/src/config/models/EvaluationEvent.json +50 -0
  55. package/src/config/scope.access.json +11 -0
  56. package/src/evaluationRouter.js +117 -0
  57. package/src/handlers/evaluation-key-handler.js +45 -0
  58. package/src/index.js +2 -0
  59. package/src/listeners/class-instructor-event-listener.js +27 -0
  60. package/src/listeners/force-set-state-listener.js +46 -0
  61. package/src/models/access-token-model.d.ts +37 -0
  62. package/src/models/access-token-model.js +38 -0
  63. package/src/models/evaluation-access-token-model.d.ts +17 -0
  64. package/src/models/evaluation-access-token-model.js +39 -0
  65. package/src/models/evaluation-document-model.d.ts +13 -0
  66. package/src/models/evaluation-document-model.js +149 -0
  67. package/src/models/evaluation-event-model.d.ts +13 -0
  68. package/src/models/evaluation-event-model.js +317 -0
@@ -0,0 +1,117 @@
1
+ import express from 'express';
2
+ import {EvaluationKeyHandler} from "./handlers/evaluation-key-handler";
3
+ import { getEntitySetFunction} from "@themost/express";
4
+ import i18n from "i18n";
5
+ import {postEntitySetFunction} from "@themost/express";
6
+
7
+ export function evaluationRouter(app) {
8
+
9
+ const router = express.Router();
10
+
11
+ router.use((req, res, next) => {
12
+ // create router context
13
+ const newContext = app.createContext();
14
+ /**
15
+ * try to find if request has already a data context
16
+ * @type {ExpressDataContext|*}
17
+ */
18
+ const interactiveContext = req.context;
19
+ // finalize already assigned context
20
+ if (interactiveContext) {
21
+ if (typeof interactiveContext.finalize === 'function') {
22
+ // finalize interactive context
23
+ return interactiveContext.finalize(() => {
24
+ // and assign new context
25
+ Object.defineProperty(req, 'context', {
26
+ enumerable: false,
27
+ configurable: true,
28
+ get: () => {
29
+ return newContext
30
+ }
31
+ });
32
+ // exit handler
33
+ return next();
34
+ });
35
+ }
36
+ }
37
+ // otherwise assign context
38
+ Object.defineProperty(req, 'context', {
39
+ enumerable: false,
40
+ configurable: true,
41
+ get: () => {
42
+ return newContext
43
+ }
44
+ });
45
+ // and exit handler
46
+ return next();
47
+ });
48
+ router.use((req, res, next) => {
49
+ // set context locale from request
50
+ req.context.locale = req.locale;
51
+ // set translation
52
+ req.context.translate = function() {
53
+ return i18n.__.apply(req.context, Array.from(arguments));
54
+ };
55
+ return next();
56
+ });
57
+ // use this handler to finalize router context
58
+ // important note: context finalization is very important in order
59
+ // to close and finalize database connections, cache connections etc.
60
+ router.use((req, res, next) => {
61
+ req.on('end', () => {
62
+ //on end
63
+ if (req.context) {
64
+ //finalize data context
65
+ return req.context.finalize( () => {
66
+ //
67
+ });
68
+ }
69
+ });
70
+ return next();
71
+ });
72
+
73
+ router.use(async (req, res, next) => {
74
+ try {
75
+ await new EvaluationKeyHandler().beginRequestAsync(req, res);
76
+ return next();
77
+ } catch (err) {
78
+ return next(err);
79
+ }
80
+ });
81
+
82
+ router.get('/', (req, res, next) => {
83
+ // set entity set parameters
84
+ Object.assign(req.params, {
85
+ entitySetFunction : 'Current'
86
+ });
87
+ return next();
88
+ }, getEntitySetFunction({
89
+ entitySet: 'EvaluationEvents'
90
+ }));
91
+
92
+ router.get('/Form', (req, res, next) => {
93
+ // set entity set parameters
94
+ Object.assign(req.params, {
95
+ entitySetFunction : 'Current',
96
+ entityFunction : 'Form'
97
+ });
98
+ return next();
99
+ }, getEntitySetFunction({
100
+ entitySet: 'EvaluationEvents'
101
+ }));
102
+
103
+ router.post('/Evaluate', (req, res, next) => {
104
+ // set entity set parameters
105
+ Object.assign(req.params, {
106
+ entitySetFunction : 'Current',
107
+ entityAction : 'Evaluate'
108
+ });
109
+ return next();
110
+ }, postEntitySetFunction({
111
+ entitySet: 'EvaluationEvents'
112
+ }));
113
+
114
+
115
+
116
+ return router;
117
+ }
@@ -0,0 +1,45 @@
1
+ import { HttpTokenExpiredError } from '@themost/common';
2
+ const EvaluationAccessToken = require('../models/evaluation-access-token-model');
3
+
4
+ export class EvaluationKeyHandler {
5
+ static createInstance() {
6
+ return new EvaluationKeyHandler();
7
+ }
8
+
9
+ beginRequest(req, res, callback) {
10
+ try {
11
+ if (Object.prototype.hasOwnProperty.call(req.headers, 'evaluation-key')) {
12
+ const evaluationKey = req.headers['evaluation-key'].replace(/-/ig, '');
13
+ return EvaluationAccessToken.inspect(req.context, evaluationKey).then((token) => {
14
+ if (token.active === false) {
15
+ return callback(new HttpTokenExpiredError('Evaluation access token has been expired'))
16
+ }
17
+ req.context.user = req.context.user || {
18
+ name: 'anonymous',
19
+ authenticationType: 'Evaluation',
20
+ authenticationToken: token.access_token,
21
+ authenticationScope: token.scope
22
+ };
23
+ return callback();
24
+ }).catch((err) => {
25
+ return callback(err);
26
+ });
27
+ }
28
+ return callback();
29
+ } catch(error){
30
+ return callback(error);
31
+ }
32
+ }
33
+
34
+ beginRequestAsync(req, res) {
35
+ return new Promise(((resolve, reject) => {
36
+ this.beginRequest( req, res, function(err) {
37
+ if (err) {
38
+ return reject(err);
39
+ }
40
+ return resolve(err);
41
+ });
42
+ }));
43
+
44
+ }
45
+ }
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './EvaluationSchemaLoader';
2
+ export * from './EvaluationService';
@@ -0,0 +1,27 @@
1
+ import { DataObjectState } from '@themost/data';
2
+ /**
3
+ *
4
+ * @param {DataEventArgs} event
5
+ */
6
+ async function beforeSaveAsync(event) {
7
+ // get organizer from courseClass
8
+ const context = event.model.context;
9
+ if (event.target.courseClassInstructor) {
10
+ const organizer = await context.model('CourseClassInstructor')
11
+ .where('id').equal(event.target.courseClassInstructor)
12
+ .select('courseClass/course/department').value();
13
+ event.target.organizer = organizer;
14
+ }
15
+ }
16
+ /**
17
+ * @param {DataEventArgs} event
18
+ * @param {Function} callback
19
+ */
20
+ export function beforeSave(event, callback) {
21
+ beforeSaveAsync(event).then(() => {
22
+ return callback()
23
+ }).catch( err => {
24
+ return callback(err);
25
+ })
26
+ }
27
+
@@ -0,0 +1,46 @@
1
+ // eslint-disable-next-line no-unused-vars
2
+ import { DataObjectState } from '@themost/data';
3
+ /**
4
+ *
5
+ * @param {DataEventArgs} event
6
+ */
7
+ async function beforeSaveAsync(event) {
8
+ if (event.state === DataObjectState.Insert) {
9
+ Object.assign(event.target, {
10
+ $state: DataObjectState.Insert
11
+ });
12
+ }
13
+ }
14
+
15
+ /**
16
+ *
17
+ * @param {DataEventArgs} event
18
+ */
19
+ async function afterSaveAsync(event) {
20
+ if (event.state === DataObjectState.Insert) {
21
+ delete event.target.$state;
22
+ }
23
+ }
24
+ /**
25
+ * @param {DataEventArgs} event
26
+ * @param {Function} callback
27
+ */
28
+ export function beforeSave(event, callback) {
29
+ beforeSaveAsync(event).then(() => {
30
+ return callback()
31
+ }).catch( err => {
32
+ return callback(err);
33
+ })
34
+ }
35
+
36
+ /**
37
+ * @param {DataEventArgs} event
38
+ * @param {Function} callback
39
+ */
40
+ export function afterSave(event, callback) {
41
+ afterSaveAsync(event).then(() => {
42
+ return callback()
43
+ }).catch( err => {
44
+ return callback(err);
45
+ })
46
+ }
@@ -0,0 +1,37 @@
1
+ import {EdmMapping,EdmType} from '@themost/data/odata';
2
+
3
+ import {DataObject} from '@themost/data/data-object';
4
+ /**
5
+ * @class
6
+ */
7
+ declare class AccessToken extends DataObject {
8
+
9
+
10
+ /**
11
+ * @description An access token is a string representing an authorization issued to the client.
12
+ */
13
+ public access_token: string;
14
+
15
+ /**
16
+ * @description The authorization server issues the registered client a client identifier -- a unique string representing the registration information provided by the client.
17
+ */
18
+ public client_id: string;
19
+
20
+ /**
21
+ * @description The expiration date and time of this access token.
22
+ */
23
+ public expires?: Date;
24
+
25
+ /**
26
+ * @description Refresh tokens are issued to the client by the authorization server and are used to obtain a new access token when the current access token becomes invalid or expires.
27
+ */
28
+ public refresh_token?: string;
29
+
30
+ /**
31
+ * @description Scope is used to limit the authorization granted to the client by the resource owner.
32
+ */
33
+ public scope?: string;
34
+
35
+ }
36
+
37
+ export = AccessToken;
@@ -0,0 +1,38 @@
1
+ import {DataObject, EdmMapping} from '@themost/data';
2
+
3
+ @EdmMapping.entityType('AccessToken')
4
+ class AccessToken extends DataObject {
5
+
6
+ constructor() {
7
+ super();
8
+ }
9
+
10
+ static async inspect(context, access_token) {
11
+ const token = await context.model('AccessToken')
12
+ .where('access_token').equal(access_token)
13
+ .silent()
14
+ .getTypedItem();
15
+ if (token == null) {
16
+ return {
17
+ active: false
18
+ }
19
+ }
20
+ return {
21
+ active: !token.isExpired(),
22
+ username: token.user_id,
23
+ client_id: token.client_id,
24
+ access_token: token.access_token,
25
+ refresh_token: token.refresh_token,
26
+ scope: token.scope
27
+ };
28
+ }
29
+
30
+ isExpired() {
31
+ if (this.expires == null) {
32
+ return true;
33
+ }
34
+ return (new Date(this.expires)).getTime() < (new Date()).getTime();
35
+ }
36
+ }
37
+
38
+ module.exports = AccessToken;
@@ -0,0 +1,17 @@
1
+ import AccessToken = require('./access-token-model');
2
+ /**
3
+ * @class
4
+ */
5
+ declare class EvaluationAccessToken extends AccessToken {
6
+
7
+
8
+ public evaluationEvent: any;
9
+
10
+ /**
11
+ * @description An access token is a string representing an authorization issued to the client.
12
+ */
13
+ public access_token: string;
14
+
15
+ }
16
+
17
+ export = EvaluationAccessToken;
@@ -0,0 +1,39 @@
1
+ import {EdmMapping} from '@themost/data';
2
+ let AccessToken = require('./access-token-model');
3
+ /**
4
+ * @class
5
+
6
+ * @property {string} access_token
7
+ * @augments {DataObject}
8
+ */
9
+ @EdmMapping.entityType('EvaluationAccessToken')
10
+ class EvaluationAccessToken extends AccessToken {
11
+ /**
12
+ * @constructor
13
+ */
14
+ constructor() {
15
+ super();
16
+ }
17
+
18
+ static async inspect(context, access_token) {
19
+ const token = await context.model('EvaluationAccessToken')
20
+ .where('access_token').equal(access_token)
21
+ .silent()
22
+ .getTypedItem();
23
+ if (token == null) {
24
+ return {
25
+ active: false
26
+ }
27
+ }
28
+ return {
29
+ active: !token.isExpired(),
30
+ username: token.user_id,
31
+ client_id: token.client_id,
32
+ access_token: token.access_token,
33
+ refresh_token: token.refresh_token,
34
+ scope: token.scope,
35
+ sub: token.evaluationEvent
36
+ };
37
+ }
38
+ }
39
+ module.exports = EvaluationAccessToken;
@@ -0,0 +1,13 @@
1
+ import {DataObject} from '@themost/data';
2
+
3
+ /**
4
+ * @class
5
+ */
6
+ declare class EvaluationDocument extends DataObject {
7
+
8
+
9
+ public id: number;
10
+
11
+ }
12
+
13
+ export = EvaluationDocument;
@@ -0,0 +1,149 @@
1
+ import {DataObject, EdmMapping, EdmType} from '@themost/data';
2
+ import _ from "lodash";
3
+ import {PrivateContentService} from "../../../../server/services/content-service";
4
+ import path from "path";
5
+ import {TraceUtils} from "@themost/common/utils";
6
+ import {DataNotFoundError, HttpServerError} from "@themost/common/errors";
7
+ import {MimeTypeError} from "../../../../server/models/enable-attachment-model";
8
+ /**
9
+ * @class
10
+ * @property {number} id
11
+ * @augments {DataObject}
12
+ */
13
+ @EdmMapping.entityType('EvaluationDocument')
14
+ class EvaluationDocument extends DataObject {
15
+ /**
16
+ * @constructor
17
+ */
18
+ constructor() {
19
+ super();
20
+ }
21
+
22
+ static async next(context) {
23
+ return await new Promise((resolve, reject) => {
24
+ context.db.nextIdentity('EvaluationDocument', 'documentNumber', (err, value) => {
25
+ if (err) {
26
+ return reject(err);
27
+ }
28
+ return resolve(value);
29
+ });
30
+ });
31
+ }
32
+
33
+ /**
34
+ * Adds an attachment
35
+ * @param {*} file
36
+ * @param {*=} extraAttributes
37
+ */
38
+ @EdmMapping.param('extraAttributes', 'Object', true, true)
39
+ @EdmMapping.param('file', EdmType.EdmStream, false)
40
+ @EdmMapping.action('Add', 'EvaluationDocument')
41
+ static async add(context, file, extraAttributes) {
42
+ const attachment = Object.assign({
43
+ name: file.contentFileName
44
+ }, file, extraAttributes);
45
+ return await EvaluationDocument._addAttachment(context, attachment);
46
+ }
47
+ /**
48
+ * Removes an attachment
49
+ * @param {*} attachment
50
+ */
51
+ @EdmMapping.param('attachment', 'EvaluationDocument', true, true)
52
+ @EdmMapping.action('RemoveAttachment', 'EvaluationDocument')
53
+ static async removeAttachment(context, attachment) {
54
+ return await EvaluationDocument._removeAttachment(context,attachment.id);
55
+ }
56
+
57
+ static _addAttachment(context, file) {
58
+ let finalResult;
59
+ return new Promise((resolve, reject)=> {
60
+ context.db.executeInTransaction((callback)=> {
61
+ if (typeof file.originalname === 'string') {
62
+ file.name = file.originalname.replace(/\.[A-Z0-9]+$/ig, function (x) {
63
+ return x.toLowerCase();
64
+ });
65
+ }
66
+ const mime = file.mimetype || file.contentType;
67
+ if (_.isNil(mime)) {
68
+ return reject(new MimeTypeError('The specified file type is not supported by the system.'))
69
+ }
70
+ const addAttributes = {};
71
+ _.forEach(context.model('EvaluationDocument').attributeNames, (attribute) => {
72
+ if (Object.prototype.hasOwnProperty.call(file, attribute)) {
73
+ addAttributes[attribute] = file[attribute];
74
+ }
75
+ });
76
+ const newAttachment = _.assign(addAttributes, {contentType: mime});
77
+ const svc = context.getApplication().getService(function PrivateContentService(){});
78
+ //add attachment to attachments
79
+ let filePath;
80
+ if (file.destination && file.filename) {
81
+ filePath = path.resolve(file.destination, file.filename);
82
+ } else {
83
+ filePath = file.path;
84
+ }
85
+ newAttachment.additionalType = 'EvaluationDocument';
86
+ svc.copyFrom(context, filePath, newAttachment, function (err) {
87
+ if (err) {
88
+ TraceUtils.error(err);
89
+ return callback(new HttpServerError());
90
+ }
91
+ svc.resolveUrl(context, newAttachment, function (err, url) {
92
+ if (err) {
93
+ return callback(err);
94
+ } else {
95
+ finalResult = _.assign({}, newAttachment, {
96
+ url: url,
97
+ name: newAttachment.filename
98
+ });
99
+ return callback();
100
+ }
101
+ });
102
+
103
+ });
104
+ }, (err)=> {
105
+ if (err) {
106
+ return reject(err);
107
+ }
108
+ return resolve(finalResult);
109
+ });
110
+ })
111
+
112
+ }
113
+
114
+ removeAttachment(context, id) {
115
+ let result;
116
+ return new Promise((resolve, reject)=> {
117
+ context.db.executeInTransaction((callback)=> {
118
+ //get attachment
119
+ return context.model('Attachment').where('id').equal(id).getItem().then((attachment)=> {
120
+ if (attachment) {
121
+ //remove attachment connection
122
+ return self.property('attachments').remove(attachment,(err)=> {
123
+ //remove attachment
124
+ if (err) {
125
+ return callback(err);
126
+ }
127
+ return context.model('Attachment').silent().remove(attachment).then(()=> {
128
+ result = attachment;
129
+ return callback();
130
+ }).catch((err)=> {
131
+ return callback(err);
132
+ });
133
+ });
134
+ }
135
+ return callback(new DataNotFoundError());
136
+ }).catch((err)=> {
137
+ return callback(err);
138
+ });
139
+ }, (err)=> {
140
+ if (err) {
141
+ return reject(err);
142
+ }
143
+ return resolve(result);
144
+ });
145
+ });
146
+ }
147
+
148
+ }
149
+ module.exports = EvaluationDocument;
@@ -0,0 +1,13 @@
1
+ import {DataObject} from '@themost/data';
2
+
3
+ /**
4
+ * @class
5
+ */
6
+ declare class EvaluationEvent extends DataObject {
7
+
8
+
9
+ public id: string;
10
+
11
+ }
12
+
13
+ export = EvaluationEvent;