public static async Task <ActivityResponse> CreateLeagueCommandLogParametersActivity(
            [ActivityTrigger] DurableActivityContext context,
            ILogger log)
        {
            ActivityResponse ret = new ActivityResponse()
            {
                FunctionName = "CreateLeagueCommandLogParametersActivity"
            };

            try
            {
                CommandRequest <Create_New_League_Definition> cmdRequest = context.GetInput <CommandRequest <Create_New_League_Definition> >();

                if (null != cmdRequest)
                {
                    if (null != log)
                    {
                        // Unable to get the request details from the orchestration
                        log.LogInformation($"CreateLeagueCommandLogParametersActivity : Logging parameters for command {cmdRequest.CommandUniqueIdentifier} ");
                    }

                    EventStream commandEvents = EventStream.Create(@"Command",
                                                                   cmdRequest.CommandName,
                                                                   cmdRequest.CommandUniqueIdentifier.ToString());

                    if (null != commandEvents)
                    {
                        // Set the event context to indicate who wrote them
                        commandEvents.SetContext(new WriteContext(ret.FunctionName, context.InstanceId));

                        Create_New_League_Definition parameters = cmdRequest.GetParameters();

                        if (null != parameters)
                        {
                            // Set the parameters
                            await commandEvents.AppendEvent(new TheLongRun.Common.Events.Command.ParameterValueSet(nameof(parameters.LeagueName),
                                                                                                                   parameters.LeagueName));

                            await commandEvents.AppendEvent(new TheLongRun.Common.Events.Command.ParameterValueSet(nameof(parameters.Email_Address),
                                                                                                                   parameters.Email_Address));

                            await commandEvents.AppendEvent(new TheLongRun.Common.Events.Command.ParameterValueSet(nameof(parameters.Date_Incorporated),
                                                                                                                   parameters.Date_Incorporated));

                            await commandEvents.AppendEvent(new TheLongRun.Common.Events.Command.ParameterValueSet(nameof(parameters.Twitter_Handle),
                                                                                                                   parameters.Twitter_Handle));

                            ret.Message = $"All parameters to set for {cmdRequest.CommandName} : {cmdRequest.CommandUniqueIdentifier}";
                        }
                        else
                        {
                            ret.Message = $"No parameters to set for {cmdRequest.CommandName} : {cmdRequest.CommandUniqueIdentifier}";
                        }
                    }
                    else
                    {
                        ret.Message    = $"Unable to get the event stream for {cmdRequest.CommandName} : {cmdRequest.CommandUniqueIdentifier}";
                        ret.FatalError = true;
                    }
                }
                else
                {
                    ret.Message    = $"Unable to get the command request details from {context.ToString()} ";
                    ret.FatalError = true;
                }
            }
            catch (Exception ex)
            {
                if (null != log)
                {
                    // Unable to get the request details from the orchestration
                    log.LogError($"CreateLeagueCommandLogParametersActivity : error {ex.Message} ");
                }
                ret.Message    = ex.Message;
                ret.FatalError = true;
            }
            return(ret);
        }
        public static async Task OnCreateLeagueCommandHandlerOrchestrator
            ([OrchestrationTrigger] DurableOrchestrationContext context,
            Microsoft.Extensions.Logging.ILogger log)
        {
            CommandRequest <Create_New_League_Definition> cmdRequest = context.GetInput <CommandRequest <Create_New_League_Definition> >();

            if (null != cmdRequest)
            {
                ActivityResponse resp = await context.CallActivityWithRetryAsync <ActivityResponse>("CreateLeagueCommandLogParametersActivity",
                                                                                                    DomainSettings.CommandRetryOptions(),
                                                                                                    cmdRequest);

                Create_New_League_Definition parameters = cmdRequest.GetParameters();
                IEnumerable <CommandNotificationImpactedEntity> impactedEntities = null;
                if (null != parameters)
                {
                    Tuple <string, string>[] entitiesImpacted = new Tuple <string, string>[] { new Tuple <string, string>(@"League", parameters.LeagueName) };
                    impactedEntities = CommandNotificationImpactedEntity.CreateImpactedEntityList(entitiesImpacted);
                }

                #region Logging
                if (null != log)
                {
                    if (null != resp)
                    {
                        log.LogInformation($"{resp.FunctionName} complete: {resp.Message } ");
                    }
                }
                #endregion
                if (null != resp)
                {
                    context.SetCustomStatus(resp);
                }

                if (!resp.FatalError)
                {
                    // validate the command
                    bool valid = await context.CallActivityWithRetryAsync <bool>("CreateLeagueCommandValidationActivity",
                                                                                 DomainSettings.CommandRetryOptions(),
                                                                                 cmdRequest);

                    if (!valid)
                    {
                        resp.Message    = $"Validation failed for command {cmdRequest.CommandName} id: {cmdRequest.CommandUniqueIdentifier }";
                        resp.FatalError = true;
                    }
                }

                if (!resp.FatalError)
                {
                    CommandStepResponse stepResponse = new CommandStepResponse()
                    {
                        CommandName             = cmdRequest.CommandName,
                        CommandUniqueIdentifier = cmdRequest.CommandUniqueIdentifier,
                        StepName         = resp.FunctionName,
                        Message          = resp.Message,
                        ImpactedEntities = impactedEntities
                    };
                    resp = await context.CallActivityAsync <ActivityResponse>("CommandStepCompleteActivity", stepResponse);
                }

                if (!resp.FatalError)
                {
                    // execute the command
                    resp = await context.CallActivityWithRetryAsync <ActivityResponse>("CreateLeagueCommandHandlerAction",
                                                                                       DomainSettings.CommandRetryOptions(),
                                                                                       cmdRequest);

                    #region Logging
                    if (null != log)
                    {
                        if (null != resp)
                        {
                            log.LogInformation($"{resp.FunctionName} complete: {resp.Message } ");
                        }
                    }
                    #endregion
                    if (null != resp)
                    {
                        context.SetCustomStatus(resp);
                    }
                }

                if (!resp.FatalError)
                {
                    // 3) Mark the step as complete
                    CommandStepResponse stepResponse = new CommandStepResponse()
                    {
                        CommandName             = cmdRequest.CommandName,
                        CommandUniqueIdentifier = cmdRequest.CommandUniqueIdentifier,
                        StepName         = resp.FunctionName,
                        Message          = resp.Message,
                        ImpactedEntities = impactedEntities
                    };
                    resp = await context.CallActivityAsync <ActivityResponse>("CommandStepCompleteActivity", stepResponse);
                }

                if (!resp.FatalError)
                {
                    resp = await context.CallActivityAsync <ActivityResponse>("CommandCompleteActivity", cmdRequest);
                }

                #region Logging
                if (null != log)
                {
                    if (null != resp)
                    {
                        log.LogInformation($"{resp.FunctionName} complete: {resp.Message } ");
                    }
                }
                #endregion
                if (null != resp)
                {
                    context.SetCustomStatus(resp);
                }

                // Fire the orchestration to do the actual work of sending notifications
                Command_Get_Notifications_Request payload = new Command_Get_Notifications_Request()
                {
                    CommandName             = cmdRequest.CommandName,
                    CommandUniqueIdentifier = cmdRequest.CommandUniqueIdentifier.ToString()
                };

                // call the orchestrator...
                resp = await context.CallSubOrchestratorAsync <ActivityResponse>("CommandNotificationOrchestrator", payload);

                #region Logging
                if (null != log)
                {
                    if (null != resp)
                    {
                        log.LogInformation($"{resp.FunctionName} complete: {resp.Message } ");
                    }
                }
                #endregion
                if (null != resp)
                {
                    context.SetCustomStatus(resp);
                }
            }
            else
            {
                #region Logging
                if (null != log)
                {
                    // Unable to get the request details from the orchestration
                    log.LogError("OnCreateLeagueCommandHandlerOrchestrator : Unable to get the command request from the context");

                    string contextAsString = context.GetInput <string>();
                    if (!string.IsNullOrWhiteSpace(contextAsString))
                    {
                        log.LogError($"Context was {contextAsString} ");
                    }
                    else
                    {
                        log.LogError($"Context was blank ");
                    }
                }
                #endregion
                return;
            }
        }
        public static async Task SetLeagueEmailAddressCommandHandlerOrchestrator(
            [OrchestrationTrigger] DurableOrchestrationContext context,
            ILogger log)
        {
            CommandRequest <Set_Email_Address_Definition> cmdRequest = context.GetInput <CommandRequest <Set_Email_Address_Definition> >();


            if (null != cmdRequest)
            {
                ActivityResponse resp = await context.CallActivityWithRetryAsync <ActivityResponse>("SetLeagueEmailAddressCommandLogParametersActivity",
                                                                                                    DomainSettings.CommandRetryOptions(),
                                                                                                    cmdRequest);

                #region Logging
                if (null != log)
                {
                    if (null != resp)
                    {
                        log.LogInformation($"{resp.FunctionName} complete: {resp.Message } ");
                    }
                }
                #endregion
                if (null != resp)
                {
                    context.SetCustomStatus(resp);
                }

                // Get the impacted entities from the command request - in this command they are passed as parameters
                IEnumerable <CommandNotificationImpactedEntity> impactedEntities = null;
                Set_Email_Address_Definition parameters = cmdRequest.GetParameters();
                if (null != parameters)
                {
                    Tuple <string, string>[] entitiesImpacted = new Tuple <string, string>[] { new Tuple <string, string>(@"League", parameters.LeagueName) };
                    impactedEntities = CommandNotificationImpactedEntity.CreateImpactedEntityList(entitiesImpacted);
                }

                if (!resp.FatalError)
                {
                    // 1) Validate the command
                    bool valid = await context.CallActivityWithRetryAsync <bool>("SetLeagueEmailAddressCommandValidationActivity",
                                                                                 DomainSettings.CommandRetryOptions(),
                                                                                 cmdRequest);

                    if (!resp.FatalError)
                    {
                        CommandStepResponse stepResponse = new CommandStepResponse()
                        {
                            CommandName             = cmdRequest.CommandName,
                            CommandUniqueIdentifier = cmdRequest.CommandUniqueIdentifier,
                            StepName         = resp.FunctionName,
                            Message          = resp.Message,
                            ImpactedEntities = impactedEntities
                        };
                        resp = await context.CallActivityAsync <ActivityResponse>("CommandStepCompleteActivity", stepResponse);
                    }

                    if (valid)
                    {
                        // 2) Perform the operation of the command
                        resp = await context.CallActivityWithRetryAsync <ActivityResponse>("SetLeagueEmailAddressCommandHandlerActivity",
                                                                                           DomainSettings.CommandRetryOptions(),
                                                                                           cmdRequest);

                        #region Logging
                        if (null != log)
                        {
                            if (null != resp)
                            {
                                log.LogInformation($"{resp.FunctionName} complete: {resp.Message } ");
                            }
                        }
                        #endregion
                        if (null != resp)
                        {
                            context.SetCustomStatus(resp);
                        }

                        //  Mark the step as complete
                        if (!resp.FatalError)
                        {
                            CommandStepResponse stepResponse = new CommandStepResponse()
                            {
                                CommandName             = cmdRequest.CommandName,
                                CommandUniqueIdentifier = cmdRequest.CommandUniqueIdentifier,
                                StepName         = resp.FunctionName,
                                Message          = resp.Message,
                                ImpactedEntities = impactedEntities
                            };
                            resp = await context.CallActivityAsync <ActivityResponse>("CommandStepCompleteActivity", stepResponse);
                        }


                        // Mark the command as complete
                        if (!resp.FatalError)
                        {
                            resp = await context.CallActivityAsync <ActivityResponse>("CommandCompleteActivity", cmdRequest);
                        }

                        #region Logging
                        if (null != log)
                        {
                            if (null != resp)
                            {
                                log.LogInformation($"{resp.FunctionName} complete: {resp.Message } ");
                            }
                        }
                        #endregion
                        if (null != resp)
                        {
                            context.SetCustomStatus(resp);
                        }
                    }
                    else
                    {
                        #region Logging
                        if (null != log)
                        {
                            log.LogWarning($"Command parameters not valid {cmdRequest.CommandName} : {cmdRequest.CommandUniqueIdentifier } ");
                        }
                        #endregion
                        if (null != resp)
                        {
                            resp.Message = $"Command parameters not valid {cmdRequest.CommandName} : {cmdRequest.CommandUniqueIdentifier } ";
                            context.SetCustomStatus(resp);
                        }
                    }
                }
            }
            else
            {
            }
        }
        public static async Task <ActivityResponse> SetLeagueEmailAddressCommandLogParametersActivity(
            [ActivityTrigger] DurableActivityContext context,
            ILogger log)
        {
            ActivityResponse ret = new ActivityResponse()
            {
                FunctionName = "SetLeagueEmailAddressCommandHandlerActivity"
            };

            try
            {
                CommandRequest <Set_Email_Address_Definition> cmdRequest = context.GetInput <CommandRequest <Set_Email_Address_Definition> >();

                if (null != cmdRequest)
                {
                    if (null != log)
                    {
                        // Unable to get the request details from the orchestration
                        log.LogInformation($"CreateLeagueCommandLogParametersActivity : Logging parameters for command {cmdRequest.CommandUniqueIdentifier} ");
                    }

                    EventStream commandEvents = new EventStream(@"Command",
                                                                cmdRequest.CommandName,
                                                                cmdRequest.CommandUniqueIdentifier.ToString());

                    if (null != commandEvents)
                    {
                        Set_Email_Address_Definition parameters = cmdRequest.GetParameters();

                        if (null != parameters)
                        {
                            // Set the parameters
                            await commandEvents.AppendEvent(new TheLongRun.Common.Events.Command.ParameterValueSet(nameof(parameters.LeagueName),
                                                                                                                   parameters.LeagueName));

                            await commandEvents.AppendEvent(new TheLongRun.Common.Events.Command.ParameterValueSet(nameof(parameters.New_Email_Address),
                                                                                                                   parameters.New_Email_Address));

                            await commandEvents.AppendEvent(new TheLongRun.Common.Events.Command.ParameterValueSet(nameof(parameters.Notes),
                                                                                                                   parameters.Notes));

                            await commandEvents.AppendEvent(new TheLongRun.Common.Events.Command.ParameterValueSet(nameof(parameters.InstanceIdentifier),
                                                                                                                   parameters.InstanceIdentifier));

                            ret.Message = $"All parameters to set for {cmdRequest.CommandName} : {cmdRequest.CommandUniqueIdentifier}";
                        }
                        else
                        {
                            ret.Message = $"No parameters to set for {cmdRequest.CommandName} : {cmdRequest.CommandUniqueIdentifier}";
                        }
                    }
                    else
                    {
                        ret.Message    = $"Unable to get the event stream for {cmdRequest.CommandName} : {cmdRequest.CommandUniqueIdentifier}";
                        ret.FatalError = true;
                    }
                }
                else
                {
                    ret.FatalError = true;
                    ret.Message    = $"Unable to read command request from {context.InstanceId }";
                }
            }
            catch (Exception ex)
            {
                ret.FatalError = true;
                ret.Message    = ex.Message;
            }

            return(ret);
        }