public void Handle(CustomerGetDetailsCommand command) { InfoAccumulator info = new InfoAccumulator(); int customerId; try { customerId = CustomerIdEncryptor.DecryptCustomerId(command.CustomerId, command.CommandOriginator); } catch (Exception ex) { Log.Error(ex.Message); info.AddError("Invalid customer id."); SendReply(info, command); return; } Customer customer = CustomerQueries.GetCustomerById(customerId); IEnumerable <CustomerAddress> addresses = CustomerQueries.GetCustomerAddresses(customerId); IEnumerable <CustomerPhone> phones = CustomerQueries.GetCustomerPhones(customerId); var requesetedLoan = LoanQueries.GetCustomerRequestedLoan(customerId); SendReply(info, command, resp => { resp.PersonalDetails = GetPersonalDetails(customer); resp.ContactDetails = GetContactDetails(phones, customer); resp.CurrentLivingAddress = GetCurrentLivingAddress(addresses, customer); resp.PreviousLivingAddress = GetPreviousLivingAddress(addresses, customer); resp.AdditionalOwnedProperties = GetAdditionalOwnedProperties(addresses, customer) .ToArray(); resp.RequestedAmount = (decimal)requesetedLoan.Map(o => o.Amount.HasValue ? o.Amount.Value : 0); }); }
/// <summary> /// Handles the specified command. /// </summary> /// <param name="command">The command.</param> public void Handle(CompanyGetDetailsCommand command) { InfoAccumulator info = new InfoAccumulator(); int customerId, companyId; try { customerId = CustomerIdEncryptor.DecryptCustomerId(command.CustomerId, command.CommandOriginator); companyId = CompanyIdEncryptor.DecryptCompanyId(command.CompanyId, command.CommandOriginator); } catch (Exception ex) { Log.Error(ex.Message); SendReply(info, command, resp => { resp.CustomerId = command.CustomerId; resp.CompanyId = command.CompanyId; }); return; } var company = CompanyQueries.GetCompanyById(companyId) .Value; var directors = CompanyQueries.GetDirectors(customerId, companyId) .Value; var directorsAddresses = CompanyQueries.GetDirectorsAddresses(customerId, companyId) .Value; var companyEmployeeCount = CompanyQueries.GetCompanyEmployeeCount(customerId, companyId) .Value; var customer = CustomerQueries.GetCustomerPartiallyById(customerId, o => o.PersonalInfo.IndustryType, o => o.PersonalInfo.OverallTurnOver); SendReply(info, command, resp => { resp.CompanyDetails = ConvertToCompanyDetails(company, customer, companyEmployeeCount); resp.Authorities = GetAuthorities(directors, directorsAddresses); }); }
/// <summary> /// Handles the specified command. /// </summary> /// <param name="command">The command.</param> public void Handle(CustomerLoginCommand command) { InfoAccumulator info = new InfoAccumulator(); bool res = CustomerQueries.IsUserExists(command.EmailAddress, EncryptPassword(command.Password)); if (!res) { Log.ErrorFormat("Could not find user. EmailAddress: {0}, Password: {1}"); info.AddError("User does not exists."); SendReply(info, command); return; } Customer customer = CustomerQueries.GetCustomerPartiallyByProperty(where => where.Name, command.EmailAddress, select => select.Id); if (customer == null) { Log.ErrorFormat("We have a security user with email: {0}, but do not have a customer with this email"); info.AddError("Server error"); RegisterError(info, command); SendReply(info, command); return; } SendReply(info, command, resp => resp.CustomerId = CustomerIdEncryptor.EncryptCustomerId(customer.Id, command.CommandOriginator)); }
/// <summary> /// Handles the specified command. /// </summary> /// <param name="command">The command.</param> public void Handle(DocsUploadCommand command) { InfoAccumulator info = new InfoAccumulator(); int customerId; try { customerId = CustomerIdEncryptor.DecryptCustomerId(command.CustomerId, command.CommandOriginator); } catch (Exception ex) { Log.Error(ex.Message); info.AddError("Invalid customer id."); SendReply(info, command, resp => resp.CustomerId = command.CustomerId); return; } var fileMetaData = command.Files.Select(path => ConvertToFileMetadata(path, command.IsBankDocuments, customerId)); bool res = DocsUploadQueries.SaveCompanyUploadedFiles(fileMetaData); if (!res) { string error = string.Format("could not save some or all uploaded files for customer: {0}", command.CustomerId); info.AddError(error); RegisterError(info, command); throw new Exception("Failed to save some files");//we want to retry } SendReply(info, command, resp => resp.CustomerId = command.CustomerId); }
/// <summary> /// Gets the ids. /// </summary> /// <param name="command">The command.</param> /// <param name="customerId">The customer identifier.</param> /// <param name="info">The information.</param> /// <param name="companyId">The company identifier.</param> /// <returns></returns> private bool GetIds(UpdateCompanyCommand command, InfoAccumulator info, out int customerId, out int companyId) { DateTime date; try { customerId = CustomerIdEncryptor.DecryptCustomerId(command.CustomerId, command.RequestOrigin, out date); } catch (Exception ex) { Log.Error(ex.Message); info.AddError("Invalid customer id."); customerId = -1; companyId = -1; return(false); } try { companyId = CompanyIdEncryptor.DecryptCompanyId(command.CustomerId, command.RequestOrigin, out date); } catch (Exception ex) { Log.Error(ex.Message); info.AddError("Invalid company id."); customerId = -1; companyId = -1; return(false); } return(true); }
/// <summary> /// Handles a message. /// </summary> /// <param name="command">The command to handle.</param> /// <remarks> /// This method will be called when a message arrives on the bus and should contain /// the custom logic to execute when the command is received. /// </remarks> public void Handle(CustomerUpdateCommand command) { InfoAccumulator info = new InfoAccumulator(); if (!ValidateCommand(command, info)) { SendReply(info, command, response => response.CustomerId = command.CustomerId); return; } int customerId; try { customerId = CustomerIdEncryptor.DecryptCustomerId(command.CustomerId, command.CommandOriginator); } catch (Exception ex) { Log.Error(ex.Message); info.AddError("Invalid customer id."); SendReply(info, command, resp => resp.CustomerId = command.CustomerId); return; } //requests only PropertyStatusId from DB Customer customer = CustomerQueries.GetCustomerPartiallyById(customerId, o => o.PropertyStatusId); FillCustomerProperties(customer, command, info); if (info.HasErrors) { SendReply(info, command, resp => resp.CustomerId = command.CustomerId); return; } IEnumerable <CustomerAddress> addresses = FillCustomerPropertyStatusAndTimeAtAddressAndGetAddresses(command, customer, customerId); IEnumerable <CustomerPhone> phones = ExtractPhoneNumbers(command, customerId); string referenceSource = null; string visitTimes = null; if (command.Cookies != null) { referenceSource = command.Cookies.ReferenceSource; visitTimes = command.Cookies.VisitTimes; } var errors = CustomerProcessor.UpdateCustomer(customer, command.RequestedAmount, referenceSource, visitTimes, command.CampaignSourceRef, addresses, phones); if (errors.HasErrors) { if (errors.IsRetry) { RegisterError(info, command); } return; } SendReply(errors, command, response => response.CustomerId = command.CustomerId); }
/// <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); }
/// <summary> /// Gets the ids. /// </summary> /// <param name="command">The command.</param> /// <param name="info">The information.</param> /// <param name="customerId">The customer identifier.</param> /// <param name="companyId">The company identifier.</param> /// <param name="authorityId">The authority identifier.</param> /// <returns></returns> private bool GetIds(CompanyUpdateAuthorityCommand command, InfoAccumulator info, out int customerId, out int companyId, ref int authorityId) { try { customerId = CustomerIdEncryptor.DecryptCustomerId(command.CustomerId, command.CommandOriginator); companyId = CompanyIdEncryptor.DecryptCompanyId(command.CompanyId, command.CommandOriginator); if (command.AuhorityId.IsNotEmpty()) { authorityId = AuthorityIdEncryptor.DecryptAuthorityId(command.AuhorityId, command.CommandOriginator, command.Authority.IsDirector); } } catch (Exception ex) { Log.Error(ex.Message); info.AddError(ex.Message); customerId = -1; companyId = -1; return(false); } return(true); }
/// <summary> /// Handles the specified command. /// </summary> /// <param name="command">The command.</param> public async void Handle(AmazonRegisterCustomerCommand command) { InfoAccumulator info = new InfoAccumulator(); AmazonGetOrders3dPartyCommand request = new AmazonGetOrders3dPartyCommand { SellerId = command.SellerId, AuthorizationToken = command.AuthorizationToken, MarketplaceId = command.MarketplaceId, DateFrom = DateTime.UtcNow.AddYears(-1) //TODO }; var response = await GetOrdersSendReceive.SendAsync(Config.Address, request); int customerId; try { customerId = CustomerIdEncryptor.DecryptCustomerId(command.CustomerId, command.CommandOriginator); } catch (Exception ex) { Log.Error(ex.Message); info.AddError("Invalid request"); SendReply(info, command, resp => resp.CustomerId = command.CustomerId); return; } int customerMarketPlaceId = 0; //TODO int marketPlaceHistoryId = 0; //TODO AmazonOrder order = new AmazonOrder { Created = DateTime.UtcNow, CustomerMarketPlaceId = customerMarketPlaceId, CustomerMarketPlaceUpdatingHistoryRecordId = marketPlaceHistoryId }; int orderId = (int)AmazonOrdersQueries.SaveOrder(order); if (orderId < 1) { throw new Exception("could not save amazon order"); } bool res = AmazonOrdersQueries.SaveOrdersPayments(response.OrderPayments, orderId); if (!res) { throw new Exception("could not save order/order's payments"); } //Get top N order items IEnumerable <AmazonOrderItem> items = AmazonOrdersQueries.GetTopNOrderItems(customerMarketPlaceId); //Get order details of top N items AmazonGetOrdersDetails3PartyCommand detailsRequestCommand = new AmazonGetOrdersDetails3PartyCommand { SellerId = command.SellerId, AuthorizationToken = command.AuthorizationToken, OrdersIds = items.Where(o => o.AmazonOrderId.HasValue) .Select(o => o.AmazonOrderId.ToString()) }; AmazonGetOrdersDetails3PartyCommandResponse detailsResponse = await GetOrderDetailsSendReceive.SendAsync(Config.Address, detailsRequestCommand); var skuCollection = detailsResponse.OrderDetailsByOrderId.Values .SelectMany(o => o) .Select(o => o.SellerSKU); //Get categories of top N order items var getProductCategoriesCommand = new AmazonGetProductCategories3dPartyCommand { SellerId = command.SellerId, AuthorizationToken = command.AuthorizationToken, SellerSKUs = skuCollection }; var productCategoriesResponse = await GetProductCategoriesSendReceive.SendAsync(Config.Address, getProductCategoriesCommand); IDictionary <string, IList <AmazonOrderItemDetail> > orderIdToOrderDetails = detailsResponse.OrderDetailsByOrderId; //save order item details var insertedOrderDetails = AmazonOrdersQueries.OrderDetails.SaveOrderDetails(orderIdToOrderDetails.Values.SelectMany(o => o)); IDictionary <string, IEnumerable <AmazonProductCategory> > skuToCategory = productCategoriesResponse.CategoriesBySku; //upserts categories IDictionary <AmazonProductCategory, int> categoryToId = AmazonOrdersQueries.Categories.UpsertCategories(productCategoriesResponse.CategoriesBySku.Values.SelectMany(o => o)); res = AmazonOrdersQueries.Categories.SaveCategoryOrderDetailsMapping(CreateOrderIdToOrderDetailsIdMappings(insertedOrderDetails, skuToCategory, categoryToId)); if (!res) { throw new Exception("could not save category order details mapping"); } }