public async Task <IActionResult> Post([FromBody] CreateJobViewModel request) { if (request == null) { ModelState.AddModelError("Save", "No data passed"); return(BadRequest(ModelState)); } Guid entityId = Guid.NewGuid(); if (request.Id == null || !request.Id.HasValue || request.Id.Equals(Guid.Empty)) { request.Id = entityId; } try { Job job = request.Map(request); //assign request to job entity Automation automation = automationRepo.GetOne(job.AutomationId ?? Guid.Empty); if (automation == null) //no automation was found { ModelState.AddModelError("Save", "No automation was found for the specified automation id"); return(NotFound(ModelState)); } AutomationVersion automationVersion = automationVersionRepo.Find(null, q => q.AutomationId == automation.Id).Items?.FirstOrDefault(); job.AutomationVersion = automationVersion.VersionNumber; job.AutomationVersionId = automationVersion.Id; foreach (var parameter in request.JobParameters ?? Enumerable.Empty <JobParameter>()) { parameter.JobId = entityId; parameter.CreatedBy = applicationUser?.UserName; parameter.CreatedOn = DateTime.UtcNow; parameter.Id = Guid.NewGuid(); jobParameterRepo.Add(parameter); } //send SignalR notification to all connected clients await _hub.Clients.All.SendAsync("botnewjobnotification", request.AgentId.ToString()); await _hub.Clients.All.SendAsync("sendjobnotification", "New Job added."); await _hub.Clients.All.SendAsync("broadcastnewjobs", Tuple.Create(request.Id, request.AgentId, request.AutomationId)); await webhookPublisher.PublishAsync("Jobs.NewJobCreated", job.Id.ToString()).ConfigureAwait(false); return(await base.PostEntity(job)); } catch (Exception ex) { return(ex.GetActionResult()); } }
public void AddAutomationVersion(AutomationViewModel automationViewModel) { AutomationVersion automationVersion = new AutomationVersion(); automationVersion.CreatedBy = httpContextAccessor.HttpContext.User.Identity.Name; automationVersion.CreatedOn = DateTime.UtcNow; automationVersion.AutomationId = (Guid)automationViewModel.Id; if (string.IsNullOrEmpty(automationViewModel.Status)) { automationVersion.Status = "Published"; } else { automationVersion.Status = automationViewModel.Status; } automationVersion.VersionNumber = automationViewModel.VersionNumber; if (automationVersion.Status.Equals("Published")) { automationVersion.PublishedBy = httpContextAccessor.HttpContext.User.Identity.Name; automationVersion.PublishedOnUTC = DateTime.UtcNow; } else { automationVersion.PublishedBy = null; automationVersion.PublishedOnUTC = null; } int automationVersionNumber = 0; automationVersion.VersionNumber = automationVersionNumber; List <Automation> automations = repo.Find(null, x => x.Name?.Trim().ToLower() == automationViewModel.Name?.Trim().ToLower())?.Items; if (automations != null) { foreach (Automation automation in automations) { var automationVersionEntity = automationVersionRepository.Find(null, q => q?.AutomationId == automation?.Id).Items?.FirstOrDefault(); if (automationVersionEntity != null && automationVersionNumber < automationVersionEntity.VersionNumber) { automationVersionNumber = automationVersionEntity.VersionNumber; } } } automationVersion.VersionNumber = automationVersionNumber + 1; automationVersionRepository.Add(automationVersion); }
public Job UpdateJob(string jobId, CreateJobViewModel request, ApplicationUser applicationUser) { Guid entityId = new Guid(jobId); var existingJob = _repo.GetOne(entityId); if (existingJob == null) { throw new EntityDoesNotExistException("Unable to find a job for the specified id"); } Automation automation = _automationRepo.GetOne(existingJob.AutomationId ?? Guid.Empty); if (automation == null) //no automation was found { throw new EntityDoesNotExistException("No automation was found for the specified automation id"); } AutomationVersion automationVersion = _automationVersionRepo.Find(null, q => q.AutomationId == automation.Id).Items?.FirstOrDefault(); existingJob.AutomationVersion = automationVersion.VersionNumber; existingJob.AutomationVersionId = automationVersion.Id; existingJob.AgentId = request.AgentId; existingJob.AgentGroupId = request.AgentGroupId; existingJob.StartTime = request.StartTime; existingJob.EndTime = request.EndTime; existingJob.ExecutionTimeInMinutes = (existingJob.EndTime.Value - existingJob.StartTime).Value.TotalMinutes; existingJob.DequeueTime = request.DequeueTime; existingJob.AutomationId = request.AutomationId; existingJob.JobStatus = request.JobStatus; existingJob.Message = request.Message; existingJob.IsSuccessful = request.IsSuccessful; UpdateJobParameters(request.JobParameters, existingJob.Id); if (request.EndTime != null) { UpdateAutomationAverages(existingJob.Id); } return(existingJob); }
public async Task <IActionResult> Put(string id, [FromBody] CreateJobViewModel request) { try { Guid entityId = new Guid(id); var existingJob = repository.GetOne(entityId); if (existingJob == null) { return(NotFound("Unable to find a Job for the specified ID")); } Automation automation = automationRepo.GetOne(existingJob.AutomationId ?? Guid.Empty); if (automation == null) //no automation was found { ModelState.AddModelError("Save", "No automation was found for the specified automation ID"); return(NotFound(ModelState)); } AutomationVersion automationVersion = automationVersionRepo.Find(null, q => q.AutomationId == automation.Id).Items?.FirstOrDefault(); existingJob.AutomationVersion = automationVersion.VersionNumber; existingJob.AutomationVersionId = automationVersion.Id; existingJob.AgentId = request.AgentId; existingJob.StartTime = request.StartTime; existingJob.EndTime = request.EndTime; existingJob.ExecutionTimeInMinutes = (existingJob.EndTime.Value - existingJob.StartTime).Value.TotalMinutes; existingJob.DequeueTime = request.DequeueTime; existingJob.AutomationId = request.AutomationId; existingJob.JobStatus = request.JobStatus; existingJob.Message = request.Message; existingJob.IsSuccessful = request.IsSuccessful; var response = await base.PutEntity(id, existingJob); jobManager.DeleteExistingParameters(entityId); var set = new HashSet <string>(); foreach (var parameter in request.JobParameters ?? Enumerable.Empty <JobParameter>()) { if (!set.Add(parameter.Name)) { ModelState.AddModelError("JobParameter", "JobParameter Name Already Exists"); return(BadRequest(ModelState)); } parameter.JobId = entityId; parameter.CreatedBy = applicationUser?.UserName; parameter.CreatedOn = DateTime.UtcNow; parameter.Id = Guid.NewGuid(); jobParameterRepo.Add(parameter); } if (request.EndTime != null) { jobManager.UpdateAutomationAverages(existingJob.Id); } //send SignalR notification to all connected clients await webhookPublisher.PublishAsync("Jobs.JobUpdated", existingJob.Id.ToString()).ConfigureAwait(false); await _hub.Clients.All.SendAsync("sendjobnotification", string.Format("Job id {0} updated.", existingJob.Id)); return(response); } catch (Exception ex) { ModelState.AddModelError("Job", ex.Message); return(BadRequest(ModelState)); } }
public async Task <IActionResult> Update(string id, [FromForm] AutomationViewModel request) { Guid entityId = new Guid(id); var existingAutomation = repository.GetOne(entityId); if (existingAutomation == null) { return(NotFound()); } if (request.File == null) { ModelState.AddModelError("Save", "No data passed"); return(BadRequest(ModelState)); } long size = request.File.Length; if (size <= 0) { ModelState.AddModelError("Automation Upload", $"File size of automation {request.File.FileName} cannot be 0"); return(BadRequest(ModelState)); } string binaryObjectId = existingAutomation.BinaryObjectId.ToString(); var binaryObject = binaryObjectRepo.GetOne(Guid.Parse(binaryObjectId)); string organizationId = binaryObject.OrganizationId.ToString(); if (!string.IsNullOrEmpty(organizationId)) { organizationId = manager.GetOrganizationId().ToString(); } try { BinaryObject newBinaryObject = new BinaryObject(); if (existingAutomation.BinaryObjectId != Guid.Empty && size > 0) { string apiComponent = "AutomationAPI"; //update file in OpenBots.Server.Web using relative directory newBinaryObject.Id = Guid.NewGuid(); newBinaryObject.Name = request.File.FileName; newBinaryObject.Folder = apiComponent; newBinaryObject.StoragePath = Path.Combine("BinaryObjects", organizationId, apiComponent, newBinaryObject.Id.ToString()); newBinaryObject.CreatedBy = applicationUser?.UserName; newBinaryObject.CreatedOn = DateTime.UtcNow; newBinaryObject.CorrelationEntityId = request.Id; binaryObjectRepo.Add(newBinaryObject); binaryObjectManager.Upload(request.File, organizationId, apiComponent, newBinaryObject.Id.ToString()); binaryObjectManager.SaveEntity(request.File, newBinaryObject.StoragePath, newBinaryObject, apiComponent, organizationId); binaryObjectRepo.Update(binaryObject); } //update automation (create new automation and automation version entities) Automation response = existingAutomation; AutomationVersion automationVersion = automationVersionRepo.Find(null, q => q.AutomationId == response.Id).Items?.FirstOrDefault(); if (existingAutomation.Name.Trim().ToLower() != request.Name.Trim().ToLower() || automationVersion.Status.Trim().ToLower() != request.Status?.Trim().ToLower()) { existingAutomation.OriginalPackageName = request.File.FileName; existingAutomation.AutomationEngine = request.AutomationEngine; automationVersion.Status = request.Status; } else { request.BinaryObjectId = newBinaryObject.Id; response = manager.UpdateAutomation(existingAutomation, request); } await webhookPublisher.PublishAsync("Files.NewFileCreated", newBinaryObject.Id.ToString(), newBinaryObject.Name).ConfigureAwait(false); await webhookPublisher.PublishAsync("Automations.AutomationUpdated", existingAutomation.Id.ToString(), existingAutomation.Name).ConfigureAwait(false); return(Ok(response)); } catch (Exception ex) { return(ex.GetActionResult()); } }