Skip to content

Commit

Permalink
Merge branch 'tom-120-cancel'
Browse files Browse the repository at this point in the history
  • Loading branch information
vpp committed May 18, 2017
2 parents c476af3 + c5a4eeb commit 47e0147
Show file tree
Hide file tree
Showing 19 changed files with 750 additions and 83 deletions.
58 changes: 58 additions & 0 deletions lib/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,5 +397,63 @@ Email.prototype.promise_reset_password_email = function(args){
});
};

Email.prototype.promise_leave_request_cancel_emails = function(args){
var self = this,
leave = args.leave,
send_mail = self.get_send_email();

var promise_email_to_supervisor = self.promise_rendered_email_template({
template_name : 'leave_request_cancel_to_supervisor',
context : {
leave : leave,
approver : leave.get('approver'),
requester : leave.get('user'),
user : leave.get('approver'),
}
})
.then(function(email_obj){

return send_mail({
from : config.get('application_sender_email'),
to : leave.get('approver').email,
subject : email_obj.subject,
html : email_obj.body,
})
.then(function(send_result){
return leave.get('approver').record_email_addressed_to_me(email_obj)
.then(function(){ return bluebird.resolve( send_result ); });
});
});

var promise_email_to_requestor = self.promise_rendered_email_template({
template_name : 'leave_request_cancel_to_requestor',
context : {
leave : leave,
approver : leave.get('approver'),
requester : leave.get('user'),
user : leave.get('user'),
}
})
.then(function(email_obj){

return send_mail({
from : config.get('application_sender_email'),
to : leave.get('user').email,
subject : email_obj.subject,
html : email_obj.body,
})
.then(function(send_result){
return leave.get('user').record_email_addressed_to_me(email_obj)
.then(function(){ return bluebird.resolve( send_result ); });
});
});

return bluebird.join(
promise_email_to_supervisor, promise_email_to_requestor,
function(){
return bluebird.resolve();
}
);
};

module.exports = Email;
17 changes: 17 additions & 0 deletions lib/model/db/leave.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ module.exports = function(sequelize, DataTypes) {
return 4;
},

status_canceled : function() {
return 5;
},


leave_day_part_all : function(){
return 1;
Expand Down Expand Up @@ -381,6 +385,18 @@ promise_to_revoke : function(){
})
},

promise_to_cancel : function(){
var self = this;

if ( ! self.is_new_leave() ) {
throw new Error('An attempt to cancel non-new leave request id : '+self.id);
}

self.status = Leave.status_canceled();

return self.save();
},

get_leave_type_name : function() {
var leave_type = this.get('leave_type');

Expand All @@ -405,6 +421,7 @@ promise_approver : function() {
})
},


},
});

