Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

2.1 KB
Raw Blame
using FluentValidation;
using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Interfaces.Common.Addresses;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Domain.Common;
using Grand.Infrastructure.Validators;
using Grand.Web.AdminShared.Extensions;
using Grand.Web.AdminShared.Models.Orders;

namespace Grand.Web.AdminShared.Validators.Orders;

public class OrderAddressModelValidator : BaseGrandValidator<OrderAddressModel>
{
    public OrderAddressModelValidator(
        IEnumerable<IValidatorConsumer<OrderAddressModel>> validators,
        IOrderService orderService,
        IAddressAttributeService addressAttributeService,
        IAddressAttributeParser addressAttributeParser,
        ITranslationService translationService)
        : base(validators)
    {
        RuleFor(x => x).CustomAsync(async (x, context, _) =>
        {
            var customAttributes =
                await x.Address.ParseCustomAddressAttributes(addressAttributeParser, addressAttributeService);
            var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);
            foreach (var error in customAttributeWarnings) context.AddFailure(error);
            var order = await orderService.GetOrderById(x.OrderId);
            if (order != null)
            {
                Address address = null;
                switch (x.BillingAddress)
                {
                    case true when order.BillingAddress != null:
                    {
                        if (order.BillingAddress.Id == x.Address.Id)
                            address = order.BillingAddress;
                        break;
                    }
                    case false when order.ShippingAddress != null:
                    {
                        if (order.ShippingAddress.Id == x.Address.Id)
                            address = order.ShippingAddress;
                        break;
                    }
                }

                if (address == null)
                    context.AddFailure("No address found with the specified id");
            }
        });
    }
}