/// <summary>Map an address from a customer to a customer order address.</summary> /// <param name="customerToMapFrom">The customer to map from.</param> /// <param name="addressType">A string to process.</param> /// <returns>A string.</returns> private static CustomerOrderAddress MapAddressFromCustomer(Customer customerToMapFrom, string addressType) { CustomerOrderAddress orderAddress = new CustomerOrderAddress() { Company = customerToMapFrom.Company, FirstName = customerToMapFrom.FirstName, LastName = customerToMapFrom.LastName, Address1 = customerToMapFrom.Address, Address2 = customerToMapFrom.Address2, City = customerToMapFrom.City, State = customerToMapFrom.State, Country = customerToMapFrom.Country, Postcode = customerToMapFrom.Postcode, Phone = customerToMapFrom.PhoneNumber, Email = customerToMapFrom.EmailAddress, Type = addressType, }; return(orderAddress); }
/// <summary>Generates a billing address from a customer.</summary> /// <param name="customerToUse">The customer to use.</param> /// <returns>The billing address for the customer.</returns> public static CustomerOrderAddress GenerateBillingAddressFromCustomer(Customer customerToUse) { CustomerOrderAddress orderAddress = MapAddressFromCustomer(customerToUse, "BILL"); return(orderAddress); }
/// <summary>Generates a shipping address from a customer.</summary> /// <param name="customerToUse">The customer to use.</param> /// <returns>The shipping address for the customer.</returns> public static CustomerOrderAddress GenerateShippingAddressFromCustomer(Customer customerToUse) { CustomerOrderAddress orderAddress = MapAddressFromCustomer(customerToUse, "SHIP"); return(orderAddress); }
public async Task <CustomerOrderDto> CreateOrderAsync(CreateOrderParams orderParams) { var senderCustomer = await _customerRepository.GetCustomerByAccountId(orderParams.AccountId); var selectedPostalService = await _postalServiceRepository.GetPostalServiceByNameAsync(orderParams.PostalServiceName); var requestedProducts = await GetRequestedProducts(orderParams.Products); var deliveryAddress = await _locationService.GetAddressFromZip(orderParams.DeliveryZipCode); if (deliveryAddress == null) { throw new Exception("Zip code does not exist"); //todo make better error handling } if (requestedProducts.Count() != orderParams.Products.Length) { //Presentation should be handle this kind of situation not api itself throw new Exception("One or more products are does not exist or out of stock"); } var customerOrderItems = new List <CustomerOrderItem>(); foreach (var orderItem in orderParams.Products) { Product requestedProduct = requestedProducts.First(x => x.ProductId == orderItem.ProductId); customerOrderItems.Add(new CustomerOrderItem { Product = requestedProduct, ListPrice = requestedProduct.ListPrice, ProductDiscount = requestedProduct.Discount, Quantity = orderParams.Products.First(x => x.ProductId == orderItem.ProductId).Quantity, StoreId = orderItem.StoreId }); } var newOrderAddress = new CustomerOrderAddress() { CountryTag = deliveryAddress.CountryTag, Province = deliveryAddress.Province, District = deliveryAddress.District, Neighborhood = deliveryAddress.District, AddressTextPrimary = orderParams.DeliveryAddress, DestinationZipCode = orderParams.DeliveryZipCode }; var newOrder = new CustomerOrder { ReceiverFirst = orderParams.FirstName, ReceiverLast = orderParams.SecondName, PhoneNumber = orderParams.PhoneNumber, OptionalMail = orderParams.OptionalMail, PostalServiceId = selectedPostalService.ServiceId, CustomerId = senderCustomer.CustomerId, OrderStatus = OrderStatus.Preparing, OrderDate = DateTime.Now, RequiredDate = DateTime.Now.AddDays((double)selectedPostalService.AvgDeliveryDay), //TODO implement new system CargoPrice = selectedPostalService.Price, AdditionalNote = orderParams.AdditionalNote, CustomerOrderItem = customerOrderItems, CustomerOrderAddress = newOrderAddress }; bool createResult = await _orderRepository.CreateOrderAsync(newOrder); if (!createResult) { throw new Exception("Order couldn't created."); } var createdOrder = await _orderRepository.GetOrderAsync(newOrder.OrderId); return(_mapper.Map <CustomerOrderDto>(createdOrder)); }