Esempio n. 1
0
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (!(context.ActionArguments.Values.FirstOrDefault(x => x is Update) is Update update))
            {
                context.Result = new StatusCodeResult(401);
                return;
            }

            var extendedUpdate = userContextProvider.Set(update);
            var id             = extendedUpdate.Type == UpdateType.CallbackQuery
                ? extendedUpdate.CallbackQuery.From.Id
                : extendedUpdate.RealMessage.From.Id;

            telegramUserIdProvider.Set(id);
            var dbUser = await loaderProvider
                         .Get <int, TelegramUser>()
                         .GetOrLoadAsync(id);

            userContextProvider.SetDatabaseUser(dbUser);
            var result = await next();

            if (result.Exception != null)
            {
                await telegramUsersNotifier
                .SendAsync((client, user) => client
                           .SendTextMessageAsync(user.Id, result.Exception.ToString()),
                           TelegramUserStates.Dev);

                result.ExceptionHandled = true;
                result.HttpContext.Response.StatusCode = 200;
            }
        }
        protected override async Task <Order> ProcessAsync(Order order)
        {
            order.State = OrderStates.Confirmed;
            var(
                notificationFormat,
                fromFormat,
                hookahRowFormat,
                tobaccoRowFormat,
                unknownTranslated,
                approveTranslated,
                rejectTranslated
                ) = await
                    TranslationsResolver.ResolveAsync(
                (TelegramTranslationKeys.NewOrderNotification, Array.Empty <object>()),
                (TelegramTranslationKeys.From, Array.Empty <object>()),
                (TelegramTranslationKeys.OrderNotificationHookahRow, Array.Empty <object>()),
                (TelegramTranslationKeys.OrderNotificationTobaccoRow, Array.Empty <object>()),
                (TelegramTranslationKeys.Unknown, Array.Empty <object>()),
                (TelegramTranslationKeys.Approve, Array.Empty <object>()),
                (TelegramTranslationKeys.Reject, Array.Empty <object>())
                );

            var from         = UserContextProvider.Update.From;
            var notification = string.Format(notificationFormat,
                                             order.Id,
                                             //todo rework as there is a lot of warning about possible empty pointer dereference.
                                             order.OrderedHookahs
                                             .AggregateListString(hookahRowFormat,
                                                                  x => x.Product.Name,
                                                                  x => x.Product.Price,
                                                                  x => x.Count,
                                                                  x => x.Count * x.Product.Price),
                                             order.OrderedTobaccos
                                             .AggregateListString(tobaccoRowFormat,
                                                                  x => x.Product.Name,
                                                                  x => x.Product.Price,
                                                                  x => x.Count,
                                                                  x => x.Count * x.Product.Price),
                                             string.Format(fromFormat,
                                                           string.IsNullOrEmpty(from.Username)
                        ? $"[{unknownTranslated}]"
                        : $"@{from.Username}",
                                                           string.IsNullOrEmpty(from.FirstName)
                        ? $"[{unknownTranslated}]"
                        : from.FirstName,
                                                           string.IsNullOrEmpty(from.LastName)
                        ? $"[{unknownTranslated}]"
                        : from.LastName)
                                             );
            await HookrRepository.SaveChangesAsync();

            await telegramUsersNotifier.SendAsync((client, user) =>
                                                  client.SendTextMessageAsync(user.Id,
                                                                              notification,
                                                                              ParseMode.MarkdownV2,
                                                                              replyMarkup: new InlineKeyboardMarkup(new[]
            {
                new InlineKeyboardButton
                {
                    Text         = approveTranslated,
                    CallbackData = $"/{nameof(ApproveOrderCommand).ExtractCommandName()} {order.Id}"
                },
                new InlineKeyboardButton
                {
                    Text         = rejectTranslated,
                    CallbackData = $"/{nameof(RejectOrderCommand).ExtractCommandName()} {order.Id}"
                }
            })),
                                                  TelegramUserStates.Service,
                                                  TelegramUserStates.Dev);

            return(order);
        }