Skip to content

Commit

Permalink
feat: add makeAddressDefault mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev committed Aug 22, 2024
1 parent 021267b commit f349644
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -36,13 +37,30 @@ public virtual MemberAggregateRootBase DeleteAddresses(IList<Address> addresses)
{
var addressToDelete = Member.Addresses.FirstOrDefault(x => x.Key == address.Key);

if (addressToDelete != null)
if (addressToDelete != null && addressToDelete.AddressType != BillingAndShipping)
{
Member.Addresses.Remove(addressToDelete);
}
}

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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using GraphQL.Types;
using VirtoCommerce.Xapi.Core.Infrastructure;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Commands;

public class MakeAddressDefaultCommand : ICommand<bool>
{
public string UserId { get; set; }
public string MemberId { get; set; }
public string AddressId { get; set; }
}

public class MakeAddressDefaultCommandType : InputObjectGraphType<MakeAddressDefaultCommand>
{
public MakeAddressDefaultCommandType()
{
Field(x => x.AddressId);
Field(x => x.MemberId);
}
}
Original file line number Diff line number Diff line change
@@ -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<MakeAddressDefaultCommand, bool, MakeAddressDefaultCommandType, BooleanGraphType>
{
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<object> context)
{
var request = base.GetRequest(context);
request.UserId = context.GetCurrentUserId();
return request;
}

protected override async Task BeforeMediatorSend(IResolveFieldContext<object> context, MakeAddressDefaultCommand request)
{
await base.BeforeMediatorSend(context, request);
await _profileAuthorizationService.CheckAuthAsync(context, request);
}
}
Original file line number Diff line number Diff line change
@@ -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<MakeAddressDefaultCommand, bool>
{
private readonly IMemberAggregateRootRepository _memberAggregateRepository;

public MakeAddressDefaultCommandHandler(IMemberAggregateRootRepository memberAggregateRepository)
{
_memberAggregateRepository = memberAggregateRepository;
}

public async Task<bool> Handle(MakeAddressDefaultCommand request, CancellationToken cancellationToken)
{
var memberAggregate = await _memberAggregateRepository.GetMemberAggregateRootByIdAsync<MemberAggregateRootBase>(request.MemberId);
memberAggregate.MakeAddressDefault(request.AddressId);
await _memberAggregateRepository.SaveAsync(memberAggregate);

return true;
}
}

0 comments on commit f349644

Please sign in to comment.