@universis/evaluations 1.0.20 → 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.
@@ -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
  /**
@@ -46,23 +47,32 @@ class EvaluationEvent extends DataObject {
46
47
  // get evaluation key
47
48
  const token = await EvaluationAccessToken.inspect(context, context.user && context.user.authenticationToken);
48
49
  if (token.active === false) {
49
- throw new HttpTokenExpiredError('Evaluation access token has been expired or is invalid.');
50
+ throw new HttpTokenExpiredError(context.__('Evaluation access token has been expired or is invalid.'));
50
51
  }
51
52
  // get evaluation event
52
53
  // get current date
53
54
  let evaluationEvent = await context.model('EvaluationEvent')
54
55
  .where('id').equal(token.sub)
55
- .and('eventStatus/alternateName').equal('EventOpened')
56
+ .expand('eventStatus')
56
57
  .silent().getTypedItem();
57
58
  if (evaluationEvent == null) {
58
59
  throw new HttpNotFoundError('The specified evaluation cannot be found');
59
60
  }
61
+ if (evaluationEvent.eventStatus )
62
+ {
63
+ if (evaluationEvent.eventStatus.alternateName!=='EventOpened') {
64
+ throw new DataError('E_CLOSED_EVENT',context.__('The course evaluation has been closed and is no longer available') );
65
+ }
66
+ }
60
67
  // check dates
61
68
  const currentDate = new Date().setHours(0,0,0,0);
62
69
  const startDate = evaluationEvent.startDate.setHours(0,0,0,0);
63
70
  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.');
71
+ if (currentDate < startDate) {
72
+ throw new DataError('E_NOT_STARTED',context.__('Evaluation period for this course has not been started yet'));
73
+ }
74
+ if (currentDate > endDate) {
75
+ throw new DataError('E_EXPIRED',context.__('Evaluation period for this course has been expired'));
66
76
  }
67
77
  if (evaluationEvent.additionalType !== 'EvaluationEvent') {
68
78
  if (evaluationEvent.additionalType === 'ClassInstructorEvaluationEvent') {
@@ -530,5 +540,146 @@ class EvaluationEvent extends DataObject {
530
540
  }
531
541
  return items;
532
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
+ }
533
684
  }
534
685
  module.exports = EvaluationEvent;