Example #1
0
        public void given_correct_account_filter_command_when_determining_report_command_then_the_account_filter_is_added(params string[] args)
        {
            var commandProvider = new CommandProvider();
            var command         = commandProvider.GetCommand(args); // saturday

            Assert.IsInstanceOf(typeof(BuildMonthReportsCommand), command, "Type of command");
            Assert.AreEqual("nwb", command.AccountFilter, "Command account filter");
        }
Example #2
0
        public void given_correct_month_command_and_date_input_when_determining_report_command_then_the_month_command_with_the_correct_first_period_date_is_returned()
        {
            var commandProvider = new CommandProvider();
            var command         = commandProvider.GetCommand(new[] { "-m", "20200229" }); // saturday

            Assert.IsInstanceOf(typeof(BuildMonthReportsCommand), command, "Type of command");
            Assert.AreEqual(new DateTime(2020, 2, 1), command.FromDay, "Command first date of period");
        }
Example #3
0
        public void given_no_arguments_when_determining_report_command_then_the_default_command_is_returned()
        {
            var commandProvider = new CommandProvider();
            var command         = commandProvider.GetCommand(null);

            Assert.IsInstanceOf(typeof(BuildWeekReportsCommand), command, "Type of command");
            Assert.IsTrue(DateTime.Today - command.FromDay < TimeSpan.FromDays(7), "Default command is for the current week");
        }
Example #4
0
        static void Main(string[] args)
        {
            var reportingCommandProvider = new CommandProvider();

            var command = (dynamic)reportingCommandProvider.GetCommand(args);
            var timeLogRepositoryFactory = new ManualTimeLogger.Persistence.CsvFileRepositoryFactory(Properties.Settings.Default.TimeLogsBasePath);
            var reportRepositoryFactory  = new CsvFileRepositoryFactory(Properties.Settings.Default.ReportsBasePath, $"{command.AccountFilter ?? "all"}");

            var commandHandler = new CommandHandler(timeLogRepositoryFactory, reportRepositoryFactory);

            commandHandler.Handle(command);
        }
