Student.tries() fixed

parent 9a22eecc
...@@ -643,45 +643,85 @@ module.exports = { ...@@ -643,45 +643,85 @@ module.exports = {
* Return all tries from a student * Return all tries from a student
* @param {request} req {} (width studentId as url parameter) * @param {request} req {} (width studentId as url parameter)
* @param {response} res * @param {response} res
* [ * {
* { "methods": [
* id: methodId, {
* student: studentId, "student": 24,
* name: 'Method Name', "id": 1,
* description: 'Method description', "name": "Test Method",
* registration: null, "description": null,
* notes: 'Method notes', "registration": null,
* last_ins: instructionId // Last instruccion executed, "notes": null
* instructions: [ "instructions": [
* { {
* id: instructionId, "id": 1,
* name: 'Instruction Name', "name": "Test Instruction",
* objective: 'Instruction Objective', "objective": null,
* status: 'instruction-status', "status": "started",
* begin: '2015-07-14T07:23:03.000Z', "begin": null,
* end: '2015-07-14T07:28:03.000Z', "end": null,
* method: methodId "method": 1,
* workingSessions: [ "working_sessions": [
* { {
* }, "id": 3,
* ... "begin": "2016-09-09T08:26:24.500Z",
* ] "end": "2016-08-28T23:36:35.000Z",
* }, "current": null,
* ... "description": "",
* ] "supervisor": 23,
* }, "instruction": 1
* ... "tries": [
* ] {
"actions": [],
"id": 1,
"begin": "2016-08-28T23:36:35.474Z",
"end": "2016-08-28T23:36:44.000Z",
"result": null,
"description": null,
"workingSession": 3
},
{
"actions": [],
"id": 2,
"begin": "2016-08-28T23:36:44.050Z",
"end": "2016-08-29T01:36:51.710Z",
"result": "SUCCESS",
"description": null,
"workingSession": 3
},
{
"actions": [],
"id": 3,
"begin": "2016-08-28T23:36:51.942Z",
"end": "2016-08-28T23:36:53.000Z",
"result": "DISCARDED",
"description": null,
"workingSession": 3
},
{
"actions": [],
"id": 4,
"begin": "2016-08-28T23:36:53.877Z",
"end": "2016-08-28T23:37:13.000Z",
"result": "SPONTANEOUS SUCCESS",
"description": null,
"workingSession": 3
}
]
}
}
]
}
]
}
*/ */
tries: function (req, res) { tries: function (req, res) {
if (!req.params.id_stu) if (!req.params.id_stu)
return res.json(500, { return res.badRequest("Student not defined");
error: 'No student defined'
});
Student.tries(req.params.id_stu, function (err, l_met) { Student.tries(req.params.id_stu, function (err, l_met) {
if (err) throw err; if (err) return res.serverError(err);
return res.json(l_met); return res.ok(l_met);
}); });
}, },
......
...@@ -456,98 +456,70 @@ module.exports = { ...@@ -456,98 +456,70 @@ module.exports = {
.populate('instructions') .populate('instructions')
.then((methods) => { .then((methods) => {
if (!methods || methods.length == 0) if (!methods || methods.length == 0)
return callback([]); return callback(null, {methods: []});
// Recorremos methods // Recorremos methods
async.eachSeries(methods, async.each(methods,
function(method, next_met) { function(method, next_met) {
if (!method.instructions || method.instructions.length == 0) if (!method || !method.instructions || method.instructions.length == 0)
next_met(); return next_met();
var l_ws = []; // Recorremos instructions
var l_ins = [];
// Recorremos instructions async.each(method.instructions,
async.eachSeries(method.instructions, function (instruction, next_ins) {
function (instruction, next_inst) {
Instruction.findOne(instruction.id)
Instruction.findOne({id: instruction}) .populate('workingSessions')
.populate('workingSessions') .then((populated_ins) => {
.then((instruction) => { if (!populated_ins || !populated_ins.workingSessions || populated_ins.workingSessions.length == 0)
if (!instruction) return next_ins();
return next_inst();
// Recorremos workingSessions
if (!instruction.workingSessions || instruction.workingSessions.length == 0) var l_ws = [];
return next_inst(); async.each(populated_ins.workingSessions,
function(ws, next_ws) {
// Recorremos workingSessions
var l_try = []; WorkingSession.findOne(ws.id)
async.eachSeries(instruction.workingSessions, .populate('tries')
function(ws, next_ws) { .then((populated_ws) => {
WorkingSession.findOne({id: ws.id}) if (!populated_ws || !populated_ws.tries || populated_ws.tries.length == 0)
.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(); return next_ws();
next_inst();
});
})
.catch(err => {
l_ws.push(populated_ws);
return next_ws();
})
.catch((err) => {throw err});
},
function(err) { // Ya tenemos todas las working sessions pobladas
if (err) throw err;
if (l_ws.length > 0) {
populated_ins.working_sessions = l_ws;
l_ins.push(populated_ins);
}
return next_ins();
}); });
})
}) .catch((err) => {throw err});
.catch((err) => {
next_inst(); },
}); function (err) { // Ya tenemos todas las instrucciones pobladas
if (err) throw err;
}, if (l_ins.length > 0) {
function (err) { method.instructions = l_ins;
l_met.push(method);
} }
); return next_met();
});
.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();
});
}, },
// 3rd final function when all is ready function (err) { // Ya tenemos los métodos poblados
function (err){ return callback(err, {methods: l_met});
console.log("Final function"); });
console.log(JSON.stringify(l_met)); })
return callback(err, l_met); .catch((err) => {
// If one iteration give an error it is sent to the controller return callback(err, {methods: l_met});
// 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