UNPKG

@universis/candidates

Version:

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

90 lines (52 loc) 4.21 kB
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.afterSave = afterSave;exports.AfterCreateCandidateUser = void 0;var _data = require("@themost/data"); var _common = require("@themost/common"); var _path = _interopRequireDefault(require("path"));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;let shouldContinue = false; // get candidate student user const user = yield context.model(event.model.name).where('id').equal(event.target.id).select('user').silent().value();if (event.state === _data.DataObjectState.Insert) {shouldContinue = false;} else if (event.state === _data.DataObjectState.Update) {const previous = event.previous;if (previous == null) {throw new _common.DataError('E_STATE', 'Invalid configuration. The previous state of an object cannot be determined.', null, 'CandidateStudent');}shouldContinue = previous.user == null && user != null;}if (shouldContinue === false) {// exit return;}const service = context.getApplication().getService(function OAuth2ClientService() {});if (service == null) {throw new Error('Invalid application configuration. Authentication client service is missing or is inaccessible state.');}if (service.settings.adminAccount == null) {throw new Error('Invalid application configuration. Authentication client service is not properly configured to handle user operations.');} // create new action yield context.model('CreateCandidateUserAction').save({ object: user, actionStatus: { alternateName: 'ActiveActionStatus' } });});return _afterSaveAsync.apply(this, arguments);}function afterSave(event, callback) {return afterSaveAsync(event).then(() => {return callback(); }).catch(err => { return callback(err); }); } class AfterCreateCandidateUser extends _common.ApplicationService { constructor(app) { super(app); this.install('CandidateUser', __filename); } install() { /** * get data configuration * @type {DataConfigurationStrategy} */ const configuration = this.getApplication().getConfiguration().getStrategy(_data.DataConfigurationStrategy); // get StudentRequestAction model definition const model = configuration.getModelDefinition('CandidateStudent'); // get this file path relative to application execution path const listenerType = './' + _path.default.relative(this.getApplication().getConfiguration().getExecutionPath(), __filename); // ensure model event listeners model.eventListeners = model.eventListeners || []; // try to find event listener const findIndex = model.eventListeners.findIndex(listener => { return listener.type === listenerType || listener.type === listenerType.replace(/\.js$/, ''); }); if (findIndex < 0) { // add event listener model.eventListeners.push({ type: listenerType }); // update StudentRequestAction model definition configuration.setModelDefinition(model); // write to log _common.TraceUtils.info(`Services: ${this.constructor.name} service has been successfully installed`); } }}exports.AfterCreateCandidateUser = AfterCreateCandidateUser; //# sourceMappingURL=after-create-candidate-user-listener.js.map