/// <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; }); } }
/// <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); } }