Working on issue #588

parent dc05f9d3
Showing with 93 additions and 41 deletions
...@@ -452,50 +452,102 @@ module.exports = { ...@@ -452,50 +452,102 @@ module.exports = {
var l_met = []; var l_met = [];
Method.find({ id_stu: id_stu }).exec(function(err, methods) { Method.find({ id_stu: id_stu })
.populate('instructions')
if (err || !methods || methods.length == 0) .then((methods) => {
return callback(err, []); if (!methods || methods.length == 0)
return callback([]);
// eachSeries
async.eachSeries( // Recorremos methods
// 1st array of items async.eachSeries(methods,
methods, function(method, next_met) {
// 2nd function to operate over one item if (!method.instructions || method.instructions.length == 0)
function(method, next) { next_met();
// Original: Devolver el objeto con el método completo / instructions / workinSessions / Tries
// y controlar en Angular var l_ws = [];
//Instruction.find({ id_met: method.id }).populate('tries').exec(function(err, instructions) {
Instruction.find({ id_met: method.id }).populate('workingSessions').exec(function(err, instructions) { // Recorremos instructions
if (err || !instructions || instructions.length == 0) async.eachSeries(method.instructions,
console.log("error finding instructions"); function (instruction, next_inst) {
// Push method Instruction.findOne({id: instruction})
l_met.push({ .populate('workingSessions')
"id": method.id, .then((instruction) => {
"name": method.name, if (!instruction)
"description": method.description, return next_inst();
"student": method.student,
"registration": method.registration, if (!instruction.workingSessions || instruction.workingSessions.length == 0)
"notes": method.notes, return next_inst();
"instructions": instructions,
// Recorremos workingSessions
var l_try = [];
async.eachSeries(instruction.workingSessions,
function(ws, next_ws) {
WorkingSession.findOne({id: ws.id})
.populate(tries)
.then((found_ws) => {
if (!found_ws || !found_ws.tries || found_ws.tries_length == 0)
return next_ws();
// POR FIN!!! Tenemos un método, una instrucción, una working session y sus tries
l_try = found_ws.tries;
})
},
function(err) {
if (err)
return next_ws();
next_inst();
});
})
.catch(err => {
});
})
.catch((err) => {
next_inst();
});
},
function (err) {
}
);
.populate('workingSessions')
.then((instructions) {
if (instructions && instructions.length == 0)
// Push method
l_met.push({
"id": method.id,
"name": method.name,
"description": method.description,
"student": method.student,
"registration": method.registration,
"notes": method.notes,
"instructions": instructions,
});
next_met();
}); });
next(); },
}); // 3rd final function when all is ready
function (err){
}, console.log("Final function");
// 3rd final function when all is ready console.log(JSON.stringify(l_met));
function (err){ return callback(err, l_met);
console.log("Final function"); // If one iteration give an error it is sent to the controller
console.log(JSON.stringify(l_met)); // with the list
return callback(err, l_met); }
// If one iteration give an error it is sent to the controller );
// with the list });
} })
); .catch((err) => {
if (err)
return callback(err, []);
}); });
}, },
// Removes logically a student // Removes logically a student
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment