async Task GetUserEmployeeInformation()
        {
            //Obtine los empleos del usuario.
            var userEmployees = await EmployeeDataStore.GetUserEmployees(App.LogUser.UserId.ToString());

            UserStoreEmployee = new ObservableCollection <Store>();

            foreach (var item in userEmployees)
            {
                //Obtine el empleado
                item.EmployeeStore = await StoreDataStore.GetAvailableStoreInformation(item.StoreId);

                if (item.EmployeeStore != null)
                {
                    var empWorkHours = await EmployeeWorkHour.GetEmployeeWorkHours(item.EmployeeId.ToString());

                    item.EmployeeWorkHours = empWorkHours.ToList();


                    var storePresenter = new StorePresenters(item.EmployeeStore);

                    StorePresenters.Add(storePresenter);
                }
                //UserStoreEmployee.Add(item.EmployeeStore);
            }

            App.LogUser.Employees = userEmployees.ToList();
        }
Пример #2
0
        public LoginViewModel(INavigation _navigation)
        {
            Navigation = _navigation;

            popupNavigation = PopupNavigation.Instance;

            Task.Run(async() =>
            {
                Username = await SecureStorage.GetAsync("username");
                Password = await SecureStorage.GetAsync("password");
            });

            ValidatorsInitializer();

            //App.ComunicationService = new ComunicationService();
            Genders   = new List <string>(Enum.GetNames(typeof(Gender)).ToList());
            IsLoading = false;


            LoginCommand = new Command(async() =>
            {
                //var currentuserID = Xamarin.Forms.Application.Current.Properties["loginId "].ToString();
                IsLoading = true;

                if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
                {
                    //Verifica si el telefono tiene acceso a internet


                    if (Connectivity.NetworkAccess == NetworkAccess.Internet)
                    {
                        //Obtine los credenciales del usuario
                        //var loginresult = userDataStore.CheckUserCredential(Username, Password);
                        //Obtiene el token de acceso
                        App.TokenDto = userDataStore.LoginCredential(Username, Password);

                        var loginresult = App.TokenDto.UserDetail;

                        //Verifica si el resultado del login no es vacio.
                        if (loginresult != null)
                        {
                            if (!loginresult.IsValidUser)
                            {
                                App.LogUser = loginresult;
                                await popupNavigation.PushAsync(new ValidateEmailCode());
                                IsLoading = false;
                            }
                            else
                            {
                                Task.Run(async() =>
                                {
                                    await App.ComunicationService.Connect();
                                }).Wait();

                                App.LogUser = loginresult;

                                bool hasPaymentCard = App.LogUser.PaymentCards.Count() > 0 ? true : false;

                                //Verfico si hay tarjetas registradas con el usuario
                                if (hasPaymentCard)
                                {
                                    var data = App.LogUser.PaymentCards;
                                    var card = new List <PaymentCard>(data);


                                    var userCardTokenId = await stripeServiceDS.GetCustomerCardId(App.LogUser.StripeUserId, card[0].StripeCardId);


                                    App.CardPaymentToken.CardTokenId = userCardTokenId;
                                }


                                if (!String.IsNullOrEmpty(App.ComunicationService.hubConnection.ConnectionId))
                                {
                                    App.UsersConnected = new UsersConnected()
                                    {
                                        HubConnectionID = App.ComunicationService.hubConnection.ConnectionId,
                                        UserID          = App.LogUser.UserId,
                                        IsDisable       = false,
                                        ConnecteDate    = DateTime.Now
                                    };

                                    var result = await userConnectedDataStore.ModifyOldConnections(App.UsersConnected);

                                    var hub_connected_Result = await userConnectedDataStore.AddItemAsync(App.UsersConnected);
                                }



                                App.Current.MainPage = new AppShell();
                                IsLoading            = false;
                            }
                        }
                        else
                        {
                            IsLoading = false;
                            await App.Current.MainPage.DisplayAlert("Notification", "Incorrect login...!", "OK");
                        }
                    }
                }
                else
                {
                    IsLoading = false;
                    await App.Current.MainPage.DisplayAlert("Notification", "Empty values...!", "OK");
                }
            });

            LoginEmployeeCommand = new Command(async() =>
            {
                IsLoading = true;
                if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
                {
                    //var loginresult = userDataStore.CheckUserCredential(Username, Password);
                    App.TokenDto = userDataStore.LoginCredential(Username, Password);


                    var loginresult = App.TokenDto.UserDetail;
                    if (loginresult != null)
                    {
                        Task.Run(async() =>
                        {
                            await App.ComunicationService.Connect();
                        }).Wait();

                        var userEmployees = await EmployeeDataStore.GetUserEmployees(loginresult.UserId.ToString());
                        App.LogUser       = loginresult;


                        if (!String.IsNullOrEmpty(App.ComunicationService.hubConnection.ConnectionId))
                        {
                            App.UsersConnected = new UsersConnected()
                            {
                                HubConnectionID = App.ComunicationService.hubConnection.ConnectionId,
                                UserID          = App.LogUser.UserId,
                                IsDisable       = false,
                                ConnecteDate    = DateTime.Now
                            };

                            var result = await userConnectedDataStore.ModifyOldConnections(App.UsersConnected);

                            var hub_connected_Result = await userConnectedDataStore.AddItemAsync(App.UsersConnected);
                        }

                        //App.UsersConnected = new UsersConnected()
                        //{
                        //    HubConnectionID = App.ComunicationService.hubConnection.ConnectionId,
                        //    UserID = App.LogUser.UserId,
                        //    IsDisable = false,
                        //    ConnecteDate = DateTime.Now
                        //};
                        //var result = await userConnectedDataStore.ModifyOldConnections(App.UsersConnected);


                        //var hub_connected_Result = await userConnectedDataStore.AddItemAsync(App.UsersConnected);

                        App.Current.MainPage = new EmployeeShell();
                        IsLoading            = false;
                    }
                    else
                    {
                        await App.Current.MainPage.DisplayAlert("Notification", "Incorrect login.", "OK");
                    }
                }
                else
                {
                    await App.Current.MainPage.DisplayAlert("Notification", "Empty inputs.", "OK");
                }
            });

            RegisterCommand = new Command(async() =>
            {
                await App.Current.MainPage.Navigation.PushAsync(new RegisterPage());
            });



            DoneCommand = new Command(async() =>
            {
                SetValidatorValues();

                //Verficamos que las propiedades esten de con la informacion correcta y llenas.
                if (!UsernameValidator.HasError && !FullNameValidator.HasError && !PhoneValidator.HasError && !AddressValidator.HasError && !PasswordValidator.HasError && !ConfirmPasswordValidator.HasError && !EmailValidator.HasError && !GenderValidator.HasError && !EmailPatternValidator.HasError)
                {
                    if (!await userDataStore.EmailExist(Email))
                    {
                        ConfirmAndPasswordValidator = ValidatorRules.PasswordAndConfirmPasswordEquals(Password, ConfirmPassword);
                        //Verificamos que el password y el confirmpassword matcheen
                        if (!ConfirmAndPasswordValidator.HasError)
                        {
                            //Si el username y password existen tendra que reinsertar esa informacion
                            if (!await userDataStore.CheckIfUsernameAndPasswordExist(Username, Password))
                            {
                                var userlogin = new Login()
                                {
                                    LoginId     = Guid.NewGuid(),
                                    IsConnected = false,
                                    Password    = Password,
                                    Username    = Username
                                };

                                Gender value;
                                Enum.TryParse(GenderSelected, out value);
                                var newUser = new User()
                                {
                                    UserId    = Guid.NewGuid(),
                                    Email     = Email,
                                    Name      = Fullname,
                                    LoginId   = userlogin.LoginId,
                                    Phone     = Phone,
                                    Address   = Address,
                                    Gender    = value,
                                    UserLogin = userlogin,
                                };

                                userlogin.UserId = newUser.UserId;

                                try
                                {
                                    var optionsCustomers = new UserDTO
                                    {
                                        Name    = Fullname,
                                        Email   = Email,
                                        Phone   = Phone,
                                        Address = Address
                                    };


                                    //Create Customer
                                    var customertokenId = await stripeServiceDS.CreateStripeCustomer(optionsCustomers);


                                    if (!string.IsNullOrEmpty(customertokenId))
                                    {
                                        Task.Run(async() =>
                                        {
                                            await App.ComunicationService.Connect();
                                        }).Wait();

                                        newUser.StripeUserId = customertokenId;
                                        var result           = await userDataStore.AddItemAsync(newUser);

                                        //var credentialsResult = userDataStore.CheckUserCredential(Username, Password);
                                        App.TokenDto = userDataStore.LoginCredential(Username, Password);

                                        if (result)
                                        {
                                            App.LogUser = App.TokenDto.UserDetail;

                                            if (!String.IsNullOrEmpty(App.ComunicationService.hubConnection.ConnectionId))
                                            {
                                                App.UsersConnected = new UsersConnected()
                                                {
                                                    HubConnectionID = App.ComunicationService.hubConnection.ConnectionId,
                                                    UserID          = App.LogUser.UserId,
                                                    IsDisable       = false,
                                                    ConnecteDate    = DateTime.Now
                                                };

                                                //var oldConnectionModify = await userConnectedDataStore.ModifyOldConnections(App.UsersConnected);

                                                var hub_connected_Result = await userConnectedDataStore.AddItemAsync(App.UsersConnected);
                                            }


                                            try
                                            {
                                                await SecureStorage.SetAsync("username", Username);
                                                await SecureStorage.SetAsync("password", Password);
                                            }
                                            catch (Exception ex)
                                            {
                                                // Possible that device doesn't support secure storage on device.
                                            }


                                            await popupNavigation.PushAsync(new RegisterValidationEmail());
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    await App.Current.MainPage.DisplayAlert("Notification", e.Message, "OK");
                                }
                            }
                            else
                            {
                                await App.Current.MainPage.DisplayAlert("Notification", "Username or password exist try to change to other one.", "OK");
                            }
                        }
                    }
                    else
                    {
                        await App.Current.MainPage.DisplayAlert("Notification", "Email is in used.", "OK");
                    }
                }
                else
                {
                    await App.Current.MainPage.DisplayAlert("Notification", "Some error ocurred check the information.", "OK");
                }
            });



            GoForgotPasswordCommand = new Command(async() =>
            {
                await App.Current.MainPage.Navigation.PushAsync(new ForgotPasswordPage());
            });
        }