@universis/evaluations 1.0.6 → 1.0.9
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.
- package/dist/config/models/ClassInstructorEvaluationEvent.json +33 -15
- package/dist/config/models/EvaluationAccessToken.json +15 -0
- package/dist/config/models/EvaluationDocument.json +0 -8
- package/dist/config/models/GenerateInstructorTokenAction.json +19 -2
- package/dist/config/models/SendInstructorTokenAction.json +52 -0
- package/dist/config/models/SendTokenAction.json +45 -0
- package/dist/models/class-instructor-evaluation-event-model.js +281 -68
- package/dist/models/class-instructor-evaluation-event-model.js.map +1 -1
- package/dist/models/evaluation-event-model.js +19 -3
- package/dist/models/evaluation-event-model.js.map +1 -1
- package/package.json +1 -1
- package/src/config/models/ClassInstructorEvaluationEvent.json +33 -15
- package/src/config/models/EvaluationAccessToken.json +15 -0
- package/src/config/models/EvaluationDocument.json +0 -8
- package/src/config/models/GenerateInstructorTokenAction.json +19 -2
- package/src/config/models/SendInstructorTokenAction.json +52 -0
- package/src/config/models/SendTokenAction.json +45 -0
- package/src/models/class-instructor-evaluation-event-model.js +216 -3
- package/src/models/evaluation-event-model.js +20 -4
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {EdmMapping} from '@themost/data';
|
|
1
|
+
import {EdmMapping, EdmType} from '@themost/data';
|
|
2
2
|
import EvaluationEvent from "./evaluation-event-model";
|
|
3
|
-
import {TraceUtils} from "@themost/common";
|
|
3
|
+
import {DataError, TraceUtils, DataNotFoundError} from "@themost/common";
|
|
4
|
+
import {getMailer} from "@themost/mailer";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* @class
|
|
@@ -25,7 +26,12 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
|
|
|
25
26
|
};
|
|
26
27
|
// get courseClass instructor
|
|
27
28
|
const event = await context.model('ClassInstructorEvaluationEvent').where('id').equal(this.id)
|
|
28
|
-
.expand(
|
|
29
|
+
.expand({
|
|
30
|
+
name: 'courseClassInstructor',
|
|
31
|
+
options: {
|
|
32
|
+
'$expand': 'courseClass'
|
|
33
|
+
}
|
|
34
|
+
})
|
|
29
35
|
.getItem();
|
|
30
36
|
if (event) {
|
|
31
37
|
//check if a GenerateInstructorTokenAction exists
|
|
@@ -88,7 +94,214 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
|
|
|
88
94
|
return await context.model('GenerateInstructorTokenAction').save(generateAction);
|
|
89
95
|
|
|
90
96
|
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
@EdmMapping.func('TokenInfo', EdmType.CollectionOf('Object'))
|
|
100
|
+
async getTokenStatistics() {
|
|
101
|
+
const tokenInfo = {
|
|
102
|
+
total: 0,
|
|
103
|
+
used: 0,
|
|
104
|
+
sent: 0
|
|
105
|
+
};
|
|
106
|
+
// get number if tokens
|
|
107
|
+
const tokens = await this.context.model('EvaluationAccessToken').where('evaluationEvent').equal(this.getId())
|
|
108
|
+
.select('count(evaluationEvent) as total', 'used', 'sent')
|
|
109
|
+
.groupBy('used', 'sent', 'evaluationEvent')
|
|
110
|
+
.silent().getItems();
|
|
111
|
+
tokens.map(item => {
|
|
112
|
+
tokenInfo.total += item.total;
|
|
113
|
+
tokenInfo.used += item.used ? item.total : 0;
|
|
114
|
+
tokenInfo.sent += item.sent ? item.total : 0;
|
|
115
|
+
});
|
|
116
|
+
return tokenInfo;
|
|
117
|
+
}
|
|
91
118
|
|
|
119
|
+
@EdmMapping.action('SendEvaluationTokens', 'SendInstructorTokenAction')
|
|
120
|
+
async sendEvaluationTokens() {
|
|
121
|
+
const context = this.context;
|
|
122
|
+
// get event
|
|
123
|
+
const event = await context.model('ClassInstructorEvaluationEvent').where('id').equal(this.getId())
|
|
124
|
+
.expand({
|
|
125
|
+
name: 'courseClassInstructor',
|
|
126
|
+
options: {
|
|
127
|
+
'$expand': 'courseClass($expand=course), instructor' // expand course and instructor so their attributes are available in the mail template
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: 'actions',
|
|
132
|
+
options: {
|
|
133
|
+
'$expand': 'actionStatus'
|
|
134
|
+
}
|
|
135
|
+
})
|
|
136
|
+
.getItem();
|
|
137
|
+
// validate event
|
|
138
|
+
if (event == null) {
|
|
139
|
+
throw new DataNotFoundError('The event cannot be found or is inaccessible.');
|
|
140
|
+
}
|
|
141
|
+
// validate status
|
|
142
|
+
if (!(event.eventStatus && event.eventStatus.alternateName === 'EventOpened')) {
|
|
143
|
+
throw new DataError('Send tokens action may only be executed for Open/Active events.');
|
|
144
|
+
}
|
|
145
|
+
// validate event actions
|
|
146
|
+
if (!(event.actions && event.actions.length)) {
|
|
147
|
+
throw new DataNotFoundError('There are not generate token actions for the specified event or they are inaccessible.');
|
|
148
|
+
}
|
|
149
|
+
// find completed token generation action (can only be one)
|
|
150
|
+
const successfulTokenAction = event.actions.find(generateTokenAction => generateTokenAction.actionStatus.alternateName === 'CompletedActionStatus');
|
|
151
|
+
if (successfulTokenAction == null) {
|
|
152
|
+
throw new DataError('The specified event does not have a completed token generation action.');
|
|
153
|
+
}
|
|
154
|
+
// get tokenInfo for this event
|
|
155
|
+
const tokenInfo = await this.getTokenStatistics();
|
|
156
|
+
// validate sent-used seperately for descriptive errors
|
|
157
|
+
if (tokenInfo.sent) {
|
|
158
|
+
throw new DataError(`Cannot proceed with sending tokens, because ${tokenInfo.sent} of them is/are already sent.`);
|
|
159
|
+
}
|
|
160
|
+
if (tokenInfo.used) {
|
|
161
|
+
throw new DataError(`Cannot proceed with sending tokens, because ${tokenInfo.used} of them is/are already used.`);
|
|
162
|
+
}
|
|
163
|
+
// start gathering students
|
|
164
|
+
let students;
|
|
165
|
+
const courseClass = event.courseClassInstructor && event.courseClassInstructor.courseClass;
|
|
166
|
+
const eventInstructor = event.courseClassInstructor && event.courseClassInstructor.instructor;
|
|
167
|
+
// get class sections
|
|
168
|
+
const sections = await context.model('CourseClassSection').where('courseClass').equal(courseClass.id).getItems();
|
|
169
|
+
// check if course class has sections
|
|
170
|
+
if (sections && sections.length > 0 && courseClass.mustRegisterSection !== 0) {
|
|
171
|
+
// get only number of students registered at specific section
|
|
172
|
+
const instructorSections = await context.model('CourseClassSectionInstructor').where('courseClass').equal(courseClass.id)
|
|
173
|
+
.and('instructor').equal(eventInstructor).select('section/section as sectionId').silent().getItems();
|
|
174
|
+
if (instructorSections && instructorSections.length === 0) {
|
|
175
|
+
throw new DataError('Instructor is not related with any of sections');
|
|
176
|
+
}
|
|
177
|
+
const sectionIds = instructorSections.map(section => {
|
|
178
|
+
return section.sectionId;
|
|
179
|
+
});
|
|
180
|
+
// get students for specific course class sections
|
|
181
|
+
students = await context.model('StudentCourseClass')
|
|
182
|
+
.where('courseClass').equal(courseClass.id)
|
|
183
|
+
.and('section').in(sectionIds)
|
|
184
|
+
.select('email')
|
|
185
|
+
.silent()
|
|
186
|
+
.getAllItems();
|
|
187
|
+
} else {
|
|
188
|
+
students = await context.model('StudentCourseClass')
|
|
189
|
+
.where('courseClass').equal(courseClass.id)
|
|
190
|
+
.select('email')
|
|
191
|
+
.silent()
|
|
192
|
+
.getAllItems();
|
|
193
|
+
}
|
|
194
|
+
if (!(Array.isArray(students) && students.length)) {
|
|
195
|
+
throw new DataError('No recipients/students found.');
|
|
196
|
+
}
|
|
197
|
+
// check total of students
|
|
198
|
+
if (students.length !== tokenInfo.total) {
|
|
199
|
+
throw new DataError(`Cannot proceed to sending tokens. Students are ${students.length} and generated tokens are ${tokenInfo.total}.`);
|
|
200
|
+
}
|
|
201
|
+
// validate student emails, must all exist and have a valid format
|
|
202
|
+
// https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
|
|
203
|
+
const mailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
204
|
+
const invalidEmail = students.find(student => {
|
|
205
|
+
return !(student.email && mailRegex.test(student.email));
|
|
206
|
+
});
|
|
207
|
+
if (invalidEmail) {
|
|
208
|
+
throw new DataError(`Cannot procced to sending tokens because at least one student's email is null or invalid. Found invalid email: ${invalidEmail.email || 'empty'}`);
|
|
209
|
+
}
|
|
210
|
+
// get tokens
|
|
211
|
+
let tokens = await this.context.model('EvaluationAccessToken').where('evaluationEvent').equal(this.getId())
|
|
212
|
+
.select('access_token')
|
|
213
|
+
.silent()
|
|
214
|
+
.getAllItems();
|
|
215
|
+
// and map them to an array
|
|
216
|
+
tokens = tokens.map(token => token.access_token);
|
|
217
|
+
// get mail template
|
|
218
|
+
const mailTemplate = await context.model('MailConfiguration')
|
|
219
|
+
.where('target').equal('SendInstructorTokenAction')
|
|
220
|
+
.silent()
|
|
221
|
+
.getItem();
|
|
222
|
+
if (mailTemplate == null) {
|
|
223
|
+
throw new DataError('Cannot execute send tokens action because template is missing for SendInstructorTokenAction.');
|
|
224
|
+
}
|
|
225
|
+
const succeeded = [];
|
|
226
|
+
const failed = [];
|
|
227
|
+
try {
|
|
228
|
+
// use an async function to send emails
|
|
229
|
+
await (async () => {
|
|
230
|
+
for (const student of students) {
|
|
231
|
+
// refresh mailer
|
|
232
|
+
const mailer = getMailer(context); // maybe investigate why this has to be refreshed.
|
|
233
|
+
// pop an access token
|
|
234
|
+
const access_token = tokens.pop();
|
|
235
|
+
// send message
|
|
236
|
+
await new Promise(resolve => {
|
|
237
|
+
mailer
|
|
238
|
+
.template(mailTemplate.template)
|
|
239
|
+
.subject(mailTemplate.subject)
|
|
240
|
+
.to(student.email)
|
|
241
|
+
.send(Object.assign(event, {
|
|
242
|
+
access_token
|
|
243
|
+
}), (err) => {
|
|
244
|
+
if (err) {
|
|
245
|
+
try {
|
|
246
|
+
TraceUtils.error(`SendEvaluationTokensAction', 'An error occurred while trying to send token to student email ${student.email}`);
|
|
247
|
+
TraceUtils.error(err);
|
|
248
|
+
} catch (err1) {
|
|
249
|
+
// do nothing
|
|
250
|
+
}
|
|
251
|
+
failed.push(access_token);
|
|
252
|
+
return resolve();
|
|
253
|
+
}
|
|
254
|
+
succeeded.push(access_token);
|
|
255
|
+
return resolve();
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
})();
|
|
260
|
+
// update successfully sent tokens
|
|
261
|
+
for (const succededToken of succeeded) {
|
|
262
|
+
await context.model('EvaluationAccessToken').silent().save({
|
|
263
|
+
access_token: succededToken, // access_token is primary
|
|
264
|
+
sent: true,
|
|
265
|
+
$state: 2
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
// create send tokens action
|
|
269
|
+
const sendTokensAction = {
|
|
270
|
+
event: this.getId(),
|
|
271
|
+
numberOfStudents: students.length,
|
|
272
|
+
total: tokenInfo.total,
|
|
273
|
+
sent: succeeded.length,
|
|
274
|
+
failed: failed.length,
|
|
275
|
+
actionStatus: {
|
|
276
|
+
alternateName: 'CompletedActionStatus'
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
// and save
|
|
280
|
+
return await context.model('SendInstructorTokenAction').silent().save(sendTokensAction);
|
|
281
|
+
} catch (err) {
|
|
282
|
+
// log error
|
|
283
|
+
TraceUtils.error(err);
|
|
284
|
+
// update successfully sent tokens
|
|
285
|
+
for (const succededToken of succeeded) {
|
|
286
|
+
await context.model('EvaluationAccessToken').silent().save({
|
|
287
|
+
access_token: succededToken, // access_token is primary
|
|
288
|
+
sent: true,
|
|
289
|
+
$state: 2
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
// create send tokens action
|
|
293
|
+
const sendTokensAction = {
|
|
294
|
+
event: this.getId(),
|
|
295
|
+
numberOfStudents: students.length,
|
|
296
|
+
total: tokenInfo.total,
|
|
297
|
+
sent: succeeded.length,
|
|
298
|
+
failed: succeeded.length ? tokenInfo.total - succeeded.length : tokenInfo.total,
|
|
299
|
+
actionStatus: {
|
|
300
|
+
alternateName: 'CompletedActionStatus'
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
// and save
|
|
304
|
+
return await context.model('SendInstructorTokenAction').silent().save(sendTokensAction);
|
|
92
305
|
}
|
|
93
306
|
}
|
|
94
307
|
}
|
|
@@ -50,7 +50,7 @@ class EvaluationEvent extends DataObject {
|
|
|
50
50
|
// get current date
|
|
51
51
|
const startDate = new Date(); // get current date with time
|
|
52
52
|
const endDate = moment(new Date()).startOf('day').toDate(); // get only date format
|
|
53
|
-
|
|
53
|
+
let evaluationEvent = await context.model('EvaluationEvent')
|
|
54
54
|
.where('id').equal(token.sub)
|
|
55
55
|
.and('date(startDate)').lowerOrEqual(startDate)
|
|
56
56
|
.and('date(endDate)').greaterOrEqual(endDate)
|
|
@@ -60,7 +60,23 @@ class EvaluationEvent extends DataObject {
|
|
|
60
60
|
throw new HttpNotFoundError('The specified evaluation cannot be found');
|
|
61
61
|
}
|
|
62
62
|
if (evaluationEvent.additionalType !== 'EvaluationEvent') {
|
|
63
|
-
|
|
63
|
+
if (evaluationEvent.additionalType === 'ClassInstructorEvaluationEvent') {
|
|
64
|
+
const event = await context.model('ClassInstructorEvaluationEvent')
|
|
65
|
+
.where('id').equal(token.sub)
|
|
66
|
+
.expand({
|
|
67
|
+
name: 'courseClassInstructor',
|
|
68
|
+
options: {
|
|
69
|
+
$expand: 'courseClass($expand=course,year,period),instructor'
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
.silent().getTypedItem();
|
|
73
|
+
const courseClass= event.courseClassInstructor.courseClass;
|
|
74
|
+
const instructor = event.courseClassInstructor.instructor;
|
|
75
|
+
evaluationEvent.courseClass = `${courseClass.course.displayCode}-${courseClass.title} (${courseClass.year.alternateName}-${courseClass.period.name}) `;
|
|
76
|
+
evaluationEvent.instructor = `${instructor.familyName} ${instructor.givenName}`;
|
|
77
|
+
} else {
|
|
78
|
+
evaluationEvent = await evaluationEvent.silent().getAdditionalObject();
|
|
79
|
+
}
|
|
64
80
|
}
|
|
65
81
|
return evaluationEvent;
|
|
66
82
|
}
|
|
@@ -329,11 +345,11 @@ class EvaluationEvent extends DataObject {
|
|
|
329
345
|
n += newItems.length;
|
|
330
346
|
items.push.apply(items, newItems);
|
|
331
347
|
}
|
|
332
|
-
await context.model('EvaluationAccessToken').save(items);
|
|
348
|
+
await context.model('EvaluationAccessToken').silent().save(items);
|
|
333
349
|
return n
|
|
334
350
|
} else {
|
|
335
351
|
items = await EvaluationEvent.generateManyTokens(context, generateOptions);
|
|
336
|
-
await context.model('EvaluationAccessToken').save(items);
|
|
352
|
+
await context.model('EvaluationAccessToken').silent().save(items);
|
|
337
353
|
return items.length;
|
|
338
354
|
}
|
|
339
355
|
}
|