Student.tries() fixed

parent 9a22eecc
......@@ -643,45 +643,85 @@ module.exports = {
* Return all tries from a student
* @param {request} req {} (width studentId as url parameter)
* @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) {
if (!req.params.id_stu)
return res.json(500, {
error: 'No student defined'
});
return res.badRequest("Student not defined");
Student.tries(req.params.id_stu, function (err, l_met) {
if (err) throw err;
return res.json(l_met);
if (err) return res.serverError(err);
return res.ok(l_met);
});
},
......
......@@ -456,98 +456,70 @@ module.exports = {
.populate('instructions')
.then((methods) => {
if (!methods || methods.length == 0)
return callback([]);
return callback(null, {methods: []});
// Recorremos methods
async.eachSeries(methods,
async.each(methods,
function(method, next_met) {
if (!method.instructions || method.instructions.length == 0)
next_met();
var l_ws = [];
if (!method || !method.instructions || method.instructions.length == 0)
return next_met();
// Recorremos instructions
async.eachSeries(method.instructions,
function (instruction, next_inst) {
var l_ins = [];
async.each(method.instructions,
function (instruction, next_ins) {
Instruction.findOne({id: instruction})
Instruction.findOne(instruction.id)
.populate('workingSessions')
.then((instruction) => {
if (!instruction)
return next_inst();
if (!instruction.workingSessions || instruction.workingSessions.length == 0)
return next_inst();
.then((populated_ins) => {
if (!populated_ins || !populated_ins.workingSessions || populated_ins.workingSessions.length == 0)
return next_ins();
// Recorremos workingSessions
var l_try = [];
async.eachSeries(instruction.workingSessions,
var l_ws = [];
async.each(populated_ins.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)
WorkingSession.findOne(ws.id)
.populate('tries')
.then((populated_ws) => {
if (!populated_ws || !populated_ws.tries || populated_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)
l_ws.push(populated_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) => {
next_inst();
});
.catch((err) => {throw err});
},
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);
}
);
.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,
return next_met();
});
next_met();
});
},
// 3rd final function when all is ready
function (err){
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
}
);
function (err) { // Ya tenemos los métodos poblados
return callback(err, {methods: l_met});
});
})
.catch((err) => {
if (err)
return callback(err, []);
return callback(err, {methods: l_met});
});
},
// 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