Ejemplo n.º 1
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());
            }
        }
Ejemplo n.º 2
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());
            }
        }