public virtual async Task <CartViewModel> UpdateRecurringOrderCartBillingAddressAsync(UpdateRecurringOrderCartBillingAddressParam param)
        {
            if (!RecurringOrdersSettings.Enabled)
            {
                return(GetEmptyRecurringOrderCartViewModel());
            }

            if (param == null)
            {
                throw new ArgumentNullException(nameof(param), ArgumentNullMessageFormatter.FormatErrorMessage(nameof(param)));
            }
            if (param.BillingAddressId == null)
            {
                throw new ArgumentNullException(nameof(param), ArgumentNullMessageFormatter.FormatErrorMessage(nameof(param.BillingAddressId)));
            }

            var cart = await CartRepository.GetCartAsync(new GetCartParam
            {
                BaseUrl     = param.BaseUrl,
                Scope       = param.ScopeId,
                CultureInfo = param.CultureInfo,
                CustomerId  = param.CustomerId,
                CartName    = param.CartName
            }).ConfigureAwait(false);

            var shipment   = cart.Shipments.First();
            var newAddress = await AddressRepository.GetAddressByIdAsync(param.BillingAddressId);

            var payment = cart.Payments.First();

            if (payment == null)
            {
                throw new InvalidOperationException("No payment");
            }
            if (newAddress == null)
            {
                throw new InvalidOperationException("Address not found");
            }
            payment.BillingAddress   = newAddress;
            payment.BillingAddressId = newAddress.Id;

            if (param.UseSameForShippingAndBilling)
            {
                shipment.Address = newAddress;
            }

            var updatedCart = await CartRepository.UpdateCartAsync(UpdateCartParamFactory.Build(cart));

            var vm = await CreateCartViewModelAsync(new CreateRecurringOrderCartViewModelParam
            {
                Cart        = updatedCart,
                CultureInfo = param.CultureInfo,
                IncludeInvalidCouponsMessages = false,
                BaseUrl = param.BaseUrl
            });

            return(vm);
        }
