示例#1
0
        /// <summary>
        /// Handles the CustomerSignupCommand.
        /// </summary>
        /// <param name="command">The command.</param>
        public void Handle(CustomerSignupCommand command)
        {
            InfoAccumulator info = ValidateSignupCommand(command);

            if (info.HasErrors)
            {
                SendReply(info, command);
                return;
            }

            //TODO: code below contains race condition. We should find a way to validate to do it properly

            if (!BrokerQueries.IsExistsBroker(command.EmailAddress))
            {
                Log.ErrorFormat("Attempt to sign in customer with email of broker: {0}", command.EmailAddress);
                info.AddError("Sign-up error");
                SendReply(info, command);
                return;
            }

            SecurityUser user;

            try {
                user = CustomerQueries.CreateSecurityUser(command.EmailAddress, command.Password, command.SequrityQuestionId ?? 0, command.SecurityQuestionAnswer, command.CommandOriginatorIP);
            } catch (SqlException ex) {
                Log.ErrorFormat("Attempt to sign in existing customer: {0}", command.EmailAddress);
                info.AddError("Sign-up error");
                SendReply(info, command);
                return;
            }



            Customer customer = ConvertToCustomer(command, user);
            int      id       = (int)CustomerQueries.UpsertCustomer(customer);

            if (id < 1)
            {
                Log.ErrorFormat("could not create customer of user: "******"could not create customer");
            }

            string encryptedCustomerId = CustomerIdEncryptor.EncryptCustomerId(customer.Id, command.CommandOriginator);

            SendReply(info, command, response => response.CustomerId = encryptedCustomerId);
        }
示例#2
0
        /// <summary>
        /// Handles a message.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <remarks>
        /// This method will be called when a message arrives on the bus and should contain
        /// the custom logic to execute when the message is received.
        /// </remarks>
        public void Handle(UpdateCompanyCommand command)
        {
            InfoAccumulator info = new InfoAccumulator();

            int customerId, companyId;

            if (!GetIds(command, info, out customerId, out companyId))
            {
                SendReply(info, command);
                return;
            }

            using (UnitOfWork unitOfWork = new UnitOfWork()) {
                if (!this.ValidateCommand(command, info) &&
                    !base.ValidateModel(command.CompanyDetails, info) &&
                    !base.ValidateModel(command.CompanyDetails.Authorities, info) &&
                    !command.CompanyDetails.Authorities.All(o => base.ValidateModel(o, info)))
                {
                    SendReply(info, command);
                    return;
                }

                Company company = this.CreateCompany(command);
                if (StringUtils.IsNotEmpty(command.CompanyId))
                {
                    company.Id = companyId;
                }

                companyId = (int)CompanyQueries.UpsertCompany(company);
                if (companyId < 1)
                {
                    info.AddError("Some DB error");
                    RegisterError(info, command);
                    return;
                }

                int countId = (int)CompanyQueries.UpsertCompanyEmployeeCount(new CompanyEmployeeCount {
                    Created       = DateTime.UtcNow,
                    CustomerId    = customerId,
                    CompanyId     = companyId,
                    EmployeeCount = command.CompanyDetails.NumberOfEmployees
                });

                if (countId < 1)
                {
                    string error = string.Format("Could not save CompanyEmployeeCount. CustomerId: {0}, CompanyId {1}", command.CustomerId, command.CompanyId);
                    info.AddError(error);
                    RegisterError(info, command);
                    return;
                }

                Customer customer = this.CreateCustomer(command);
                customer.Id        = customerId;
                customer.CompanyId = companyId;

                var customerIdFromQuery = CustomerQueries.UpsertCustomer(customer);
                if (!customerIdFromQuery.HasValue)
                {
                    info.AddError("Some DB error");
                    RegisterError(info, command);
                    return;
                }

                if (command.ExperianCompanyInfo != null)
                {
                    CustomerAddress address = this.CreateExperianAddress(command, customerId);
                    var             res     = CustomerQueries.UpsertCustomerAddress(address);
                    if (!res.HasValue || !res.Value)
                    {
                        info.AddError("could not save customer address");
                        RegisterError(info, command);
                    }
                }

                unitOfWork.Commit();

                SendReply(info, command, response => {
                    response.CustomerId = command.CustomerId;
                    response.CompanyId  = command.CompanyId;
                });
            }
        }
