From f349644065f484b4e8b45a1b723a6ecfe3e46b84 Mon Sep 17 00:00:00 2001 From: Konstantin Savosteev Date: Thu, 22 Aug 2024 15:44:56 +0200 Subject: [PATCH] feat: add makeAddressDefault mutation --- .../Aggregates/MemberAggregateRootBase.cs | 20 +++++++++- .../Commands/MakeAddressDefaultCommand.cs | 20 ++++++++++ .../MakeAddressDefaultCommandBuilder.cs | 39 +++++++++++++++++++ .../MakeAddressDefaultCommandHandler.cs | 25 ++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommand.cs create mode 100644 src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommandBuilder.cs create mode 100644 src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommandHandler.cs diff --git a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Aggregates/MemberAggregateRootBase.cs b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Aggregates/MemberAggregateRootBase.cs index df278b18..00869c92 100644 --- a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Aggregates/MemberAggregateRootBase.cs +++ b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Aggregates/MemberAggregateRootBase.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using VirtoCommerce.CustomerModule.Core.Model; +using static VirtoCommerce.CoreModule.Core.Common.AddressType; namespace VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates { @@ -36,7 +37,7 @@ public virtual MemberAggregateRootBase DeleteAddresses(IList
addresses) { var addressToDelete = Member.Addresses.FirstOrDefault(x => x.Key == address.Key); - if (addressToDelete != null) + if (addressToDelete != null && addressToDelete.AddressType != BillingAndShipping) { Member.Addresses.Remove(addressToDelete); } @@ -44,5 +45,22 @@ public virtual MemberAggregateRootBase DeleteAddresses(IList
addresses) return this; } + + public virtual MemberAggregateRootBase MakeAddressDefault(string addressKey) + { + var addressToDefault = Member.Addresses.FirstOrDefault(x => x.Key == addressKey); + + if (addressToDefault != null) + { + foreach (var address in Member.Addresses.Where(x => x.AddressType == addressToDefault.AddressType)) + { + address.IsDefault = false; + } + + addressToDefault.IsDefault = true; + } + + return this; + } } } diff --git a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommand.cs b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommand.cs new file mode 100644 index 00000000..f975de21 --- /dev/null +++ b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommand.cs @@ -0,0 +1,20 @@ +using GraphQL.Types; +using VirtoCommerce.Xapi.Core.Infrastructure; + +namespace VirtoCommerce.ProfileExperienceApiModule.Data.Commands; + +public class MakeAddressDefaultCommand : ICommand +{ + public string UserId { get; set; } + public string MemberId { get; set; } + public string AddressId { get; set; } +} + +public class MakeAddressDefaultCommandType : InputObjectGraphType +{ + public MakeAddressDefaultCommandType() + { + Field(x => x.AddressId); + Field(x => x.MemberId); + } +} diff --git a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommandBuilder.cs b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommandBuilder.cs new file mode 100644 index 00000000..9ab3a2c0 --- /dev/null +++ b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommandBuilder.cs @@ -0,0 +1,39 @@ +using System.Threading.Tasks; +using GraphQL; +using GraphQL.Types; +using MediatR; +using Microsoft.AspNetCore.Authorization; +using VirtoCommerce.ProfileExperienceApiModule.Data.Services; +using VirtoCommerce.Xapi.Core.BaseQueries; +using VirtoCommerce.Xapi.Core.Extensions; + +namespace VirtoCommerce.ProfileExperienceApiModule.Data.Commands; + +public class MakeAddressDefaultCommandBuilder : CommandBuilder +{ + protected override string Name => "makeAddressDefault"; + + private readonly IProfileAuthorizationService _profileAuthorizationService; + + public MakeAddressDefaultCommandBuilder( + IMediator mediator, + IAuthorizationService authorizationService, + IProfileAuthorizationService profileAuthorizationService) + : base(mediator, authorizationService) + { + _profileAuthorizationService = profileAuthorizationService; + } + + protected override MakeAddressDefaultCommand GetRequest(IResolveFieldContext context) + { + var request = base.GetRequest(context); + request.UserId = context.GetCurrentUserId(); + return request; + } + + protected override async Task BeforeMediatorSend(IResolveFieldContext context, MakeAddressDefaultCommand request) + { + await base.BeforeMediatorSend(context, request); + await _profileAuthorizationService.CheckAuthAsync(context, request); + } +} diff --git a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommandHandler.cs b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommandHandler.cs new file mode 100644 index 00000000..f06203e6 --- /dev/null +++ b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommandHandler.cs @@ -0,0 +1,25 @@ +using System.Threading; +using System.Threading.Tasks; +using MediatR; +using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates; + +namespace VirtoCommerce.ProfileExperienceApiModule.Data.Commands; + +public class MakeAddressDefaultCommandHandler : IRequestHandler +{ + private readonly IMemberAggregateRootRepository _memberAggregateRepository; + + public MakeAddressDefaultCommandHandler(IMemberAggregateRootRepository memberAggregateRepository) + { + _memberAggregateRepository = memberAggregateRepository; + } + + public async Task Handle(MakeAddressDefaultCommand request, CancellationToken cancellationToken) + { + var memberAggregate = await _memberAggregateRepository.GetMemberAggregateRootByIdAsync(request.MemberId); + memberAggregate.MakeAddressDefault(request.AddressId); + await _memberAggregateRepository.SaveAsync(memberAggregate); + + return true; + } +}