public IEnumerable <MedicationEditModel> GetMedications(long customerId) { var medications = _medicationRepository.GetByCustomerId(customerId); if (medications.IsNullOrEmpty()) { return(null); } return(_medicationFactory.CreateViewModel(medications)); }
private void ParseMedicationUploaded(IEnumerable <DataRow> rows, List <MedicationUploadLog> failedRecordsList, MedicationUpload medicationUpload, IEnumerable <Unit> units) { var medicationUploadLogs = rows.Select(row => _medicationUploadHelper.GetUploadLog(row, medicationUpload.Id)).ToList(); var ndcProductCodesFromFile = medicationUploadLogs.Where(x => x.IsSuccessFull == true).Select(x => x.NdcProductCode).ToList(); var ndcList = _ndcRepository.GetByNdcCode(ndcProductCodesFromFile); if (medicationUploadLogs.IsNullOrEmpty()) { _logger.Info("No Record Found For Parsing in file"); return; } foreach (var medicationUploadLog in medicationUploadLogs) { try { if (!medicationUploadLog.IsSuccessFull) { failedRecordsList.Add(medicationUploadLog); continue; } var serviceDate = _medicationUploadHelper.CovertToDate(medicationUploadLog.ServiceDate).Value; if (serviceDate >= DateTime.Today.AddDays(1)) { medicationUploadLog.ErrorMessage = "Date of Service can not be future date."; failedRecordsList.Add(medicationUploadLog); continue; } var ndc = ndcList.FirstOrDefault(x => x.NdcCode.ToUpper() == medicationUploadLog.NdcProductCode.ToUpper()); if (ndc == null) { medicationUploadLog.ErrorMessage = "Invalid product Ndc"; medicationUploadLog.IsSuccessFull = false; failedRecordsList.Add(medicationUploadLog); continue; } var memberDob = _medicationUploadHelper.CovertToDate(medicationUploadLog.MemberDob).Value; var unit = units.FirstOrDefault(x => x.Name == ndc.ActiveIngredUnit); var alias = unit != null ? unit.Alias : UnknownUnitAlias; var customers = _customerRepository.GetCustomerForMedicationParsing(medicationUploadLog.CmsHicn, medicationUploadLog.MemberId, memberDob); if (customers.IsNullOrEmpty()) { medicationUploadLog.IsSuccessFull = false; medicationUploadLog.ErrorMessage = "Customer not found for with following criteria:"; if (!string.IsNullOrEmpty(medicationUploadLog.CmsHicn)) { medicationUploadLog.ErrorMessage = medicationUploadLog.ErrorMessage + " Hicn"; if (!string.IsNullOrEmpty(medicationUploadLog.MemberId)) { medicationUploadLog.ErrorMessage = medicationUploadLog.ErrorMessage + " or MemberId"; } } else if (!string.IsNullOrEmpty(medicationUploadLog.MemberId)) { medicationUploadLog.ErrorMessage = medicationUploadLog.ErrorMessage + " MemberId"; } failedRecordsList.Add(medicationUploadLog); continue; } if (customers.Count() > 1) { medicationUploadLog.IsSuccessFull = false; medicationUploadLog.ErrorMessage = "Multiple Customer found for with following criteria:"; if (!string.IsNullOrEmpty(medicationUploadLog.CmsHicn)) { medicationUploadLog.ErrorMessage = medicationUploadLog.ErrorMessage + " Hicn and Date Of Birth"; } else if (!string.IsNullOrEmpty(medicationUploadLog.MemberId)) { medicationUploadLog.ErrorMessage = medicationUploadLog.ErrorMessage + " MemberId and Date Of Birth"; } failedRecordsList.Add(medicationUploadLog); continue; } long customerId = customers.Single().CustomerId; var medicationsForCustomer = (IReadOnlyCollection <Medication>)_medicationRepository.GetByCustomerId(customerId); Medication doesMedicationAlreadyExists = null; if (!medicationsForCustomer.IsNullOrEmpty()) { doesMedicationAlreadyExists = medicationsForCustomer.FirstOrDefault(x => x.ServiceDate == serviceDate && x.NdcProductCode.ToUpper() == medicationUploadLog.NdcProductCode.ToUpper()); } if (doesMedicationAlreadyExists != null) { medicationUploadLog.IsSuccessFull = false; medicationUploadLog.ErrorMessage = "Medication for this record already present"; failedRecordsList.Add(medicationUploadLog); continue; } SaveDataForCustomer(customerId, medicationUploadLog, medicationUpload.UploadedBy, ndc, alias, memberDob, serviceDate); medicationUploadLog.IsSuccessFull = true; medicationUploadLog.ErrorMessage = null; } catch (Exception ex) { medicationUploadLog.IsSuccessFull = false; medicationUploadLog.ErrorMessage = "Some Error occurred while parsing Recored"; _logger.Error("Hicn " + medicationUploadLog.CmsHicn); _logger.Error("MemberId " + medicationUploadLog.MemberId); _logger.Error(string.Format("Error on Message: {0} \n Stack Trace: {1}", ex.Message, ex.StackTrace)); } } try { _medicationUploadLogRepository.BulkSaveMedicationUploadLog(medicationUploadLogs); // medication upload LOGs are being saved according to paging } catch (Exception ex) { _logger.Error(string.Format("Error while saving MedicationLog , exception message: {0} \n\t Stack Trace: {1}", ex.Message, ex.StackTrace)); } }