Esempio n. 1
0
        public void Can_Get_Populated_Instance(int taskCount, int lastGroupId)
        {
            IContent node = Scaffold.Node(ApplicationContext.Current.Services.ContentService);

            Guid guid = Guid.NewGuid();

            WorkflowInstancePoco instance = Scaffold.Instance(guid, (int)WorkflowType.Publish, node.Id);

            _service.InsertInstance(instance);
            for (var i = 1; i <= taskCount; i += 1)
            {
                _tasksService.InsertTask(Scaffold.Task(guid, DateTime.Now,
                                                       i < taskCount ? i : lastGroupId,
                                                       i,
                                                       i < taskCount ? 1 : 3));
            }

            // this has groups, tasks, everything. Or it should.
            WorkflowInstancePoco populatedInstance = _service.GetPopulatedInstance(guid);

            Assert.Equal(taskCount, populatedInstance.TaskInstances.Count);
            Assert.Equal(0, populatedInstance.TotalSteps);                                        // this shouldn't be set yet
            Assert.Equal(lastGroupId, populatedInstance.TaskInstances.First().UserGroup.GroupId); // tasks are descending by id
            Assert.Equal(WorkflowStatus.PendingApproval, populatedInstance.WorkflowStatus);
        }
Esempio n. 2
0
        public IHttpActionResult ApproveWorkflowTask(TaskData model)
        {
            WorkflowInstancePoco instance = _instancesService.GetPopulatedInstance(model.InstanceGuid);
            WorkflowProcess      process  = instance.GetProcess();

            try
            {
                IUser currentUser = _utility.GetCurrentUser();

                instance = process.ActionWorkflow(
                    instance,
                    WorkflowAction.Approve,
                    currentUser.Id,
                    model.Comment
                    );

                string msg    = string.Empty;
                string logMsg = string.Empty;

                string typeDescription     = instance.WorkflowType.Description(instance.ScheduledDate);
                string typeDescriptionPast = instance.WorkflowType.DescriptionPastTense(instance.ScheduledDate);

                switch (instance.WorkflowStatus)
                {
                case WorkflowStatus.PendingApproval:
                    msg    = $"Approval completed successfully. Page will be {typeDescriptionPast.ToLower()} following workflow completion.";
                    logMsg = $"Workflow {typeDescription} task on {instance.Node.Name} [{instance.NodeId}] approved by {currentUser.Name}";
                    break;

                case WorkflowStatus.Approved:
                    msg    = "Workflow approved successfully.";
                    logMsg = $"Workflow approved by {currentUser.Name} on {instance.Node.Name} [{instance.NodeId}]";

                    if (instance.ScheduledDate.HasValue)
                    {
                        string scheduled = $" Page scheduled for {typeDescription} at {instance.ScheduledDate.Value.ToString("dd MMM yyyy", CultureInfo.CurrentCulture)}";
                        msg    += scheduled;
                        logMsg += scheduled;
                    }
                    else
                    {
                        msg += $" Page has been {typeDescriptionPast.ToLower()}";
                    }
                    break;
                }

                Log.Info(logMsg);

                _hubContext.Clients.All.TaskApproved(
                    _tasksService.ConvertToWorkflowTaskList(instance.TaskInstances.ToList(), instance: instance));

                if (model.Offline)
                {
                    UmbracoContext.Security.ClearCurrentLogin();
                }

                return(Json(new
                {
                    message = msg,
                    status = 200,
                    notifications = NotificationHelpers.MapEventMessagesToNotifications(process.EventMessages)
                }, ViewHelpers.CamelCase));
            }
            catch (UmbracoOperationFailedException e)
            {
                string msg = $"A Publishing failure occurred processing the approval on {instance.Node.Name} [{instance.Node.Id}]";
                Log.Error(msg, e);

                return(Json(new
                {
                    message = msg,
                    status = 200,
                    isUmbracoOperationError = true,
                    notifications = NotificationHelpers.MapEventMessagesToNotifications(process.EventMessages)
                }
                            , ViewHelpers.CamelCase));
            }
            catch (Exception ex)
            {
                string msg = $"An error occurred processing the approval on {instance.Node.Name} [{instance.Node.Id}]";
                Log.Error(msg, ex);

                return(Content(HttpStatusCode.InternalServerError, ViewHelpers.ApiException(ex, msg)));
            }
        }