using LCU.Library.Internal.DispatcherModels.Enums; using System.Collections.Generic; namespace LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices; public class IngestionService : IIngestionService { private readonly IJobService _jobService; private readonly IWorkItemService _workItemService; private readonly IJobErrorRecipientService _jobErrorRecipientService; private readonly IJobTagService _jobTagService; public IngestionService(IJobService jobService, IWorkItemService workItemService, IJobErrorRecipientService jobErrorRecipientService, IJobTagService jobTagService) { _jobService = jobService; _workItemService = workItemService; _jobErrorRecipientService = jobErrorRecipientService; _jobTagService = jobTagService; } public async Task HandleIngestionRequest(Job request) { try { ArgumentNullException.ThrowIfNull(request); var result = await PerformIngestion(request); if (!result) { throw new ServiceException("Failure to PerformIngestion."); } } catch (Exception exception) { Log.Error(exception, $"Failed to perform execution."); throw new ServiceException($"Failed to perform ingestion."); } } public async Task PerformIngestion(Job request) { try { if (request.IsNewJob) { return await SaveNewRequest(request); } return await UpdateRequest(request); } catch (Exception ex) { Log.Information(ex, $"Failed to perform ingestion."); return false; } } public async Task SaveNewRequest(Job request) { request = await _jobService.SaveAsync(request); if (request == null) { return false; } var updatedWorkItems = await _workItemService.PopulateActionFieldsAndUpdateList(request.WorkItems); if (updatedWorkItems == null) { return false; } return true; } public async Task UpdateRequest(Job request) { var existingJob = await _jobService.GetByIdAsync(request.JobId); await UpdateRequestWorkItems(request, existingJob); UpdateRequestErrorRecipients(request, existingJob); await UpdateRequestJobTags(request, existingJob); request.JobStatus = (request.NextRunTime == null && request.CronSchedule == null) ? JobStatus.Inactive : existingJob.JobStatus; var updatedJob = await _jobService.UpdateAsync(request); if (updatedJob == null) { return false; } return true; } public async Task UpdateRequestWorkItems(Job request, Job existingJob) { var removedWorkItems = existingJob.WorkItems.Where(wi => !request.WorkItems.Any(newWI => newWI.WorkItemId == wi.WorkItemId)).ToList(); await _workItemService.DeleteList(removedWorkItems); var newWorkItems = request.WorkItems.Where(wi => wi.IsNewWorkItem).ToList(); _workItemService.SaveList(newWorkItems); var updatedWorkItems = await _workItemService.PopulateActionFieldsAndUpdateList(request.WorkItems); if (updatedWorkItems == null) { throw new Exception($"WorkItem Error while Populating Action Fields and updating."); } } public void UpdateRequestErrorRecipients(Job request, Job existingJob) { var removedJobErrorRecipients = existingJob.JobErrorRecipients.Where(recipient => !request.JobErrorRecipients.Any(newRecipient => newRecipient.Recipient == recipient.Recipient)).ToList(); _jobErrorRecipientService.DeleteList(removedJobErrorRecipients); var newJobErrorRecipients = new List(); foreach (var recipient in request.JobErrorRecipients) { var jobErrorRecipient = existingJob.JobErrorRecipients.FirstOrDefault(existing => existing.Recipient == recipient.Recipient); if (jobErrorRecipient == null) { newJobErrorRecipients.Add(recipient); } } _jobErrorRecipientService.SaveList(newJobErrorRecipients); } public async Task UpdateRequestJobTags(Job request, Job existingJob) { var removedJobTags = existingJob.JobTags.Where(tag => !request.JobTags.Any(jobTags => jobTags.TagId == tag.TagId)).ToList(); foreach (var tag in removedJobTags) { await _jobTagService.Delete(tag.JobTagId); } var newJobTags = new List(); foreach (var tag in request.JobTags) { var existingTag = existingJob.JobTags.FirstOrDefault(existing => existing.TagId == tag.TagId); if (existingTag == null) { newJobTags.Add(tag); var result = await _jobTagService.SaveAsync(tag); } } } }