@universis/candidates
Version:
Universis api server plugin for study program candidates, internship selection etc
240 lines (40 loc) • 9.67 kB
JavaScript
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.beforeSave = beforeSave;var _common = require("@themost/common");
var _data = require("@themost/data");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
beforeSaveAsync(_x) {return _beforeSaveAsync.apply(this, arguments);}
/**
* @param {DataEventArgs} event
* @param {Function} callback
*/function _beforeSaveAsync() {_beforeSaveAsync = _asyncToGenerator(function* (event) {// operate only on insert
if (event.state !== _data.DataObjectState.Insert) {return;}if (!Object.prototype.hasOwnProperty.call(event.target, 'candidate')) {return;}const context = event.model.context;const candidate = event.target.candidate; // if candidate is set on insert, exit
if (typeof candidate === 'number' || typeof candidate === 'string' || typeof candidate === 'object' && Object.prototype.hasOwnProperty.call(candidate, 'id')) {return;} // validate isAccessibleForFree property of event
if (event.target.studyProgramEnrollmentEvent == null) {throw new _common.DataError('E_ENROLLMENT_EVENT', 'The study program enrollment event cannot be empty at this context.');}const enrollmentEvent = yield context.model('StudyProgramEnrollmentEvent').where('id').equal(event.target.studyProgramEnrollmentEvent.id || event.target.studyProgramEnrollmentEvent).select('id', 'isAccessibleForFree', 'studyProgram', 'inscriptionMode', 'inscriptionYear', 'inscriptionPeriod').flatten().silent().getItem();if (enrollmentEvent == null) {throw new _common.DataNotFoundError('The specified enrollment event cannot be found');}if (!enrollmentEvent.isAccessibleForFree) {throw new _common.DataError('E_ACCESS', 'The study program does not accept self-registration.');}if (enrollmentEvent.inscriptionMode == null) {throw new _common.DataError('E_DEFAULT_INSCR_MODE', 'The default inscription mode is not set for the event.');} // force set candidate study program from enrollment event
if (candidate.studyProgram == null) {candidate.studyProgram = enrollmentEvent.studyProgram;} // find study program and always validate it
if (!candidate.studyProgram) {throw new _common.DataError('E_STUDY_PROGRAM', 'The candidate study program cannot be empty at this context');}const studyProgram = yield context.model('StudyProgram').where('id').equal(candidate.studyProgram.id || candidate.studyProgram).and('isActive').equal(true).expand('specialties').getItem();if (studyProgram == null) {throw new _common.DataNotFoundError('The (active) study program cannot be found or is inaccessible.');} // validate that study program
// indeed belongs to the event
if (enrollmentEvent.studyProgram !== studyProgram.id) {throw new _common.DataError('E_STUDY_PROGRAM', 'The provided study program is invalid.');} // if no specialization is set
if (!candidate.specialtyId || !candidate.studyProgramSpecialty) {const specialtyId = candidate.specialtyId != null ? candidate.specialtyId : -1; // find study program specialty
const studyProgramSpecialty = studyProgram.specialties.find(specialization => specialization.specialty === specialtyId);if (studyProgramSpecialty == null) {throw new _common.DataNotFoundError('The study program specialty cannot be found or is inaccessible.');} // assign data to candidate
candidate.studyProgramSpecialty = studyProgramSpecialty;candidate.specialtyId = specialtyId; // also, append specialization to study program register action
event.target.specialization = studyProgramSpecialty;} else {// if provided, always validate that study program and specialization match
const trueSpecialty = studyProgram.specialties.find(specialization => specialization.specialty == candidate.specialtyId);const studyProgramSpecialty = candidate.studyProgramSpecialty.id || candidate.studyProgramSpecialty;if (studyProgramSpecialty != trueSpecialty.id) {throw new _common.DataError('E_SPECIALTY', 'The provided specialty is invalid.');}} // check if the current user is a service agent and belongs to CandidateServices group
const User = context.model('User').getDataObjectType(); /**
* @type {import('@themost/data').DataQueryable}
*/const q = yield User.getMe(context);const user = yield q.expand('groups').getItem();const isCandidateService = user.groups.find(group => group.alternateName === 'CandidateServices') != null;let alreadyEnrolled = 0;if (isCandidateService === false) {// check if candidate has already enrolled in this study program and specialty
alreadyEnrolled = yield context.model('StudyProgramRegisterAction').where('studyProgram').equal(enrollmentEvent.studyProgram).and('actionStatus/alternateName').notEqual('CancelledActionStatus').and('owner').equal(event.target.owner).and('specialization').equal(candidate.studyProgramSpecialty.id || candidate.studyProgramSpecialty).select('id').silent().count();} else {var _event$target$candida, _event$target$candida2; // try to find candidate by user name
alreadyEnrolled = yield context.model('StudyProgramRegisterAction').where('studyProgram').equal(enrollmentEvent.studyProgram).and('actionStatus/alternateName').notEqual('CancelledActionStatus').and('candidate/user/name').equal((_event$target$candida = event.target.candidate) === null || _event$target$candida === void 0 ? void 0 : (_event$target$candida2 = _event$target$candida.user) === null || _event$target$candida2 === void 0 ? void 0 : _event$target$candida2.name).and('candidate/user/name').notEqual(null).and('specialization').equal(candidate.studyProgramSpecialty.id || candidate.studyProgramSpecialty).select('id').silent().count();}if (alreadyEnrolled) {throw new _common.DataError('E_ALREADY_ENROLLED', 'The user has already enrolled (or has a pending enrollment request) in the specified study program and specialty.');}if (isCandidateService) {var _event$target$candida3, _event$target$candida4; // if the candidate user given by the candidate service does not exist
let candidateUser = yield context.model('User').where('name').equal((_event$target$candida3 = event.target.candidate) === null || _event$target$candida3 === void 0 ? void 0 : (_event$target$candida4 = _event$target$candida3.user) === null || _event$target$candida4 === void 0 ? void 0 : _event$target$candida4.name).silent().getItem();if (candidateUser == null) {var _event$target$candida5, _event$target$candida6, _event$target$candida7, _event$target$candida8, _event$target$candida9, _event$target$candida10;const group = yield context.model('Group').where('alternateName').equal('Candidates').silent().getItem();candidateUser = { name: (_event$target$candida5 = event.target.candidate) === null || _event$target$candida5 === void 0 ? void 0 : (_event$target$candida6 = _event$target$candida5.user) === null || _event$target$candida6 === void 0 ? void 0 : _event$target$candida6.name, description: `${(_event$target$candida7 = event.target.candidate) === null || _event$target$candida7 === void 0 ? void 0 : (_event$target$candida8 = _event$target$candida7.person) === null || _event$target$candida8 === void 0 ? void 0 : _event$target$candida8.familyName} ${(_event$target$candida9 = event.target.candidate) === null || _event$target$candida9 === void 0 ? void 0 : (_event$target$candida10 = _event$target$candida9.person) === null || _event$target$candida10 === void 0 ? void 0 : _event$target$candida10.givenName}`, groups: [group] };yield (0, _data.executeInUnattendedModeAsync)(context, /*#__PURE__*/_asyncToGenerator(function* () {// create a new user as candidate
yield context.model('UserReference').insert(candidateUser);}));}Object.assign(candidate, { user: candidateUser });} // assign extra attributes
Object.assign(candidate, { // note: active studentStatus is forced when converting candidate to student
studentStatus: { alternateName: 'candidate' }, // first semester and inscriptionSemester
semester: 1, inscriptionSemester: 1, studyProgram: studyProgram.id, // force set the study program of the enrollment event
department: studyProgram.department, // force set the department of the enrollment event
inscriptionYear: enrollmentEvent.inscriptionYear, // force set inscription year
inscriptionPeriod: enrollmentEvent.inscriptionPeriod, // force set inscription period
// a guid for inscriptionNumber
inscriptionNumber: _common.Guid.newGuid().toString(), // and the event's default inscriptionMode
inscriptionMode: enrollmentEvent.inscriptionMode }); // and emulate nested attribute by saving candidate
yield context.model('CandidateStudent').save(candidate);});return _beforeSaveAsync.apply(this, arguments);}function beforeSave(event, callback) {return beforeSaveAsync(event).then(() => {return callback();}).catch(err => {return callback(err);});}
//# sourceMappingURL=save-candidate-on-register-action-insertion.js.map