UNPKG

@universis/candidates

Version:

Universis api server plugin for study program candidates, internship selection etc

146 lines (49 loc) 6.49 kB
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.afterSave = afterSave;exports.CreateStudentAfterAcceptCandidate = void 0;var _data = require("@themost/data"); var _common = require("@themost/common"); var _path = _interopRequireDefault(require("path")); var _insertListener = require("../utils/insertListener");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {Promise.resolve(value).then(_next, _throw);}}function _asyncToGenerator(fn) {return function () {var self = this,args = arguments;return new Promise(function (resolve, reject) {var gen = fn.apply(self, args);function _next(value) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);}function _throw(err) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);}_next(undefined);});};} /** * @param {DataEventArgs} event */function afterSaveAsync(_x) {return _afterSaveAsync.apply(this, arguments);} /** * @param {DataEventArgs} event * @param {Function} callback */function _afterSaveAsync() {_afterSaveAsync = _asyncToGenerator(function* (event) {const context = event.model.context; // get current status const actionStatus = yield context.model('StudyProgramRegisterAction').where('id').equal(event.target.id).select('actionStatus/alternateName').value();if (actionStatus !== 'CompletedActionStatus') {return;}if (event.state === _data.DataObjectState.Insert) {throw new _common.AccessDeniedError('A study program registration cannot be completed automatically.', null);}if (event.state === _data.DataObjectState.Update) {// get previous status let previousStatus = 'UnknownActionStatus';if (event.previous) {previousStatus = event.previous.actionStatus.alternateName;} else {throw new _common.DataError('E_STATE', 'The previous state of an object cannot be determined', null, 'StudyProgramRegisterAction');}if (previousStatus === 'ActiveActionStatus') {// get register action const item = yield context.model('StudyProgramRegisterAction').where('id').equal(event.target.id).expand('studyProgram', { name: 'candidate', options: { $expand: 'person($expand=gender)' } }, 'attachments', 'studyProgramEnrollmentEvent').silent().getItem();if (item == null) {throw new _common.DataNotFoundError('Current item cannot be found or is inaccessible', null, 'StudyProgramRegisterAction');} /** * @type {Student} */let student = item.candidate.student ? context.model('Student').convert(item.candidate.student) : null;if (student == null) {/** * @type {CandidateStudent} */const candidate = context.model('CandidateStudent').convert(item.candidate);student = yield candidate.convertCandidateToStudent(context); // if the event requires automated obligatory courses registration if (item.studyProgramEnrollmentEvent && item.studyProgramEnrollmentEvent.autoRegisterCourses) {// autoRegisterClasses method is static, so load base class through ModelClassLoaderStrategy // to prevent direct import from the parent api const applicationConfiguration = context.getApplication().getConfiguration(); // get schema loader const schemaLoader = applicationConfiguration.getStrategy(_data.SchemaLoaderStrategy);_common.Args.notNull(schemaLoader, 'Schema loader'); // get student model definition const model = schemaLoader.getModelDefinition('Student');_common.Args.notNull(model, 'Student model'); // get model class loader const loader = applicationConfiguration.getStrategy(_data.ModelClassLoaderStrategy);_common.Args.notNull(loader, 'Model class loader'); // get student class const studentBaseClass = loader.resolve(model);if (studentBaseClass && typeof studentBaseClass.autoRegisterClasses === 'function') {// create an AutoRegisterClassesAction through autoRegisterClasses student method const autoRegistrationPayload = { students: [student.id], data: { // only obligatory courses of the student's semester onlyObligatory: true, department: student.department } }; // start the async auto courses registration process yield studentBaseClass.autoRegisterClasses(context, autoRegistrationPayload);}}} // add also student attachments if (item.attachments) {const attachments = []; // add to student attachments for (let i = 0; i < item.attachments.length; i++) {attachments.push(item.attachments[i].id);}student.attachments = attachments;if (attachments && attachments.length) {yield context.model('Student').silent().save(student);}}const cache = context.getApplication().getConfiguration().getStrategy(_data.DataCacheStrategy);if (cache != null) {cache.clear();}}}});return _afterSaveAsync.apply(this, arguments);}function afterSave(event, callback) {return afterSaveAsync(event).then(() => {return callback();}).catch(err => {return callback(err);});}class CreateStudentAfterAcceptCandidate extends _common.ApplicationService {constructor(app) {super(app);this.install('StudyProgramRegisterAction', __filename);}install() {/** * get data configuration * @type {DataConfigurationStrategy} */const configuration = this.getApplication().getConfiguration().getStrategy(_data.DataConfigurationStrategy); // get StudyProgramRegisterAction model definition const model = configuration.getModelDefinition('StudyProgramRegisterAction'); // prepare listener const listener = { // get this file path relative to application execution path type: './' + _path.default.relative(this.getApplication().getConfiguration().getExecutionPath(), __filename), insertBefore: 'send-mail-after-status-change' }; // insert listener const updateModelDefinition = (0, _insertListener.insertListener)(listener, model); if (updateModelDefinition) { // set model definition configuration.setModelDefinition(model); } _common.TraceUtils.info(`Services: ${this.constructor.name} service has been successfully installed`); }}exports.CreateStudentAfterAcceptCandidate = CreateStudentAfterAcceptCandidate; //# sourceMappingURL=accept-study-program-registration-listener.js.map