Expand Down
22 changes: 21 additions & 1 deletion lib/model/mixin/user/absence_aware.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ module.exports = function(sequelize){

return this_user.getMy_leaves({
where : {
status : { $ne : sequelize.models.Leave.status_rejected()},
$and : [
{ status : { $ne : sequelize.models.Leave.status_rejected() } },
{ status : { $ne : sequelize.models.Leave.status_canceled() } },
],

$or : {
date_start : days_filter,
Expand Down Expand Up @@ -309,6 +312,23 @@ module.exports = function(sequelize){
});
}; // END of promise_leaves_to_be_processed

this.promise_cancelable_leaves = function(){
var self = this;

return self.promise_my_leaves({
ignore_year : true,
filter_status : [ sequelize.models.Leave.status_new() ],
})
.then(function(leaves){
return Promise.map(leaves, function(leave){
return leave.user.promise_schedule_I_obey();
},{
concurrency : 10,
})
.then(function(){ return Promise.resolve(leaves) });
});
};


this.calculate_number_of_days_taken_from_allowance = function(args){
var self = this,
Expand Down
54 changes: 54 additions & 0 deletions lib/route/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,60 @@ router.post(
})
);

router.post('/cancel/', function(req, res){

var request_id = validator.trim( req.param('request') );

Promise.try(function(){
return req.user.promise_cancelable_leaves()
})
.then(function(leaves){
var leave_to_cancel = _.find(leaves, function(leave){
return String(leave.id) === String(request_id);
});

if ( ! leave_to_cancel ) {
throw new Error('Given leave request is not amoung those current user can cancel');
}

return Promise.resolve(leave_to_cancel);
})
.then(function(leave){
return leave.promise_to_cancel()
.then(function(){ return Promise.resolve(leave)});
})
.then(function(leave){
return leave.reload({
include : [
{model : req.app.get('db_model').User, as : 'user'},
{model : req.app.get('db_model').User, as : 'approver'},
{model : req.app.get('db_model').LeaveType, as : 'leave_type' },
],
});
})
.then(function(leave){

var Email = new EmailTransport();

return Email.promise_leave_request_cancel_emails({
leave : leave,
})
.then(function(){
return Promise.resolve(leave);
});
})
.then(function(leave){
req.session.flash_message('The leave request was canceled');
})
.catch(function(error){
console.log('An error occurred: '+error);
req.session.flash_error('Failed to cancel leave request');
})
.finally(function(){
return res.redirect_with_session('/requests/');
});
});

router.post(
'/revoke/',
function(req, res){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "TimeOff.Management",
"version": "0.4.4",
"version": "0.5.0",
"private": false,
"description": "Simple yet powerful absence management software for small and medium size business",
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
'use strict';

var test = require('selenium-webdriver/testing'),
config = require('../lib/config'),
config = require('../../lib/config'),
application_host = config.get_application_host(),
By = require('selenium-webdriver').By,
expect = require('chai').expect,
_ = require('underscore'),
Promise = require("bluebird"),
moment = require('moment'),
until = require('selenium-webdriver').until,
login_user_func = require('../lib/login_with_user'),
register_new_user_func = require('../lib/register_new_user'),
logout_user_func = require('../lib/logout_user'),
open_page_func = require('../lib/open_page'),
submit_form_func = require('../lib/submit_form'),
check_elements_func = require('../lib/check_elements'),
check_booking_func = require('../lib/check_booking_on_calendar'),
add_new_user_func = require('../lib/add_new_user');
login_user_func = require('../../lib/login_with_user'),
register_new_user_func = require('../../lib/register_new_user'),
logout_user_func = require('../../lib/logout_user'),
open_page_func = require('../../lib/open_page'),
submit_form_func = require('../../lib/submit_form'),
check_elements_func = require('../../lib/check_elements'),
check_booking_func = require('../../lib/check_booking_on_calendar'),
add_new_user_func = require('../../lib/add_new_user');


/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
'use strict';

var test = require('selenium-webdriver/testing'),
config = require('../lib/config'),
config = require('../../lib/config'),
application_host = config.get_application_host(),
By = require('selenium-webdriver').By,
expect = require('chai').expect,
_ = require('underscore'),
Promise = require("bluebird"),
moment = require('moment'),
until = require('selenium-webdriver').until,
login_user_func = require('../lib/login_with_user'),
register_new_user_func = require('../lib/register_new_user'),
logout_user_func = require('../lib/logout_user'),
open_page_func = require('../lib/open_page'),
submit_form_func = require('../lib/submit_form'),
check_elements_func = require('../lib/check_elements'),
check_booking_func = require('../lib/check_booking_on_calendar'),
add_new_user_func = require('../lib/add_new_user');
login_user_func = require('../../lib/login_with_user'),
register_new_user_func = require('../../lib/register_new_user'),
logout_user_func = require('../../lib/logout_user'),
open_page_func = require('../../lib/open_page'),
submit_form_func = require('../../lib/submit_form'),
check_elements_func = require('../../lib/check_elements'),
check_booking_func = require('../../lib/check_booking_on_calendar'),
add_new_user_func = require('../../lib/add_new_user');

describe("Check the client side logic to facilitate filling new absence form", function(){

Expand Down
Loading

0 comments on commit 47e0147

Please sign in to comment.