Example #5
0
        private void BotClient_OnMessage(object sender, MessageEventArgs e)
        {
            try
            {
                Message message = e ? .Message;

                if (message is null)
                {
                    return;
                }

                TUser           user = UserProvider.GetUser(message.From.Id);
                Session <TUser> currentSession;
                object          tag = null;

                if (user is null)
                {
                    if (message.Chat.Type == ChatType.Private)
                    {
                        currentSession = SessionProvider.GetSession(message.From.Id);
                        currentSession.PrivateChatId = message.Chat.Id;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    currentSession = SessionProvider.GetSession(user);
                    if (message.Chat.Type == ChatType.Private)
                    {
                        currentSession.PrivateChatId = message.Chat.Id;
                    }
                }

                currentSession.BotClient = BotClient;

                ICommand <TUser> routeTarget      = currentSession.RouteBind;
                string []        args             = default;
                bool             isExactlyMatched = false;

                string text = message.Text ? .Normalize(NormalizationForm.FormKD) ? .Trim( );

                if (routeTarget is null)
                {
                    if (string.IsNullOrWhiteSpace(text))
                    {
                        return;
                    }

                    if (text.First( ) == '/')
                    {
                        text = text.TrimStart('/');

                        args = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                        if (args.Length > 0)
                        {
                            string commandName = args.First( ).GetCommandName( );

                            routeTarget = CommandProvider.GetCommand(commandName, currentSession);

                            if (routeTarget is null)
                            {
                                routeTarget      = CommandProvider.GetCommandFuzzy(commandName, currentSession);
                                isExactlyMatched = false;
                            }
                            else
                            {
                                isExactlyMatched = true;
                            }
                        }
                        else
                        {
                            routeTarget      = EmptyCommand <TUser> .Current;
                            isExactlyMatched = false;
                        }
                    }
                    else
                    {
                        //no command
                        //
                        if (message.Chat.Type == ChatType.Private)
                        {
                            routeTarget      = EmptyCommand <TUser> .Current;
                            isExactlyMatched = false;
                        }
                    }
                }
                else
                {
                    args = text ? .Split(' ', StringSplitOptions.RemoveEmptyEntries) ?? new string [] { };

                    isExactlyMatched = true;
                }

                if (!(routeTarget is null))
                {
                    if (!PermissionProvider.IsAllowedToInvoke(user, routeTarget.PermissionGroup))
                    {
                        tag         = routeTarget;
                        routeTarget = PermissionDeniedCommand <TUser> .Current;
                    }

                    TaskDispatcher.Dispatch(
                        new OnetimeTask(
                            ()
                            => routeTarget.Process(
                                message,
                                args ?? new string [] { },
                                currentSession,
                                isExactlyMatched,
                                tag),
                            routeTarget.Timeout));
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
Example #6
0
        private void BotClient_OnCallbackQuery(object sender, CallbackQueryEventArgs e)
        {
            try
            {
                CallbackQuery callbackQuery = e ? .CallbackQuery;

                if (callbackQuery is null)
                {
                    return;
                }

                string requestData = callbackQuery.Data;
                if (string.IsNullOrWhiteSpace(requestData))
                {
                    return;
                }

                requestData = requestData.Normalize(NormalizationForm.FormKD).Trim( );
                string [] args = requestData.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                TUser           user = UserProvider.GetUser(callbackQuery.From.Id);
                Session <TUser> currentSession;
                object          tag = null;

                if (user is null)
                {
                    if (callbackQuery.Message.Chat.Type == ChatType.Private)
                    {
                        currentSession = SessionProvider.GetSession(callbackQuery.Message.From.Id);
                        currentSession.PrivateChatId = callbackQuery.Message.Chat.Id;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    currentSession = SessionProvider.GetSession(user);
                    if (callbackQuery.Message.Chat.Type == ChatType.Private)
                    {
                        currentSession.PrivateChatId = callbackQuery.Message.Chat.Id;
                    }
                }

                currentSession.BotClient = BotClient;

                ICommand <TUser> routeTarget = currentSession.RouteBind
                                               ?? CommandProvider.GetCommand(
                    args.First( ).TrimEndPattern("Command"),
                    currentSession);

                if (routeTarget is null)
                {
                    Logger.LogWarning($"CallbackQuery {e . CallbackQuery} can not be routed.");

                    //Wrong Callback?
                }
                else
                {
                    if (!PermissionProvider.IsAllowedToInvoke(user, routeTarget.PermissionGroup))
                    {
                        tag         = routeTarget;
                        routeTarget = PermissionDeniedCommand <TUser> .Current;
                    }

                    TaskDispatcher.Dispatch(
                        new OnetimeTask(
                            ()
                            => routeTarget.Process(
                                callbackQuery,
                                args,
                                currentSession,
                                tag),
                            routeTarget.Timeout));
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
Example #7
0
        public void given_wrong_command_input_when_determining_report_command_then_an_argument_exception_is_thrown()
        {
            var commandProvider = new CommandProvider();

            Assert.Throws <ArgumentException>(() => commandProvider.GetCommand(new[] { "-f", "20100202" }));
        }
Example #8
0
        public void given_wrong_date_input_when_determining_report_command_then_an_argument_exception_is_thrown(string dateInput)
        {
            var commandProvider = new CommandProvider();

            Assert.Throws <ArgumentException>(() => commandProvider.GetCommand(new[] { "-w", dateInput }));
        }
Example #9
0
        public void given_wrong_number_of_arguments_when_determining_report_command_then_an_argument_exception_is_thrown()
        {
            var commandProvider = new CommandProvider();

            Assert.Throws <ArgumentException>(() => commandProvider.GetCommand(new [] { "1", "2", "3" }));
        }