Exemplo n.º 1
0
        public async Task <string> Handle(InstantiateSubTaskCommand request, CancellationToken cancellationToken)
        {
            if (request.Claims == null || !request.Claims.Any())
            {
                _logger.LogError("User is not authenticated");
                throw new NotAuthenticatedException(Global.UserNotAuthenticated);
            }

            var humanTaskInstance = await _humanTaskInstanceQueryRepository.Get(request.HumanTaskInstanceId, cancellationToken);

            if (humanTaskInstance == null)
            {
                _logger.LogError($"HumanTask '{request.HumanTaskInstanceId}' doesn't exist");
                throw new UnknownHumanTaskInstanceException(string.Format(Global.UnknownHumanTaskInstance, request.HumanTaskInstanceId));
            }

            var subTask = humanTaskInstance.SubTasks.FirstOrDefault(_ => _.HumanTaskName == request.SubTaskName);

            if (subTask == null)
            {
                _logger.LogError($"'{request.SubTaskName}' is not a subtask of '{humanTaskInstance.HumanTaskDefName}'");
                throw new BadRequestException(string.Format(Global.NotSubTask, request.SubTaskName, humanTaskInstance.HumanTaskDefName));
            }

            var subTasks = await _humanTaskInstanceQueryRepository.GetSubTasks(humanTaskInstance.AggregateId, cancellationToken);

            if (subTasks.Any(_ => _.HumanTaskDefName == request.SubTaskName))
            {
                _logger.LogError($"The sub task '{request.SubTaskName}' is already created");
                throw new BadRequestException(string.Format(Global.SubTaskAlreadyCreated, request.SubTaskName));
            }

            var operationParameters = _parameterParser.ParseToPartParameters(subTask.ToParts, humanTaskInstance.InputParameters);
            var result = await _mediator.Send(new CreateHumanTaskInstanceCommand
            {
                Claims              = request.Claims,
                HumanTaskName       = request.SubTaskName,
                OperationParameters = operationParameters,
                IgnorePermissions   = true
            }, cancellationToken);

            var hi = await _humanTaskInstanceQueryRepository.Get(result, cancellationToken);

            hi.SetParent(humanTaskInstance.HumanTaskDefName, humanTaskInstance.AggregateId);
            await _humanTaskInstanceCommandRepository.Update(hi, cancellationToken);

            await _humanTaskInstanceCommandRepository.SaveChanges(cancellationToken);

            return(result);
        }
        protected override async Task Execute(CancellationToken token)
        {
            try
            {
                var currentDateTime      = DateTime.UtcNow;
                var humanTaskInstanceLst = await _humanTaskInstanceQueryRepository.GetPendingDeadLines(token, currentDateTime);

                foreach (var humanTaskInstance in humanTaskInstanceLst)
                {
                    var deadLines        = humanTaskInstance.DeadLines.Where(d => currentDateTime >= d.EndDateTime);
                    var executionContext = new BaseExpressionContext(humanTaskInstance.InputParameters);
                    var deadLineIds      = deadLines.Select(_ => _.Id).ToList();
                    foreach (var id in deadLineIds)
                    {
                        var deadLine = deadLines.First(_ => _.Id == id);
                        if (deadLine.Escalations != null && deadLine.Escalations.Any())
                        {
                            foreach (var escalation in deadLine.Escalations)
                            {
                                if (!string.IsNullOrWhiteSpace(escalation.Condition) && !ExpressionParser.IsValid(escalation.Condition, executionContext))
                                {
                                    continue;
                                }

                                if (!string.IsNullOrWhiteSpace(escalation.NotificationId))
                                {
                                    var parameters = _parameterParser.ParseToPartParameters(escalation.ToParts, humanTaskInstance.InputParameters);
                                    await _mediator.Send(new CreateNotificationInstanceCommand { NotificationId = escalation.NotificationId, Parameters = parameters }, token);
                                }
                            }
                        }

                        humanTaskInstance.RemoveDeadLine(deadLine.Name, deadLine.Usage);
                    }

                    await _humanTaskInstanceCommandRepository.Update(humanTaskInstance, token);
                }

                await _humanTaskInstanceCommandRepository.SaveChanges(token);
            }
            catch (Exception)
            {
            }
        }