@universis/evaluations 1.0.20 → 1.0.21

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.
@@ -128,51 +128,54 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
128
128
 
129
129
  @EdmMapping.action('SendEvaluationTokens', 'SendInstructorTokenAction')
130
130
  async sendEvaluationTokens() {
131
+
131
132
  const context = this.context;
132
133
  // get event and check if event endDate has passed
134
+ try
135
+ {
133
136
  // get current date
134
137
  const currentDate = moment(new Date()).startOf('day').toDate(); // get only date format
135
138
  const event = await context.model('ClassInstructorEvaluationEvent').where('id').equal(this.getId())
136
139
  .and('date(endDate)').greaterOrEqual(currentDate)
137
140
  .expand({
138
- name: 'courseClassInstructor',
139
- options: {
140
- '$expand': 'courseClass($expand=course), instructor' // expand course and instructor so their attributes are available in the mail template
141
- }
142
- },
143
- {
144
- name: 'actions',
145
- options: {
146
- '$expand': 'actionStatus'
147
- }
148
- })
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
+ })
149
152
  .getItem();
150
153
  // validate event
151
154
  if (event == null) {
152
- throw new DataNotFoundError('The event cannot be found or is inaccessible.');
155
+ throw new DataNotFoundError( 'The event cannot be found or is inaccessible.');
153
156
  }
154
157
  // validate status
155
158
  if (!(event.eventStatus && event.eventStatus.alternateName === 'EventOpened')) {
156
- throw new DataError('Send tokens action may only be executed for Open/Active events.');
159
+ throw new DataError('E_INVALID_PERIOD',context.__('Send tokens action may only be executed for Open/Active events'));
157
160
  }
158
161
  // validate event actions
159
162
  if (!(event.actions && event.actions.length)) {
160
- throw new DataNotFoundError('There are not generate token actions for the specified event or they are inaccessible.');
163
+ throw new DataNotFoundError('E_NO_TOKENS',context.__('Token actions were not found for the specified event or they are inaccessible.'));
161
164
  }
162
165
  // find completed token generation action (can only be one)
163
166
  const successfulTokenAction = event.actions.find(generateTokenAction => generateTokenAction.actionStatus.alternateName === 'CompletedActionStatus');
164
167
  if (successfulTokenAction == null) {
165
- throw new DataError('The specified event does not have a completed token generation action.');
168
+ throw new DataError('E_NO_TOKENS','The specified event does not have a completed token generation action.');
166
169
  }
167
170
  // get tokenInfo for this event
168
171
  const tokenInfo = await this.getTokenStatistics();
169
- // validate sent-used seperately for descriptive errors
172
+ // validate sent-used separately for descriptive errors
170
173
  if (tokenInfo.sent) {
171
- throw new DataError(`Cannot proceed with sending tokens, because ${tokenInfo.sent} of them is/are already 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})`);
172
175
  }
173
176
  if (tokenInfo.used) {
174
- throw new DataError(`Cannot proceed with sending tokens, because ${tokenInfo.used} of them is/are already used.`);
175
- }
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
+ }
176
179
  // start gathering students
177
180
  let students;
178
181
  const courseClass = event.courseClassInstructor && event.courseClassInstructor.courseClass;
@@ -185,8 +188,8 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
185
188
  const instructorSections = await context.model('CourseClassSectionInstructor').where('courseClass').equal(courseClass.id)
186
189
  .and('instructor').equal(eventInstructor).select('section/section as sectionId').silent().getItems();
187
190
  if (instructorSections && instructorSections.length === 0) {
188
- throw new DataError('Instructor is not related with any of sections');
189
- }
191
+ throw new DataError('E_INVALID_SECTION','Instructor is not related with any of sections');
192
+ }
190
193
  const sectionIds = instructorSections.map(section => {
191
194
  return section.sectionId;
192
195
  });
@@ -200,16 +203,18 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
200
203
  } else {
201
204
  students = await context.model('StudentCourseClass')
202
205
  .where('courseClass').equal(courseClass.id)
203
- .select('email')
206
+ .select('email','student')
204
207
  .silent()
205
208
  .getAllItems();
206
209
  }
207
210
  if (!(Array.isArray(students) && students.length)) {
208
- throw new DataError('No recipients/students found.');
211
+ throw new DataError('E_STUDENTS_FOUND','No recipients/students found.');
209
212
  }
210
213
  // check total of students
211
214
  if (students.length !== tokenInfo.total) {
212
- throw new DataError(`Cannot proceed to sending tokens. Students are ${students.length} and generated tokens are ${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}.`);
213
218
  }
214
219
  // validate student emails, must all exist and have a valid format
215
220
  // https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
@@ -218,7 +223,7 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
218
223
  return !(student.email && mailRegex.test(student.email));
219
224
  });
