Esempio n. 1
0
        public IHttpActionResult UpdateAccount(DTO.Account account)
        {
            try
            {
                base.SetRequestInfo();

                return(Ok(accountApp.Save(account, base.ClientId, base.AccountCode)));
            }
            catch (LockedUpMemberException ex)
            {
                return(BadRequest(new ArgumentException(ex.Message)));
            }
            catch (PasswordException ex)
            {
                return(BadRequest(new ArgumentException(ex.Message)));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(ex));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Esempio n. 2
0
        public IHttpActionResult AddAccountByAdmin(DTO.Account account)
        {
            try
            {
                base.SetRequestInfo();

                var _account = accountApp.Save(account, base.ClientId, Guid.Empty, false, true);

                if (!_account.IsNull() && !_account.Password.IsNullOrWhiteSpace())
                {
                    _account.Password = null;
                }

                return(Ok(_account));
            }
            catch (LockedUpMemberException ex)
            {
                return(BadRequest(new ArgumentException(ex.Message)));
            }
            catch (PasswordException ex)
            {
                return(BadRequest(new ArgumentException(ex.Message)));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(ex));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Esempio n. 3
0
        public DTO.Account UnlockUser(Guid code, Guid currentAccount)
        {
            var account = accountService.GetIfHasPermissionToUpdate(currentAccount, code);

            if (!account.IsNull() && !account.Customer.IsNull())
            {
                account.Customer.HandlerToGet();
            }

            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    account = accountService.UnlockUser(account);
                }
                catch
                {
                    transaction.Rollback();

                    throw new ArgumentException("Não foi possível realizar essa operação nesse momento, tente mais tarde novamente.");
                }
            }

            var _account = new DTO.Account(account, account.AccountApplicationStoreCollection.Select(a => a.ApplicationStore).ToList());

            return(_account);
        }
Esempio n. 4
0
 public IHttpActionResult AddSimplifiedAccount(DTO.Account customerSimplified)
 {
     try
     {
         base.SetRequestInfo();
         return(Ok(accountApp.SaveCustomerSimplified(customerSimplified, base.ClientId)));
     }
     catch (LockedUpMemberException ex)
     {
         return(BadRequest(new ArgumentException(ex.Message)));
     }
     catch (PasswordException ex)
     {
         return(BadRequest(new ArgumentException(ex.Message)));
     }
     catch (ArgumentException ex)
     {
         return(BadRequest(ex));
     }
     catch (Exception ex)
     {
         return(InternalServerError(ex));
     }
 }
Esempio n. 5
0
        public List <DTO.Account> GetCustomers(DO.Requisition requisition, int skip, int take)
        {
            string DBAccountsCustomerImport = GetDbAccountsCustomerImport();

            var dicAddresses = GetAddresses(requisition);
            var accounts     = new List <DTO.Account>();

            using (var connection = new SqlConnection(strConnection))
            {
                using (var command = new SqlCommand(string.Format("{0}.[SPRK_UserList]", DBAccountsCustomerImport), connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@Requisition", requisition.Code);
                    command.Parameters.AddWithValue("@Skip", skip);
                    command.Parameters.AddWithValue("@Take", take);

                    connection.Open();

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var customer = new DTO.Customer();
                            var document = string.Empty;

                            if (reader["ConTipo"].AsInt() == 1)
                            {
                                document = reader["ConCPF"].ToString().Trim();

                                string gender = null;
                                if (!reader["UsuSexo"].AsString().IsNullorEmpty())
                                {
                                    gender = reader["UsuSexo"].AsInt() == 1 ? "M" : "F";
                                }

                                customer = new DTO.Person
                                {
                                    FirstName = reader["ConNome"].ToString().Trim(),
                                    LastName  = reader["ConSobreNome"].ToString().Trim(),
                                    Cpf       = reader["ConCPF"].ToString().Trim(),
                                    Rg        = reader["ConRG"].ToString().Trim(),
                                    BirthDate = reader["ConDtNascimento"].AsDateTime(),
                                    Gender    = gender,
                                    Type      = CustomerType.Person
                                };
                            }
                            else
                            {
                                document = reader["ConCNPJ"].ToString().Trim();

                                customer = new DTO.Company
                                {
                                    CompanyName = reader["ConRazaoSocial"].ToString().Trim(),
                                    TradeName   = reader["ConNomeFantasia"].ToString().Trim(),
                                    Cnpj        = reader["ConCNPJ"].ToString().Trim(),
                                    Ie          = reader["ConIE"].ToString().Trim(),
                                    Im          = reader["ConIM"].ToString().Trim(),
                                    Type        = CustomerType.Company
                                };
                            }

                            customer.Status     = reader["ConStatus"].AsInt() != 0 || reader["ConStatus"].AsInt() != 2 ? true : false;
                            customer.SaveDate   = reader["ConDtInclusao"].AsDateTime();
                            customer.UpdateDate = reader["ConDtAtualizacao"].AsDateTime();

                            customer.Addresses = new List <DTO.Address>();

                            if (dicAddresses.ContainsKey(reader["ConCodigo"].AsInt()))
                            {
                                customer.Addresses.AddRange(dicAddresses[reader["ConCodigo"].AsInt()]);
                            }

                            var account = new DTO.Account()
                            {
                                Email    = reader["UsuEmail"].ToString().Trim(),
                                Login    = reader["UsuLogin"].ToString().Trim(),
                                Document = document,
                                Password = reader["UsuSenhaBkp"].ToString().Trim(),
                                Customer = customer,
                                Status   = customer.Status,
                                Removed  = false
                            };

                            accounts.Add(account);
                        }
                    }
                }
            }

            return(accounts);
        }
Esempio n. 6
0
        public DTO.Account SaveCustomerSimplified(DTO.Account customerSimplified, Guid clientId)
        {
            var account = Save(customerSimplified, clientId, Guid.Empty, true, true);

            return(account.TransferSimplified());
        }
Esempio n. 7
0
        public DTO.Account Save(DTO.Account account, Guid clientId, Guid currentAccount, bool addCurrentAppStore = true, bool simplifiedCustomer = false)
        {
            if (account.IsNull())
            {
                throw new ArgumentException("Necessário informar os dados de usuário");
            }

            if (account.Email.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("E-mail não informado");
            }

            Account _account = null;
            bool    convertCustomerToCompany = false;

            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    ApplicationStore applicationStore = applicationStoreRepository.GetByClientId(clientId);

                    if (!applicationStore.IsNull())
                    {
                        Account accountDO = null;
                        var     document  = !account.Document.IsNullOrWhiteSpace() ? account.Document : null;

                        if (account.Code.IsEmpty())
                        {
                            accountDO = accountService.Get(account.Email, document, applicationStore, true, true);
                        }

                        if (account.Code.IsEmpty() && accountDO.IsNull())
                        {
                            if (account.Password.IsNullorEmpty())
                            {
                                account.Password = Account.GeneratePassword();
                            }

                            //get store code
                            var storeCode = applicationStore.GetStoreCodeByAuthType(account.StoreCode, true);

                            //get application on parameters
                            var applicationStores = applicationStoreService.Get(account.Applications, storeCode);

                            if (applicationStores.IsNull())
                            {
                                applicationStores = new List <ApplicationStore>();
                            }

                            if (addCurrentAppStore)
                            {
                                applicationStores.Add(applicationStoreService.Get(applicationStore, storeCode));
                            }

                            _account = account.Transfer(applicationStores);

                            Role _role;
                            applicationStores.ForEach(x =>
                            {
                                _role = roleRepository.GetByApplication(x.ApplicationCode, storeCode);

                                if (!_role.IsNull())
                                {
                                    _account.ConnectRole(_role);
                                }
                            });

                            Guid originStore = applicationStore.Store.IsMain ? Guid.Empty : applicationStore.Store.Code;

                            accountService.Add(_account, applicationStore, originStore, simplifiedCustomer);
                        }
                        else if (!currentAccount.IsEmpty())
                        {
                            if (!account.Code.IsEmpty())
                            {
                                var accounts = accountService.CheckEmailAndDocument(account.Email, account.Document, applicationStore, false);

                                if (accounts.Any(a => a.Code != account.Code))
                                {
                                    throw new ArgumentException("E-mail ou Docuemnto já cadastrados com outros dados");
                                }

                                _account = accountService.GetIfHasPermissionToUpdate(currentAccount, account.Code);
                            }
                            else
                            {
                                _account = accountService.GetIfHasPermissionToUpdate(currentAccount, accountDO.Code);
                            }

                            var newAccount = account.Transfer();

                            if (_account.Customer.IsNull() && !newAccount.Customer.IsNull())
                            {
                                if (newAccount.Customer.Type == CustomerType.Person)
                                {
                                    _account.Customer = new Person()
                                    {
                                        FirstName   = !newAccount.Customer.Name.IsNullOrWhiteSpace() ? newAccount.Customer.Name : newAccount.Customer.Email,
                                        Email       = newAccount.Customer.Email,
                                        Gender      = ((Person)newAccount.Customer).Gender,
                                        Cpf         = ((Person)newAccount.Customer).Cpf.ClearStrings(),
                                        Addresses   = newAccount.Customer.Addresses,
                                        AddressData = newAccount.Customer.Addresses.Serialize(),
                                        Password    = newAccount.Password
                                    };
                                }
                                else
                                {
                                    _account.Customer = new Company()
                                    {
                                        Cnpj        = ((Company)newAccount.Customer).Cnpj.ClearStrings(),
                                        Email       = newAccount.Customer.Email,
                                        Name        = !newAccount.Customer.Name.IsNullOrWhiteSpace() ? newAccount.Customer.Name : newAccount.Customer.Email,
                                        Addresses   = newAccount.Customer.Addresses,
                                        AddressData = newAccount.Customer.Addresses.Serialize(),
                                        Password    = newAccount.Password
                                    };
                                }

                                _account.Customer = customerRepository.Save(_account.Customer);
                            }

                            accountService.Update(_account, applicationStore.Store, newAccount);
                        }
                        else
                        {
                            throw new ArgumentException("Usuário já cadastrado");
                        }


                        if (!_account.Customer.IsNull() && !_account.Code.IsEmpty() && !_account.Customer.Email.IsNullOrWhiteSpace())
                        {
                            customerImportService.RemoveMember(_account.Email, _account.Document);
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Aplicação inválida");
                    }

                    account.Metadata = metadataService.SaveValue(account.GetMetadata(_account.Code)).Cast <AccountMetadata>().ToList();

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();

                    if (ex.Message == "Necessário converter customer do tipo person para company")
                    {
                        convertCustomerToCompany = true;
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (convertCustomerToCompany && !_account.IsNull())
            {
                customerRepository.ChangeCustomerType(_account, CustomerType.Company);

                account = Save(account, clientId, currentAccount);

                return(account);
            }

            return(new DTO.Account(_account));
        }