@universis/candidates
Version:
Universis api server plugin for study program candidates, internship selection etc
160 lines (24 loc) • 6.61 kB
JavaScript
;Object.defineProperty(exports, "__esModule", { value: true });exports.afterSave = afterSave;var _data = require("@themost/data");
var _common = require("@themost/common");
var _mailer = require("@themost/mailer");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;const actionStatus = yield context.model('CreateCandidateUserAction').where('id').equal(event.target.id).select('actionStatus/alternateName').silent().value();let previousStatus = 'UnknownActionStatus';if (event.state === _data.DataObjectState.Insert) {previousStatus = 'UnknownActionStatus';} else if (event.state === _data.DataObjectState.Update) {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 (actionStatus === 'ActiveActionStatus' && previousStatus !== 'ActiveActionStatus') {const service = context.getApplication().getService(function OAuth2ClientService() {});if (typeof service.createUser !== 'function') {throw new Error('The operation is not supported by a service.');} // get access token (for admin account)
const adminAccount = service.settings.adminAccount;if (adminAccount == null) {throw new Error('The operation cannot be completed due to invalid or missing configuration.');}const authorizeUser = yield service.authorize(adminAccount); // get user
_common.TraceUtils.debug('CreateCandidateUserAction', 'get', JSON.stringify(event.target.object));const object = yield context.model('CreateCandidateUserAction').where('id').equal(event.target.id).select('object').silent().value();const user = yield context.model('User').where('id').equal(object).silent().getItem();if (user == null) {throw new _common.DataError('E_NOENT', 'User cannot be found', null, 'CreateCandidateUserAction', 'object');} // check if user already exists and is disabled
const userExists = yield service.getUser(user.name, authorizeUser);if (userExists) {const updateAction = { id: event.target.id, actionStatus: { alternateName: 'CompletedActionStatus' } };if (userExists.enabled === false) {updateAction.description = 'User already exists at the OAuth2 server and is disabled.';} else {updateAction.description = 'User already exists at the OAuth2 server and is enabled.';}yield event.model.silent().save(updateAction);return;} // get candidate for extra data
const candidate = yield context.model('CandidateStudent').where('user').equal(user).expand('person').silent().getItem(); // get activation code as new password
const newPassword = yield context.model('CandidateUser').where('id').equal(object).select('activationCode').silent().value();if (newPassword == null) {throw new _common.DataError('E_EMPTY', 'An error occurred while validating user data', null, 'CandidateUser', 'activationCode');}yield service.createUser({ name: user.name, description: `${candidate.person.givenName} ${candidate.person.familyName}`, alternateName: candidate.person.email, enabled: true, familyName: candidate.person.familyName, givenName: candidate.person.givenName, userCredentials: { userPassword: `{clear}${newPassword}` } }, authorizeUser);try {// send notification for the newly created user
const mailer = (0, _mailer.getMailer)(context);let mailTemplate = yield context.model('MailConfiguration').where('target').equal('CreateCandidateUserAction').and('state').equal(_data.DataObjectState.Update).silent().getItem(); // if mail template is null do nothing and exit
// otherwise send mail
if (mailTemplate != null) {// get candidate
const candidateUser = yield context.model('CandidateUser').where('id').equal(user).silent().getItem();Object.assign(candidate, { user: candidateUser }); // get register application
const application = yield context.model('WebApplication').where('alternateName').equal('register').and('applicationSuite').equal('universis').silent().getItem();if (application == null) {_common.TraceUtils.warn('CreateCandidateUserAction', 'Universis register is missing from application collection. User notification for account activation cannot include information about this app.');}yield new Promise(resolve => {mailer.template(mailTemplate.template).subject(mailTemplate.subject).to(candidate.person.email).send({ model: { candidate, application } }, err => {if (err) {try {_common.TraceUtils.error('CreateCandidateUserAction', 'An error occurred while trying to send an email notification for account activation.');_common.TraceUtils.error('CreateCandidateUserAction', 'Candidate', `${candidate.id}`, 'Email Address', `${candidate.person.email || 'empty'}`);_common.TraceUtils.error(err);} catch (err1) {// do nothing
}return resolve();}return resolve();});});} else {_common.TraceUtils.warn('CreateCandidateUserAction', 'A mail template for account activation notification is missing. User cannot be notified for this action.');}} catch (err) {_common.TraceUtils.error('CreateCandidateUserAction', 'An error occurred while trying to send an email notification for account activation.');_common.TraceUtils.error(err);} // complete action
yield context.model('CreateCandidateUserAction').silent().save({ id: event.target.id, actionStatus: { alternateName: 'CompletedActionStatus' } });}});return _afterSaveAsync.apply(this, arguments);}function afterSave(event, callback) {return afterSaveAsync(event).then(() => {return callback();}).catch(err => {return callback(err);});}
//# sourceMappingURL=complete-create-candidate-user-action.js.map