Skip to content

Commit

Permalink
adding auto approved leave types (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
fery authored Jun 16, 2022
1 parent 274e671 commit e913a88
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 18 deletions.
5 changes: 3 additions & 2 deletions lib/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const
config = require('./config'),
nodemailer = require('nodemailer'),
smtpTransport = require('nodemailer-smtp-transport'),
model = require('./model/db'),
{getCommentsForLeave} = require('./model/comment');

function Email(){
Expand Down Expand Up @@ -166,7 +167,7 @@ Email.prototype.promise_leave_request_revoke_emails = function(args){
let template_name_to_supervisor = 'leave_request_revoke_to_supervisor';
let template_name_to_requestor = 'leave_request_revoke_to_requestor';

if ( leave.get('user').is_auto_approve() ) {
if ( model.Leave.does_skip_approval( leave.get('user'), leave.get('leave_type') ) ) {
template_name_to_supervisor = 'leave_request_revoke_to_supervisor_autoapprove';
template_name_to_requestor = 'leave_request_revoke_to_requestor_autoapprove';
}
Expand Down Expand Up @@ -226,7 +227,7 @@ Email.prototype.promise_leave_request_emails = function(args){
var template_name_to_supervisor = 'leave_request_to_supervisor';
var template_name_to_requestor = 'leave_request_to_requestor';

if ( leave.get('user').is_auto_approve() ) {
if ( model.Leave.does_skip_approval( leave.get('user'), leave.get('leave_type') ) ) {
template_name_to_supervisor = 'leave_request_to_supervisor_autoapprove';
template_name_to_requestor = 'leave_request_to_requestor_autoapprove';
}
Expand Down
30 changes: 19 additions & 11 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) {
leave_day_part_all : () => 1,
leave_day_part_morning : () => 2,
leave_day_part_afternoon : () => 3,

does_skip_approval: function( user, leave_type ) {
return user.is_auto_approve() || leave_type.is_auto_approve();
}
},

instanceMethods : {
Expand Down Expand Up @@ -189,6 +193,10 @@ is_approved_leave : function() {
this.status === Leave.status_pended_revoke() ;
},

is_auto_approve : function() {
return Leave.does_skip_approval(this.user, this.leave_type);
},

// Determine if leave starts with half day in the morning
//
does_start_half_morning : function() {
Expand Down Expand Up @@ -346,28 +354,28 @@ promise_to_approve : function(args) {
},

promise_to_revoke : function(){
let self = this;

return self.getUser({
return this.reload({
include : [
{
model : sequelize.models.Department,
as : 'department',
}
model : sequelize.models.User,
as : 'user',
include: [ {model: sequelize.models.Department, as: 'department'} ]
},
{model : sequelize.models.LeaveType, as : 'leave_type' },
],
})
.then(function(user){
.then(function(leave){

var new_leave_status = user.is_auto_approve()
var new_leave_status = leave.is_auto_approve()
? Leave.status_rejected()
: Leave.status_pended_revoke();

// By default it is user main boss is one who has to approve the revoked request
self.approverId = user.department.bossId;
leave.approverId = leave.user.department.bossId;

self.status = new_leave_status;
leave.status = new_leave_status;

return self.save();
return leave.save();
});
},

Expand Down
10 changes: 9 additions & 1 deletion lib/model/db/leave_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ module.exports = function(sequelize, DataTypes) {
defaultValue : 0,
comment : "Is used to determine sorting order of leave types",
},
auto_approve : {
type : DataTypes.BOOLEAN,
allowNull : false,
defaultValue : false,
},
}, {
classMethods: {
associate : function( models ) {
Expand Down Expand Up @@ -62,8 +67,11 @@ module.exports = function(sequelize, DataTypes) {
return value_in_db.match(/^\s*\#/)
? 'leave_type_color_1'
: value_in_db;
}
},

is_auto_approve : function(){
return this.auto_approve === true;
}
}
});

Expand Down
2 changes: 1 addition & 1 deletion lib/model/leave/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function createNewLeave(args){
.then(() => employee.promise_boss())
.then(main_supervisor => {

const new_leave_status = employee.is_auto_approve()
const new_leave_status = Models.Leave.does_skip_approval(employee, leave_type)
? Models.Leave.status_approved()
: Models.Leave.status_new();

Expand Down
2 changes: 1 addition & 1 deletion lib/route/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ router.post(
req.session.flash_message(
'You have requested leave to be revoked. '
+ (
processed_leave.user.is_auto_approve()
processed_leave.is_auto_approve()
? ''
: 'Your supervisor needs to approve it'
)
Expand Down
4 changes: 4 additions & 0 deletions lib/route/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ function get_and_validate_leave_type(args) {
first_record = validator.trim(req.body['first_record']) || 0,
use_allowance = validator.toBoolean(
req.body['use_allowance__'+suffix]
),
auto_approve = validator.toBoolean(
req.body['auto_approve__'+suffix]
);

// If no name for leave type was provided: do nothing - treat case
Expand Down Expand Up @@ -536,6 +539,7 @@ function get_and_validate_leave_type(args) {
name : name,
color : color,
use_allowance : use_allowance,
auto_approve : auto_approve,
limit : limit,
sort_order : ( (first_record && (String(first_record)===String(suffix))? 1 : 0) ),
};
Expand Down
26 changes: 26 additions & 0 deletions migrations/20200204185448-add_auto_approve_flag_to_leave_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

var models = require('../lib/model/db');

module.exports = {
up: function (queryInterface, Sequelize) {

queryInterface.describeTable('LeaveTypes').then(function(attributes){

if (attributes.hasOwnProperty('auto_approve')) {
return 1;
}

return queryInterface.addColumn(
'LeaveTypes',
'auto_approve',
models.LeaveType.attributes.auto_approve
);
});

},

down: function (queryInterface, Sequelize) {
return queryInterface.removeColumn('LeaveTypes', 'auto_approve');
}
};
File renamed without changes.
Loading

0 comments on commit e913a88

Please sign in to comment.