Skip to content
Snippets Groups Projects
Commit a782dc34 authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Change simulation model

parent 1c540973
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!90Issue/347
Pipeline #
......@@ -2,114 +2,20 @@ const mongoose = require('mongoose')
const libs = `${process.cwd()}/libs`;
const log = require(`${libs}/log`)(module);
const User = require(`${libs}/models/user`);
const Schema = mongoose.Schema;
// Should define this somewhere else
const MAX_SIMULATION_TIME = 10;
let SimulationSchema = new Schema({
name: {
type: String,
userId: {
type: Schema.Types.ObjectId,
required: true,
ref: 'User'
},
location: Object,
time: Number,
failure_rate: Array,
goals_care: Array,
goals_inclusion: Array,
enrollments: Array,
timestamp: Date,
});
SimulationSchema.methods.setTime = function (t) {
t = parseInt(t, 10);
if (!t || t > MAX_SIMULATION_TIME) {
// Throw an error?
return false;
}
this.time = t;
return true;
};
SimulationSchema.methods.setLocation = function (l) {
// Should sanitize
this.location = l;
return true;
};
SimulationSchema.methods.setFailureRate = function (fr) {
// Should sanitize
this.failure_rate = fr;
return true;
};
SimulationSchema.methods.setCareGoals = function (g) {
// Should sanitize
this.goals_care = g;
return true;
};
SimulationSchema.methods.setInclusionGoals = function (g) {
// Should sanitize
this.goals_inclusion = g;
return true;
};
SimulationSchema.methods.setEnrollments = function (e) {
try{
e = JSON.parse(e);
} catch (err) {
return false;
}
let success = true;
for(let i=0; i<e.length; i++){
if(!(e[i] instanceof Array)){
return false;
}
if(e[i].length !== this.time){
return false;
}
e[i].forEach((n, i, array) => {
if(n !== parseInt(n, 10)){
success = false;
}
});
}
if (success) this.enrollments = e;
return success;
}
SimulationSchema.methods.update = function (property, value) {
let success = true;
switch(property){
case 'time':
if (!this.setTime(value)) success = false;
break;
case 'location':
if (!this.setLocation(value)) success = false;
break;
case 'failure_rate':
if (!this.setFailureRate(value)) success = false;
break;
case 'goals_care':
if (!this.setCareGoals(value)) success = false;
break;
case 'goals_inclusion':
if (!this.setInclusionGoals(value)) success = false;
break;
case 'enrollments':
if (!this.setEnrollments(value)) success = false;
break;
case 'name':
this.name = value;
break;
content: {
type: String,
required: true,
}
return success;
};
SimulationSchema.methods.run = function () {
/* Runs the Simulation with given parameters */
// if (!this.name || !this.location || !this.time) {
// console.log('This is supposed to be an error.');
// }
};
});
module.exports = mongoose.model('Simulation', SimulationSchema);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment