From a02cc43595e0eaf2b7d98ebf59996e4f330728c8 Mon Sep 17 00:00:00 2001 From: Eliseu Egewarth Date: Sun, 24 Jun 2018 01:18:40 -0300 Subject: [PATCH 1/6] =?UTF-8?q?#22=20#30=20Adicionando=20valida=C3=A7?= =?UTF-8?q?=C3=B5es=20de=20Editar=20um=20hor=C3=A1rio=20(Client)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Eliseu Egewarth Signed-off-by: João Egewarth Co-authored-by: João Egewarth --- Client/api.js | 163 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 128 insertions(+), 35 deletions(-) diff --git a/Client/api.js b/Client/api.js index b6c18f9..1c8564e 100644 --- a/Client/api.js +++ b/Client/api.js @@ -1,11 +1,33 @@ currentWeekNumber = require('current-week-number'); + // The RegExp Object above validates MongoBD ObjectIds function IsInvalidObjectId(objectIdString){ var checkObjectId = new RegExp('^[0-9a-fA-F]{24}$'); return (! checkObjectId.test(objectIdString)) } + +function validate_date(date_field, result){ + date = new Date(date_field.value); + today = new Date(); + if (date == "Invalid Date") { + result[date_field.field_name + '_error'] = date_field.verbose + ' inválido'; + } else if (date < today){ + result[date_field.field_name + '_error'] = 'Você não pode inserir um ' + date_field.verbose + ' que já passou'; + } + return result; +}; + +function validate_id(field, result){ + if (field.value == null || field.value == ''){ + result[field.field_name + '_error'] = 'O campo ' + field.verbose + ' é obrigatório.'; + } else if (IsInvalidObjectId(field.value)) { + result[field.field_name + '_error'] = 'O ' + field.verbose +' é inválido.'; + } + return result; +}; + var milliseconds_in_one_hour = 3600000.0 // Alternative python string.format() for javascript @@ -20,7 +42,7 @@ if (!String.prototype.format) { return this }else{ var new_string = this; - for (i=0; i < replaceable_count; i++){ + for (i = 0; i < replaceable_count; i++){ new_string = new_string.replace('{}', arguments[i]); } return new_string @@ -45,7 +67,7 @@ function get_schedule_duration( start_time, end_time){ function api(options){ //############################################################################# - this.add('role:api,path:createSchedule', function(msg, respond){ + this.add('role:api,path:create', function(msg, respond){ // result variable store attribute errors or the success created schedule var result = {}; @@ -115,14 +137,14 @@ function api(options){ // verify that an error has occurred - if (Object.entries(result)[0]) { + if (Object.entries(result).length > 0) { console.log("Result:"); console.log(result); result.success = false; respond(null, result) - // else, everything sucess + // else, everything success } else { - this.act('role:schedule,cmd:createSchedule',{ + this.act('role:schedule,cmd:edit',{ start_time: start_time, end_time: end_time, sector_id: sector_id, @@ -132,7 +154,7 @@ function api(options){ }); //############################################################################# - this.add('role:api,path:createScheduleSettings', function(msg, respond){ + this.add('role:api,path:createSettings', function(msg, respond){ var max_hours_month = parseInt(msg.args.body.max_hours_month) var max_hours_week = parseInt(msg.args.body.max_hours_week) var min_hours_month = parseInt(msg.args.body.min_hours_month) @@ -142,7 +164,7 @@ function api(options){ // Lista de ids de templates var templates = msg.args.body.templates - result = {} + var result = {} // validar se ids de templates são validos // 24*7 = 168 @@ -250,13 +272,13 @@ function api(options){ result.hours_day_interval_invalid_error = 'O máximo de horas por dia deve ser maior que o mínimo de horas da escala'; console.log(result.hours_week_interval_invalid_error); } - if (Object.entries(result)[0]) { + if (Object.entries(result).length > 0) { console.log("Result:"); console.log(result); result.success = false; respond(null, result) } else { - this.act('role:schedule,cmd:createScheduleSettings',{ + this.act('role:schedule,cmd:createSettings',{ max_hours_month:max_hours_month, max_hours_week:max_hours_week, min_hours_month:min_hours_month, @@ -270,7 +292,7 @@ function api(options){ //############################################################################# - this.add('role:api,path:listYearByProfile', function (msg, respond) { + this.add('role:api,path:listByProfile', function (msg, respond) { var profile_id = msg.args.query.profile_id; result = {success:false}; if(IsInvalidObjectId(profile_id)){ @@ -289,7 +311,7 @@ function api(options){ console.log("Start" + start_year); console.log("End" + end_year); - this.act('role:schedule,cmd:listYearByProfile', { + this.act('role:schedule,cmd:listByProfile', { start_year:start_year, end_year:end_year, profile_id:profile_id @@ -299,7 +321,7 @@ function api(options){ //############################################################################# - this.add('role:api,path:listYearBySector', function (msg, respond) { + this.add('role:api,path:listBySector', function (msg, respond) { console.log(msg.args) var currentDate = new Date(); var year = msg.args.query.year; @@ -314,7 +336,7 @@ function api(options){ console.log("End" + end_year) var sector_id = msg.args.query.sector_id; - this.act('role:schedule,cmd:listYearBySector', { + this.act('role:schedule,cmd:listBySector', { start_year:start_year, end_year:end_year, sector_id:sector_id @@ -323,7 +345,7 @@ function api(options){ //############################################################################# - this.add('role:api,path:changeListYearBySector', function (msg, respond) { + this.add('role:api,path:changeListBySector', function (msg, respond) { console.log(msg.args) var currentDate = new Date(); var year = msg.args.query.year; @@ -339,7 +361,7 @@ function api(options){ var sector_id = msg.args.query.sector_id; var profile_id = msg.args.query.profile_id; - this.act('role:schedule,cmd:changeListYearBySector', { + this.act('role:schedule,cmd:changeListBySector', { start_year:start_year, end_year:end_year, sector_id:sector_id, @@ -349,7 +371,7 @@ function api(options){ //############################################################################# - this.add('role:api,path:listYearByUser', function (msg, respond) { + this.add('role:api,path:listByUser', function (msg, respond) { console.log(msg.args) var currentDate = new Date(); var year = msg.args.query.year; @@ -364,7 +386,7 @@ function api(options){ console.log("End" + end_year) var user_id = msg.args.query.user_id; - this.act('role:schedule,cmd:listYearByUser', { + this.act('role:schedule,cmd:listByUser', { start_year:start_year, end_year:end_year, user_id:user_id @@ -372,13 +394,87 @@ function api(options){ }); //############################################################################# + this.add('role:api,path:edit', function(msg, respond){ + // result variable store attribute errors or the success created schedule + var result = {}; + // check if body is empty. + if (msg.args.body == {}){ + result.body_error = 'Nenhum parametro informado'; + respond(null, result); + } else { + // Do nothing + } - this.add('role:api,path:listByProfile', function (msg, respond) { - var id = msg.args.query.id - console.log("id informado:" + id); - this.act('role:schedule, cmd:listByProfile', { - id:id - }, respond) + // variables + var schedule_id = { + verbose: 'ID do Horário', + field_name: 'schedule_id' + }; + var start_time = { + verbose: 'Horário de início', + field_name: 'start_time' + }; + var end_time = { + verbose: 'Horário de término', + field_name: 'end_time' + }; + + schedule_id.value = msg.args.body.schedule_id; + start_time.value = msg.args.body.start_time; + end_time.value = msg.args.body.end_time; + + console.log("Client MSG"); + console.log(msg.args.body); + + result = validate_id(schedule_id, result); + + console.log("Client 1 "); + console.log(result); + console.log("Horáio 1 "); + console.log(schedule_id); + + var new_start_time = null; + var new_end_time = null; + + // validate start time + if (start_time.value != null) { + result = validate_date(start_time, result) + new_start_time = new Date(start_time.value) + console.log("Client 2 "); + console.log(result); + console.log("Date 1 "); + console.log(new_start_time); + } else { + // All Good, do Nothing + } + // validate end time + if (end_time.value != null) { + result = validate_date(end_time, result) + new_end_time = new Date(end_time.value) + console.log("Date 2 "); + console.log(new_end_time); + } else { + // All Good, do Nothing + } + + if (new_start_time == null && new_end_time == null){ + result.no_change_error = "Nenhuma mudança detectada" + } + + // verify that an error has occurred + if (Object.entries(result).length > 0) { + console.log("Result:"); + console.log(result); + result.success = false; + respond(null, result) + // else, everything success + } else { + this.act('role:schedule,cmd:edit',{ + start_time: new_start_time, + end_time: new_end_time, + schedule_id : schedule_id.value + }, respond) + } }); //############################################################################# @@ -405,34 +501,31 @@ function api(options){ prefix: '/api/schedule', pin: 'role:api,path:*', map: { - createSchedule: { + create: { POST: true }, listByProfile: { GET: true }, - listYearByProfile: { + listBySector: { GET: true }, - listYearBySector: { + changeListBySector: { GET: true }, - changeListYearBySector: { + listByUser: { GET: true }, - listYearByUser: { + edit: { + PUT: true + }, + listByProfile: { GET: true }, delete: { DELETE: true }, - // listSectorWeek: { GET: true, - // auth: { - // strategy: 'jwt', - // fail: '/api/schedule/error' - // } - // }, - createScheduleSettings: { + createSettings: { POST:true }, error: {GET: true } From 7587a27e8dac85a3a311a43529e9b03f3f8ab5cf Mon Sep 17 00:00:00 2001 From: Eliseu Egewarth Date: Sun, 24 Jun 2018 01:21:07 -0300 Subject: [PATCH 2/6] =?UTF-8?q?#22=20#30=20Adicionando=20valida=C3=A7?= =?UTF-8?q?=C3=B5es=20de=20Editar=20hor=C3=A1rio=20(Server)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Eliseu Egewarth Signed-off-by: João Egewarth Co-authored-by: João Egewarth --- Server/plg_schedule.js | 370 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 356 insertions(+), 14 deletions(-) diff --git a/Server/plg_schedule.js b/Server/plg_schedule.js index a23f834..be444a2 100644 --- a/Server/plg_schedule.js +++ b/Server/plg_schedule.js @@ -4,6 +4,15 @@ var Promise = require('bluebird'); var schedule_db = 'schedules' var schedule_settings_db = 'scheduleSettings' +var default_settings = { + "max_hours_month": 145, + "max_hours_week": 50, + "min_hours_month": 60, + "min_hours_week": 11, + "min_hours_schedule": 2, + "max_hours_day": 6, + "templates": [1,2,3, 4] +}; // 3600000.0 (1000ms * 60s * 60m), is the number of milliseconds in 1 hour. var milliseconds_in_one_hour = 3600000.0 @@ -43,7 +52,7 @@ if (!String.prototype.format) { } module.exports = function(options){ - this.add('role:schedule,cmd:createSchedule', async function create (msg,respond) { + this.add('role:schedule,cmd:create', async function create (msg,respond) { // schedule Entity var schedule = this.make(schedule_db); // scheduleSettings Entity @@ -59,14 +68,7 @@ module.exports = function(options){ var sector_id = msg.sector_id; var profile_id = msg.profile_id; - var settings = {} - settings = { - "max_hours_month": 145, - "max_hours_week": 50, - "min_hours_month": 60, - "min_hours_week": 11, - "templates": [1,2,3, 4] - } + var settings = default_settings await settings_list$({}) .then(await function(list_of_settings){ @@ -351,7 +353,7 @@ module.exports = function(options){ // ############################################################################# - this.add('role:schedule,cmd:listYearByProfile', function (msg, respond) { + this.add('role:schedule,cmd:listByProfile', function (msg, respond) { console.log(msg); var schedule = this.make(schedule_db); schedule.profile_id = msg.profile_id; @@ -380,7 +382,7 @@ module.exports = function(options){ // ############################################################################# - this.add('role:schedule,cmd:listYearBySector', function (msg, respond) { + this.add('role:schedule,cmd:listBySector', function (msg, respond) { console.log(msg); var schedule = this.make(schedule_db); schedule.sector_id = msg.sector_id; @@ -401,7 +403,7 @@ module.exports = function(options){ // ############################################################################# - this.add('role:schedule,cmd:changeListYearBySector', function (msg, respond) { + this.add('role:schedule,cmd:changeListBySector', function (msg, respond) { console.log(msg); var schedule = this.make(schedule_db); sector_id = msg.sector_id; @@ -424,7 +426,7 @@ module.exports = function(options){ // ############################################################################# -this.add('role:schedule,cmd:listYearByUser', function (msg, respond) { +this.add('role:schedule,cmd:listByUser', function (msg, respond) { console.log(msg); var schedule = this.make(schedule_db); user_id = msg.user_id; @@ -454,7 +456,7 @@ this.add('role:schedule,cmd:listYearByUser', function (msg, respond) { // ############################################################################# - this.add('role:schedule, cmd:createScheduleSettings', function error(msg, respond){ + this.add('role:schedule, cmd:createSettings', function error(msg, respond){ var scheduleSettings = this.make(schedule_settings_db) scheduleSettings.max_hours_month = parseInt(msg.max_hours_month) scheduleSettings.max_hours_week = parseInt(msg.max_hours_week) @@ -470,6 +472,346 @@ this.add('role:schedule,cmd:listYearByUser', function (msg, respond) { }) // ############################################################################# + this.add('role:schedule,cmd:edit', async function edit (msg,respond) { + // schedule Entity + var schedule = this.make(schedule_db); + // scheduleSettings Entity + var scheduleSettings = this.make(schedule_settings_db); + // Promise of Schedule list$ function + var schedule_list$ = Promise.promisify(schedule.list$, { context: schedule }); + var settings_list$ = Promise.promisify(scheduleSettings.list$, { context: scheduleSettings }); + var load$ = Promise.promisify(schedule.load$, { context: schedule }); + var schedule_id = msg.schedule_id + console.log("Edit msg "); + console.log(msg); + + var edited_schedule = {}; + var result = {success:false}; + + await load$(schedule_id) + .then(function(current_schedule){ + if (current_schedule == null){ + console.log("if"); + console.log(current_schedule); + result.schedule_not_find = "Horário não encontrado"; + respond(null, result); + }else{ + console.log("else"); + console.log(current_schedule); + edited_schedule = current_schedule + } + }) + .catch(function(error){ + respond(null, error); + }) + + console.log("edited"); + console.log(edited_schedule); + + if (msg.start_time != null){ + console.log(msg.start_time); + edited_schedule.start_time = new Date(msg.start_time); + } else { + console.log("deu ruim na data edited_schedule.start_time"); + } + if (msg.end_time != null){ + console.log(msg.end_time); + edited_schedule.end_time = new Date(msg.end_time); + } else { + console.log("deu ruim na data edited_schedule.end_time"); + } + + if((edited_schedule.end_time - edited_schedule.start_time) < 0){ + result.date_interval_error = 'O fim do horário deve ser maior que o início do horário' + console.log(result.date_interval_error); + }else if((edited_schedule.end_time - edited_schedule.start_time) == 0){ + result.date_equals_error = 'O horário de início e de fim não podem ser iguais' + console.log(result.date_interval_error); + } + if (get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) > 24){ + result.schedule_bigger_than_limit_error = 'O horário deve ser de no máximo 1 dia'; + console.log(result.schedule_bigger_than_limit_error) + } + + console.log("new edited"); + console.log(edited_schedule); + + var settings = default_settings + + await settings_list$({}) + .then(await function(list_of_settings){ + if (list_of_settings.length > 0){ + settings = list_of_settings[0] + } else { + // Do nothing + } + }) + .catch(function(err){ + // result.scheduleSettings_catch_error = "Não conseguiu achar schedule settings"; + }) + + // validates if the amount of minimum hours in a schedule is less than allowed + if (get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) < scheduleSettings.min_hours_schedule){ + result.min_hours_limit_error = ( + 'Limite mínimo de {} horas por horário'.format( + settings.min_hours_schedule + ) + ) + } + + console.log("result::::"); + console.log(result); + + var start_interval = new Date(edited_schedule.start_time) + var end_interval = new Date(edited_schedule.end_time) + start_interval.setDate(start_interval.getDate()-1) + end_interval.setDate(end_interval.getDate()+1) + // validates if any schedule conflict has occurred + console.log("id!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + console.log(edited_schedule.id); + await schedule_list$( + { + start_time: { + $gte: start_interval, + $lt: end_interval, + $not:{$eq:edited_schedule.start_time} + }, + profile_id: edited_schedule.profile_id + }) + .then(await function(list_of_schedules){ + console.log("list_of_schedules::::"); + console.log(list_of_schedules); + if (list_of_schedules.length != 0){ + list_of_schedules.forEach(function(schedule_element){ + if (edited_schedule.start_time >= schedule_element.start_time && edited_schedule.start_time < schedule_element.end_time){ + // if start_time are in schedule_element interval, it gives an error + result.conflicts_error = ( + '{} já possui um horário de {} à {}'.format( + 'Plantonista', + schedule_element.start_time, + schedule_element.end_time + ) + ) + } else if (edited_schedule.end_time > schedule_element.start_time && edited_schedule.end_time <= schedule_element.end_time){ + // if end_time are in schedule_element interval, it gives an error + result.conflicts_error = ( + '{} já possui um horário de {} à {}'.format( + 'Plantonista', + schedule_element.start_time, + schedule_element.end_time + ) + ) + } else if (edited_schedule.start_time <= schedule_element.start_time && edited_schedule.end_time >= schedule_element.end_time){ + // + result.conflicts_error = ( + '{} já possui um horário de {} à {}'.format( + 'Plantonista', + schedule_element.start_time, + schedule_element.end_time + ) + ) + } + }); + + } else { + // sucess + // nothing to do + } + }) + .catch(function(err) { + console.log('error') + }) + console.log("result::::::::::"); + console.log(result); + + + // creates a one day interval + var first_hour_of_day = new Date(edited_schedule.start_time); + var last_hour_of_day = new Date(edited_schedule.start_time); + first_hour_of_day.setHours(0, 0); + last_hour_of_day.setHours(24,0); + current_amount_of_day_work_hours = 0; + + // lists every schedule in a one day interval + await schedule_list$( + { + start_time: { + $gte: first_hour_of_day, + $lt: last_hour_of_day, + $not:{$eq:edited_schedule.start_time} + }, + profile_id: edited_schedule.profile_id + }) + .then(await function(list_of_schedules){ + console.log("::::::list_of_schedules::::::"); + console.log(list_of_schedules); + // Walks on the list of schedules and counts the amount of hours worked + list_of_schedules.forEach( + function(schedule_element){ + console.log( + 'Parcial Working Hours: <|{}|>'.format( + current_amount_of_day_work_hours + ) + ); + current_amount_of_day_work_hours = ( + current_amount_of_day_work_hours + + get_schedule_duration( + schedule_element.start_time, + schedule_element.end_time + ) + ); + } + ) + new_amount_of_day_work_hours = ( + current_amount_of_day_work_hours + + get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) + ) + // validates if the amount of hours in a day is bigger than allowed + if (new_amount_of_day_work_hours > settings.max_hours_day){ + result.max_hours_limit_error = ( + 'Você já tem {} horas nesse dia. O limite é de {} horas por dia'.format( + current_amount_of_day_work_hours, + settings.max_hours_day + ) + ); + return result; + } else { + // sucess + // nothing to do + } + }) + .catch(function(err) { + console.log('error') + console.log(err); + }) + + // crates an interval of one week + first_day_of_week = new Date(edited_schedule.start_time.getFullYear(), + edited_schedule.start_time.getMonth(), + (edited_schedule.start_time.getDate() - + edited_schedule.start_time.getDay()), 0, 0, 0); + last_day_of_week = new Date(edited_schedule.start_time.getFullYear(), + edited_schedule.start_time.getMonth(), + (edited_schedule.start_time.getDate() - + edited_schedule.start_time.getDay() + 7), 0, 0, 0); + + var current_amount_of_week_work_hours = 0; + + // lists every schedule in a one week interval + await schedule_list$( + { + and$: [ + {or$:[ + {start_time: { + $gte: first_day_of_week, + $lt: last_day_of_week, + $not:{$eq:edited_schedule.start_time} + }}, + {end_time: { + $lte: last_day_of_week, + $gte: first_day_of_week + }} + ]}, + {profile_id: edited_schedule.profile_id} + ] + }) + .then(await function(list_of_schedules){ + // Walks on the schedules list and count the amount of hours + list_of_schedules.forEach(function(time){ + console.log('Parcial: {}'.format(current_amount_of_week_work_hours)); + current_amount_of_week_work_hours = ( + current_amount_of_week_work_hours + + get_schedule_duration( + time.start_time, + time.end_time + ) + ) + }) + console.log("Horas trabalhadas na semana: {}".format(current_amount_of_week_work_hours)) + var new_amount_of_week_work_hours = ( + current_amount_of_week_work_hours + + get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) + ); + console.log("Horas trabalhadas com o novo horário: {}".format(new_amount_of_week_work_hours)) + + // validates if the amount of hours in a week is bigger than allowed + if (new_amount_of_week_work_hours > settings.max_hours_week){ + result.max_week_hours_limit_error = ( + 'Você já tem {} horas nessa semana. O limite é de {} horas por semana'.format( + new_amount_of_week_work_hours, + settings.max_hours_week + ) + ); + } else { + // sucess!! nothing to do + } + }) + .catch(function(error){ + console.log('error'); + console.log(err); + }) + + respond(null,{"resultado": "chegou depois das validações de conflitos!"}) + + // creates an interval of one month + month = parseInt(edited_schedule.start_time.getMonth()); + year = parseInt(edited_schedule.start_time.getFullYear()); + start_of_month = new Date(year, month, 1); + end_of_month = new Date(year, month+1, 1); + var current_amount_of_month_work_hours = 0 + + await schedule_list$( + { + and$: [ + {start_time: { + $gte: start_of_month, + $lt: end_of_month, + $not:{$eq:edited_schedule.start_time} + }}, + {profile_id: edited_schedule.profile_id} + ] + }) + .then(await function(list_of_schedules){ + + list_of_schedules.forEach(function(time){ + current_amount_of_month_work_hours = ( + current_amount_of_month_work_hours + + get_schedule_duration(time.start_time, time.end_time) + ); + }) + + var new_amount_of_month_work_hours = ( + current_amount_of_month_work_hours + + get_schedule_duration(edited_schedule.start_time,edited_schedule.end_time)); + // validates if the amount of hours in month is bigger than allowed + if (new_amount_of_month_work_hours > settings.max_hours_month){ + result.max_hours_limit_error = ( + 'Você já tem {} horas nesse mês. O limite é de {} horas por mês'.format( + current_amount_of_month_work_hours, + settings.max_hours_month + ) + ); + } else { + //success + //nothing to do + } + }) + .catch(function(error){ + console.log(error); + }) + + if(Object.entries(result).length > 1){ + console.log('Alguns erros foram encontrados...'); + console.log(result); + respond(null, result); + } else { + edited_schedule.save$(function(err,schedule){ + respond(null, schedule); + }) + } + }) + +// ############################################################################# this.add('role:schedule, cmd:delete', async function (msg, respond) { var schedule = this.make(schedule_db); From b437e7ab4bceea5f03b6e31b76af9223023ba701 Mon Sep 17 00:00:00 2001 From: Eliseu Egewarth Date: Sun, 24 Jun 2018 16:04:09 -0300 Subject: [PATCH 3/6] WIP #22 #30 Signed-off-by: Eliseu Egewarth --- Server/plg_schedule.js | 104 +++++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 40 deletions(-) diff --git a/Server/plg_schedule.js b/Server/plg_schedule.js index be444a2..c9dcd94 100644 --- a/Server/plg_schedule.js +++ b/Server/plg_schedule.js @@ -61,7 +61,7 @@ module.exports = function(options){ var schedule_list$ = Promise.promisify(schedule.list$, { context: schedule }); var settings_list$ = Promise.promisify(scheduleSettings.list$, { context: scheduleSettings }); - var result = {success:false}; + var result = {}; var start_time = new Date(msg.start_time); var end_time = new Date(msg.end_time); @@ -319,7 +319,8 @@ module.exports = function(options){ console.log(error); }) - if(Object.entries(result).length > 1){ + if(Object.entries(result).length > 0){ + result.success = false; console.log('Alguns erros foram encontrados...'); console.log(result); respond(null, result); @@ -486,19 +487,21 @@ this.add('role:schedule,cmd:listByUser', function (msg, respond) { console.log(msg); var edited_schedule = {}; - var result = {success:false}; + var current_schedule = {}; + var result = {}; await load$(schedule_id) - .then(function(current_schedule){ - if (current_schedule == null){ + .then(function(schedule){ + if (schedule == null){ console.log("if"); - console.log(current_schedule); + console.log(schedule); result.schedule_not_find = "Horário não encontrado"; respond(null, result); }else{ console.log("else"); - console.log(current_schedule); - edited_schedule = current_schedule + console.log(schedule); + edited_schedule = schedule + current_schedule = schedule } }) .catch(function(error){ @@ -686,34 +689,26 @@ this.add('role:schedule,cmd:listByUser', function (msg, respond) { }) // crates an interval of one week - first_day_of_week = new Date(edited_schedule.start_time.getFullYear(), - edited_schedule.start_time.getMonth(), - (edited_schedule.start_time.getDate() - - edited_schedule.start_time.getDay()), 0, 0, 0); - last_day_of_week = new Date(edited_schedule.start_time.getFullYear(), - edited_schedule.start_time.getMonth(), - (edited_schedule.start_time.getDate() - - edited_schedule.start_time.getDay() + 7), 0, 0, 0); + first_day_of_week = new Date(current_schedule.start_time.getFullYear(), + current_schedule.start_time.getMonth(), + (current_schedule.start_time.getDate() - + current_schedule.start_time.getDay()), 0, 0, 0); + last_day_of_week = new Date(current_schedule.start_time.getFullYear(), + current_schedule.start_time.getMonth(), + (current_schedule.start_time.getDate() - + current_schedule.start_time.getDay() + 7), 0, 0, 0); var current_amount_of_week_work_hours = 0; // lists every schedule in a one week interval await schedule_list$( { - and$: [ - {or$:[ - {start_time: { - $gte: first_day_of_week, - $lt: last_day_of_week, - $not:{$eq:edited_schedule.start_time} - }}, - {end_time: { - $lte: last_day_of_week, - $gte: first_day_of_week - }} - ]}, - {profile_id: edited_schedule.profile_id} - ] + start_time: { + $gte: first_day_of_week, + $lt: last_day_of_week, + $not:{$eq:current_schedule.start_time} + }, + profile_id: edited_schedule.profile_id }) .then(await function(list_of_schedules){ // Walks on the schedules list and count the amount of hours @@ -727,19 +722,43 @@ this.add('role:schedule,cmd:listByUser', function (msg, respond) { ) ) }) - console.log("Horas trabalhadas na semana: {}".format(current_amount_of_week_work_hours)) + var new_amount_of_week_work_hours = ( - current_amount_of_week_work_hours + - get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) + current_amount_of_week_work_hours ); + var is_greater_than_sunday = first_day_of_week.getDate() < edited_schedule.start_time.getDate() + var is_lower_than_saturday = last_day_of_week.getDate() > edited_schedule.start_time.getDate() + if(is_greater_than_sunday && is_lower_than_saturday){ + new_amount_of_week_work_hours = ( + new_amount_of_week_work_hours + + get_schedule_duration( + edited_schedule.start_time, + edited_schedule.end_time + ) + ) + if (new_amount_of_week_work_hours > settings.max_hours_week){ + result.max_week_hours_limit_error = ( + 'Você já tem {} horas nessa semana. O limite é de {} horas por semana'.format( + new_amount_of_week_work_hours, + settings.max_hours_week + ) + ); + } else { + // sucess!! nothing to do + } + }else { + // Do nothing + } + console.log("Horas trabalhadas na semana: {}".format(current_amount_of_week_work_hours)) console.log("Horas trabalhadas com o novo horário: {}".format(new_amount_of_week_work_hours)) + // validates if the amount of hours in a week is bigger than allowed - if (new_amount_of_week_work_hours > settings.max_hours_week){ - result.max_week_hours_limit_error = ( - 'Você já tem {} horas nessa semana. O limite é de {} horas por semana'.format( - new_amount_of_week_work_hours, - settings.max_hours_week + if (new_amount_of_week_work_hours < settings.min_hours_week){ + result.min_week_hours_limit_error = ( + 'O limite mínimo de horas por semana é de {}. Você possui apenas {} horas nessa semana'.format( + settings.min_hours_week, + new_amount_of_week_work_hours ) ); } else { @@ -800,12 +819,17 @@ this.add('role:schedule,cmd:listByUser', function (msg, respond) { console.log(error); }) - if(Object.entries(result).length > 1){ + if(Object.entries(result).length > 0){ + result.success = false; console.log('Alguns erros foram encontrados...'); console.log(result); respond(null, result); } else { - edited_schedule.save$(function(err,schedule){ + schedule.start_time = edited_schedule.start_time + schedule.end_time = edited_schedule.end_time + schedule.sector_id = edited_schedule.sector_id + schedule.profile_id = edited_schedule.profile_id + schedule.save$(function(err,schedule){ respond(null, schedule); }) } From a6022e8ecf522110cf3d58a85f88237107ad0645 Mon Sep 17 00:00:00 2001 From: Eliseu Egewarth Date: Mon, 25 Jun 2018 09:44:38 -0300 Subject: [PATCH 4/6] Fix #22, Fix #30 Signed-off-by: Eliseu Egewarth --- Client/api.js | 3 - Server/plg_schedule.js | 1302 ++++++++++++++++++++-------------------- 2 files changed, 653 insertions(+), 652 deletions(-) diff --git a/Client/api.js b/Client/api.js index 1c8564e..dd69aae 100644 --- a/Client/api.js +++ b/Client/api.js @@ -519,9 +519,6 @@ function api(options){ edit: { PUT: true }, - listByProfile: { - GET: true - }, delete: { DELETE: true }, diff --git a/Server/plg_schedule.js b/Server/plg_schedule.js index c9dcd94..aa3e57e 100644 --- a/Server/plg_schedule.js +++ b/Server/plg_schedule.js @@ -1,159 +1,158 @@ -var Promise = require('bluebird'); - -// BDs names -var schedule_db = 'schedules' -var schedule_settings_db = 'scheduleSettings' - -var default_settings = { - "max_hours_month": 145, - "max_hours_week": 50, - "min_hours_month": 60, - "min_hours_week": 11, - "min_hours_schedule": 2, - "max_hours_day": 6, - "templates": [1,2,3, 4] -}; -// 3600000.0 (1000ms * 60s * 60m), is the number of milliseconds in 1 hour. -var milliseconds_in_one_hour = 3600000.0 - -// get_schedule_duration returns schedule duration in hours -function get_schedule_duration( start_time, end_time){ - // The diference between Dates are given in milliseconds. - // So we divide by the amount of milliseconds in 1 hour - console.log(start_time); - console.log(end_time); - var duration = ( - (end_time - start_time) / - milliseconds_in_one_hour - ); - console.log('Duração:{}'.format(duration)); - return duration; -} - -// Alternative python string.format() for javascript -// First, checks if it isn't implemented yet. -if (!String.prototype.format) { - String.prototype.format = function(){ - var args = arguments; - var replaceable_count = (this.match(/{}/g) || []).length; - if(args.length < replaceable_count){ - console.log( - "Expecting " + replaceable_count + "arguments, " + - args.length + 'found.'); - return this - }else{ - var new_string = this; - for (i=0; i < replaceable_count; i++){ - new_string = new_string.replace('{}', args[i]); - } - return new_string - } + var Promise = require('bluebird'); + + // BDs names + var schedule_db = 'schedules' + var schedule_settings_db = 'scheduleSettings' + + var default_settings = { + "max_hours_month": 145, + "max_hours_week": 50, + "min_hours_month": 60, + "min_hours_week": 11, + "min_hours_schedule": 2, + "max_hours_day": 6, + "templates": [1,2,3, 4] }; -} + // 3600000.0 (1000ms * 60s * 60m), is the number of milliseconds in 1 hour. + var milliseconds_in_one_hour = 3600000.0 + + // get_schedule_duration returns schedule duration in hours + function get_schedule_duration( start_time, end_time){ + // The diference between Dates are given in milliseconds. + // So we divide by the amount of milliseconds in 1 hour + console.log(start_time); + console.log(end_time); + var duration = ( + (end_time - start_time) / + milliseconds_in_one_hour + ); + console.log('Duração:{}'.format(duration)); + return duration; + } + + // Alternative python string.format() for javascript + // First, checks if it isn't implemented yet. + if (!String.prototype.format) { + String.prototype.format = function(){ + var replaceable_count = (this.match(/{}/g) || []).length; + if(arguments.length < replaceable_count){ + console.log( + "Expecting " + replaceable_count + "arguments, " + + arguments.length + 'found.'); + return this + }else{ + var new_string = this; + for (i=0; i < replaceable_count; i++){ + new_string = new_string.replace('{}', arguments[i]); + } + return new_string + } + }; + } -module.exports = function(options){ - this.add('role:schedule,cmd:create', async function create (msg,respond) { - // schedule Entity - var schedule = this.make(schedule_db); - // scheduleSettings Entity - var scheduleSettings = this.make(schedule_settings_db); - // Promise of Schedule list$ function - var schedule_list$ = Promise.promisify(schedule.list$, { context: schedule }); - var settings_list$ = Promise.promisify(scheduleSettings.list$, { context: scheduleSettings }); + module.exports = function(options){ + this.add('role:schedule,cmd:create', async function create (msg,respond) { + // schedule Entity + var schedule = this.make(schedule_db); + // scheduleSettings Entity + var scheduleSettings = this.make(schedule_settings_db); + // Promise of Schedule list$ function + var schedule_list$ = Promise.promisify(schedule.list$, { context: schedule }); + var settings_list$ = Promise.promisify(scheduleSettings.list$, { context: scheduleSettings }); - var result = {}; + var result = {}; - var start_time = new Date(msg.start_time); - var end_time = new Date(msg.end_time); - var sector_id = msg.sector_id; - var profile_id = msg.profile_id; + var start_time = new Date(msg.start_time); + var end_time = new Date(msg.end_time); + var sector_id = msg.sector_id; + var profile_id = msg.profile_id; - var settings = default_settings + var settings = default_settings - await settings_list$({}) + await settings_list$({}) .then(await function(list_of_settings){ if (list_of_settings.length > 0){ settings = list_of_settings[0] } }) .catch(function(err){ - // result.scheduleSettings_catch_error = "Não conseguiu achar schedule settings"; - }) + // result.scheduleSettings_catch_error = "Não conseguiu achar schedule settings"; + }) - // validates if the amount of minimum hours in a schedule is less than allowed - if (get_schedule_duration(start_time, end_time) < scheduleSettings.min_hours_schedule){ - result.min_hours_limit_error = ( - 'Limite mínimo de {} horas por horário'.format( - settings.min_hours_schedule - ) - ) - } - var start_interval = new Date(start_time) - var end_interval = new Date(end_time) - start_interval.setDate(start_interval.getDate()-1) - end_interval.setDate(end_interval.getDate()+1) - // validates if any schedule conflict has occurred - await schedule_list$( - { - start_time: { - $gte: start_interval, - $lt: end_interval - }, - profile_id: profile_id - }) - .then(await function(list_of_schedules){ - if (list_of_schedules.length != 0){ - list_of_schedules.forEach(function(schedule_element){ - if (start_time >= schedule_element.start_time && start_time < schedule_element.end_time){ - // if start_time are in schedule_element interval, it gives an error - result.conflicts_error = ( - '{} já possui um horário de {} à {}'.format( - 'Plantonista', - schedule_element.start_time, - schedule_element.end_time - ) - ) - } else if (end_time > schedule_element.start_time && end_time <= schedule_element.end_time){ - // if end_time are in schedule_element interval, it gives an error - result.conflicts_error = ( - '{} já possui um horário de {} à {}'.format( - 'Plantonista', - schedule_element.start_time, - schedule_element.end_time - ) + // validates if the amount of minimum hours in a schedule is less than allowed + if (get_schedule_duration(start_time, end_time) < scheduleSettings.min_hours_schedule){ + result.min_hours_limit_error = ( + 'Limite mínimo de {} horas por horário'.format( + settings.min_hours_schedule ) - } else if (start_time <= schedule_element.start_time && end_time >= schedule_element.end_time){ - // - result.conflicts_error = ( - '{} já possui um horário de {} à {}'.format( - 'Plantonista', - schedule_element.start_time, - schedule_element.end_time - ) - ) - } - }); - - } else { - // sucess - // nothing to do + ) } - }) - .catch(function(err) { - console.log('error') - }) + var start_interval = new Date(start_time) + var end_interval = new Date(end_time) + start_interval.setDate(start_interval.getDate()-1) + end_interval.setDate(end_interval.getDate()+1) + // validates if any schedule conflict has occurred + await schedule_list$( + { + start_time: { + $gte: start_interval, + $lt: end_interval + }, + profile_id: profile_id + }) + .then(await function(list_of_schedules){ + if (list_of_schedules.length != 0){ + list_of_schedules.forEach(function(schedule_element){ + if (start_time >= schedule_element.start_time && start_time < schedule_element.end_time){ + // if start_time are in schedule_element interval, it gives an error + result.conflicts_error = ( + '{} já possui um horário de {} à {}'.format( + 'Plantonista', + schedule_element.start_time, + schedule_element.end_time + ) + ) + } else if (end_time > schedule_element.start_time && end_time <= schedule_element.end_time){ + // if end_time are in schedule_element interval, it gives an error + result.conflicts_error = ( + '{} já possui um horário de {} à {}'.format( + 'Plantonista', + schedule_element.start_time, + schedule_element.end_time + ) + ) + } else if (start_time <= schedule_element.start_time && end_time >= schedule_element.end_time){ + // + result.conflicts_error = ( + '{} já possui um horário de {} à {}'.format( + 'Plantonista', + schedule_element.start_time, + schedule_element.end_time + ) + ) + } + }); - // creates a one day interval - var first_hour_of_day = new Date(start_time); - var last_hour_of_day = new Date(start_time); - first_hour_of_day.setHours(0, 0); - last_hour_of_day.setHours(24,0); - current_amount_of_day_work_hours = 0; + } else { + // sucess + // nothing to do + } + }) + .catch(function(err) { + console.log('error') + }) - // lists every schedule in a one day interval - await schedule_list$( - { - and$: [ + // creates a one day interval + var first_hour_of_day = new Date(start_time); + var last_hour_of_day = new Date(start_time); + first_hour_of_day.setHours(0, 0); + last_hour_of_day.setHours(24,0); + current_amount_of_day_work_hours = 0; + + // lists every schedule in a one day interval + await schedule_list$( + { + and$: [ {or$:[ {start_time: { $gte: first_hour_of_day, @@ -163,68 +162,68 @@ module.exports = function(options){ $lte: last_hour_of_day, $gte: first_hour_of_day }} - ]}, - {profile_id: profile_id}, - {sector_id: sector_id} - ] - }) - .then(await function(list_of_schedules){ - // Walks on the list of schedules and counts the amount of hours worked - list_of_schedules.forEach( - function(schedule_element){ - console.log( - 'Parcial Working Hours: <|{}|>'.format( - current_amount_of_day_work_hours - ) - ); - current_amount_of_day_work_hours = ( - current_amount_of_day_work_hours + - get_schedule_duration( - schedule_element.start_time, - schedule_element.end_time - ) - ); - } - ) - - new_amount_of_day_work_hours = ( - current_amount_of_day_work_hours + - get_schedule_duration(start_time, end_time) - ) - // validates if the amount of hours in a day is bigger than allowed - if (new_amount_of_day_work_hours > settings.max_hours_day){ - result.max_hours_limit_error = ( - 'Você já tem {} horas nesse dia. O limite é de {} horas por dia'.format( - worked_hours_in_a_day, - settings.max_hours_day + ]}, + {profile_id: profile_id}, + {sector_id: sector_id} + ] + }) + .then(await function(list_of_schedules){ + // Walks on the list of schedules and counts the amount of hours worked + list_of_schedules.forEach( + function(schedule_element){ + console.log( + 'Parcial Working Hours: <|{}|>'.format( + current_amount_of_day_work_hours + ) + ); + current_amount_of_day_work_hours = ( + current_amount_of_day_work_hours + + get_schedule_duration( + schedule_element.start_time, + schedule_element.end_time + ) + ); + } ) - ); - return result; - } else { - // sucess - // nothing to do - } - }) - .catch(function(err) { - console.log('error') - }) - // crates an interval of one week - first_day_of_week = new Date(start_time.getFullYear(), - start_time.getMonth(), - (start_time.getDate() - - start_time.getDay()), 0, 0, 0); - last_day_of_week = new Date(start_time.getFullYear(), - start_time.getMonth(), - (start_time.getDate() - - start_time.getDay() + 7), 0, 0, 0); + new_amount_of_day_work_hours = ( + current_amount_of_day_work_hours + + get_schedule_duration(start_time, end_time) + ) + // validates if the amount of hours in a day is bigger than allowed + if (new_amount_of_day_work_hours > settings.max_hours_day){ + result.max_hours_limit_error = ( + 'Você já tem {} horas nesse dia. O limite é de {} horas por dia'.format( + worked_hours_in_a_day, + settings.max_hours_day + ) + ); + return result; + } else { + // sucess + // nothing to do + } + }) + .catch(function(err) { + console.log('error') + }) - var current_amount_of_week_work_hours = 0; + // crates an interval of one week + first_day_of_week = new Date(start_time.getFullYear(), + start_time.getMonth(), + (start_time.getDate() - + start_time.getDay()), 0, 0, 0); + last_day_of_week = new Date(start_time.getFullYear(), + start_time.getMonth(), + (start_time.getDate() - + start_time.getDay() + 7), 0, 0, 0); - // lists every schedule in a one week interval - await schedule_list$( - { - and$: [ + var current_amount_of_week_work_hours = 0; + + // lists every schedule in a one week interval + await schedule_list$( + { + and$: [ {or$:[ {start_time: { $gte: first_day_of_week, @@ -234,60 +233,60 @@ module.exports = function(options){ $lte: last_day_of_week, $gte: first_day_of_week }} - ]}, - {profile_id: profile_id} - ] - }) - .then(await function(list_of_schedules){ - // Walks on the schedules list and count the amount of hours - list_of_schedules.forEach(function(time){ - console.log('Parcial: {}'.format(current_amount_of_week_work_hours)); - current_amount_of_week_work_hours = ( + ]}, + {profile_id: profile_id} + ] + }) + .then(await function(list_of_schedules){ + // Walks on the schedules list and count the amount of hours + list_of_schedules.forEach(function(time){ + console.log('Parcial: {}'.format(current_amount_of_week_work_hours)); + current_amount_of_week_work_hours = ( + current_amount_of_week_work_hours + + get_schedule_duration( + time.start_time, + time.end_time + ) + ) + }) + console.log("Horas trabalhadas na semana: {}".format(current_amount_of_week_work_hours)) + var new_amount_of_week_work_hours = ( current_amount_of_week_work_hours + - get_schedule_duration( - time.start_time, - time.end_time - ) - ) + get_schedule_duration(start_time, end_time) + ); + console.log("Horas trabalhadas com o novo horário: {}".format(new_amount_of_week_work_hours)) + + // validates if the amount of hours in a week is bigger than allowed + if (new_amount_of_week_work_hours > settings.max_hours_week){ + result.max_week_hours_limit_error = ( + 'Você já tem {} horas nessa semana. O limite é de {} horas por semana'.format( + new_amount_of_week_work_hours, + settings.max_hours_week + ) + ); + } else { + // sucess!! nothing to do + } + }) + .catch(function(error){ + console.log('error') }) - console.log("Horas trabalhadas na semana: {}".format(current_amount_of_week_work_hours)) - var new_amount_of_week_work_hours = ( - current_amount_of_week_work_hours + - get_schedule_duration(start_time, end_time) - ); - console.log("Horas trabalhadas com o novo horário: {}".format(new_amount_of_week_work_hours)) - - // validates if the amount of hours in a week is bigger than allowed - if (new_amount_of_week_work_hours > settings.max_hours_week){ - result.max_week_hours_limit_error = ( - 'Você já tem {} horas nessa semana. O limite é de {} horas por semana'.format( - new_amount_of_week_work_hours, - settings.max_hours_week - ) - ); - } else { - // sucess!! nothing to do - } - }) - .catch(function(error){ - console.log('error') - }) - // creates an interval of one month - month = parseInt(start_time.getMonth()); - year = parseInt(start_time.getFullYear()); - start_of_month = new Date(year, month, 1); - end_of_month = new Date(year, month+1, 1); - var current_amount_of_month_work_hours = 0 + // creates an interval of one month + month = parseInt(start_time.getMonth()); + year = parseInt(start_time.getFullYear()); + start_of_month = new Date(year, month, 1); + end_of_month = new Date(year, month+1, 1); + var current_amount_of_month_work_hours = 0 - await schedule_list$( + await schedule_list$( { and$: [ - {start_time: { - $gte: start_of_month, - $lt: end_of_month - }}, - {profile_id: profile_id} + {start_time: { + $gte: start_of_month, + $lt: end_of_month + }}, + {profile_id: profile_id} ] }) .then(await function(list_of_schedules){ @@ -296,25 +295,25 @@ module.exports = function(options){ current_amount_of_month_work_hours = ( current_amount_of_month_work_hours + get_schedule_duration(time.start_time, time.end_time) - ); + ); }) var new_amount_of_month_work_hours = ( current_amount_of_month_work_hours + get_schedule_duration(start_time,end_time)); - // validates if the amount of hours in month is bigger than allowed - if (new_amount_of_month_work_hours > settings.max_hours_month){ - result.max_hours_limit_error = ( - 'Você já tem {} horas nesse mês. O limite é de {} horas por mês'.format( - current_amount_of_month_work_hours, - settings.max_hours_month - ) - ); - } else { - //success - //nothing to do - } - }) + // validates if the amount of hours in month is bigger than allowed + if (new_amount_of_month_work_hours > settings.max_hours_month){ + result.max_hours_limit_error = ( + 'Você já tem {} horas nesse mês. O limite é de {} horas por mês'.format( + current_amount_of_month_work_hours, + settings.max_hours_month + ) + ); + } else { + //success + //nothing to do + } + }) .catch(function(error){ console.log(error); }) @@ -333,26 +332,26 @@ module.exports = function(options){ respond(null, schedule); }) } - }) + }) -// ############################################################################# + // ############################################################################# this.add('role:schedule, cmd:listByProfile', function (msg, respond) { - var schedule = this.make(schedule_db); - var id = msg.id; - console.log("id informado: {}".format(id)); - schedule.list$( - { - profile_id: id, - }, - function (error, schedule) { - console.log("Schedules:" + schedule); - respond(null, schedule); - } - ); + var schedule = this.make(schedule_db); + var id = msg.id; + console.log("id informado: {}".format(id)); + schedule.list$( + { + profile_id: id, + }, + function (error, schedule) { + console.log("Schedules:" + schedule); + respond(null, schedule); + } + ); }) -// ############################################################################# + // ############################################################################# this.add('role:schedule,cmd:listByProfile', function (msg, respond) { console.log(msg); @@ -362,35 +361,35 @@ module.exports = function(options){ end_year = new Date(msg.end_year); schedule.list$( - { - start_time: { - $gte: start_year, - $lt: end_year - }, - profile_id: schedule.profile_id, + { + start_time: { + $gte: start_year, + $lt: end_year }, - function(err,list){ - if (list.length > 0) { - console.log("Have list:"); - console.log(list); - console.log("EnDHave list"); - }else{ - console.log("We DONT Have list:"); - } - respond (null, list) + profile_id: schedule.profile_id, + }, + function(err,list){ + if (list.length > 0) { + console.log("Have list:"); + console.log(list); + console.log("EnDHave list"); + }else{ + console.log("We DONT Have list:"); + } + respond (null, list) }) }) - // ############################################################################# + // ############################################################################# - this.add('role:schedule,cmd:listBySector', function (msg, respond) { - console.log(msg); - var schedule = this.make(schedule_db); - schedule.sector_id = msg.sector_id; - start_year = new Date(msg.start_year); - end_year = new Date(msg.end_year); + this.add('role:schedule,cmd:listBySector', function (msg, respond) { + console.log(msg); + var schedule = this.make(schedule_db); + schedule.sector_id = msg.sector_id; + start_year = new Date(msg.start_year); + end_year = new Date(msg.end_year); - schedule.list$( + schedule.list$( { start_time: { $gte: start_year, @@ -399,20 +398,20 @@ module.exports = function(options){ sector_id: schedule.sector_id, }, function(err, sector_schedules){ respond (null, sector_schedules) + }); }); - }); - // ############################################################################# + // ############################################################################# - this.add('role:schedule,cmd:changeListBySector', function (msg, respond) { - console.log(msg); - var schedule = this.make(schedule_db); - sector_id = msg.sector_id; - profile_id = msg.profile_id; - start_year = new Date(msg.start_year); - end_year = new Date(msg.end_year); + this.add('role:schedule,cmd:changeListBySector', function (msg, respond) { + console.log(msg); + var schedule = this.make(schedule_db); + sector_id = msg.sector_id; + profile_id = msg.profile_id; + start_year = new Date(msg.start_year); + end_year = new Date(msg.end_year); - schedule.list$( + schedule.list$( { start_time: { $gte: start_year, @@ -422,40 +421,40 @@ module.exports = function(options){ profile_id:{$not:{$eq:profile_id}} }, function(err, sector_schedules){ respond (null, sector_schedules) + }); }); - }); -// ############################################################################# + // ############################################################################# -this.add('role:schedule,cmd:listByUser', function (msg, respond) { - console.log(msg); - var schedule = this.make(schedule_db); - user_id = msg.user_id; - start_year = msg.start_year; - end_year = msg.end_year; + this.add('role:schedule,cmd:listByUser', function (msg, respond) { + console.log(msg); + var schedule = this.make(schedule_db); + user_id = msg.user_id; + start_year = msg.start_year; + end_year = msg.end_year; - //buscar lista de profile_id a partir do user_id - profiles_id = [12, 1023, 131, 1231] + //buscar lista de profile_id a partir do user_id + profiles_id = [12, 1023, 131, 1231] - schedule.list$( + schedule.list$( { start_time: { $gte: start_year, $lt: end_year }, profile_id: [ - 12, - 1023, - 131, - 1231 + 12, + 1023, + 131, + 1231 ], }, function(err,list){ respond (null, list) + }) }) -}) -// ############################################################################# + // ############################################################################# this.add('role:schedule, cmd:createSettings', function error(msg, respond){ var scheduleSettings = this.make(schedule_settings_db) @@ -472,323 +471,320 @@ this.add('role:schedule,cmd:listByUser', function (msg, respond) { }) }) - // ############################################################################# - this.add('role:schedule,cmd:edit', async function edit (msg,respond) { - // schedule Entity - var schedule = this.make(schedule_db); - // scheduleSettings Entity - var scheduleSettings = this.make(schedule_settings_db); - // Promise of Schedule list$ function - var schedule_list$ = Promise.promisify(schedule.list$, { context: schedule }); - var settings_list$ = Promise.promisify(scheduleSettings.list$, { context: scheduleSettings }); - var load$ = Promise.promisify(schedule.load$, { context: schedule }); - var schedule_id = msg.schedule_id - console.log("Edit msg "); - console.log(msg); + // ############################################################################# + this.add('role:schedule,cmd:edit', async function edit (msg,respond) { + // schedule Entity + var schedule = this.make(schedule_db); + // scheduleSettings Entity + var scheduleSettings = this.make(schedule_settings_db); + // Promise of Schedule list$ function + var schedule_list$ = Promise.promisify(schedule.list$, { context: schedule }); + var settings_list$ = Promise.promisify(scheduleSettings.list$, { context: scheduleSettings }); + var load$ = Promise.promisify(schedule.load$, { context: schedule }); + var schedule_id = msg.schedule_id + console.log("Edit msg "); + console.log(msg); + + var edited_schedule = {}; + var current_schedule = {}; + var result = {}; + + await load$(schedule_id) + .then(function(schedule){ + if (schedule == null){ + console.log("if"); + console.log(schedule); + result.schedule_not_find = "Horário não encontrado"; + respond(null, result); + }else{ + console.log("else"); + console.log(schedule); + edited_schedule = schedule + current_schedule = schedule + } + }) + .catch(function(error){ + respond(null, error); + }) - var edited_schedule = {}; - var current_schedule = {}; - var result = {}; + console.log("edited"); + console.log(edited_schedule); - await load$(schedule_id) - .then(function(schedule){ - if (schedule == null){ - console.log("if"); - console.log(schedule); - result.schedule_not_find = "Horário não encontrado"; - respond(null, result); - }else{ - console.log("else"); - console.log(schedule); - edited_schedule = schedule - current_schedule = schedule + if (msg.start_time != null){ + console.log(msg.start_time); + edited_schedule.start_time = new Date(msg.start_time); + } else { + console.log("deu ruim na data edited_schedule.start_time"); + } + if (msg.end_time != null){ + console.log(msg.end_time); + edited_schedule.end_time = new Date(msg.end_time); + } else { + console.log("deu ruim na data edited_schedule.end_time"); } - }) - .catch(function(error){ - respond(null, error); - }) - - console.log("edited"); - console.log(edited_schedule); - - if (msg.start_time != null){ - console.log(msg.start_time); - edited_schedule.start_time = new Date(msg.start_time); - } else { - console.log("deu ruim na data edited_schedule.start_time"); - } - if (msg.end_time != null){ - console.log(msg.end_time); - edited_schedule.end_time = new Date(msg.end_time); - } else { - console.log("deu ruim na data edited_schedule.end_time"); - } - if((edited_schedule.end_time - edited_schedule.start_time) < 0){ - result.date_interval_error = 'O fim do horário deve ser maior que o início do horário' - console.log(result.date_interval_error); - }else if((edited_schedule.end_time - edited_schedule.start_time) == 0){ - result.date_equals_error = 'O horário de início e de fim não podem ser iguais' - console.log(result.date_interval_error); - } - if (get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) > 24){ - result.schedule_bigger_than_limit_error = 'O horário deve ser de no máximo 1 dia'; - console.log(result.schedule_bigger_than_limit_error) - } + if((edited_schedule.end_time - edited_schedule.start_time) < 0){ + result.date_interval_error = 'O fim do horário deve ser maior que o início do horário' + console.log(result.date_interval_error); + }else if((edited_schedule.end_time - edited_schedule.start_time) == 0){ + result.date_equals_error = 'O horário de início e de fim não podem ser iguais' + console.log(result.date_interval_error); + } + if (get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) > 24){ + result.schedule_bigger_than_limit_error = 'O horário deve ser de no máximo 1 dia'; + console.log(result.schedule_bigger_than_limit_error) + } - console.log("new edited"); - console.log(edited_schedule); + console.log("new edited"); + console.log(edited_schedule); - var settings = default_settings + var settings = default_settings - await settings_list$({}) + await settings_list$({}) .then(await function(list_of_settings){ if (list_of_settings.length > 0){ settings = list_of_settings[0] } else { - // Do nothing + // Do nothing + } + }) + .catch(function(err){ + // result.scheduleSettings_catch_error = "Não conseguiu achar schedule settings"; + }) + + // validates if the amount of minimum hours in a schedule is less than allowed + if (get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) < scheduleSettings.min_hours_schedule){ + result.min_hours_limit_error = ( + 'Limite mínimo de {} horas por horário'.format( + settings.min_hours_schedule + ) + ) + } + + console.log("result::::"); + console.log(result); + + var start_interval = new Date(edited_schedule.start_time) + var end_interval = new Date(edited_schedule.end_time) + start_interval.setDate(start_interval.getDate()-1) + end_interval.setDate(end_interval.getDate()+1) + // validates if any schedule conflict has occurred + console.log("id!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + console.log(edited_schedule.id); + await schedule_list$( + { + start_time: { + $gte: start_interval, + $lt: end_interval, + $not:{$eq:edited_schedule.start_time} + }, + profile_id: edited_schedule.profile_id + }) + .then(await function(list_of_schedules){ + console.log("list_of_schedules::::"); + console.log(list_of_schedules); + if (list_of_schedules.length != 0){ + list_of_schedules.forEach(function(schedule_element){ + if (edited_schedule.start_time >= schedule_element.start_time && edited_schedule.start_time < schedule_element.end_time){ + // if start_time are in schedule_element interval, it gives an error + result.conflicts_error = ( + '{} já possui um horário de {} à {}'.format( + 'Plantonista', + schedule_element.start_time, + schedule_element.end_time + ) + ) + } else if (edited_schedule.end_time > schedule_element.start_time && edited_schedule.end_time <= schedule_element.end_time){ + // if end_time are in schedule_element interval, it gives an error + result.conflicts_error = ( + '{} já possui um horário de {} à {}'.format( + 'Plantonista', + schedule_element.start_time, + schedule_element.end_time + ) + ) + } else if (edited_schedule.start_time <= schedule_element.start_time && edited_schedule.end_time >= schedule_element.end_time){ + // + result.conflicts_error = ( + '{} já possui um horário de {} à {}'.format( + 'Plantonista', + schedule_element.start_time, + schedule_element.end_time + ) + ) + } + }); + + } else { + // sucess + // nothing to do } }) - .catch(function(err){ - // result.scheduleSettings_catch_error = "Não conseguiu achar schedule settings"; + .catch(function(err) { + console.log('error') }) + console.log("result::::::::::"); + console.log(result); - // validates if the amount of minimum hours in a schedule is less than allowed - if (get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) < scheduleSettings.min_hours_schedule){ - result.min_hours_limit_error = ( - 'Limite mínimo de {} horas por horário'.format( - settings.min_hours_schedule - ) - ) - } - console.log("result::::"); - console.log(result); - - var start_interval = new Date(edited_schedule.start_time) - var end_interval = new Date(edited_schedule.end_time) - start_interval.setDate(start_interval.getDate()-1) - end_interval.setDate(end_interval.getDate()+1) - // validates if any schedule conflict has occurred - console.log("id!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - console.log(edited_schedule.id); - await schedule_list$( - { - start_time: { - $gte: start_interval, - $lt: end_interval, - $not:{$eq:edited_schedule.start_time} - }, - profile_id: edited_schedule.profile_id - }) - .then(await function(list_of_schedules){ - console.log("list_of_schedules::::"); - console.log(list_of_schedules); - if (list_of_schedules.length != 0){ - list_of_schedules.forEach(function(schedule_element){ - if (edited_schedule.start_time >= schedule_element.start_time && edited_schedule.start_time < schedule_element.end_time){ - // if start_time are in schedule_element interval, it gives an error - result.conflicts_error = ( - '{} já possui um horário de {} à {}'.format( - 'Plantonista', - schedule_element.start_time, - schedule_element.end_time - ) - ) - } else if (edited_schedule.end_time > schedule_element.start_time && edited_schedule.end_time <= schedule_element.end_time){ - // if end_time are in schedule_element interval, it gives an error - result.conflicts_error = ( - '{} já possui um horário de {} à {}'.format( - 'Plantonista', - schedule_element.start_time, - schedule_element.end_time - ) - ) - } else if (edited_schedule.start_time <= schedule_element.start_time && edited_schedule.end_time >= schedule_element.end_time){ - // - result.conflicts_error = ( - '{} já possui um horário de {} à {}'.format( - 'Plantonista', + // creates a one day interval + var first_hour_of_day = new Date(edited_schedule.start_time); + var last_hour_of_day = new Date(edited_schedule.start_time); + first_hour_of_day.setHours(0, 0); + last_hour_of_day.setHours(24,0); + current_amount_of_day_work_hours = 0; + + // lists every schedule in a one day interval + await schedule_list$( + { + start_time: { + $gte: first_hour_of_day, + $lt: last_hour_of_day, + $not:{$eq:edited_schedule.start_time} + }, + profile_id: edited_schedule.profile_id + }) + .then(await function(list_of_schedules){ + console.log("::::::list_of_schedules::::::"); + console.log(list_of_schedules); + // Walks on the list of schedules and counts the amount of hours worked + list_of_schedules.forEach( + function(schedule_element){ + console.log( + 'Parcial Working Hours: <|{}|>'.format( + current_amount_of_day_work_hours + ) + ); + current_amount_of_day_work_hours = ( + current_amount_of_day_work_hours + + get_schedule_duration( schedule_element.start_time, schedule_element.end_time - ) - ) + ) + ); } - }); - - } else { - // sucess - // nothing to do - } - }) - .catch(function(err) { - console.log('error') - }) - console.log("result::::::::::"); - console.log(result); + ) + new_amount_of_day_work_hours = ( + current_amount_of_day_work_hours + + get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) + ) + // validates if the amount of hours in a day is bigger than allowed + if (new_amount_of_day_work_hours > settings.max_hours_day){ + result.max_hours_limit_error = ( + 'Você já tem {} horas nesse dia. O limite é de {} horas por dia'.format( + current_amount_of_day_work_hours, + settings.max_hours_day + ) + ); + return result; + } else { + // sucess + // nothing to do + } + }) + .catch(function(err) { + console.log('error') + console.log(err); + }) + // crates an interval of one week + first_day_of_week = new Date(current_schedule.start_time.getFullYear(), + current_schedule.start_time.getMonth(), + (current_schedule.start_time.getDate() - + current_schedule.start_time.getDay()), 0, 0, 0); + last_day_of_week = new Date(current_schedule.start_time.getFullYear(), + current_schedule.start_time.getMonth(), + (current_schedule.start_time.getDate() - + current_schedule.start_time.getDay() + 7), 0, 0, 0); - // creates a one day interval - var first_hour_of_day = new Date(edited_schedule.start_time); - var last_hour_of_day = new Date(edited_schedule.start_time); - first_hour_of_day.setHours(0, 0); - last_hour_of_day.setHours(24,0); - current_amount_of_day_work_hours = 0; + var current_amount_of_week_work_hours = 0; - // lists every schedule in a one day interval - await schedule_list$( - { - start_time: { - $gte: first_hour_of_day, - $lt: last_hour_of_day, - $not:{$eq:edited_schedule.start_time} - }, - profile_id: edited_schedule.profile_id - }) - .then(await function(list_of_schedules){ - console.log("::::::list_of_schedules::::::"); - console.log(list_of_schedules); - // Walks on the list of schedules and counts the amount of hours worked - list_of_schedules.forEach( - function(schedule_element){ - console.log( - 'Parcial Working Hours: <|{}|>'.format( - current_amount_of_day_work_hours + // lists every schedule in a one week interval + await schedule_list$( + { + start_time: { + $gte: first_day_of_week, + $lt: last_day_of_week, + $not:{$eq:current_schedule.start_time} + }, + profile_id: edited_schedule.profile_id + }) + .then(await function(list_of_schedules){ + // Walks on the schedules list and count the amount of hours + list_of_schedules.forEach(function(time){ + console.log('Parcial: {}'.format(current_amount_of_week_work_hours)); + current_amount_of_week_work_hours = ( + current_amount_of_week_work_hours + + get_schedule_duration( + time.start_time, + time.end_time + ) ) + }) + + var new_amount_of_week_work_hours = ( + current_amount_of_week_work_hours ); - current_amount_of_day_work_hours = ( - current_amount_of_day_work_hours + + var is_greater_than_sunday = first_day_of_week.getDate() < edited_schedule.start_time.getDate() + var is_lower_than_saturday = last_day_of_week.getDate() > edited_schedule.start_time.getDate() + if(is_greater_than_sunday && is_lower_than_saturday){ + new_amount_of_week_work_hours = ( + new_amount_of_week_work_hours + get_schedule_duration( - schedule_element.start_time, - schedule_element.end_time + edited_schedule.start_time, + edited_schedule.end_time + ) ) - ); + if (new_amount_of_week_work_hours > settings.max_hours_week){ + result.max_week_hours_limit_error = ( + 'Você já tem {} horas nessa semana. O limite é de {} horas por semana'.format( + new_amount_of_week_work_hours, + settings.max_hours_week + ) + ); + } else { + // sucess!! nothing to do + } + }else { + // Do nothing } - ) - new_amount_of_day_work_hours = ( - current_amount_of_day_work_hours + - get_schedule_duration(edited_schedule.start_time, edited_schedule.end_time) - ) - // validates if the amount of hours in a day is bigger than allowed - if (new_amount_of_day_work_hours > settings.max_hours_day){ - result.max_hours_limit_error = ( - 'Você já tem {} horas nesse dia. O limite é de {} horas por dia'.format( - current_amount_of_day_work_hours, - settings.max_hours_day - ) - ); - return result; - } else { - // sucess - // nothing to do - } - }) - .catch(function(err) { - console.log('error') - console.log(err); - }) - - // crates an interval of one week - first_day_of_week = new Date(current_schedule.start_time.getFullYear(), - current_schedule.start_time.getMonth(), - (current_schedule.start_time.getDate() - - current_schedule.start_time.getDay()), 0, 0, 0); - last_day_of_week = new Date(current_schedule.start_time.getFullYear(), - current_schedule.start_time.getMonth(), - (current_schedule.start_time.getDate() - - current_schedule.start_time.getDay() + 7), 0, 0, 0); + console.log("Horas trabalhadas na semana: {}".format(current_amount_of_week_work_hours)) + console.log("Horas trabalhadas com o novo horário: {}".format(new_amount_of_week_work_hours)) - var current_amount_of_week_work_hours = 0; - // lists every schedule in a one week interval - await schedule_list$( - { - start_time: { - $gte: first_day_of_week, - $lt: last_day_of_week, - $not:{$eq:current_schedule.start_time} - }, - profile_id: edited_schedule.profile_id - }) - .then(await function(list_of_schedules){ - // Walks on the schedules list and count the amount of hours - list_of_schedules.forEach(function(time){ - console.log('Parcial: {}'.format(current_amount_of_week_work_hours)); - current_amount_of_week_work_hours = ( - current_amount_of_week_work_hours + - get_schedule_duration( - time.start_time, - time.end_time - ) - ) - }) - - var new_amount_of_week_work_hours = ( - current_amount_of_week_work_hours - ); - var is_greater_than_sunday = first_day_of_week.getDate() < edited_schedule.start_time.getDate() - var is_lower_than_saturday = last_day_of_week.getDate() > edited_schedule.start_time.getDate() - if(is_greater_than_sunday && is_lower_than_saturday){ - new_amount_of_week_work_hours = ( - new_amount_of_week_work_hours + - get_schedule_duration( - edited_schedule.start_time, - edited_schedule.end_time - ) - ) - if (new_amount_of_week_work_hours > settings.max_hours_week){ - result.max_week_hours_limit_error = ( - 'Você já tem {} horas nessa semana. O limite é de {} horas por semana'.format( - new_amount_of_week_work_hours, - settings.max_hours_week - ) - ); + // validates if the amount of hours in a week is bigger than allowed + if (new_amount_of_week_work_hours < settings.min_hours_week){ + result.min_week_hours_limit_error = ( + 'O limite mínimo de horas por semana é de {}. Você possui apenas {} horas nessa semana'.format( + settings.min_hours_week, + new_amount_of_week_work_hours + ) + ); } else { // sucess!! nothing to do } - }else { - // Do nothing - } - console.log("Horas trabalhadas na semana: {}".format(current_amount_of_week_work_hours)) - console.log("Horas trabalhadas com o novo horário: {}".format(new_amount_of_week_work_hours)) - - - // validates if the amount of hours in a week is bigger than allowed - if (new_amount_of_week_work_hours < settings.min_hours_week){ - result.min_week_hours_limit_error = ( - 'O limite mínimo de horas por semana é de {}. Você possui apenas {} horas nessa semana'.format( - settings.min_hours_week, - new_amount_of_week_work_hours - ) - ); - } else { - // sucess!! nothing to do - } - }) - .catch(function(error){ - console.log('error'); - console.log(err); - }) + }) + .catch(function(error){ + console.log('error'); + console.log(err); + }) - respond(null,{"resultado": "chegou depois das validações de conflitos!"}) - // creates an interval of one month - month = parseInt(edited_schedule.start_time.getMonth()); - year = parseInt(edited_schedule.start_time.getFullYear()); - start_of_month = new Date(year, month, 1); - end_of_month = new Date(year, month+1, 1); - var current_amount_of_month_work_hours = 0 + // creates an interval of one month + month = parseInt(current_schedule.start_time.getMonth()); + year = parseInt(current_schedule.start_time.getFullYear()); + start_of_month = new Date(year, month, 1); + end_of_month = new Date(year, month+1, 1); + var current_amount_of_month_work_hours = 0; - await schedule_list$( + await schedule_list$( { - and$: [ - {start_time: { - $gte: start_of_month, - $lt: end_of_month, - $not:{$eq:edited_schedule.start_time} - }}, - {profile_id: edited_schedule.profile_id} - ] + start_time: { + $gte: start_of_month, + $lt: end_of_month, + $not:{$eq:current_schedule.start_time} + }, + profile_id: current_schedule.profile_id }) .then(await function(list_of_schedules){ @@ -796,25 +792,37 @@ this.add('role:schedule,cmd:listByUser', function (msg, respond) { current_amount_of_month_work_hours = ( current_amount_of_month_work_hours + get_schedule_duration(time.start_time, time.end_time) - ); + ); }) - var new_amount_of_month_work_hours = ( - current_amount_of_month_work_hours + - get_schedule_duration(edited_schedule.start_time,edited_schedule.end_time)); - // validates if the amount of hours in month is bigger than allowed - if (new_amount_of_month_work_hours > settings.max_hours_month){ - result.max_hours_limit_error = ( - 'Você já tem {} horas nesse mês. O limite é de {} horas por mês'.format( - current_amount_of_month_work_hours, - settings.max_hours_month + var new_amount_of_month_work_hours = current_amount_of_month_work_hours + + var is_greater_than_sunday = start_of_month.getDate() < edited_schedule.start_time.getDate() + var is_lower_than_saturday = end_of_month.getDate() > edited_schedule.start_time.getDate() + if(is_greater_than_sunday && is_lower_than_saturday){ + new_amount_of_month_work_hours = ( + new_amount_of_month_work_hours + + get_schedule_duration( + edited_schedule.start_time, + edited_schedule.end_time + ) ) - ); - } else { - //success - //nothing to do - } - }) + // validates if the amount of hours in month is bigger than allowed + if (new_amount_of_month_work_hours > settings.max_hours_month){ + result.max_hours_limit_error = ( + 'Você já tem {} horas nesse mês. O limite é de {} horas por mês'.format( + current_amount_of_month_work_hours, + settings.max_hours_month + ) + ); + } else { + //success + //nothing to do + } + }else { + // Do nothing + } + }) .catch(function(error){ console.log(error); }) @@ -825,48 +833,44 @@ this.add('role:schedule,cmd:listByUser', function (msg, respond) { console.log(result); respond(null, result); } else { - schedule.start_time = edited_schedule.start_time - schedule.end_time = edited_schedule.end_time - schedule.sector_id = edited_schedule.sector_id - schedule.profile_id = edited_schedule.profile_id - schedule.save$(function(err,schedule){ + edited_schedule.save$(function(err,schedule){ respond(null, schedule); }) } - }) + }) -// ############################################################################# + // ############################################################################# this.add('role:schedule, cmd:delete', async function (msg, respond) { - var schedule = this.make(schedule_db); - var schedule_id = msg.schedule_id; - result = {} + var schedule = this.make(schedule_db); + var schedule_id = msg.schedule_id; + result = {} - console.log("excluir:" + schedule_id); + console.log("excluir:" + schedule_id); - var remove$ = Promise.promisify(schedule.remove$, { context: schedule }); + var remove$ = Promise.promisify(schedule.remove$, { context: schedule }); - await remove$({id: schedule_id}) - .then(function(deleted_schedule){ - if (deleted_schedule != null){ - deleted_schedule.sucess = true; - respond(null, deleted_schedule); - }else{ - result.sucess = false; - result.schedule_not_find = "Horário não encontrado"; - respond(null, result); - } - }) - .catch(function(error){ + await remove$({id: schedule_id}) + .then(function(deleted_schedule){ + if (deleted_schedule != null){ + deleted_schedule.sucess = true; + respond(null, deleted_schedule); + }else{ result.sucess = false; - result.remove_error = "Erro ao remover horário"; + result.schedule_not_find = "Horário não encontrado"; respond(null, result); - }) + } + }) + .catch(function(error){ + result.sucess = false; + result.remove_error = "Erro ao remover horário"; + respond(null, result); + }) }) -// ############################################################################# + // ############################################################################# this.add('role:schedule, cmd:error', function error(msg, respond) { - respond(null, { success: false, message: 'acesso negado' }); + respond(null, { success: false, message: 'acesso negado' }); }) } From 98b8b98c2462ae39aeaa6d96e6ebc532eea1cff9 Mon Sep 17 00:00:00 2001 From: Eliseu Egewarth Date: Mon, 25 Jun 2018 10:07:53 -0300 Subject: [PATCH 5/6] Atualizando testes Signed-off-by: Eliseu Egewarth --- Server/plg_schedule.js | 4 ++-- Server/test/schedule.test.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Server/plg_schedule.js b/Server/plg_schedule.js index aa3e57e..00fda0b 100644 --- a/Server/plg_schedule.js +++ b/Server/plg_schedule.js @@ -9,8 +9,8 @@ "max_hours_week": 50, "min_hours_month": 60, "min_hours_week": 11, - "min_hours_schedule": 2, - "max_hours_day": 6, + "min_hours_schedule": 1, + "max_hours_day": 24, "templates": [1,2,3, 4] }; // 3600000.0 (1000ms * 60s * 60m), is the number of milliseconds in 1 hour. diff --git a/Server/test/schedule.test.js b/Server/test/schedule.test.js index 11d67da..066720b 100644 --- a/Server/test/schedule.test.js +++ b/Server/test/schedule.test.js @@ -20,7 +20,7 @@ describe('Create schedule', function() { seneca.act({ role: "schedule", - cmd: "createSchedule", + cmd: "create", start_time: "2018-09-09T20:00", end_time: "2018-09-09T22:00", sector_id: "121212121212121212121212", @@ -42,7 +42,7 @@ describe('Create schedule settings', function() { seneca.act({ role: "schedule", - cmd: "createScheduleSettings", + cmd: "createSettings", max_hours_month: 150, max_hours_week: 45, min_hours_month: 50, @@ -62,7 +62,7 @@ describe('Create schedule settings', function() { seneca.act({ role: "schedule", - cmd: "createScheduleSettings", + cmd: "createSettings", max_hours_month: "150", max_hours_week: "45", min_hours_month: "50", @@ -85,7 +85,7 @@ describe('List schedules by profile', function() { seneca.act({ role: "schedule", - cmd: "createSchedule", + cmd: "create", start_time: "2018-09-09T19:00", end_time: "2018-09-09T23:00", sector_id: "121212121212121212121212", @@ -121,7 +121,7 @@ describe('List schedules by profile', function() { // // seneca.act({ // role: "schedule", -// cmd: "createSchedule", +// cmd: "create", // start_time: "2018-09-09T19:00", // end_time: "2018-09-09T23:00", // sector_id: "121212121212121212121212", @@ -166,7 +166,7 @@ describe('List schedules by profile', function() { // // seneca.act({ // role: "schedule", -// cmd: "createSchedule", +// cmd: "create", // start_time: start_time, // end_time: end_time, // sector_id: sector_id, From d6ccd4d11166c31e1aa214c2e54c075dc62b74fa Mon Sep 17 00:00:00 2001 From: Eliseu Egewarth Date: Thu, 12 Jul 2018 10:35:04 -0300 Subject: [PATCH 6/6] WIP Signed-off-by: Eliseu Egewarth --- Client/api.js | 2 +- Server/plg_schedule.js | 36 ++++++++++++++---------------------- Server/test/schedule.test.js | 4 +++- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/Client/api.js b/Client/api.js index dd69aae..0f6772e 100644 --- a/Client/api.js +++ b/Client/api.js @@ -144,7 +144,7 @@ function api(options){ respond(null, result) // else, everything success } else { - this.act('role:schedule,cmd:edit',{ + this.act('role:schedule,cmd:create',{ start_time: start_time, end_time: end_time, sector_id: sector_id, diff --git a/Server/plg_schedule.js b/Server/plg_schedule.js index 00fda0b..4e7a34d 100644 --- a/Server/plg_schedule.js +++ b/Server/plg_schedule.js @@ -329,37 +329,29 @@ schedule.sector_id = sector_id schedule.profile_id = profile_id schedule.save$(function(err,schedule){ + console.log(schedule.start_time); + console.log(schedule.end_time); + console.log(schedule.sector_id); + console.log(schedule.profile_id); respond(null, schedule); }) } }) - // ############################################################################# - - this.add('role:schedule, cmd:listByProfile', function (msg, respond) { - var schedule = this.make(schedule_db); - var id = msg.id; - console.log("id informado: {}".format(id)); - schedule.list$( - { - profile_id: id, - }, - function (error, schedule) { - console.log("Schedules:" + schedule); - respond(null, schedule); - } - ); - }) - - // ############################################################################# + // ############################################################################# this.add('role:schedule,cmd:listByProfile', function (msg, respond) { console.log(msg); var schedule = this.make(schedule_db); - schedule.profile_id = msg.profile_id; - start_year = new Date(msg.start_year); - end_year = new Date(msg.end_year); - + console.log("Field: profile_id"); + var profile_id = msg.profile_id; + console.log(schedule.profile_id); + console.log("Field: start_year"); + var start_year = new Date(msg.start_year); + console.log(start_year); + console.log("Field: end_year"); + var end_year = new Date(msg.end_year); + console.log(end_year); schedule.list$( { start_time: { diff --git a/Server/test/schedule.test.js b/Server/test/schedule.test.js index 066720b..1276ab9 100644 --- a/Server/test/schedule.test.js +++ b/Server/test/schedule.test.js @@ -94,7 +94,9 @@ describe('List schedules by profile', function() { seneca.act({ role: "schedule", cmd: "listByProfile", - id: "131313131313131313131313", + profile_id: "131313131313131313131313", + start_year: "2018-01-01T00:00", + end_year: "2019-01-01T00:00" }, function(err, result) { expect(result[0].sector_id).to.equal('121212121212121212121212') expect(result[0].profile_id).to.equal('131313131313131313131313')