Example #1
0
        public async Task <IActionResult> Post([FromBody] AutomationViewModel request)
        {
            try
            {
                Guid versionId  = Guid.NewGuid();
                var  automation = new Automation()
                {
                    Name             = request.Name,
                    AutomationEngine = request.AutomationEngine,
                    Id = request.Id
                };

                var response = await base.PostEntity(automation);

                manager.AddAutomationVersion(request);

                await webhookPublisher.PublishAsync("Automations.NewAutomationCreated", automation.Id.ToString(), automation.Name).ConfigureAwait(false);

                return(response);
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }
Example #2
0
        public Automation UpdateAutomation(string id, AutomationViewModel request)
        {
            Guid entityId           = new Guid(id);
            var  existingAutomation = _repo.GetOne(entityId);

            if (existingAutomation == null)
            {
                throw new EntityDoesNotExistException($"Automation with id {id} could not be found");
            }

            existingAutomation.Name             = request.Name;
            existingAutomation.AutomationEngine = GetAutomationEngine(request.AutomationEngine);

            var automationVersion = _automationVersionRepository.Find(null, q => q.AutomationId == existingAutomation.Id).Items?.FirstOrDefault();

            if (!string.IsNullOrEmpty(automationVersion.Status))
            {
                //check if previous value was not published before setting published properties
                automationVersion.Status = request.Status;
                if (automationVersion.Status == "Published")
                {
                    automationVersion.PublishedBy    = _httpContextAccessor.HttpContext.User.Identity.Name;
                    automationVersion.PublishedOnUTC = DateTime.UtcNow;
                }
                _automationVersionRepository.Update(automationVersion);
            }
            return(existingAutomation);
        }
        public Automation UpdateAutomationFile(string id, AutomationViewModel request)
        {
            Guid entityId           = new Guid(id);
            var  existingAutomation = _repo.GetOne(entityId);

            if (existingAutomation == null)
            {
                throw new EntityDoesNotExistException($"Automation with id {id} could not be found");
            }

            string fileId = existingAutomation.FileId.ToString();

            if (string.IsNullOrEmpty(request.DriveId))
            {
                var fileToUpdate = _storageFileRepository.GetOne(existingAutomation.FileId.Value);
                request.DriveId = fileToUpdate.StorageDriveId.ToString();
            }

            var file = _fileManager.GetFileFolder(fileId, request.DriveId, "Files");

            if (file == null)
            {
                throw new EntityDoesNotExistException($"Automation file with id {fileId} could not be found or does not exist");
            }

            var response = AddAutomation(request);

            return(response);
        }
        public void CheckStoragePathExists(FileFolderViewModel view, AutomationViewModel request, string driveName)
        {
            //check if storage path exists; if it doesn't exist, create folder
            var folder = _fileManager.GetFileFolderByStoragePath(view.StoragePath, driveName);

            CreateFolderIfEmpty(folder, request, "Automations", view.StoragePath);
            folder             = _fileManager.GetFileFolderByStoragePath(view.FullStoragePath, driveName);
            folder.StoragePath = view.StoragePath;
            CreateFolderIfEmpty(folder, request, request.Id.ToString(), view.StoragePath);
        }
 private void CreateFolderIfEmpty(FileFolderViewModel folder, AutomationViewModel request, string folderName, string storagePath)
 {
     if (string.IsNullOrEmpty(folder.Name))
     {
         folder.Name        = folderName;
         folder.IsFile      = false;
         folder.Size        = request.File.Length;
         folder.StoragePath = storagePath;
         _fileManager.AddFileFolder(folder, request.DriveId);
     }
 }
        public Automation AddAutomation(AutomationViewModel request)
        {
            var drive = new StorageDrive();

            if (string.IsNullOrEmpty(request.DriveId))
            {
                drive = _storageDriveRepository.Find(null, q => q.IsDefault == true).Items?.FirstOrDefault();
                if (drive == null)
                {
                    throw new EntityDoesNotExistException("Default drive does not exist or could not be found");
                }
                else
                {
                    request.DriveId = drive.Id.ToString();
                }
            }
            else
            {
                drive = _storageDriveRepository.GetOne(Guid.Parse(request.DriveId));
            }

            IFormFile[] fileArray = { request.File };
            string      shortPath = Path.Combine(drive.Name, "Automations");
            string      path      = Path.Combine(shortPath, request.Id.ToString());

            var fileView = new FileFolderViewModel()
            {
                Files           = fileArray,
                StoragePath     = shortPath,
                FullStoragePath = path,
                ContentType     = fileArray[0].ContentType,
                IsFile          = true
            };

            CheckStoragePathExists(fileView, request, drive.Name);
            fileView.StoragePath = fileView.FullStoragePath;
            fileView             = _fileManager.AddFileFolder(fileView, request.DriveId)[0];

            var automationEngine = GetAutomationEngine(request.AutomationEngine);

            var automation = new Automation()
            {
                Name             = request.Name,
                AutomationEngine = automationEngine,
                Id     = request.Id,
                FileId = fileView.Id,
                OriginalPackageName = request.File.FileName
            };

            AddAutomationVersion(request);

            return(automation);
        }
Example #7
0
        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);
        }
