public async Task <ActivateActivitiesDTO> Activate(Guid curPlanId, bool planBuilderActivate) { var result = new ActivateActivitiesDTO(); List <Task <ActivityDTO> > activitiesTask = new List <Task <ActivityDTO> >(); using (var uow = ObjectFactory.GetInstance <IUnitOfWork>()) { var plan = uow.PlanRepository.GetById <PlanDO>(curPlanId); if (plan.PlanState == PlanState.Deleted) { EventManager.PlanActivationFailed(plan, "Cannot activate deleted plan"); throw new ApplicationException("Cannot activate deleted plan"); } try { foreach (var curActionDO in plan.GetDescendants().OfType <ActivityDO>()) { activitiesTask.Add(_activity.Activate(curActionDO)); } await Task.WhenAll(activitiesTask); } catch (AggregateException ex) { foreach (Exception e in ex.InnerExceptions) { if (e is InvalidTokenRuntimeException) { ReportAuthError(uow, plan.Fr8Account, (InvalidTokenRuntimeException)e); } } EventManager.PlanActivationFailed(plan, "Unable to activate plan"); throw; } catch (InvalidTokenRuntimeException ex) { ReportAuthError(uow, plan.Fr8Account, (InvalidTokenRuntimeException)ex); EventManager.PlanActivationFailed(plan, "Unable to activate plan"); throw; } foreach (var resultActivate in activitiesTask.Select(x => x.Result)) { var errors = new ValidationErrorsDTO(ExtractValidationErrors(resultActivate)); if (errors.ValidationErrors.Count > 0) { result.ValidationErrors[resultActivate.Id] = errors; } } if (result.ValidationErrors.Count == 0 && plan.PlanState != PlanState.Executing) { plan.PlanState = IsMonitoringPlan(uow, plan) ? PlanState.Active : PlanState.Executing; plan.LastUpdated = DateTimeOffset.UtcNow; uow.SaveChanges(); } } return(result); }