Commit 159f7463 authored by Odair M.'s avatar Odair M. Committed by Matheus Horstmann

#34 corrige o erro de horario

#34: problem with horario

fix problem with rebase conflicts

fix query course
parent 9ca250bb
......@@ -13,8 +13,8 @@ var lbTables = models.models;
var LOG_CREATE = false;
ds.automigrate(lbTables, function(err) {
if (err) throw err;
async.waterfall([
if (err) throw err;
async.waterfall([
loadModelFromOldDB(old_db.get_salas, app.models.Sala, LOG_CREATE),
loadModelFromOldDB(old_db.get_blocos, app.models.Bloco, LOG_CREATE),
criaUsuario,
......@@ -24,6 +24,7 @@ ds.automigrate(lbTables, function(err) {
criaRecursodesala,
criaTipodesala,
criaEnsalamento,
criaSemestre,
criaTurmas,
criaHorarios,
loadModelFromOldDB(old_db.get_professores, app.models.Professor, LOG_CREATE),
......@@ -32,13 +33,13 @@ ds.automigrate(lbTables, function(err) {
loadModelFromOldDB(old_db.get_disciplinas, app.models.Disciplina, LOG_CREATE),
loadModelFromOldDB(old_db.get_equivalenciasDisciplinas, app.models.Equivalenciadisciplina, LOG_CREATE),
loadModelFromOldDB(old_db.get_cursos, app.models.Curso, LOG_CREATE),
loadRelationsFromOldDB(old_db.get_cursoDisciplina, app.models.Curso, "disciplinas", LOG_CREATE),
],
function(err) {
if (err) throw err;
ds.disconnect();
})
})
});
......@@ -106,22 +107,32 @@ function loadRelationsFromOldDB(oldDataAccess, model, modelRelationName, create_
}
}
function criaSemestre(cb) {
app.models.Semestre.create({"semestre":"2017/1"},(err,obj)=>{
cb(err);
});
}
function criaTurmas(cb){
app.models.Turma.create([
{
"disciplinaCod":"CI055",
"disciplinaCod":"ci055",
"data_inicio":"2019-02-26T13:58:50.150Z",
"data_fim":"2019-02-26T13:58:50.150Z",
"semestre": "2017/1",
"codigo": "t1",
"organizador":"a",
"departamentoCod":"informatica",
"vagas":49,
"ano":2019,
"periodo": 1
},
{
"disciplinaCod":"CI055",
"disciplinaCod":"ci055",
"data_inicio":"2019-02-26T13:58:50.150Z",
"departamentoCod":"informatica",
"data_fim":"2019-02-26T13:58:50.150Z",
"semestre": "2018/1",
"codigo": "t2",
"organizador":"a",
"vagas":49,
......@@ -129,23 +140,27 @@ function criaTurmas(cb){
"periodo": 1
},
{
"disciplinaCod":"CI055",
"disciplinaCod":"ci055",
"data_inicio":"2019-02-26T13:58:50.150Z",
"semestre": "2018/1",
"data_fim":"2019-02-26T13:58:50.150Z",
"codigo": "t3",
"organizador":"a",
"departamentoCod":"informatica",
"vagas":49,
"ano":2019,
"periodo": 1
},
{
"disciplinaCod":"CI055",
"disciplinaCod":"ci055",
"data_inicio":"2019-02-26T13:58:50.150Z",
"data_fim":"2019-02-26T13:58:50.150Z",
"semestre": "2018/1",
"codigo": "t4",
"organizador":"a",
"vagas":49,
"ano":2019,
"departamentoCod":"informatica",
"periodo": 1
},
], function(err, turmas){
......
......@@ -15,6 +15,32 @@ module.exports = function(Disciplina) {
});
};
/**
* pesquisa por uma disciplina, retorna turmas
* @param {string} query palavra chave para a pesquisa
* @param {Function(Error, array)} callback
*/
Disciplina.search = async function(query) {
var _query = {ilike:"%"+query+"%"}
var disciplinas = await app.models.Disciplina.find({where: {or: [{nome: _query}, {codigo: _query}]}});
var turmas = [];
for (var i=0; i< disciplinas.length; i++) {
var disciplina = disciplinas[i]
var t = await disciplina.turmas.find();
for (var j=0; j< t.length; j++) {
var horarios = await t[j].horarios.find();
var professor = await t[j].professor.get()
turmas.push({"turma":t[j],
"horarios":horarios,
"professor":professor,
"disciplina":disciplina}
);
}
}
return turmas
};
Disciplina.prototype.getEquivalencias = function(cb) {
var Eq = app.models.Equivalenciadisciplina;
......
......@@ -135,6 +135,34 @@
"verb": "delete",
"path": "/equivalencias"
}
},
"search": {
"accepts": [
{
"arg": "query",
"type": "string",
"required": true,
"description": "palavra chave para a pesquisa",
"http": {
"source": "query"
}
}
],
"returns": [
{
"arg": "turmas",
"type": "array",
"root": false,
"description": "lista de turmas da disciplina"
}
],
"description": "pesquisa por uma disciplina, retorna turmas",
"http": [
{
"path": "/search",
"verb": "post"
}
]
}
}
}
'use strict';
const _ = require('lodash');
const { fork } = require('child_process');
const loopback = require('loopback');
var async = require('async');
......@@ -76,6 +79,14 @@ function getAllRoomsInstances(newEnsalamento,cb){
cb(err, new RoomColection(newEnsalamento.id,rooms));
});
}
async function get_user(res) {
var user_id = res.req.accessToken.userId;
const Usuario = app.models.Usuario;
return await Usuario.findById(user_id);
}
module.exports = function(Ensalamento) {
Ensalamento.ensalar = function(cb) {
var Horario = app.models.Horario;
......@@ -135,4 +146,61 @@ module.exports = function(Ensalamento) {
});
});
};
/**
* upload planilha do sie
* @param {string} delimiter separador de campos do csv
* @param {string} semester semestre label
* @param {date} letiveInit inicio do periodo letivo
* @param {date} letiveEnd fim do periodo letivo
* @param {Function(Error, string)} callback
*/
Ensalamento.upload = async function(req, res) {
var status = "ok";
const Container = app.models.container;
const FileUpload = app.models.FileUpload;
const Semestre = app.models.Semestre;
const container_name = `turma-${Math.round(Date.now())}-${Math.round(Math.random() * 1000)}`;
// create a container (directory) for upload file
var container = await Container.createContainer({name:container_name});
// upload file (copy file in request to container)
var arg1 = await Container.upload(req,res,{container:container.name});
var args = arg1.fields //arguments as semester, letiveinit ... etc
console.log(args)
// create semester instance
var semestre = {"semestre":args.semester}
var s = await Semestre.create(semestre);
var file = await Container.getFiles(container.name,{});
// create a instance of models FileUpload
var file_upload = await FileUpload.create({
'date': new Date(),
'container_name':container.name,
'letive_init': new Date(args.letiveInit),
'letive_end': new Date(args.letiveEnd),
"semestre": s.semestre,
'status':"Em andamento"
});
//get user by token
var user = await get_user(res);
// fork process of populate data
var param = {
"fileUpload": file_upload.id,
"root": app.datasources.container.settings.root,
"container": container.name,
"letive_init": file_upload.letive_init,
"letive_end": file_upload.letive_end,
"delimiter": (args.delimiter) ?args.delimiter : ";",
"user": user.nome,
"semester": semestre.semestre,
"file": file[0].name
};
fork("/app/server/turma-import.js",[JSON.stringify(param)]);
return file_upload;
};
};
......@@ -52,6 +52,63 @@
"verb": "post",
"path": "/ensalar"
}
},
"upload": {
"accepts": [
{
"arg": "req",
"type": "object",
"http": {
"source": "req"
}
},
{
"arg": "res",
"type": "object",
"http": {
"source": "res"
}
},
{
"arg": "semester",
"type": "string",
"required": false,
"description": "semestre label"
},
{
"arg": "letiveInit",
"type": "date",
"required": false,
"description": "MM/DD/AAAA inicio do periodo letivo."
},
{
"arg": "letiveEnd",
"type": "date",
"required": false,
"description": "MM/DD/AAAA fim do periodo letivo"
},
{
"arg": "delimiter",
"type": "string",
"required": false,
"description": "separador de campos do csv"
}
],
"returns": [
{
"arg": "status",
"type": "object",
"root": false,
"description": "status do upload"
}
],
"description": "upload planilha do sie",
"http": [
{
"path": "/upload",
"verb": "post"
}
]
}
}
}
......@@ -7,15 +7,20 @@
"validateUpsert": true
},
"properties": {
"id": {
"type": "number"
},
"date": {
"type": "date",
"required": true,
"defaultFn": "now"
},
"fileType": {
"letive_init": {
"type": "date",
"required": true
},
"letive_end": {
"type": "date",
"required": true
},
"container_name": {
"type": "string",
"required": true
},
......@@ -26,9 +31,13 @@
"validations": [],
"relations": {
"errors": {
"type": "hasMany",
"model": "FileUploadError",
"foreignKey": "fileUploadId"
"type": "hasAndBelongsToMany",
"model": "FileUploadError"
},
"semestre": {
"type": "belongsTo",
"model": "Semestre",
"foreignKey": "semestre"
}
},
"acls": [],
......
......@@ -12,11 +12,11 @@
"required": true
},
"horario_inicial": {
"type": "number",
"type": "string",
"required": true
},
"horario_final": {
"type": "number",
"type": "string",
"required": true
},
"livre": {
......
......@@ -25,10 +25,15 @@ var models = [
'Curso',
"CursoDisciplina",
"Professor",
"ProfessorTurma",
"Horario",
"Semestre",
"Ensalamento",
"EventoHorario",
"FileUpload",
"FileUploadError",
"FileUploadFileUploadError",
"TurmaHorario",
];
/**
......
......@@ -35,8 +35,9 @@
"foreignKey": "departamentoCod"
},
"turmas": {
"type": "hasAndBelongsToMany",
"model": "Turma"
"type": "hasMany",
"model": "Turma",
"foreignKey": "turmaId"
}
},
"acls": [
......
'use strict';
module.exports = function(Semestre) {
};
{
"name": "Semestre",
"plural": "semestres",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"semestre": {
"type": "string",
"id": true,
"required": true,
"length": 64,
"precision": null,
"scale": null
},
"public": {
"type": "boolean",
"default": false
}
},
"validations": [],
"relations": {
"turmas": {
"type": "hasMany",
"model": "Turma",
"foreignKey": "semestre"
},
"fileUploads": {
"type": "hasOne",
"model": "FileUpload",
"foreignKey": "fileuploadId"
}
},
"acls": [],
"methods": {}
}
'use strict';
const _ = require('lodash');
var app = require('../../server/server');
const async = require('async');
const csv = require('fast-csv');
const { fork } = require('child_process');
const fs = require('fs');
const path = require('path');
const loopback = require('loopback');
const Disciplina = app.models.Disciplina;
module.exports = function(Turma) {
Turma.validatesPresenceOf('disciplinaCod');
Turma.remoteMethod('upload', {
accepts: [
{ arg: 'req', type: 'object', http: { source: 'req' }},
{ arg: 'res', type: 'object', 'http': {source: 'res'}},
{ arg: 'letiveInit', type: 'date'},
{ arg: 'letiveEnd', type: 'date'}
],
http: {
verb: 'post',
path: '/upload'
},
returns: { arg: 'status', type: 'string' }
});
Turma.upload = function(req,res, letiveInit, letiveEnd, callback) {
var container_name = `turma-${Math.round(Date.now())}-${Math.round(Math.random() * 1000)}`;
var Container = Turma.app.models.container;
Container.createContainer({name:container_name},function(err,c) {
Container.upload(req,res,{container: c.name},callback);
/**
* junta turmas
* @param {array} turmas lista de turmas a serem unidas
* @param {Function(Error)} callback
*/
// transform array of json in turmas instance
async function get_turmas(turmas) {
var _turmas = [];
for(var i=0; i < turmas.length ; i++) {
var turma = turmas[i];
console.log(turma);
var t = await app.models.Turma.findById(turma.id)
_turmas.push(t);
}
return _turmas
}
async function check_horario(t1,t2) {
var h_x = t1.horarios.find()
var h_y = t2.horarios.find()
var mesmo_horario = false;
for(var i=0; i<h_x.length; i++){
for(var j=0; j<h_y.length && !mesmo_horario; j++){
if (h_x[i].horario_inicial == h_y[j].horario_inicial &&
h_x[i].horario_final == h_y[j].horario_final &&
h_x[i].dia == h_y[j].dia)
mesmo_horario = true;
}
if (mesmo_horario == false)
return false;
mesmo_horario = false;
}
return true;
}
async function check_disciplina(t1,t2) {
var disciplina_x = t1.disciplina.get()
var disciplina_y = t2.disciplina.get()
//var equivalencias_x = await disciplina_x.getEquivalencias()
//console.log(equivalencias_x)
//if (Array.isArray(equivalencias_x)) {
// for(var i=0; i < equivalencias_x.length; i++)
// if (equivalencias_x[i].codigo == disciplina_y.codigo)
// return true;
//}
//else {
// if (disciplina_y.codigo == equivalencias_x.codigo)
// return true;
//}
if (disciplina_x.codigo == disciplina_y.codigo)
return true;
return false;
}
Turma.juntar = async function(_turmas) {
if (!_turmas || !_turmas.length)
return {"status":0, "msg": "Lista de turmas vazia"}
var turmas = await get_turmas(_turmas);
console.log(turmas)
turmas.map(async (i)=>{
var horarios = await i.horarios.find()
var disciplina = await i.disciplina.get()
console.log("Horarios: ",horarios,"disciplina: ",disciplina)
});
var turma_x = turmas[0];
console.log("turma x antes",turma_x)
var turmas_to_merged = []
for(var j=1; j < turmas.length; j++) {
var turma_y = turmas[j];
// verificar se são a mesma disciplina ou disciplina equivalentes
var disciplina_result = await check_disciplina(turma_x,turma_y);
if(!disciplina_result)
return {"status":1,
"msg":"As disciplinas não são equivalentes",
"turmas":[ turma_x, turma_y]
};
// verificar se são o mesmo horario ou horario logo apos
var horarios_result = await check_horario(turma_x,turma_y);
if(!horarios_result)
return {"status":2,
"msg":"Horarios não são compatíveis",
"turmas":[ turma_x, turma_y]
};
// se positivo adicionar turma_y na lista de turmas a serem unidas
turmas_to_merged.push(turma_y);
}
// se todas as turmas forem compativeis então une todas na primeira
for (var i = 0; i < turmas_to_merged.length; i++) {
turma_y = turmas_to_merged[i]
turma_x.vagas += turma_y.vagas
turma_y.horarios.destroyAll()
}
turma_y.destroy()
console.log("turma x depois",turma_x)
turma_x.save()
return turmas;
};
};
......@@ -31,6 +31,21 @@
"type": "belongsTo",
"model": "Disciplina",
"foreignKey": "disciplinaCod"
},
"semestre": {
"type": "belongsTo",
"model": "Semestre",
"foreignKey": "semestre"
},
"professor": {
"type": "belongsTo",
"model": "Professor",
"foreignKey": "professorCod"
},
"cursos": {
"type": "belongsTo",
"model": "Curso",
"foreignKey": "cursoCod"
}
},
"acls": [
......@@ -59,5 +74,31 @@
"property": "*"
}
],
"methods": {}
"methods": {
"juntar": {
"accepts": [
{
"arg": "turmas",
"type": "array",
"required": true,
"description": "lista de turmas a serem unidas"
}
],
"returns": [
{
"arg": "status",
"type": "object",
"root": false,
"description": "status da união"
}
],
"description": "junta turmas",
"http": [
{
"path": "/merge",
"verb": "post"
}
]
}
}
}
......@@ -19,14 +19,14 @@ services:
POSTGRES_PASSWORD: ${ENSALAMENTO_DB_PASSWORD}
NODE_ENV: ${ENSALAMENTO_NODE_ENV}
volumes:
- ./sie-report:/app/sie-report/
- ./bin/:/app/bin/
- ./common/:/app/common/
- ./server/:/app/server/
- ./src/:/app/src/
- ./sie-report/:/app/sie-report/
- ./client/:/app/client/
ports:
- '3000:3000'
- '8000:8000'
depends_on:
- ensalamento-postgres
networks:
......
'use strict';
module.exports = function enableAuthentication(server) {
// enable authentication
server.enableAuth();
//enable authentication
server.enableAuth();
};
......@@ -30,7 +30,7 @@
},
"Role": {
"dataSource": "ensalamento",
"public": false
"public": true
},
"Sala": {
"dataSource": "ensalamento",
......@@ -101,15 +101,19 @@
"public": true
},
"FileUpload": {
"dataSource": "db",
"dataSource": "ensalamento",
"public": true
},
"FileUploadError": {
"dataSource": "db",
"dataSource": "ensalamento",
"public": true
},
"container": {
"dataSource": "container",
"public": true
},
"Semestre": {
"dataSource": "ensalamento",
"public": true
}
}
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const server = require('./server.js');
const options = JSON.parse(process.argv[2]);