Esempio n. 2
0
        protected virtual async Task UpdateRegisteredBillingAddress(Overture.ServiceModel.Orders.Cart cart, RegisteredBillingAddressViewModel registeredBillingAddressViewModel)
        {
            if (registeredBillingAddressViewModel == null)
            {
                return;
            }

            var payment = cart.Payments.FirstOrDefault();

            if (payment == null)
            {
                return;
            }

            if (registeredBillingAddressViewModel.UseShippingAddress)
            {
                var shipment = cart.Shipments.FirstOrDefault();

                if (shipment?.Address == null)
                {
                    return;
                }

                var isBillingChanged = payment.BillingAddress == null || !IsEqual(payment.BillingAddress, shipment.Address);

                if (isBillingChanged)
                {
                    payment.BillingAddress    = shipment.Address.Clone();
                    payment.BillingAddress.Id = Guid.Empty;
                }
            }
            else
            {
                if (registeredBillingAddressViewModel.BillingAddressId == Guid.Empty)
                {
                    return;
                }

                var newAddress = await AddressRepository.GetAddressByIdAsync(registeredBillingAddressViewModel.BillingAddressId).ConfigureAwait(false);

                if (newAddress == null)
                {
                    return;
                }

                var isBillingChanged = payment.BillingAddress == null || !IsEqual(payment.BillingAddress, newAddress);

                if (isBillingChanged)
                {
                    payment.BillingAddress = newAddress;
                    payment.BillingAddress.PropertyBag[AddressBookIdPropertyBagKey] = newAddress.Id;
                    payment.BillingAddress.Id = Guid.Empty;
                }
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> Update(int addressId, Address address)
        {
            try
            {
                var addressAux = (Address)await _repo.GetAddressByIdAsync(addressId);

                if (addressAux == null)
                {
                    return(NotFound());
                }

                addressAux.Street = address.Street;
                addressAux.Street = address.Street;
                addressAux.Street = address.Street;
                addressAux.Street = address.Street;
                addressAux.Street = address.Street;
                addressAux.Street = address.Street;
                addressAux.Street = address.Street;
                addressAux.Street = address.Street;

                _repo.Update(addressAux);

                if (await _repo.SaveChangesAsync())
                {
                    return(Ok(addressAux));
                }
            }
            catch (System.Exception ex)
            {
                return(this.StatusCode(
                           StatusCodes.Status500InternalServerError,
                           "Erro ao atualizar o endereço\n"
                           + ex.InnerException));
            }

            return(BadRequest());
        }
        protected virtual async Task UpdateAddressAsync(Guid customerId, Guid otherAddressId, Guid addressId)
        {
            if (otherAddressId == addressId)
            {
                return;
            }

            var otherAddress = await AddressRepository.GetAddressByIdAsync(otherAddressId).ConfigureAwait(false);

            if (otherAddress != null && otherAddress.IsPreferredShipping)
            {
                otherAddress.IsPreferredShipping = false;
                await CustomerAddressRepository.UpdateAddressAsync(customerId, otherAddress).ConfigureAwait(false);
            }
        }
        /// <summary>
        /// Set default Address for the given customer
        /// </summary>
        /// <param name="param">Service call params <see cref="SetDefaultAddressParam"/></param>
        /// <returns>
        /// The status representing a possible cause of errors.
        /// </returns>
        public virtual async Task <SetDefaultAddressStatusViewModel> SetDefaultAddressAsync(SetDefaultAddressParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (param.CustomerId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.CustomerId)), nameof(param));
            }
            if (param.AddressId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.AddressId)), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.Scope))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.Scope)), nameof(param));
            }

            var address = await AddressRepository.GetAddressByIdAsync(param.AddressId).ConfigureAwait(false);

            if (address == null)
            {
                return(null);
            }

            if (!await EnsureAddressBelongsToCustomer(param.CustomerId, param.Scope, address.Id).ConfigureAwait(false))
            {
                return(null);
            }

            address.IsPreferredShipping = true;
            address.IsPreferredBilling  = true;

            await CustomerAddressRepository.UpdateAddressAsync(param.CustomerId, address).ConfigureAwait(false);

            return(new SetDefaultAddressStatusViewModel());
        }
        protected virtual async Task UpdateRegisteredShippingAddress(Overture.ServiceModel.Orders.Cart cart, RegisteredShippingAddressViewModel registeredShippingAddressViewModel)
        {
            if (registeredShippingAddressViewModel == null)
            {
                return;
            }

            if (registeredShippingAddressViewModel.ShippingAddressId == Guid.Empty)
            {
                return;
            }

            var shipment = cart.Shipments.FirstOrDefault();

            if (shipment == null)
            {
                return;
            }

            var newAddress = await AddressRepository.GetAddressByIdAsync(registeredShippingAddressViewModel.ShippingAddressId).ConfigureAwait(false);

            var isShippingChanged = shipment.Address == null || !IsEqual(shipment.Address, newAddress);

            if (isShippingChanged)
            {
                shipment.Address = newAddress;
                shipment.Address.PropertyBag[AddressBookIdPropertyBagKey] = newAddress.Id;
                shipment.Address.Id = Guid.Empty;
            }

            //In the case the user didn't do estimate shipping before
            await ShippingMethodViewService.EstimateShippingAsync(new EstimateShippingParam
            {
                Cart        = cart,
                CultureInfo = CultureInfo.GetCultureInfo(cart.CultureName), //TODO: Fix me
                ForceUpdate = isShippingChanged
            }).ConfigureAwait(false);
        }
        /// <summary>
        /// Gets the <see cref="EditAddressViewModel"/> for the edition of a specified address of a specified customer id.
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public virtual async Task <EditAddressViewModel> GetEditAddressViewModelAsync(GetEditAddressViewModelAsyncParam param)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (param.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(param.CultureInfo)), nameof(param));
            }
            if (param.CustomerId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.CustomerId)), nameof(param));
            }
            if (param.AddressId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(param.AddressId)), nameof(param));
            }
            if (string.IsNullOrWhiteSpace(param.Scope))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(param.Scope)), nameof(param));
            }

            if (!await EnsureAddressBelongsToCustomer(param.CustomerId, param.Scope, param.AddressId).ConfigureAwait(false))
            {
                return(null);
            }

            var address = await AddressRepository.GetAddressByIdAsync(param.AddressId);

            return(await GetEditAddressViewModel(new GetEditAddressViewModelParam
            {
                Address = address,
                IsUpdating = true,
                CultureInfo = param.CultureInfo
            }).ConfigureAwait(false));
        }
        /// <summary>
        /// Update an Address for the given customer
        /// </summary>
        /// <param name="editAddressParam">Service call params <see cref="EditAddressParam"/></param>
        /// <returns>
        /// The created Address and a status representing a possible cause of errors.
        /// </returns>
        public virtual async Task <EditAddressViewModel> UpdateAddressAsync(EditAddressParam editAddressParam)
        {
            if (editAddressParam == null)
            {
                throw new ArgumentNullException(nameof(editAddressParam));
            }
            if (editAddressParam.EditAddress == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(editAddressParam.EditAddress)), nameof(editAddressParam));
            }
            if (editAddressParam.AddressId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(editAddressParam.AddressId)), nameof(editAddressParam));
            }
            if (editAddressParam.CustomerId == Guid.Empty)
            {
                throw new ArgumentException(GetMessageOfEmpty(nameof(editAddressParam.CustomerId)), nameof(editAddressParam));
            }
            if (editAddressParam.CultureInfo == null)
            {
                throw new ArgumentException(GetMessageOfNull(nameof(editAddressParam.CultureInfo)), nameof(editAddressParam));
            }
            if (string.IsNullOrWhiteSpace(editAddressParam.Scope))
            {
                throw new ArgumentException(GetMessageOfNullWhiteSpace(nameof(editAddressParam.Scope)), nameof(editAddressParam));
            }

            var baseAddress = await AddressRepository.GetAddressByIdAsync(editAddressParam.AddressId).ConfigureAwait(false);

            if (baseAddress == null)
            {
                return(null);
            }

            if (!await EnsureAddressBelongsToCustomer(editAddressParam.CustomerId, editAddressParam.Scope, baseAddress.Id).ConfigureAwait(false))
            {
                return(null);
            }

            var updatedAddress = await CustomerAddressRepository
                                 .UpdateAddressAsync(editAddressParam.CustomerId,
                                                     ConvertToAddress(baseAddress, editAddressParam.EditAddress))
                                 .ConfigureAwait(false);

            if (editAddressParam.EditAddress.IsPreferredShipping || editAddressParam.EditAddress.IsPreferredBilling)
            {
                await SetDefaultAddressAsync(new SetDefaultAddressParam
                {
                    AddressId  = editAddressParam.AddressId,
                    CustomerId = editAddressParam.CustomerId,
                    Scope      = editAddressParam.Scope
                }).ConfigureAwait(false);
            }

            //Update recurring carts with this address Id
            await RecurringOrderCartsViewService.UpdateRecurringOrderCartsAddressesAsync(new Cart.Parameters.UpdateRecurringOrderCartsAddressesParam
            {
                BaseUrl     = "Empty", //Dont need but can't allow empty
                CultureInfo = editAddressParam.CultureInfo,
                CustomerId  = editAddressParam.CustomerId,
                ScopeId     = editAddressParam.Scope,
                AddressId   = updatedAddress.Id,
            }).ConfigureAwait(false);

            return(await GetEditAddressViewModel(new GetEditAddressViewModelParam
            {
                Address = updatedAddress,
                Status = MyAccountStatus.Success,
                ReturnUrl = editAddressParam.ReturnUrl,
                IsUpdating = false,
                CultureInfo = editAddressParam.CultureInfo
            }).ConfigureAwait(false));
        }
        //TODO: rename to MapShippingAddressAsync if used
        protected virtual async Task <RecurringOrderTemplateAddressViewModel> MapShippingAddress(Guid shippingAddressId, CultureInfo culture)
        {
            var address = await AddressRepository.GetAddressByIdAsync(shippingAddressId).ConfigureAwait(false);

            return(GetAddressViewModel(address, culture));
        }