示例#3
0
        /// <summary>
        /// Updates customer.
        /// </summary>
        /// <param name="customer">The customer.</param>
        /// <param name="requestedAmount">The requested amount.</param>
        /// <param name="sourceRefList">The source reference list.</param>
        /// <param name="visitTimeList">The visit time list.</param>
        /// <param name="campaignSrcRef">The campaign source reference.</param>
        /// <param name="addresses">The addresses.</param>
        /// <param name="phoneNumbers">The phone numbers.</param>
        /// <returns></returns>
        public InfoAccumulator UpdateCustomer(Customer customer, decimal requestedAmount, string sourceRefList, string visitTimeList,
                                              CampaignSourceRef campaignSrcRef, IEnumerable <CustomerAddress> addresses, IEnumerable <CustomerPhone> phoneNumbers)
        {
            InfoAccumulator info = new InfoAccumulator();

            using (var unitOfWork = new UnitOfWork()) {
                DateTime now = DateTime.UtcNow;

                int customerId = (int)CustomerQueries.UpsertCustomer(customer);
                if (customerId < 1)
                {
                    info.AddError("could not save customer");
                    return(info);
                }

                bool isSuccess = true;

                if (requestedAmount > 0)
                {
                    CustomerRequestedLoan requestedLoan = new CustomerRequestedLoan {
                        CustomerId = customerId,
                        Amount     = requestedAmount,
                        Created    = DateTime.UtcNow
                    };

                    int id = (int)LoanQueries.UpsertCustomerRequestedLoan(requestedLoan);
                    if (id < 1)
                    {
                        info.AddError("could not save requested loan");
                        return(info);
                    }
                }


//                var session = new CustomerSession() {
//                    CustomerId = customer.Id,
//                    StartSession = now,
//                    Ip = customer.LoginInfo != null ? customer.LoginInfo.RemoteIp ?? "unknown" : "unknown",//TODO: review
//                    IsPasswdOk = true,
//                    ErrorMessage = "Registration" //TODO: do something with this
//                };
//
//                isSuccess = CustomerQueries.SaveCustomerSession(session) ?? true;
//                if (!isSuccess) {
//                    info.AddError("could not save customer session");
//                    return info;
//                }

                if (sourceRefList != null && visitTimeList != null)
                {
                    isSuccess = CustomerQueries.SaveSourceRefHistory(customer.Id, sourceRefList, visitTimeList) ?? true;
                    if (!isSuccess)
                    {
                        info.AddError("could not save customer ref history");
                    }
                }

                if (campaignSrcRef != null)
                {
                    isSuccess = CustomerQueries.SaveCampaignSourceRef(customer.Id, campaignSrcRef);
                    if (!isSuccess)
                    {
                        info.AddError("could not save campaign source ref for customer: " + customer.Id);
                        return(info);
                    }
                }

                if (customer.AlibabaId != null && customer.IsAlibaba)
                {
                    AlibabaBuyer alibabaBuyer = new AlibabaBuyer {
                        AliId      = Convert.ToInt64(customer.AlibabaId),
                        CustomerId = customer.Id
                    };

                    isSuccess = AlibabaQueries.CreateAlibabaBuyer(alibabaBuyer) ?? true;
                    if (!isSuccess)
                    {
                        info.AddError("could not create alibaba buyer");
                    }
                }

                if (customer.CustomerAddress != null)
                {
                    customer.CustomerAddress.CustomerId = customer.Id;
                    isSuccess = CustomerQueries.UpsertCustomerAddress(customer.CustomerAddress) ?? true;
                    if (!isSuccess)
                    {
                        info.AddError("could not save customer address");
                    }
                }

                if (CollectionUtils.IsNotEmpty(addresses))
                {
                    foreach (CustomerAddress customerAddress in addresses)
                    {
                        bool?res = CustomerQueries.UpsertCustomerAddress(customerAddress);
                        if (res == null || res.Value == false)
                        {
                            info.AddError("could not save customer address");
                            return(info);
                        }
                    }
                }

                if (CollectionUtils.IsNotEmpty(phoneNumbers))
                {
                    foreach (CustomerPhone phone in phoneNumbers)
                    {
                        bool saveCustomerPhone = CustomerQueries.SaveCustomerPhone(phone);
                        if (!saveCustomerPhone)
                        {
                            info.AddError("could not save customer phone");
                            return(info);
                        }
                    }
                }

                unitOfWork.Commit();
                return(info);
            }
        }