import LeaveRequest from '../models/leave-request.model';

function load(req, res, next, id) {
  LeaveRequest.get(id)
    .then((leaveRequest) => {
      req.leaveRequest = leaveRequest;
      return next();
    })
    .catch(e => next(e));
}

function get(req, res) {
  return res.json(req.user);
}

function create(req, res, next) {
  const user = req.user;
  const leave = new LeaveRequest(
  {
    start: req.body.start,
    end: req.body.end,
    leaveType: req.body.leaveType,
    userId: user.id,
    status: req.body.status,
    workDays: req.body.workDays
  });

  leave.save()
    .then(savedLeave => res.json(savedLeave))
    .catch(e => next(e));
}

function update(req, res, next) {
  const leave = req.leaveRequest;

  leave.status = req.body.status;

  leave.save()
    .then(savedRequest => res.json(savedRequest))
    .catch(e => next(e));
}

function list(req, res, next) {
  const { limit = 50, skip = 0 } = req.query;
  LeaveRequest.list({ limit, skip })
    .then(users => res.json(users))
    .catch(e => next(e));
}

function remove(req, res, next) {
  const user = req.user;
  user.remove()
    .then(deletedUser => res.json(deletedUser))
    .catch(e => next(e));
}

function getForUser(req, res, next) {
  const { userId } = req.params;

  LeaveRequest.find({ userId })
    .then(leaves => res.json(leaves))
    .catch(e => next(e));
}

export default { load, get, create, update, list, remove, getForUser };

@@ -50,16 +50,57 @@
 .model';
+%0Aimport User from '../models/user.model';
 %0A%0Afuncti
@@ -649,28 +649,396 @@
  =%3E 
-res.json(savedLeave)
+%7B%0A      // decrease the remaining days only for annual leave%0A      if (savedLeave.leaveType === 'annual-leave') %7B%0A        return User.get(savedLeave.userId)%0A          .then(usr =%3E %7B%0A            usr.holidays -= savedLeave.workDays;%0A%0A            return usr.save()%0A              .then(() =%3E res.json(savedLeave))%0A          %7D)%0A      %7D else %7B%0A        return res.json(savedLeave);%0A      %7D%0A    %7D
 )%0A  

decrease users remaining days when a leave request of type annual-leave is created