Esempio n. 1
0
        private async Task <(SendMessageInput model, bool toast)> Command(MessageModel myMessage)
        {
            var buttonId       = myMessage.AuxData == null ? "" : myMessage.AuxData.ButtonId;
            var chatId         = myMessage.ChatId;
            var messageText    = myMessage.Text;
            var userDataEntity = await _userDataService.GetUserData(chatId);

            var model = new SendMessageInput
            {
                Keypad = new KeypadModel
                {
                    Rows = new List <KeypadRowModel>()
                },
                ReplyTimeout = Variables.ReplyTimeout,
                ChatId       = chatId
            };

            //start
            if (userDataEntity == null)
            {
                await _userDataService.Update(chatId, 1);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 1, new GroupEntity());

                model.Text = Messages.WelcomeMessage;
                return(model, false);
            }

            if (string.IsNullOrEmpty(buttonId))
            {
                var info = await _groupService.GetGroupInfo(chatId);

                if (info.IsFinished)
                {
                    model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 6, info);

                    model.Text = null;
                    return(model, true);
                }

                return(await InvalidCommand(model, userDataEntity.StateId));
            }

            if (buttonId == "centerName")
            {
                var info = await _groupService.SetCenterName(chatId, messageText);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 1, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "centerType")
            {
                var centerType = messageText.GetCenterTypeEnumByValue();
                var info       = await _groupService.SetCenterType(chatId, centerType.Value);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 1, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "gender")
            {
                var gender = messageText.GetGenderEnumByValue();
                var info   = await _groupService.SetGenderType(chatId, gender.Value);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 1, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "province")
            {
                var province = await _provinceService.GetProvinceAsync(messageText);

                var info = await _groupService.SetProvince(chatId, province);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 1, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "address")
            {
                var info = await _groupService.SetAddress(chatId, messageText);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 1, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "postalCode")
            {
                GroupEntity info;
                if (messageText.IsValidPostalCode())
                {
                    info = await _groupService.SetPostalCode(chatId, messageText.PersianToEnglish());
                }
                else
                {
                    info = await _groupService.GetGroupInfo(model.ChatId);

                    model.Text = Messages.InvalidPostalCode;
                }
                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 1, info);

                return(model, true);
            }

            if (buttonId == "phone")
            {
                var info = await _groupService.SetPhone(chatId, messageText.PersianToEnglish());

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 1, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "managerName")
            {
                var info = await _groupService.SetManagerName(chatId, messageText);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 1, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "1-continue")
            {
                var info = await _groupService.GetGroupInfo(chatId);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 2, info);

                model.Text = "اطلاعات مرکز آموزشی ثبت شد.";
                return(model, true);
            }

            if (buttonId == "supervisorName")
            {
                var info = await _groupService.SetSupervisorName(chatId, messageText);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 2, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "supervisorEmail")
            {
                GroupEntity info;

                if (messageText.IsValidEmail())
                {
                    info = await _groupService.SetSupervisorEmail(chatId, messageText);

                    model.Text = null;
                }
                else
                {
                    info = await _groupService.GetGroupInfo(chatId);

                    model.Text = Messages.InvalidEmail;
                }
                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 2, info);

                return(model, true);
            }

            if (buttonId == "supervisorMobile")
            {
                GroupEntity info;
                if (messageText.IsValidMobile())
                {
                    info = await _groupService.SetSupervisorMobile(chatId, messageText);

                    model.Text = null;
                }
                else
                {
                    info = await _groupService.GetGroupInfo(chatId);

                    model.Text = Messages.InvalidMobileNumber;
                }
                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 2, info);

                return(model, true);
            }

            if (buttonId == "2-continue")
            {
                var info = await _groupService.GetGroupInfo(chatId);

                model.Text   = "اطلاعات دبیر راهنما ثبت شد.";
                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 3, info);

                return(model, true);
            }

            if (buttonId == "2-return")
            {
                var info = await _groupService.GetGroupInfo(chatId);

                model.Text   = null;
                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 1, info);

                return(model, true);
            }

            if (buttonId == "subject")
            {
                var info = await _groupService.SetProjectSubject(chatId, messageText);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 3, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "ax")
            {
                var ax = await _festivalService.GetFestivalAxes(messageText);

                var info = await _groupService.SetFestivalAxes(chatId, ax);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 3, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "major")
            {
                var major = await _festivalService.GetFestivalMajor(messageText);

                var info = await _groupService.SetFestivalMajor(chatId, major);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 3, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "field")
            {
                var field = await _festivalService.GetFestivalField(messageText);

                var info = await _groupService.SetFestivalField(chatId, field);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 3, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "membersCount")
            {
                var info = await _groupService.SetMembersCount(chatId, Int32.Parse(messageText));

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 3, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId == "3-continue")
            {
                var info = await _groupService.GetGroupInfo(chatId);

                model.Text = "اطلاعات پروژه ثبت شد.";

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 4, info);

                return(model, true);
            }

            if (buttonId == "3-return")
            {
                var info = await _groupService.GetGroupInfo(chatId);

                model.Text   = null;
                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 2, info);

                return(model, true);
            }

            if (buttonId == "grade")
            {
                var grade = EnumValue.GetGradeEnumByValue(messageText);
                var info  = await _groupService.SetMembersGrade(chatId, grade.Value);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 4, info);

                model.Text = null;
                return(model, true);
            }

            if (buttonId.StartsWith("memberName"))
            {
                var info = await _groupService.GetGroupInfo(chatId);

                var number = int.Parse(buttonId.Replace("memberName", ""));

                await _groupService.SetMemberName(chatId, info.Id, number, messageText);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 4, info);

                return(model, true);
            }

            if (buttonId.StartsWith("memberNationalCode"))
            {
                var info = await _groupService.GetGroupInfo(chatId);

                var number = int.Parse(buttonId.Replace("memberNationalCode", ""));

                if (messageText.IsValidNationalCode())
                {
                    await _groupService.SetMemberNationalCode(chatId, info.Id, number, messageText);
                }
                else
                {
                    model.Text = Messages.InvalidNationalCode;
                }

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 4, info);

                return(model, true);
            }

            if (buttonId == "4-continue")
            {
                var info = await _groupService.SetPrice(chatId);

                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 5, info);

                model.Text = GetGroupInfo(info);
                await _messengerService.SendMessage(model);

                model.Text = await GetMembersInfo(info);

                return(model, true);
            }

            if (buttonId == "4-return")
            {
                var info = await _groupService.GetGroupInfo(chatId);

                model.Text   = null;
                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 3, info);

                return(model, true);
            }

            //Payment
            if (buttonId == "payment")
            {
                var groupInfo = await _groupService.GetGroupInfo(model.ChatId);

                if (myMessage.AuxData == null || string.IsNullOrEmpty(myMessage.AuxData.OrderId))
                {
                    model.Text   = Messages.ErrorPay;
                    model.Keypad = await _keypadService.GenerateButtonsAsync(model.ChatId, 4, groupInfo);

                    return(model, true);
                }

                var orderId = int.Parse(myMessage.AuxData.OrderId);

                var order = await _orderService.UpdateOrderStatus(orderId, myMessage.AuxData.OrderStatus);

                if (order == null || myMessage.AuxData.OrderStatus == OrderStatusEnum.Error)
                {
                    model.Text   = Messages.ErrorPay;
                    model.Keypad = await _keypadService.GenerateButtonsAsync(model.ChatId, 4, groupInfo);

                    return(model, true);
                }

                else
                {
                    var settle = await _messengerService.SettlePayment(model.ChatId, order.Id.ToString(), order.PaymentToken);

                    await _orderService.UpdateSettleStatus(orderId, settle.Data.SettleStatus);

                    if (settle.Data.SettleStatus == SettleStatusEnum.Reject)
                    {
                        model.Text   = Messages.ErrorPay;
                        model.Keypad = await _keypadService.GenerateButtonsAsync(model.ChatId, 4, groupInfo);

                        return(model, true);
                    }
                    else //(settle.Data.SettleStatus == SettleStatusEnum.Done)
                    {
                        await _groupService.SetFinished(model.ChatId, orderId);

                        model.Text   = Messages.SuccessRegister;
                        model.Keypad = await _keypadService.GenerateButtonsAsync(model.ChatId, 6, groupInfo);

                        return(model, false);
                    }
                }
            }

            if (buttonId == "5-return")
            {
                var info = await _groupService.GetGroupInfo(chatId);

                model.Text   = null;
                model.Keypad = await _keypadService.GenerateButtonsAsync(chatId, 4, info);

                return(model, true);
            }

            return(await InvalidCommand(model, userDataEntity.StateId));
        }