Ejemplo n.º 1
0
        public IResult AddUsersToConductInterview(List <ScheduleUserForCandidateModel> scheduleUserForCandidateModelList)
        {
            var result = new Result
            {
                Operation = Operation.Read,
                Status    = Status.Success
            };

            try
            {
                ScheduleUserForCandidate scheduleUser = new ScheduleUserForCandidate();
                scheduleUser.MapFromViewModel(scheduleUserForCandidateModelList.First());
                var scheduledUsersModelList = scheduleUserForCandidateModelList.Select(x => x.UserId).ToList();
                var scheduledUsers          = _candidateRepository.GetScheduledUserByApprovalEvent(scheduleUser);
                var scheduledUsersList      = scheduledUsers.Select(x => x.UserId).ToList();
                var addingScheduledUsers    = new List <ScheduleUserForCandidateModel>();
                if (scheduledUsersList.Any())
                {
                    var existingScheduledUsers = scheduledUsersModelList.Intersect(scheduledUsersList).ToList();
                    var addScheduledUsers      = scheduledUsersModelList.Except(existingScheduledUsers).ToList();
                    var removingScheduledusers = scheduledUsersList.Except(existingScheduledUsers).ToList();

                    if (existingScheduledUsers.Any())
                    {
                        var scheduledUserList = scheduledUsers.Where(x => existingScheduledUsers.Contains(x.UserId)).ToList();
                        scheduledUserList.ForEach(x => x.MapAuditColumns((ClaimsIdentity)_principal.Identity));
                    }

                    if (removingScheduledusers.Any())
                    {
                        var scheduledUsersModeList = new List <ScheduleUserForCandidateModel>();
                        var scheduledUserList      = scheduledUsers.Where(x => removingScheduledusers.Contains(x.UserId)).ToList();
                        scheduledUserList.ForEach(x => x.MapDeleteColumns((ClaimsIdentity)_principal.Identity));

                        foreach (var scheduledUser in scheduledUserList)
                        {
                            var scheduleUserModel = new ScheduleUserForCandidateModel();
                            scheduleUserModel.MapFromModel(scheduledUser);

                            var approvalEvent = new ApprovalEventViewModel();
                            scheduleUserModel.ApprovalEvent = (ApprovalEventViewModel)approvalEvent.MapFromModel(scheduledUser.ApprovalEvent);

                            var user = new UserViewModel();
                            scheduleUserModel.User = (UserViewModel)user.MapFromModel(scheduledUser.User);

                            CandidateViewModel candidateView = new CandidateViewModel();
                            candidateView.MapFromModel(_candidateRepository.GetByID(scheduledUser.CandidateId));

                            scheduleUserModel.Candidate = candidateView;
                            scheduleUserModel.InterviewScheduledDate = scheduledUser.ScheduledOn.ToString("yyyy-MM-dd");
                            scheduleUserModel.InterviewScheduledTime = scheduledUser.ScheduledOn.ToString("hh:mm a");
                            MailDetailModel mailDetail = new MailDetailModel();
                            mailDetail.EmailId     = scheduleUserModel.User.Email;
                            mailDetail.Subject     = "Interview Cancelled";
                            mailDetail.Template    = TemplateType.InterviewCancelled;
                            mailDetail.MessageBody = scheduleUserModel;
                            GenericHelper.Send(mailDetail, _configuration, _hostingEnvironment);
                        }
                    }

                    if (addScheduledUsers.Any())
                    {
                        addingScheduledUsers = scheduleUserForCandidateModelList.Where(x => addScheduledUsers.Contains(x.UserId)).ToList();
                    }
                }
                else
                {
                    addingScheduledUsers = scheduleUserForCandidateModelList;
                }
                if (addingScheduledUsers.Any())
                {
                    foreach (var scheduledUser in addingScheduledUsers)
                    {
                        var scheduledUserForCandidate = new ScheduleUserForCandidate();
                        scheduledUserForCandidate.MapFromViewModel(scheduledUser, (ClaimsIdentity)_principal.Identity);
                        _candidateRepository.AddScheduledUsers(scheduledUserForCandidate);
                        CandidateViewModel candidateView = new CandidateViewModel();
                        candidateView.MapFromModel(_candidateRepository.GetByID(scheduledUser.CandidateId));
                        scheduledUser.Candidate = candidateView;
                        scheduledUser.InterviewScheduledDate = scheduledUser.ScheduledOn.ToString("yyyy-MM-dd");
                        scheduledUser.InterviewScheduledTime = scheduledUser.ScheduledOn.ToString("hh:mm tt");
                        MailDetailModel mailDetail = new MailDetailModel();
                        mailDetail.EmailId     = scheduledUser.User.Email;
                        mailDetail.Subject     = "Interview Scheduled";
                        mailDetail.Template    = TemplateType.ScheduleUserForInterview;
                        mailDetail.MessageBody = scheduledUser;
                        GenericHelper.Send(mailDetail, _configuration, _hostingEnvironment);
                    }
                }
                _candidateRepository.SaveChanges();
                result.Body = scheduleUserForCandidateModelList;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                result.Status  = Status.Error;
            }
            return(result);
        }