UNPKG

@universis/candidates

Version:

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

181 lines (119 loc) 7.54 kB
"use strict";var _data = require("@themost/data"); var _EnableAttachmentModel = _interopRequireDefault(require("./EnableAttachmentModel")); var _unirest = _interopRequireDefault(require("unirest")); var _common = require("@themost/common"); var _middlewares = require("../middlewares");var _dec, _dec2, _dec3, _dec4, _class, _class2;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);});};}function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {var desc = {};Object.keys(descriptor).forEach(function (key) {desc[key] = descriptor[key];});desc.enumerable = !!desc.enumerable;desc.configurable = !!desc.configurable;if ('value' in desc || desc.initializer) {desc.writable = true;}desc = decorators.slice().reverse().reduce(function (desc, decorator) {return decorator(target, property, desc) || desc;}, desc);if (context && desc.initializer !== void 0) {desc.value = desc.initializer ? desc.initializer.call(context) : void 0;desc.initializer = undefined;}if (desc.initializer === void 0) {Object.defineProperty(target, property, desc);desc = null;}return desc;} function tryCloseStream(stream) { if (stream && typeof stream.close === 'function') { try { stream.close(); } catch (error) { _common.TraceUtils.warn( `(tryCloseStream) An error occurred while trying to close a stream.`); _common.TraceUtils.warn(error); } } }let CandidateSource = (_dec = _data.EdmMapping.entityType('CandidateSource'), _dec2 = _data.EdmMapping.param('file', _data.EdmType.EdmStream, false), _dec3 = _data.EdmMapping.action('attachConfigurationSchema', 'Attachment'), _dec4 = _data.EdmMapping.func('exportCandidates', _data.EdmType.EdmStream), _dec(_class = (_class2 = class CandidateSource extends _EnableAttachmentModel.default {constructor() {super();}attachConfigurationSchema(file) {var _superprop_getAddAttachment = () => super.addAttachment,_this = this;return _asyncToGenerator(function* () {try {const context = _this.context; // validate self const self = yield context.model('CandidateSource').where('id').equal(_this.getId()).getItem();if (self == null) {throw new _common.DataNotFoundError();}const attachment = Object.assign({ name: file.contentFileName }, file); // try to add attachment const result = yield _superprop_getAddAttachment().call(_this, attachment); // close stream tryCloseStream(file); // define schemaURL based on added attachment's url self.schemaURL = result.url; // update (do not use silent) yield context.model('CandidateSource').save(self); // and return the attachment return result;} catch (err) {// try to close stream tryCloseStream(file); // and throw error throw err;}})();}exportFromSource() {var _this2 = this;return _asyncToGenerator(function* () {const context = _this2.context; // fetch and validate self const self = yield context.model('CandidateSource').where('id').equal(_this2.getId()).getItem();if (self == null) { throw new _common.DataNotFoundError( 'The specified candidate source cannot be found or is inaccessible.'); } // validate source url if (!(self.externalSource && self.sourceURL)) { throw new _common.DataError( 'E_EXT_SOURCE_URL', 'The external source url cannot be empty while exporting students.', null, 'CandidateSource', 'sourceURL'); } let keychainItem = null; // check if the source has some keychain identifier if (self.keychainIdentifier) { // and validate it const keychain = context. getConfiguration(). getSourceAt('settings/universis/keychain'); // first of all, validate keychain existance if (!(Array.isArray(keychain) && keychain.length)) { throw new _common.DataError( 'E_KEYCHAIN_CONFIG', 'The process cannot continue due to invalid keychain configuration.'); } // then try to find the specified identifier keychainItem = keychain.find( item => item.identifier === self.keychainIdentifier); if (keychainItem == null) { throw new _common.DataError( 'E_KEYCHAIN_ITEM', 'The specified keychain identifier cannot be found inside the keychain configuration', null, 'CandidateSource', 'keychainIdentifier'); } } // prepare request const CandidatesDataRequest = _unirest.default. get(new URL(self.sourceURL)). header('Accept', 'application/json'). header('Content-Type', 'application/json'); // if keychain item requires auth if (keychainItem && keychainItem.authorizationType) { switch (keychainItem.authorizationType) { case 'Basic': // validate username and password if (!(keychainItem.username && keychainItem.password)) { throw new _common.DataError( 'E_CREDENTIALS', 'The username and/or password is/are missing from the keychain configuration. The process cannot continue.'); } // apply basic auth header to request CandidatesDataRequest.header( 'Authorization', 'Basic ' + Buffer.from( `${keychainItem.username}:${keychainItem.password}`). toString('base64')); break; default: // throw error for not yet implemented auth type throw new _common.DataError( 'E_AUTH_TYPE', 'The specified authorization type is not yet implemented.');} } // and send the request return yield new Promise((resolve, reject) => { return CandidatesDataRequest.end(function (response) { // handle errors first if (response.error) { if (response.clientError) { return reject(new _common.HttpError(response.error.status)); } return reject(response.error); } // convert response body (json) to xlsx return (0, _middlewares.toXlsx)(response.body). then(buffer => { // and resolve the buffer return resolve(buffer); }). catch(err => { return reject(err); }); }); });})(); }}, (_applyDecoratedDescriptor(_class2.prototype, "attachConfigurationSchema", [_dec2, _dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "attachConfigurationSchema"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "exportFromSource", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "exportFromSource"), _class2.prototype)), _class2)) || _class); module.exports = CandidateSource; //# sourceMappingURL=CandidateSource.js.map