Ejemplo n.º 1
0
        public void Save(VacationsRequest vacationsRequest, int userId)
        {
            var attorney = context.Attorneys
                           .Where(x => x.UserId == userId)
                           .Select(a => new { a.Id, a.Name, a.Email }).FirstOrDefault();

            vacationsRequest.AttorneyId = attorney.Id;
            //Get all the emails and userIds to Notify
            var approverAttorneys = context.Attorneys
                                    .Where(x => x.CanApproveVacations)
                                    .Select(a => new { a.Id, a.Email, a.UserId }).ToList();

            //Add the approval fields to the request made
            vacationsRequest.VacationsRequestAnswers = approverAttorneys.Select(a => new VacationsRequestAnswer
            {
                ApproverId = a.UserId,
                IsApproved = null
            }).ToList();
            context.VacationsRequests.Add(vacationsRequest);
            context.SaveChanges();
            string message = "El abogado '{0}' ha realizado una solicitud de {1} día(s) de vacaciones a comenzar el {2}, " +
                             "debido al motivo '{3}'. Esta solicitud se encuentra en espera de aprobación. **Este es un mensaje autogenerado por el sistema, " +
                             "favor no responder**";
            var mailList = approverAttorneys.Select(a => a.Email).ToList();
            var subject  = "Nueva solicitud de vacaciones";

            mailList.Add(attorney.Email);
            message = String.Format(message, attorney.Name, vacationsRequest.Quantity, vacationsRequest.StartDate.ToString("dd/MM/yyyy"),
                                    vacationsRequest.Reason);
            _mailer.SendMail(mailList, subject, message);
        }
Ejemplo n.º 2
0
        public void Approve(VacationsRequest vacationsRequest, int approverId, out string message)
        {
            message = "";
            //Get the answer to be updated
            var approverAnswer = context.VacationsRequestAnswers
                                 .Where(a => a.RequestId == vacationsRequest.Id && a.ApproverId == approverId && a.IsApproved == null).FirstOrDefault();

            if (approverAnswer == null)
            {
                message = "No se puede responder a esta solicitud";
                return;
            }
            string subject = "";

            //To handle rejection
            if (vacationsRequest.IsApproved == false)
            {
                approverAnswer.IsApproved = false;
                //Notify of the rejection to everyone involved
                var notificationEmails = new List <string>();
                var requestor          = context.Attorneys.Where(a => a.Id == vacationsRequest.AttorneyId).Select(a => new { a.Email, a.Id, a.Name }).First();

                //Not used the following since only to the requestor is notified of the rejectal
                //var otherApprovers = context.VacationsRequestAnswers
                //    .Include(x => x.Approver)
                //    .ThenInclude(u => u.Attorney)
                //    .Where(x => x.RequestId == vacationsRequest.Id && x.ApproverId != approverId)
                //    .Select(a => new { a.Approver.Attorney.Id, a.Approver.Attorney.Name, a.Approver.Attorney.Email })
                //    .ToList();

                var rejector = context.Attorneys.Where(a => a.UserId == approverId).Select(a => new { a.Name, a.Email, a.Id, a.UserId }).First();
                notificationEmails.Add(requestor.Email);
                //notificationEmails.Add(rejector.Email);
                //Notification for the rest of approvers so they dont have to worry about their approval
                //if (otherApprovers.Any())
                //{
                //    notificationEmails.AddRange(otherApprovers.Select(o => o.Email).ToList());
                //}

                string messageBody = $"La solicitud realizada por {requestor.Name} de {vacationsRequest.Quantity} día(s) de vacaciones para la fecha {vacationsRequest.StartDate.ToString("dd/MM/yyyy")}, " +
                                     $"por motivo de '{vacationsRequest.Reason}', ha sido rechazada por {rejector.Name} debido a '{vacationsRequest.RejectReason}'. **Este es un mensaje autogenerado por el sistema, favor no responder**";
                message = "Se ha rechazado la solicitud exitosamente!";
                subject = "Rechazo de solicitud de vacaciones";
                context.Update(vacationsRequest);
                _mailer.SendMail(notificationEmails, subject, messageBody);
            }
            else if (vacationsRequest.IsApproved == true)
            {
                approverAnswer.IsApproved = true;
                //Check if there is someone else to approve besides the approver
                bool anyOtherPendingAnswer = context.VacationsRequestAnswers
                                             .Any(x => x.RequestId == vacationsRequest.Id && x.ApproverId != approverId && x.IsApproved == null);
                if (anyOtherPendingAnswer)
                {
                    message = "Se ha respondido con éxito a la solicitud de vacaciones, la solicitud se encuentra en espera del resto de aprobaciones";
                }
                else
                {
                    vacationsRequest.RejectReason = null;

                    //Notify that it was marked as completed
                    var notificationEmails = new List <string>();
                    var requestor          = context.Attorneys.Where(a => a.Id == vacationsRequest.AttorneyId).Select(a => new { a.Email, a.Id, a.Name }).First();

                    var otherApprovers = context.VacationsRequestAnswers
                                         .Include(x => x.Approver)
                                         .ThenInclude(u => u.Attorney)
                                         .Where(x => x.RequestId == vacationsRequest.Id && x.ApproverId != approverId)
                                         .Select(a => new { a.Approver.Attorney.Id, a.Approver.Attorney.Name, a.Approver.Attorney.Email })
                                         .ToList();

                    var approver = context.Attorneys.Where(a => a.UserId == approverId).Select(a => new { a.Name, a.Email, a.Id, a.UserId }).First();

                    notificationEmails.Add(requestor.Email);
                    //Notify to the persons allowed via config of the application
                    string notifyTo = Convert.ToString(config["LexincorpAdmin:NotifyToVacationApproval"]);
                    notificationEmails.Add(notifyTo);
                    //notificationEmails.Add(approver.Email);

                    string otherApproverNamesArray = "";
                    //If i wanted to notify to the rest of the approvers
                    if (otherApprovers.Any())
                    {
                        otherApproverNamesArray  = String.Join(", ", otherApprovers.Select(a => a.Name).ToList());
                        otherApproverNamesArray += " y ";
                        //If i wanted to notify the other approvers
                        //notificationEmails.AddRange(otherApprovers.Select(o => o.Email).ToList());
                    }

                    string messageBody = $"La solicitud realizada por {requestor.Name} de {vacationsRequest.Quantity} día(s) de vacaciones para la fecha {vacationsRequest.StartDate.ToString("dd/MM/yyyy")}, " +
                                         $"por motivo de '{vacationsRequest.Reason}', ha sido completamente aprobada por {otherApproverNamesArray}{approver.Name}. **Este es un mensaje autogenerado por el sistema, favor no responder**";
                    message = "Se ha completado con éxito la aprobación de la solicitud de vacaciones";
                    subject = $"Solicitud de vacaciones aprobada para el {vacationsRequest.StartDate.ToString("dd/MM/yyyy")}";
                    context.Update(vacationsRequest);


                    _mailer.SendMail(notificationEmails, subject, messageBody);
                }
            }
            else
            {
                message = "No se hizo nada, ni se afirmó ni rechazó la solicitud";
                return;
            }
            context.SaveChanges();
            //var attorney = context.Attorneys.Where(a => a.Id == vacationsRequest.AttorneyId).FirstOrDefault();
            //if(vacationsRequest.IsApproved == true && attorney.VacationCount >= vacationsRequest.Quantity)
            //{
            //    attorney.VacationCount -= vacationsRequest.Quantity;
            //}
            //context.SaveChanges();
        }