@universis/evaluations
Version:
Universis evaluations library
76 lines (71 loc) • 2.75 kB
JavaScript
;Object.defineProperty(exports, "__esModule", { value: true });exports.afterExecute = afterExecute;exports.afterExecuteAsync = afterExecuteAsync;exports.afterSave = afterSave;var _data = require("@themost/data");
/**
*
* @param {DataEventArgs} event
*/
async function afterSaveAsync(event) {
// get organizer from courseClass
const context = event.model.context;
const target = event.target;
if (event.state === _data.DataObjectState.Update) {
const previous = event.previous;
// update also child evaluation tokens (send, used, metadata)
let relatedTokens = await context.model('EvaluationAccessToken').
where('parentToken').equal(event.target.access_token).
silent().
getAllItems();
if (relatedTokens && relatedTokens.length > 0) {
relatedTokens = relatedTokens.map((x) => {
x.sent = target.hasOwnProperty('sent') ? target.sent : x.sent;
x.metadata = target.hasOwnProperty('metadata') ? target.metadata : x.metadata;
return x;
});
}
await context.model('EvaluationAccessToken').
silent().save(relatedTokens);
}
}
function afterExecute(event, callback) {
return afterExecuteAsync(event).then(() => {
return callback();
}).catch((err) => {
return callback(err);
});
}
async function afterExecuteAsync(event) {
// calculate total tokens and total used tokens if events are expanded
if (event.emitter && event.emitter.$expand && Array.isArray(event.emitter.$expand)) {
// if tokens of classInstructorEvaluationEvents are expanded and $select and $groupby are present to mapping options
if (event.emitter.$expand.find((mapping) => {
// $select and $groupby are present to mapping options
return typeof mapping === 'object' ?
mapping.name === 'tokens' && mapping.options && mapping.options.$select && mapping.options.$groupby :
mapping === 'tokens';
})) {
const results = Array.isArray(event.result) ? event.result : [event.result];
for (const result of results) {
if (Array.isArray(result.tokens) && result.tokens.length) {
// calculate total and used tokens
result.calculatedTotal = result.tokens.reduce((partial_sum, a) => partial_sum + a.total, 0);
const findUsed = result.tokens.find((x) => {
return x.used;
});
result.calculatedUsed = findUsed ? findUsed.total : 0;
}
}
}
}
}
/**
* @param {DataEventArgs} event
* @param {Function} callback
*/
function afterSave(event, callback) {
// execute async method
return afterSaveAsync(event).then(() => {
return callback();
}).catch((err) => {
return callback(err);
});
}
//# sourceMappingURL=access-token-listener.js.map