private async Task <ImportResult> ValidateAndImportNotificationGroupAsync(PerformContext context, string requestId, List <Notification> notifications)
        {
            var patientName  = notifications.First().PatientDetails.FullName;
            var importResult = new ImportResult(patientName);

            _logger.LogInformation(context, requestId, $"{notifications.Count} notifications found to import for {patientName}");

            // Verify that no repeated NotificationIds have returned
            var ids = notifications.Select(n => n.LegacyId).ToList();

            if (ids.Distinct().Count() != ids.Count)
            {
                var errorMessage = $"Duplicate records found ({String.Join(',', ids)}) - aborting import for {patientName}";
                importResult.AddGroupError(errorMessage);
                _logger.LogImportFailure(context, requestId, errorMessage);
                return(importResult);
            }

            bool isAnyNotificationInvalid = false;

            foreach (var notification in notifications)
            {
                var linkedNotificationId = notification.LegacyId;
                _logger.LogInformation(context, requestId, $"Validating notification with Id={linkedNotificationId}");

                var validationErrors = await _importValidator.CleanAndValidateNotification(context,
                                                                                           requestId,
                                                                                           notification);

                if (!validationErrors.Any())
                {
                    _logger.LogInformation(context, requestId, "No validation errors found");
                    importResult.AddValidNotification(linkedNotificationId);
                }
                else
                {
                    isAnyNotificationInvalid = true;
                    importResult.AddValidationErrorsMessages(linkedNotificationId, validationErrors);
                    _logger.LogWarning(context, requestId, $"{validationErrors.Count} validation errors found for notification with Id={linkedNotificationId}:");
                    foreach (var validationError in validationErrors)
                    {
                        _logger.LogWarning(context, requestId, validationError.ErrorMessage);
                    }
                }
            }

            if (isAnyNotificationInvalid)
            {
                _logger.LogImportFailure(context, requestId, $"Terminating importing notifications for {patientName} due to validation errors");
                return(importResult);
            }

            _logger.LogSuccess(context, requestId, $"Importing {notifications.Count} valid notifications");
            try
            {
                var savedNotifications = await _notificationImportRepository.AddLinkedNotificationsAsync(notifications);

                await _migratedNotificationsMarker.MarkNotificationsAsImportedAsync(savedNotifications);

                importResult.NtbsIds = savedNotifications.ToDictionary(x => x.LegacyId, x => x.NotificationId);
                await ImportReferenceLabResultsAsync(context, requestId, savedNotifications, importResult);

                var newIdsString = string.Join(" ,", savedNotifications.Select(x => x.NotificationId));
                _logger.LogSuccess(context, requestId, $"Imported notifications have following Ids: {newIdsString}");

                _logger.LogInformation(context, requestId, $"Finished importing notification for {patientName}");
            }
            catch (MarkingNotificationsAsImportedFailedException e)
            {
                Log.Error(e, e.Message);
                _logger.LogWarning(context, requestId, message: e.Message);
                importResult.AddGroupError($"{e.Message}: {e.StackTrace}");
            }
            catch (Exception e)
            {
                Log.Error(e, e.Message);
                _logger.LogImportFailure(context, requestId, message: $"Failed to save notification for {patientName} or mark it as imported ", e);
                importResult.AddGroupError($"{e.Message}: {e.StackTrace}");
            }
            return(importResult);
        }
        private async Task <ImportResult> ValidateAndImportNotificationGroupAsync(PerformContext context, int runId,
                                                                                  List <Notification> notifications)
        {
            var importResult = new ImportResult(notifications.First().PatientDetails.FullName);

            _logger.LogInformation(context, runId,
                                   $"{notifications.Count} notifications found to import in notification group containing legacy ID {notifications.First().LegacyId}");

            // Verify that no repeated NotificationIds have returned
            var ids = notifications.Select(n => n.LegacyId).ToList();

            if (ids.Distinct().Count() != ids.Count)
            {
                importResult.AddGroupError("Aborting group import due to duplicate records found");
                await _logger.LogImportGroupFailure(context, runId, notifications, "due to duplicate records found");

                return(importResult);
            }

            var hasAnyRecordAlreadyBeenImported = false;

            foreach (var notification in notifications)
            {
                // Check that record hasn't already been imported
                var foundLtbr = notification.LTBRID is null
                    ? false
                    : await _notificationRepository.NotificationWithLegacyIdExistsAsync(notification.LTBRID);

                var foundEts = notification.ETSID is null
                    ? false
                    : await _notificationRepository.NotificationWithLegacyIdExistsAsync(notification.ETSID);

                if (foundEts || foundLtbr)
                {
                    hasAnyRecordAlreadyBeenImported = true;
                    var errorId      = foundLtbr ? $"LTBRId = {notification.LTBRID}" : $"ETSId = {notification.ETSID}";
                    var errorMessage = $"A notification has already been imported with {errorId}. " +
                                       "Please contact your system administrator to fix this issue.";
                    importResult.AddNotificationError(notification.LegacyId, errorMessage);
                }
            }

            if (hasAnyRecordAlreadyBeenImported)
            {
                await _logger.LogImportGroupFailure(context, runId, notifications, "due to notification having already been imported");

                return(importResult);
            }

            var isAnyNotificationInvalid = false;

            foreach (var notification in notifications)
            {
                var linkedNotificationId = notification.LegacyId;
                _logger.LogInformation(context, runId, $"Validating notification with Id={linkedNotificationId}");

                var validationErrors = await _importValidator.CleanAndValidateNotification(context,
                                                                                           runId,
                                                                                           notification);

                if (!validationErrors.Any())
                {
                    _logger.LogInformation(context, runId, "No validation errors found");
                    importResult.AddValidNotification(linkedNotificationId);
                }
                else
                {
                    isAnyNotificationInvalid = true;
                    importResult.AddValidationErrorsMessages(linkedNotificationId, validationErrors);
                    await _logger.LogNotificationWarning(context, runId, linkedNotificationId,
                                                         $"has {validationErrors.Count} validation errors");

                    foreach (var validationError in validationErrors)
                    {
                        await _logger.LogNotificationWarning(context, runId, linkedNotificationId, validationError.ErrorMessage);
                    }
                }
            }

            if (isAnyNotificationInvalid)
            {
                await _logger.LogImportGroupFailure(context, runId, notifications, "due to validation errors");

                return(importResult);
            }

            _logger.LogSuccess(context, runId, $"Importing {notifications.Count} valid notifications");

            var savedNotifications = await SaveLinkedNotifications(context, runId, importResult, notifications);

            if (savedNotifications != null &&
                await MarkNotificationsAsImported(context, runId, importResult, savedNotifications))
            {
                importResult.NtbsIds = savedNotifications.ToDictionary(x => x.LegacyId, x => x.NotificationId);
                await ImportLabResults(context, runId, importResult, savedNotifications);
                await MigrateCultureResistanceSummaries(context, runId, savedNotifications);
                await UpdateDrugResistanceProfiles(context, runId, savedNotifications);
                await UpdateClusterInformation(context, runId, savedNotifications);

                await _logger.LogGroupSuccess(context, runId, notifications);
            }

            return(importResult);
        }