Example #8
0
        public async Task <IActionResult> Put(string id, [FromBody] AutomationViewModel request)
        {
            try
            {
                var existingAutomation = _manager.UpdateAutomation(id, request);
                await _webhookPublisher.PublishAsync("Automations.AutomationUpdated", existingAutomation.Id.ToString(), existingAutomation.Name).ConfigureAwait(false);

                return(await base.PutEntity(id, existingAutomation));
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }
Example #9
0
        public AutomationViewModel GetAutomationView(AutomationViewModel automationView, string id)
        {
            var automationVersion = automationVersionRepository.Find(null, q => q.AutomationId == Guid.Parse(id))?.Items?.FirstOrDefault();

            if (automationVersion != null)
            {
                automationView.VersionId      = (Guid)automationVersion.Id;
                automationView.VersionNumber  = automationVersion.VersionNumber;
                automationView.Status         = automationVersion.Status;
                automationView.PublishedBy    = automationVersion.PublishedBy;
                automationView.PublishedOnUTC = automationVersion.PublishedOnUTC;
            }

            return(automationView);
        }
Example #10
0
        public FileFolderViewModel CheckStoragePathExists(FileFolderViewModel view, AutomationViewModel request)
        {
            //check if storage path exists; if it doesn't exist, create folder
            var folder = _fileManager.GetFileFolderByStoragePath(view.FullStoragePath, request.DriveName);

            if (folder.Name == null)
            {
                folder.Name        = request.Id.ToString();
                folder.StoragePath = Path.Combine(request.DriveName, "Automations");
                folder.IsFile      = false;
                folder.Size        = request.File.Length;
                folder             = _fileManager.AddFileFolder(folder, request.DriveName)[0];
            }
            return(folder);
        }
Example #11
0
        public async Task <IActionResult> Update(string id, [FromForm] AutomationViewModel request)
        {
            try
            {
                var automation = _manager.UpdateAutomationFile(id, request);
                var response   = await base.PostEntity(automation);

                await _webhookPublisher.PublishAsync("Automations.NewAutomationCreated", automation.Id.ToString(), automation.Name).ConfigureAwait(false);

                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }
Example #12
0
        public Automation UpdateAutomation(Automation existingAutomation, AutomationViewModel request)
        {
            Automation automation = new Automation()
            {
                Id                  = request.Id,
                Name                = request.Name,
                CreatedBy           = httpContextAccessor.HttpContext.User.Identity.Name,
                CreatedOn           = DateTime.UtcNow,
                BinaryObjectId      = (Guid)request.BinaryObjectId,
                OriginalPackageName = request.File.FileName,
                AutomationEngine    = request.AutomationEngine
            };

            repo.Add(automation);
            AddAutomationVersion(request);

            return(automation);
        }
Example #13
0
        public Automation AddAutomation(AutomationViewModel request)
        {
            if (request.DriveName != "Files" && !string.IsNullOrEmpty(request.DriveName))
            {
                throw new EntityOperationException("Component files can only be saved in the Files drive");
            }
            else if (string.IsNullOrEmpty(request.DriveName))
            {
                request.DriveName = "Files";
            }

            IFormFile[] fileArray = { request.File };
            string      path      = Path.Combine(request.DriveName, "Automations", request.Id.ToString());

            var fileView = new FileFolderViewModel()
            {
                Files           = fileArray,
                StoragePath     = path,
                FullStoragePath = path,
                ContentType     = fileArray[0].ContentType,
                IsFile          = true
            };

            CheckStoragePathExists(fileView, request);
            fileView = _fileManager.AddFileFolder(fileView, request.DriveName)[0];

            var automationEngine = GetAutomationEngine(request.AutomationEngine);

            var automation = new Automation()
            {
                Name             = request.Name,
                AutomationEngine = automationEngine,
                Id     = request.Id,
                FileId = fileView.Id,
                OriginalPackageName = request.File.FileName
            };

            AddAutomationVersion(request);

            return(automation);
        }
Example #14
0
        public async Task <IActionResult> View(string id)
        {
            try
            {
                IActionResult actionResult = await base.GetEntity <AutomationViewModel>(id);

                OkObjectResult okResult = actionResult as OkObjectResult;

                if (okResult != null)
                {
                    AutomationViewModel view = okResult.Value as AutomationViewModel;
                    view = manager.GetAutomationView(view, id);
                }

                return(actionResult);
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }
Example #15
0
        public Automation UpdateAutomationFile(string id, AutomationViewModel request)
        {
            Guid entityId           = new Guid(id);
            var  existingAutomation = _repo.GetOne(entityId);

            if (existingAutomation == null)
            {
                throw new EntityDoesNotExistException($"Automation with id {id} could not be found");
            }

            string fileId = existingAutomation.FileId.ToString();
            var    file   = _fileManager.GetFileFolder(fileId, request.DriveName);

            if (file == null)
            {
                throw new EntityDoesNotExistException($"Automation file with id {fileId} could not be found");
            }

            var response = AddAutomation(request);

            return(response);
        }
Example #16
0
        public async Task <IActionResult> Put(string id, [FromBody] AutomationViewModel value)
        {
            try
            {
                Guid entityId = new Guid(id);

                var existingAutomation = repository.GetOne(entityId);
                if (existingAutomation == null)
                {
                    return(NotFound());
                }

                existingAutomation.Name             = value.Name;
                existingAutomation.AutomationEngine = value.AutomationEngine;

                var automationVersion = automationVersionRepo.Find(null, q => q.AutomationId == existingAutomation.Id).Items?.FirstOrDefault();
                if (!string.IsNullOrEmpty(automationVersion.Status))
                {
                    //determine a way to check if previous value was not published before setting published properties
                    automationVersion.Status = value.Status;
                    if (automationVersion.Status == "Published")
                    {
                        automationVersion.PublishedBy    = applicationUser?.Email;
                        automationVersion.PublishedOnUTC = DateTime.UtcNow;
                    }
                    automationVersionRepo.Update(automationVersion);
                }
                await webhookPublisher.PublishAsync("Automations.AutomationUpdated", existingAutomation.Id.ToString(), existingAutomation.Name).ConfigureAwait(false);

                return(await base.PutEntity(id, existingAutomation));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Automation", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Example #17
0
        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());
            }
        }