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
* [
* {
* id: methodId,
* student: studentId,
* name: 'Method Name',
* description: 'Method description',
* registration: null,
* notes: 'Method notes',
* last_ins: instructionId // Last instruccion executed,
* instructions: [
* {
* id: instructionId,
* name: 'Instruction Name',
* objective: 'Instruction Objective',
* status: 'instruction-status',
* begin: '2015-07-14T07:23:03.000Z',
* end: '2015-07-14T07:28:03.000Z',
* method: methodId
* workingSessions: [
* { * {
* }, "methods": [
* ... {
* ] "student": 24,
* }, "id": 1,
* ... "name": "Test Method",
* ] "description": null,
* }, "registration": null,
* ... "notes": null
* ] "instructions": [
{
"id": 1,
"name": "Test Instruction",
"objective": null,
"status": "started",
"begin": null,
"end": null,
"method": 1,
"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 // Recorremos instructions
async.eachSeries(method.instructions, var l_ins = [];
function (instruction, next_inst) { async.each(method.instructions,
function (instruction, next_ins) {
Instruction.findOne({id: instruction}) Instruction.findOne(instruction.id)
.populate('workingSessions') .populate('workingSessions')
.then((instruction) => { .then((populated_ins) => {
if (!instruction) if (!populated_ins || !populated_ins.workingSessions || populated_ins.workingSessions.length == 0)
return next_inst(); return next_ins();
if (!instruction.workingSessions || instruction.workingSessions.length == 0)
return next_inst();
// Recorremos workingSessions // Recorremos workingSessions
var l_try = []; var l_ws = [];
async.eachSeries(instruction.workingSessions, async.each(populated_ins.workingSessions,
function(ws, next_ws) { function(ws, next_ws) {
WorkingSession.findOne({id: ws.id})
.populate(tries) WorkingSession.findOne(ws.id)
.then((found_ws) => { .populate('tries')
if (!found_ws || !found_ws.tries || found_ws.tries_length == 0) .then((populated_ws) => {
if (!populated_ws || !populated_ws.tries || populated_ws.tries.length == 0)
return next_ws(); return next_ws();
// POR FIN!!! Tenemos un método, una instrucción, una working session y sus tries l_ws.push(populated_ws);
l_try = found_ws.tries;
})
},
function(err) {
if (err)
return next_ws(); return next_ws();
next_inst();
});
}) })
.catch(err => { .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) => { .catch((err) => {throw err});
next_inst();
});
}, },
function (err) { function (err) { // Ya tenemos todas las instrucciones pobladas
if (err) throw err;
if (l_ins.length > 0) {
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);
// If one iteration give an error it is sent to the controller
// with the list
}
);
}); });
}) })
.catch((err) => { .catch((err) => {
if (err) return callback(err, {methods: l_met});
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