@universis/evaluations 1.0.21 → 1.0.22
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/locales/el.json +2 -1
- package/dist/models/class-instructor-evaluation-event-model.js +62 -108
- package/dist/models/class-instructor-evaluation-event-model.js.map +1 -1
- package/dist/models/evaluation-event-model.js +143 -1
- package/dist/models/evaluation-event-model.js.map +1 -1
- package/package.json +1 -1
- package/src/locales/el.json +2 -1
- package/src/models/class-instructor-evaluation-event-model.js +150 -196
- package/src/models/evaluation-event-model.js +142 -0
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {EdmMapping, EdmType} from '@themost/data';
|
|
2
2
|
import EvaluationEvent from "./evaluation-event-model";
|
|
3
3
|
import {DataError, TraceUtils, DataNotFoundError} from "@themost/common";
|
|
4
|
-
import {getMailer} from "@themost/mailer";
|
|
5
4
|
import moment from "moment";
|
|
6
5
|
|
|
7
6
|
/**
|
|
@@ -26,7 +25,7 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
|
|
|
26
25
|
}
|
|
27
26
|
}
|
|
28
27
|
@EdmMapping.action('GenerateEvaluationTokens', 'GenerateInstructorTokenAction')
|
|
29
|
-
|
|
28
|
+
async generateEvaluationTokens() {
|
|
30
29
|
const context = this.context;
|
|
31
30
|
const action = {
|
|
32
31
|
event: this.id,
|
|
@@ -82,13 +81,13 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
|
|
|
82
81
|
{
|
|
83
82
|
evaluationEvent: event.id,
|
|
84
83
|
n: action.numberOfStudents,
|
|
85
|
-
expires
|
|
84
|
+
expires: event.endDate
|
|
86
85
|
};
|
|
87
86
|
try {
|
|
88
87
|
// save number of students to event
|
|
89
88
|
event.numberOfStudents = action.numberOfStudents;
|
|
90
89
|
await context.model('ClassInstructorEvaluationEvent').save(event);
|
|
91
|
-
if (event.numberOfStudents>0) {
|
|
90
|
+
if (event.numberOfStudents > 0) {
|
|
92
91
|
await EvaluationEvent.generateTokens(context, generateOptions);
|
|
93
92
|
}
|
|
94
93
|
generateAction.total = action.numberOfStudents;
|
|
@@ -130,122 +129,140 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
|
|
|
130
129
|
async sendEvaluationTokens() {
|
|
131
130
|
|
|
132
131
|
const context = this.context;
|
|
132
|
+
let sendAction = {};
|
|
133
|
+
let subject;
|
|
133
134
|
// get event and check if event endDate has passed
|
|
134
|
-
try
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
throw new DataError('E_NO_TOKENS','The specified event does not have a completed token generation action.');
|
|
169
|
-
}
|
|
170
|
-
// get tokenInfo for this event
|
|
171
|
-
const tokenInfo = await this.getTokenStatistics();
|
|
172
|
-
// validate sent-used separately for descriptive errors
|
|
173
|
-
if (tokenInfo.sent) {
|
|
174
|
-
throw new DataError('E_ALREADY_SENT',`${context.__('The bulk sending of the tokens could not be completed because some of them had already been sent')} (${tokenInfo.sent})`);
|
|
175
|
-
}
|
|
176
|
-
if (tokenInfo.used) {
|
|
177
|
-
throw new DataError('E_ALREADY_USED',`${context.__('The bulk sending of the tokens could not be completed because some of them had already been used')} (${tokenInfo.used})`);
|
|
178
|
-
}
|
|
179
|
-
// start gathering students
|
|
180
|
-
let students;
|
|
181
|
-
const courseClass = event.courseClassInstructor && event.courseClassInstructor.courseClass;
|
|
182
|
-
const eventInstructor = event.courseClassInstructor && event.courseClassInstructor.instructor;
|
|
183
|
-
// get class sections
|
|
184
|
-
const sections = await context.model('CourseClassSection').where('courseClass').equal(courseClass.id).getItems();
|
|
185
|
-
// check if course class has sections
|
|
186
|
-
if (sections && sections.length > 0 && courseClass.mustRegisterSection !== 0) {
|
|
187
|
-
// get only number of students registered at specific section
|
|
188
|
-
const instructorSections = await context.model('CourseClassSectionInstructor').where('courseClass').equal(courseClass.id)
|
|
189
|
-
.and('instructor').equal(eventInstructor).select('section/section as sectionId').silent().getItems();
|
|
190
|
-
if (instructorSections && instructorSections.length === 0) {
|
|
191
|
-
throw new DataError('E_INVALID_SECTION','Instructor is not related with any of sections');
|
|
135
|
+
try {
|
|
136
|
+
// get current date
|
|
137
|
+
const currentDate = moment(new Date()).startOf('day').toDate(); // get only date format
|
|
138
|
+
const event = await context.model('ClassInstructorEvaluationEvent').where('id').equal(this.getId())
|
|
139
|
+
.and('date(endDate)').greaterOrEqual(currentDate)
|
|
140
|
+
.expand({
|
|
141
|
+
name: 'courseClassInstructor',
|
|
142
|
+
options: {
|
|
143
|
+
'$expand': 'courseClass($expand=course), instructor' // expand course and instructor so their attributes are available in the mail template
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
name: 'actions',
|
|
148
|
+
options: {
|
|
149
|
+
'$expand': 'actionStatus'
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
.getItem();
|
|
153
|
+
// validate event
|
|
154
|
+
if (event == null) {
|
|
155
|
+
throw new DataNotFoundError('The event cannot be found or is inaccessible.');
|
|
156
|
+
}
|
|
157
|
+
// validate status
|
|
158
|
+
if (!(event.eventStatus && event.eventStatus.alternateName === 'EventOpened')) {
|
|
159
|
+
throw new DataError('E_INVALID_PERIOD', context.__('Send tokens action may only be executed for Open/Active events'));
|
|
160
|
+
}
|
|
161
|
+
// validate event actions
|
|
162
|
+
if (!(event.actions && event.actions.length)) {
|
|
163
|
+
throw new DataNotFoundError('E_NO_TOKENS', context.__('Token actions were not found for the specified event or they are inaccessible.'));
|
|
164
|
+
}
|
|
165
|
+
// find completed token generation action (can only be one)
|
|
166
|
+
const successfulTokenAction = event.actions.find(generateTokenAction => generateTokenAction.actionStatus.alternateName === 'CompletedActionStatus');
|
|
167
|
+
if (successfulTokenAction == null) {
|
|
168
|
+
throw new DataError('E_NO_TOKENS', 'The specified event does not have a completed token generation action.');
|
|
192
169
|
}
|
|
193
|
-
|
|
194
|
-
|
|
170
|
+
// get tokenInfo for this event
|
|
171
|
+
const tokenInfo = await this.getTokenStatistics();
|
|
172
|
+
// validate sent-used separately for descriptive errors
|
|
173
|
+
if (tokenInfo.sent) {
|
|
174
|
+
throw new DataError('E_ALREADY_SENT', `${context.__('The bulk sending of the tokens could not be completed because some of them had already been sent')} (${tokenInfo.sent})`);
|
|
175
|
+
}
|
|
176
|
+
if (tokenInfo.used) {
|
|
177
|
+
throw new DataError('E_ALREADY_USED', `${context.__('The bulk sending of the tokens could not be completed because some of them had already been used')} (${tokenInfo.used})`);
|
|
178
|
+
}
|
|
179
|
+
// start gathering students
|
|
180
|
+
let students;
|
|
181
|
+
const courseClass = event.courseClassInstructor && event.courseClassInstructor.courseClass;
|
|
182
|
+
const eventInstructor = event.courseClassInstructor && event.courseClassInstructor.instructor;
|
|
183
|
+
// get class sections
|
|
184
|
+
const sections = await context.model('CourseClassSection').where('courseClass').equal(courseClass.id).getAllItems();
|
|
185
|
+
// check if course class has sections
|
|
186
|
+
if (sections && sections.length > 0 && courseClass.mustRegisterSection !== 0) {
|
|
187
|
+
// get only number of students registered at specific section
|
|
188
|
+
const instructorSections = await context.model('CourseClassSectionInstructor').where('courseClass').equal(courseClass.id)
|
|
189
|
+
.and('instructor').equal(eventInstructor).select('section/section as sectionId').silent().getAllItems();
|
|
190
|
+
if (instructorSections && instructorSections.length === 0) {
|
|
191
|
+
throw new DataError('E_INVALID_SECTION', 'Instructor is not related with any of sections');
|
|
192
|
+
}
|
|
193
|
+
const sectionIds = instructorSections.map(section => {
|
|
194
|
+
return section.sectionId;
|
|
195
|
+
});
|
|
196
|
+
// get students for specific course class sections
|
|
197
|
+
students = await context.model('StudentCourseClass')
|
|
198
|
+
.where('courseClass').equal(courseClass.id)
|
|
199
|
+
.and('section').in(sectionIds)
|
|
200
|
+
.select('email')
|
|
201
|
+
.silent()
|
|
202
|
+
.getAllItems();
|
|
203
|
+
} else {
|
|
204
|
+
students = await context.model('StudentCourseClass')
|
|
205
|
+
.where('courseClass').equal(courseClass.id)
|
|
206
|
+
.select('email', 'student')
|
|
207
|
+
.silent()
|
|
208
|
+
.getAllItems();
|
|
209
|
+
}
|
|
210
|
+
if (!(Array.isArray(students) && students.length)) {
|
|
211
|
+
throw new DataError('E_STUDENTS_FOUND', 'No recipients/students found.');
|
|
212
|
+
}
|
|
213
|
+
// check total of students
|
|
214
|
+
if (students.length !== tokenInfo.total) {
|
|
215
|
+
throw new DataError('E_TOTAL_STUDENTS',
|
|
216
|
+
`${this.context.__("The number of students in the class differs from the generated tokens")}` +
|
|
217
|
+
`(${this.context.__("Students")}: ${students.length}, ${this.context.__("Number of tokens")}:${tokenInfo.total}.`);
|
|
218
|
+
}
|
|
219
|
+
// validate student emails, must all exist and have a valid format
|
|
220
|
+
// https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
|
|
221
|
+
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])?)*$/;
|
|
222
|
+
const invalidEmail = students.find(student => {
|
|
223
|
+
return !(student.email && mailRegex.test(student.email));
|
|
195
224
|
});
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
225
|
+
if (invalidEmail) {
|
|
226
|
+
throw new DataError('E_INVALID_EMAIL', `${context.__('At least one student\'s email is null or invalid')} (email:${invalidEmail.email || ' '} ${context.__('Student')}:${invalidEmail.student})`);
|
|
227
|
+
}
|
|
228
|
+
// get tokens
|
|
229
|
+
let tokens = await this.context.model('EvaluationAccessToken').where('evaluationEvent').equal(this.getId())
|
|
230
|
+
.select('access_token')
|
|
201
231
|
.silent()
|
|
202
232
|
.getAllItems();
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
233
|
+
// and map them to an array
|
|
234
|
+
tokens = tokens.map(token => token.access_token);
|
|
235
|
+
// get mail template
|
|
236
|
+
const mailTemplate = await context.model('MailConfiguration')
|
|
237
|
+
.where('target').equal('SendInstructorTokenAction')
|
|
207
238
|
.silent()
|
|
208
|
-
.
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
.silent()
|
|
232
|
-
.getAllItems();
|
|
233
|
-
// and map them to an array
|
|
234
|
-
tokens = tokens.map(token => token.access_token);
|
|
235
|
-
// get mail template
|
|
236
|
-
const mailTemplate = await context.model('MailConfiguration')
|
|
237
|
-
.where('target').equal('SendInstructorTokenAction')
|
|
238
|
-
.silent()
|
|
239
|
-
.getItem();
|
|
240
|
-
if (mailTemplate == null) {
|
|
241
|
-
throw new DataError('E_MISSING_TEMPLATE','Cannot execute send tokens action because template is missing for SendInstructorTokenAction.');
|
|
242
|
-
}
|
|
243
|
-
const succeeded = [];
|
|
244
|
-
const failed = [];
|
|
245
|
-
try {
|
|
239
|
+
.getItem();
|
|
240
|
+
if (mailTemplate == null) {
|
|
241
|
+
throw new DataError('E_MISSING_TEMPLATE', 'Cannot execute send tokens action because template is missing for SendInstructorTokenAction.');
|
|
242
|
+
}
|
|
243
|
+
// check if there is already an active sendTokensAction
|
|
244
|
+
const alreadySendingTokens = await context.model('SendInstructorTokenAction')
|
|
245
|
+
.where('event').equal(event.id)
|
|
246
|
+
.and('actionStatus/alternateName').equal('ActiveActionStatus')
|
|
247
|
+
.silent().count();
|
|
248
|
+
if (alreadySendingTokens) {
|
|
249
|
+
throw new DataError('E_ALREADY_SENDING', `${context.__('The bulk sending process of tokens is already in progress')}`);
|
|
250
|
+
}
|
|
251
|
+
// save SendInstructorTokenAction
|
|
252
|
+
const sendTokenAction = {
|
|
253
|
+
event: this.getId(),
|
|
254
|
+
numberOfStudents: students.length,
|
|
255
|
+
total: tokenInfo.total,
|
|
256
|
+
sent: 0,
|
|
257
|
+
failed: 0,
|
|
258
|
+
actionStatus: {
|
|
259
|
+
alternateName: 'ActiveActionStatus'
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
sendAction = await context.model('SendInstructorTokenAction').silent().save(sendTokenAction);
|
|
246
263
|
// format mail subject expected params ${course}, ${instructor}
|
|
247
264
|
const exp = new RegExp('\\$\\{\\w+\\}+', 'ig');
|
|
248
|
-
|
|
265
|
+
subject = mailTemplate.subject;
|
|
249
266
|
const params = subject.match(exp);
|
|
250
267
|
if (params.length > 0) {
|
|
251
268
|
// parse only custom parameters ${instructor},${course}
|
|
@@ -261,103 +278,40 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
|
|
|
261
278
|
}
|
|
262
279
|
});
|
|
263
280
|
}
|
|
264
|
-
|
|
265
|
-
await (async () => {
|
|
266
|
-
for (const student of students) {
|
|
267
|
-
// refresh mailer
|
|
268
|
-
const mailer = getMailer(context); // maybe investigate why this has to be refreshed.
|
|
269
|
-
// pop an access token
|
|
270
|
-
const access_token = tokens.pop();
|
|
271
|
-
// send message
|
|
272
|
-
await new Promise(resolve => {
|
|
273
|
-
mailer
|
|
274
|
-
.template(mailTemplate.template)
|
|
275
|
-
.subject(subject)
|
|
276
|
-
.to(student.email)
|
|
277
|
-
.send(Object.assign(event, {
|
|
278
|
-
access_token
|
|
279
|
-
}), (err) => {
|
|
280
|
-
if (err) {
|
|
281
|
-
try {
|
|
282
|
-
TraceUtils.error(`SendEvaluationTokensAction', 'An error occurred while trying to send token to student email ${student.email}`);
|
|
283
|
-
TraceUtils.error(err);
|
|
284
|
-
} catch (err1) {
|
|
285
|
-
// do nothing
|
|
286
|
-
}
|
|
287
|
-
failed.push(access_token);
|
|
288
|
-
return resolve();
|
|
289
|
-
}
|
|
290
|
-
succeeded.push(access_token);
|
|
291
|
-
return resolve();
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
})();
|
|
296
|
-
// update successfully sent tokens
|
|
297
|
-
for (const succeededToken of succeeded) {
|
|
298
|
-
await context.model('EvaluationAccessToken').silent().save({
|
|
299
|
-
access_token: succeededToken, // access_token is primary
|
|
300
|
-
sent: true,
|
|
301
|
-
$state: 2
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
// create send tokens action
|
|
305
|
-
const sendTokensAction = {
|
|
306
|
-
event: this.getId(),
|
|
307
|
-
numberOfStudents: students.length,
|
|
308
|
-
total: tokenInfo.total,
|
|
309
|
-
sent: succeeded.length,
|
|
310
|
-
failed: failed.length,
|
|
311
|
-
actionStatus: {
|
|
312
|
-
alternateName: tokenInfo.total === failed.length ?'FailedActionStatus':'CompletedActionStatus'
|
|
313
|
-
},
|
|
314
|
-
description: tokenInfo.total===failed.length? context.__('Sending tokens failed for all students'):null
|
|
315
|
-
}
|
|
316
|
-
// and save
|
|
317
|
-
return await context.model('SendInstructorTokenAction').silent().save(sendTokensAction);
|
|
281
|
+
EvaluationEvent.sendMessages(context, students, tokens, tokenInfo, mailTemplate, subject, sendAction, event);
|
|
318
282
|
} catch (err) {
|
|
319
|
-
// log error
|
|
320
283
|
TraceUtils.error(err);
|
|
321
|
-
// update successfully sent tokens
|
|
322
|
-
for (const succeededToken of succeeded) {
|
|
323
|
-
await context.model('EvaluationAccessToken').silent().save({
|
|
324
|
-
access_token: succeededToken, // access_token is primary
|
|
325
|
-
sent: true,
|
|
326
|
-
$state: 2
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
284
|
// create send tokens action
|
|
330
|
-
const
|
|
331
|
-
event: this.getId(),
|
|
332
|
-
numberOfStudents: students.length,
|
|
333
|
-
total: tokenInfo.total,
|
|
334
|
-
sent: succeeded.length,
|
|
335
|
-
failed: succeeded.length ? tokenInfo.total - succeeded.length : tokenInfo.total,
|
|
336
|
-
actionStatus: {
|
|
337
|
-
alternateName: 'CompletedActionStatus'
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
// and save
|
|
341
|
-
return await context.model('SendInstructorTokenAction').silent().save(sendTokensAction);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
catch(err)
|
|
345
|
-
{
|
|
346
|
-
TraceUtils.error(err);
|
|
347
|
-
// create send tokens action
|
|
348
|
-
const sendTokensAction = {
|
|
285
|
+
const sendTokenAction = {
|
|
349
286
|
event: this.getId(),
|
|
350
287
|
numberOfStudents: 0,
|
|
351
288
|
total: 0,
|
|
352
289
|
sent: 0,
|
|
353
|
-
failed:0,
|
|
290
|
+
failed: 0,
|
|
354
291
|
actionStatus: {
|
|
355
292
|
alternateName: 'FailedActionStatus'
|
|
356
293
|
},
|
|
357
294
|
description: err.message
|
|
358
295
|
}
|
|
359
296
|
// and save
|
|
360
|
-
|
|
297
|
+
if (sendAction && sendAction.id) {
|
|
298
|
+
sendTokenAction.id = sendAction.id;
|
|
299
|
+
}
|
|
300
|
+
if(!subject) {
|
|
301
|
+
// load courseClass
|
|
302
|
+
const item = await context.model('ClassInstructorEvaluationEvent').where('id').equal(this.getId())
|
|
303
|
+
.expand({
|
|
304
|
+
name: 'courseClassInstructor',
|
|
305
|
+
options: {
|
|
306
|
+
'$expand': 'courseClass($expand=course)'
|
|
307
|
+
}
|
|
308
|
+
})
|
|
309
|
+
.getItem();
|
|
310
|
+
const courseClass =item && item.courseClassInstructor && item.courseClassInstructor.courseClass
|
|
311
|
+
subject =courseClass? `${courseClass.course.displayCode} ${courseClass.title}`: '';
|
|
312
|
+
}
|
|
313
|
+
EvaluationEvent.sendSse(context.user,context, this.getId(), subject, err);
|
|
314
|
+
return await context.model('SendInstructorTokenAction').silent().save(sendTokenAction);
|
|
361
315
|
}
|
|
362
316
|
}
|
|
363
317
|
}
|
|
@@ -23,6 +23,7 @@ import {lt} from 'semver';
|
|
|
23
23
|
import util from "util";
|
|
24
24
|
import fs from "fs";
|
|
25
25
|
import moment from "moment";
|
|
26
|
+
import {getMailer} from "@themost/mailer";
|
|
26
27
|
const math = require('mathjs');
|
|
27
28
|
|
|
28
29
|
/**
|
|
@@ -539,5 +540,146 @@ class EvaluationEvent extends DataObject {
|
|
|
539
540
|
}
|
|
540
541
|
return items;
|
|
541
542
|
}
|
|
543
|
+
|
|
544
|
+
static sendMessages(appContext, students, tokens, tokenInfo, mailTemplate, subject, action, evaluationEvent) {
|
|
545
|
+
const app = appContext.getApplication();
|
|
546
|
+
const context = app.createContext();
|
|
547
|
+
const user= appContext.user;
|
|
548
|
+
let failed = [];
|
|
549
|
+
let succeeded = [];
|
|
550
|
+
let message = subject;
|
|
551
|
+
// use an async function to send emails
|
|
552
|
+
(async () => {
|
|
553
|
+
for (const student of students) {
|
|
554
|
+
// refresh mailer
|
|
555
|
+
const mailer = getMailer(context); // maybe investigate why this has to be refreshed.
|
|
556
|
+
// pop an access token
|
|
557
|
+
const access_token = tokens.pop();
|
|
558
|
+
// send message
|
|
559
|
+
await new Promise(resolve => {
|
|
560
|
+
mailer
|
|
561
|
+
.template(mailTemplate.template)
|
|
562
|
+
.subject(subject)
|
|
563
|
+
.to(student.email)
|
|
564
|
+
.send(Object.assign(evaluationEvent, {
|
|
565
|
+
access_token
|
|
566
|
+
}), (err) => {
|
|
567
|
+
if (err) {
|
|
568
|
+
try {
|
|
569
|
+
TraceUtils.error(`SendEvaluationTokensAction', 'An error occurred while trying to send token to student email ${student.email} for evaluation event ${evaluationEvent.id}`);
|
|
570
|
+
TraceUtils.error(err);
|
|
571
|
+
} catch (err1) {
|
|
572
|
+
// do nothing
|
|
573
|
+
}
|
|
574
|
+
failed.push(access_token);
|
|
575
|
+
return resolve();
|
|
576
|
+
}
|
|
577
|
+
succeeded.push(access_token);
|
|
578
|
+
// update successfully sent token
|
|
579
|
+
return context.model('EvaluationAccessToken').silent().save({
|
|
580
|
+
access_token: access_token, // access_token is primary
|
|
581
|
+
sent: true,
|
|
582
|
+
$state: 2
|
|
583
|
+
}).then(χ => {
|
|
584
|
+
return resolve();
|
|
585
|
+
}).catch(err => {
|
|
586
|
+
TraceUtils.error(err);
|
|
587
|
+
return resolve();
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
});
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
})().then(() => {
|
|
594
|
+
context.finalize(() => {
|
|
595
|
+
// create send tokens action
|
|
596
|
+
const sendTokensAction = {
|
|
597
|
+
id: action && action.id || null,
|
|
598
|
+
event: evaluationEvent,
|
|
599
|
+
numberOfStudents: students.length,
|
|
600
|
+
total: tokenInfo.total,
|
|
601
|
+
sent: succeeded.length,
|
|
602
|
+
failed: failed.length,
|
|
603
|
+
actionStatus: {
|
|
604
|
+
alternateName: tokenInfo.total === failed.length ? 'FailedActionStatus' : 'CompletedActionStatus'
|
|
605
|
+
},
|
|
606
|
+
description: tokenInfo.total === failed.length ? context.__('Sending tokens failed for all students') : null
|
|
607
|
+
}
|
|
608
|
+
// and save
|
|
609
|
+
return context.model('SendInstructorTokenAction').silent().save(sendTokensAction).then(item => {
|
|
610
|
+
EvaluationEvent.sendSse(user,context,evaluationEvent.id,message, sendTokensAction.description?{message: sendTokensAction.description}:null);
|
|
611
|
+
}).catch(err => {
|
|
612
|
+
TraceUtils.error(err);
|
|
613
|
+
});
|
|
614
|
+
});
|
|
615
|
+
}).catch(err => {
|
|
616
|
+
context.finalize(() => {
|
|
617
|
+
// log error
|
|
618
|
+
TraceUtils.error(err);
|
|
619
|
+
// update successfully sent tokens
|
|
620
|
+
const succeededTokens = succeeded.map(succeededToken => {
|
|
621
|
+
return {
|
|
622
|
+
access_token: succeededToken,
|
|
623
|
+
sent: true,
|
|
624
|
+
$state: 2
|
|
625
|
+
}
|
|
626
|
+
})
|
|
627
|
+
return context.model('EvaluationAccessToken').silent().save(succeededTokens).then(evaluationTokens => {
|
|
628
|
+
// create send tokens action
|
|
629
|
+
const sendTokensAction = {
|
|
630
|
+
id: action && action.id || null,
|
|
631
|
+
event: evaluationEvent,
|
|
632
|
+
numberOfStudents: students.length,
|
|
633
|
+
total: tokenInfo.total,
|
|
634
|
+
sent: succeeded.length,
|
|
635
|
+
failed: failed.length,
|
|
636
|
+
actionStatus: {
|
|
637
|
+
alternateName: tokenInfo.total === failed.length ? 'FailedActionStatus' : 'CompletedActionStatus'
|
|
638
|
+
},
|
|
639
|
+
description: tokenInfo.total === failed.length ? context.__('Sending tokens failed for all students') : null
|
|
640
|
+
}
|
|
641
|
+
// and save
|
|
642
|
+
return context.model('SendInstructorTokenAction').silent().save(sendTokensAction).save().then(tokenAction => {
|
|
643
|
+
EvaluationEvent.sendSse(user,context,evaluationEvent.id,message,err);
|
|
644
|
+
}).catch(err => {
|
|
645
|
+
TraceUtils.error(err);
|
|
646
|
+
});
|
|
647
|
+
});
|
|
648
|
+
});
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
static sendSse(user, context, evaluationEvent, title, error) {
|
|
653
|
+
// try to find ServerSentEventService
|
|
654
|
+
const sse = context.getApplication().getService(function ServerSentEventService() {});
|
|
655
|
+
// only if it exists - it is not required
|
|
656
|
+
if (sse != null) {
|
|
657
|
+
if (user != null) {
|
|
658
|
+
try {
|
|
659
|
+
// create an event
|
|
660
|
+
const event = {
|
|
661
|
+
entitySet: 'EvaluationEvents',
|
|
662
|
+
entityType: "EvaluationEvent",
|
|
663
|
+
target: {
|
|
664
|
+
id: evaluationEvent,
|
|
665
|
+
message: error? `${title} ${error.message}`: title
|
|
666
|
+
},
|
|
667
|
+
status: {
|
|
668
|
+
success: !error ,
|
|
669
|
+
},
|
|
670
|
+
dateCreated: new Date()
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
// and send it to the user that is performing the approval (registrar scope)
|
|
674
|
+
sse.emit(user.name, user.authenticationScope || "qa", event);
|
|
675
|
+
}
|
|
676
|
+
catch (err){
|
|
677
|
+
TraceUtils.error(`An error occurred while trying to send sse event`);
|
|
678
|
+
TraceUtils.error(err);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
542
684
|
}
|
|
543
685
|
module.exports = EvaluationEvent;
|