public CallQueueCustomerNotesViewModel GetCustomerNotes(long callId, long callQueueCustomerId) { var callHistoryModel = new List <CallHistoryViewModel>(); CallQueueCustomer callQueueCustomer = null; long customerId = 0; long prospectCustomerId = 0; if (callQueueCustomerId > 0) { callQueueCustomer = _callQueueCustomerRepository.GetById(callQueueCustomerId); customerId = callQueueCustomer.CustomerId ?? 0; prospectCustomerId = callQueueCustomer.ProspectCustomerId ?? 0; } else { var call = _callCenterCallRepository.GetById(callId); customerId = call != null ? call.CalledCustomerId : 0; } var calls = _callCenterCallRepository.GetCallsForCallQueueCustomer(callId, customerId, prospectCustomerId); if (calls != null && calls.Any()) { var callCenterNotes = _callCenterNotesRepository.GetByCallIds(calls.Select(x => x.CallId)); var agentNameIds = _organizationRoleUserRepository.GetNameIdPairofUsers(calls.Select(x => x.CreatedByOrgRoleUserId).ToArray()); var callHistoryList = new List <CallHistoryViewModel>(); foreach (var customerCall in calls) { var agentNameId = agentNameIds.First(c => c.FirstValue == customerCall.CreatedByOrgRoleUserId); var dispositionValue = "N/A"; if (!string.IsNullOrEmpty(customerCall.Disposition)) { ProspectCustomerTag disposition; Enum.TryParse(customerCall.Disposition, out disposition); if (Enum.IsDefined(typeof(ProspectCustomerTag), disposition)) { dispositionValue = disposition.GetDescription(); } } var callHistory = new CallHistoryViewModel() { CallId = customerCall.CallId, DateCreated = customerCall.CallDateTime, CallOutcome = ((CallStatus)customerCall.Status).GetDescription(), RequestedCallBack = callQueueCustomer != null ? callQueueCustomer.CallDate : (DateTime?)null, CreatedBy = agentNameId.SecondValue, Disposition = dispositionValue, NotInterestedReason = customerCall.NotInterestedReasonId.HasValue ? ((NotInterestedReason)customerCall.NotInterestedReasonId).GetDescription() : "N/A" }; if (callCenterNotes != null && callCenterNotes.Any()) { var customerNotes = callCenterNotes.Where(ccn => ccn.CallId == customerCall.CallId).Select(ccn => ccn).ToArray(); callHistory.Notes = customerNotes.Select(cn => new NotesViewModel() { Note = cn.Notes, EnteredOn = cn.DateCreated }).OrderByDescending(x => x.EnteredOn); } callHistoryList.Add(callHistory); } callHistoryModel.AddRange(callHistoryList.OrderByDescending(x => x.DateCreated).ToList()); } var mailHistoryList = new List <DirectMailViewModel>(); if (customerId > 0) { var directMails = _directMailRepository.GetByCustomerId(customerId); if (directMails != null && directMails.Any()) { var agentNameIds = _organizationRoleUserRepository.GetNameIdPairofUsers(directMails.Select(x => x.Mailedby).ToArray()); var directMailTypes = _directMailTypeRepository.GetAll(); foreach (var directMail in directMails) { var agentNameId = agentNameIds.First(c => c.FirstValue == directMail.Mailedby); var directMailSentToCustomer = "N/A"; if (directMail.DirectMailTypeId.HasValue) { directMailSentToCustomer = directMailTypes.Single(x => x.Id == directMail.DirectMailTypeId.Value).Name; } mailHistoryList.Add(new DirectMailViewModel() { DateCreated = directMail.MailDate, CreatedBy = agentNameId.SecondValue, DirectMailType = directMailSentToCustomer, Notes = directMail.IsInvalidAddress ? directMail.Notes : "N/A" }); } } } var customerCallNotesViewModel = new List <CustomerCallNotesViewModel>(); if (customerId > 0) { var customerCallNotes = _customerCallNotesRepository.GetCustomerNotes(customerId).ToList(); var orders = _orderRepository.GetAllOrdersForCustomer(customerId); if (orders != null && orders.Count() > 0) { var refundRequests = _refundRequestRepository.GeRefundRequestByOrderIds(orders.Select(oec => oec.Id).ToArray(), RefundRequestType.CustomerCancellation); if (refundRequests != null && refundRequests.Count() > 0) { var refundRequestNotes = new List <CustomerCallNotes>(); foreach (var refundRequest in refundRequests) { refundRequestNotes.Add(new CustomerCallNotes { CustomerId = customerId, Notes = refundRequest.Reason, NotesType = CustomerRegistrationNotesType.CancellationNote, DataRecorderMetaData = new DataRecorderMetaData(refundRequest.RequestedByOrgRoleUserId, refundRequest.RequestedOn, null) }); } customerCallNotes = customerCallNotes.Concat(refundRequestNotes).ToList(); } } if (!customerCallNotes.IsNullOrEmpty()) { customerCallNotes = customerCallNotes.OrderByDescending(x => x.DataRecorderMetaData.DateCreated).ToList(); var orgRoleUserIds = customerCallNotes.Select(x => x.DataRecorderMetaData.DataRecorderCreator.Id).ToArray(); orgRoleUserIds = orgRoleUserIds.Distinct().ToArray(); var agentNameIds = _organizationRoleUserRepository.GetNameIdPairofUsers(orgRoleUserIds); customerCallNotesViewModel.AddRange(from notes in customerCallNotes let createdBy = agentNameIds.First(x => x.FirstValue == notes.DataRecorderMetaData.DataRecorderCreator.Id).SecondValue select GetCustomerCallNotesViewModel(notes, createdBy)); } } var notesViewModel = new CallQueueCustomerNotesViewModel { CallHistory = callHistoryModel, CustomerCallNotes = customerCallNotesViewModel, DirectMail = mailHistoryList }; return(notesViewModel); }