220
225
  if (invalidEmail) {
221
- 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'}`);
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})`);
222
227
  }
223
228
  // get tokens
224
229
  let tokens = await this.context.model('EvaluationAccessToken').where('evaluationEvent').equal(this.getId())
@@ -233,21 +238,21 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
233
238
  .silent()
234
239
  .getItem();
235
240
  if (mailTemplate == null) {
236
- throw new DataError('Cannot execute send tokens action because template is missing for SendInstructorTokenAction.');
241
+ throw new DataError('E_MISSING_TEMPLATE','Cannot execute send tokens action because template is missing for SendInstructorTokenAction.');
237
242
  }
238
243
  const succeeded = [];
239
244
  const failed = [];
240
245
  try {
241
246
  // format mail subject expected params ${course}, ${instructor}
242
- const exp = new RegExp('\\$\\{\\w+\\}+','ig');
247
+ const exp = new RegExp('\\$\\{\\w+\\}+', 'ig');
243
248
  let subject = mailTemplate.subject;
244
249
  const params = subject.match(exp);
245
- if (params.length>0) {
250
+ if (params.length > 0) {
246
251
  // parse only custom parameters ${instructor},${course}
247
252
  params.forEach(param => {
248
253
  if (param === '${instructor}') {
249
254
  // get instructor full name
250
- subject = subject.replace(param, eventInstructor? `${eventInstructor.familyName} ${eventInstructor.givenName}` : '');
255
+ subject = subject.replace(param, eventInstructor ? `${eventInstructor.familyName} ${eventInstructor.givenName}` : '');
251
256
  } else if (param === '${course}') {
252
257
  // get courseClass code and title
253
258
  subject = subject.replace(param, courseClass.course ? `${courseClass.course.displayCode} ${courseClass.title}` : '');
@@ -289,9 +294,9 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
289
294
  }
290
295
  })();
291
296
  // update successfully sent tokens
292
- for (const succededToken of succeeded) {
297
+ for (const succeededToken of succeeded) {
293
298
  await context.model('EvaluationAccessToken').silent().save({
294
- access_token: succededToken, // access_token is primary
299
+ access_token: succeededToken, // access_token is primary
295
300
  sent: true,
296
301
  $state: 2
297
302
  });
@@ -304,8 +309,9 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
304
309
  sent: succeeded.length,
305
310
  failed: failed.length,
306
311
  actionStatus: {
307
- alternateName: 'CompletedActionStatus'
308
- }
312
+ alternateName: tokenInfo.total === failed.length ?'FailedActionStatus':'CompletedActionStatus'
313
+ },
314
+ description: tokenInfo.total===failed.length? context.__('Sending tokens failed for all students'):null
309
315
  }
310
316
  // and save
311
317
  return await context.model('SendInstructorTokenAction').silent().save(sendTokensAction);
@@ -313,9 +319,9 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
313
319
  // log error
314
320
  TraceUtils.error(err);
315
321
  // update successfully sent tokens
316
- for (const succededToken of succeeded) {
322
+ for (const succeededToken of succeeded) {
317
323
  await context.model('EvaluationAccessToken').silent().save({
318
- access_token: succededToken, // access_token is primary
324
+ access_token: succeededToken, // access_token is primary
319
325
  sent: true,
320
326
  $state: 2
321
327
  });
@@ -335,5 +341,24 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
335
341
  return await context.model('SendInstructorTokenAction').silent().save(sendTokensAction);
336
342
  }
337
343
  }
344
+ catch(err)
345
+ {
346
+ TraceUtils.error(err);
347
+ // create send tokens action
348
+ const sendTokensAction = {
349
+ event: this.getId(),
350
+ numberOfStudents: 0,
351
+ total: 0,
352
+ sent: 0,
353
+ failed:0,
354
+ actionStatus: {
355
+ alternateName: 'FailedActionStatus'
356
+ },
357
+ description: err.message
358
+ }
359
+ // and save
360
+ return await context.model('SendInstructorTokenAction').silent().save(sendTokensAction);
361
+ }
362
+ }
338
363
  }
339
364
  module.exports = ClassInstructorEvaluationEvent;
@@ -46,23 +46,32 @@ class EvaluationEvent extends DataObject {
46
46
  // get evaluation key
47
47
  const token = await EvaluationAccessToken.inspect(context, context.user && context.user.authenticationToken);
48
48
  if (token.active === false) {
49
- throw new HttpTokenExpiredError('Evaluation access token has been expired or is invalid.');
49
+ throw new HttpTokenExpiredError(context.__('Evaluation access token has been expired or is invalid.'));
50
50
  }
51
51
  // get evaluation event
52
52
  // get current date
53
53
  let evaluationEvent = await context.model('EvaluationEvent')
54
54
  .where('id').equal(token.sub)
55
- .and('eventStatus/alternateName').equal('EventOpened')
55
+ .expand('eventStatus')
56
56
  .silent().getTypedItem();
57
57
  if (evaluationEvent == null) {
58
58
  throw new HttpNotFoundError('The specified evaluation cannot be found');
59
59
  }
60
+ if (evaluationEvent.eventStatus )
61
+ {
62
+ if (evaluationEvent.eventStatus.alternateName!=='EventOpened') {
63
+ throw new DataError('E_CLOSED_EVENT',context.__('The course evaluation has been closed and is no longer available') );
64
+ }
65
+ }
60
66
  // check dates
61
67
  const currentDate = new Date().setHours(0,0,0,0);
62
68
  const startDate = evaluationEvent.startDate.setHours(0,0,0,0);
63
69
  const endDate = evaluationEvent.endDate.setHours(0,0,0,0);
64
- if (startDate>currentDate || endDate<currentDate) {
65
- throw new HttpNotFoundError('The specified evaluation cannot be found, it is expired or not started.');
70
+ if (currentDate < startDate) {
71
+ throw new DataError('E_NOT_STARTED',context.__('Evaluation period for this course has not been started yet'));
72
+ }
73
+ if (currentDate > endDate) {
74
+ throw new DataError('E_EXPIRED',context.__('Evaluation period for this course has been expired'));
66
75
  }
67
76
  if (evaluationEvent.additionalType !== 'EvaluationEvent') {
68
77
  if (evaluationEvent.additionalType === 'ClassInstructorEvaluationEvent') {