Skip to content

Commit

Permalink
Merge pull request #51 from IliyanIlievPH/50
Browse files Browse the repository at this point in the history
Closes #50
  • Loading branch information
starkmsu committed Jun 1, 2020
2 parents 0e3e4fa + c38e667 commit 877b2ef
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface ILocationService

Task<Location> GetByExternalIdAsync(string externalId);

Task<IReadOnlyCollection<Location>> CreateLocationsContactPersonForPartnerAsync(Partner partner);
Task CreateLocationsContactPersonForPartnerAsync(Partner partner);

Task<IReadOnlyCollection<Location>> UpdateRangeAsync(Partner partner,
IReadOnlyCollection<Location> locations, IReadOnlyCollection<Location> existingLocations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Geohash;
using Lykke.Common.Log;
using MAVN.Service.CustomerProfile.Client;
using MAVN.Service.CustomerProfile.Client.Models.Enums;
using MAVN.Service.CustomerProfile.Client.Models.Requests;
using MAVN.Service.PartnerManagement.Domain.Exceptions;
using MAVN.Service.PartnerManagement.Domain.Models;
Expand Down Expand Up @@ -47,10 +46,9 @@ public Task<Location> GetByExternalIdAsync(string externalId)
return _locationRepository.GetByExternalIdAsync(externalId);
}

public async Task<IReadOnlyCollection<Location>> CreateLocationsContactPersonForPartnerAsync(Partner partner)
public async Task CreateLocationsContactPersonForPartnerAsync(Partner partner)
{
var customerProfileCreateActions =
new List<Task<(PartnerContactErrorCodes ErrorCode, Location Location)>>();
var customerProfileCreateActions = new List<Task>();

if (await _locationRepository.AreExternalIdsNotUniqueAsync(partner.Id,
partner.Locations.Select(l => l.ExternalId)))
Expand All @@ -68,20 +66,11 @@ public async Task<IReadOnlyCollection<Location>> CreateLocationsContactPersonFor

_log.Info("Location creating", context: $"location: {location.ToJson()}");

customerProfileCreateActions.Add(CreatePartnerContact(location));
if(location.ContactPerson != null && !string.IsNullOrEmpty(location.ContactPerson.Email))
customerProfileCreateActions.Add(CreateOrUpdatePartnerContact(location));
}

var createResult = await Task.WhenAll(customerProfileCreateActions);

if (createResult.Any(r => r.ErrorCode != PartnerContactErrorCodes.None))
{
var exception =
new LocationContactRegistrationFailedException("Creating the contact person data failed.");
_log.Error(exception, context: createResult);
throw exception;
}

return createResult.Select(l => l.Location).ToList();
await Task.WhenAll(customerProfileCreateActions);
}

public async Task<IReadOnlyCollection<Location>> UpdateRangeAsync(Partner partner,
Expand All @@ -105,11 +94,9 @@ public async Task<IReadOnlyCollection<Location>> CreateLocationsContactPersonFor
updatedLocations.Add(location);
}

var repositoryActions = new List<Task>();
var customerProfileUpdateActions =
new List<Task<(PartnerContactErrorCodes ErrorCode, Location Location)>>();
var customerProfileCreateActions =
new List<Task<(PartnerContactErrorCodes ErrorCode, Location Location)>>();
var deleteActions = new List<Task>();
var customerProfileUpdateActions = new List<Task>();
var customerProfileCreateActions = new List<Task>();

// TODO: Add transaction
if (deletedLocations.Any())
Expand All @@ -118,7 +105,7 @@ public async Task<IReadOnlyCollection<Location>> CreateLocationsContactPersonFor
{
_log.Info("Location deleting", context: $"location: {location.ToJson()}");
repositoryActions.Add(_customerProfileClient.PartnerContact.DeleteAsync(location.Id.ToString()));
deleteActions.Add(_customerProfileClient.PartnerContact.DeleteIfExistAsync(location.Id.ToString()));
});
}

Expand All @@ -133,8 +120,10 @@ public async Task<IReadOnlyCollection<Location>> CreateLocationsContactPersonFor
await SetCountryIso3Code(location);

_log.Info("Location updating", context: $"location: {location.ToJson()}");

customerProfileUpdateActions.Add(UpdatePartnerContact(location));
if (location.ContactPerson != null && !string.IsNullOrEmpty(location.ContactPerson.Email))
customerProfileUpdateActions.Add(CreateOrUpdatePartnerContact(location));
else
deleteActions.Add(_customerProfileClient.PartnerContact.DeleteIfExistAsync(location.Id.ToString()));
}
}

Expand All @@ -149,35 +138,19 @@ public async Task<IReadOnlyCollection<Location>> CreateLocationsContactPersonFor

_log.Info("Location creating", context: $"location: {location.ToJson()}");

customerProfileCreateActions.Add(CreatePartnerContact(location));
if (location.ContactPerson != null && !string.IsNullOrEmpty(location.ContactPerson.Email))
customerProfileCreateActions.Add(CreateOrUpdatePartnerContact(location));
}
}

var updateResult = await Task.WhenAll(customerProfileUpdateActions);

if (updateResult.Any(r => r.Item1 != PartnerContactErrorCodes.None))
{
var exception = new LocationContactUpdateFailedException("Updating the contact person data failed.");
_log.Error(exception, context: updateResult);
throw exception;
}

var createResult = await Task.WhenAll(customerProfileCreateActions);

if (createResult.Any(r => r.Item1 != PartnerContactErrorCodes.None))
{
var exception =
new LocationContactRegistrationFailedException("Creating the Contact person data failed.");
_log.Error(exception, context: createResult);
throw exception;
}

await Task.WhenAll(repositoryActions);
await Task.WhenAll(customerProfileUpdateActions);
await Task.WhenAll(customerProfileCreateActions);
await Task.WhenAll(deleteActions);

var processedLocations = new List<Location>();

processedLocations.AddRange(updateResult.Select(r => r.Location));
processedLocations.AddRange(createResult.Select(r => r.Location));
processedLocations.AddRange(createdLocations);
processedLocations.AddRange(updatedLocations);

return processedLocations;
}
Expand All @@ -201,23 +174,9 @@ private bool IsCoordinatesDetermined(Location location)
return location?.Latitude != null && location?.Longitude != null;
}

private async Task<(PartnerContactErrorCodes, Location)> UpdatePartnerContact(Location location)
private async Task CreateOrUpdatePartnerContact(Location location)
{
var result = await _customerProfileClient.PartnerContact.UpdateAsync(new PartnerContactUpdateRequestModel
{
LocationId = location.Id.ToString(),
FirstName = location.ContactPerson.FirstName,
LastName = location.ContactPerson.LastName,
Email = location.ContactPerson.Email.ToLower(),
PhoneNumber = location.ContactPerson.PhoneNumber
});

return (result, location);
}

private async Task<(PartnerContactErrorCodes, Location)> CreatePartnerContact(Location location)
{
var result = await _customerProfileClient.PartnerContact.CreateIfNotExistAsync(
await _customerProfileClient.PartnerContact.CreateOrUpdateAsync(
new PartnerContactRequestModel
{
LocationId = location.Id.ToString(),
Expand All @@ -226,8 +185,6 @@ private async Task<(PartnerContactErrorCodes, Location)> CreatePartnerContact(Lo
Email = location.ContactPerson.Email.ToLower(),
PhoneNumber = location.ContactPerson.PhoneNumber
});

return (result, location);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="geohash-dotnet" Version="1.0.4" />
<PackageReference Include="Lykke.RabbitMqBroker" Version="7.13.2" />
<PackageReference Include="MAVN.Service.Credentials.Client" Version="1.0.1" />
<PackageReference Include="MAVN.Service.CustomerProfile.Client" Version="1.2.0" />
<PackageReference Include="MAVN.Service.PaymentManagement.Client" Version="1.13.4" />
<PackageReference Include="MAVN.Service.Credentials.Client" Version="1.1.0" />
<PackageReference Include="MAVN.Service.CustomerProfile.Client" Version="2.0.0" />
<PackageReference Include="MAVN.Service.PaymentManagement.Client" Version="1.18.0" />
<PackageReference Include="MAVN.Service.Sessions.Client" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task<Guid> CreateAsync(Partner partner)
{
partner.Id = Guid.NewGuid();

partner.Locations = await _locationService.CreateLocationsContactPersonForPartnerAsync(partner);
await _locationService.CreateLocationsContactPersonForPartnerAsync(partner);

if (partner.UseGlobalCurrencyRate)
{
Expand Down Expand Up @@ -316,12 +316,12 @@ private async Task<Partner> EnrichPartner(Partner partner)
private async Task GetContactPerson(Location location)
{
var partnerContact = await _customerProfileClient.PartnerContact.GetByLocationIdAsync(location.Id.ToString());
location.ContactPerson = _mapper.Map<ContactPerson>(partnerContact);
location.ContactPerson = _mapper.Map<ContactPerson>(partnerContact.PartnerContact);
}

private async Task DeleteContactPerson(Location location)
{
await _customerProfileClient.PartnerContact.DeleteAsync(location.Id.ToString());
await _customerProfileClient.PartnerContact.DeleteIfExistAsync(location.Id.ToString());
location.ContactPerson = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
using FluentValidation;
using MAVN.Service.PartnerManagement.Client.Models;
using MAVN.Service.PartnerManagement.Client.Models.Partner;
Expand All @@ -13,6 +13,7 @@ public ContactPersonModelValidation()
RuleFor(p => p.FirstName)
.NotNull()
.NotEmpty()
.When(x => !string.IsNullOrEmpty(x.Email) || !string.IsNullOrEmpty(x.LastName) || !string.IsNullOrEmpty(x.PhoneNumber))
.MinimumLength(2)
.MaximumLength(50)
.WithMessage("The First Name should be present and within a range of 2 to 50 characters long.")
Expand All @@ -22,6 +23,7 @@ public ContactPersonModelValidation()
RuleFor(p => p.LastName)
.NotNull()
.NotEmpty()
.When(x => !string.IsNullOrEmpty(x.Email) || !string.IsNullOrEmpty(x.FirstName) || !string.IsNullOrEmpty(x.PhoneNumber))
.MinimumLength(2)
.MaximumLength(50)
.WithMessage("The Last Name should be present and within a range of 2 to 50 characters long.")
Expand All @@ -31,12 +33,14 @@ public ContactPersonModelValidation()
RuleFor(p => p.Email)
.NotNull()
.NotEmpty()
.When(x => !string.IsNullOrEmpty(x.FirstName) || !string.IsNullOrEmpty(x.LastName) || !string.IsNullOrEmpty(x.PhoneNumber))
.EmailAddress()
.WithMessage("The Email should be present and within a valid email address.");

RuleFor(p => p.PhoneNumber)
.NotNull()
.NotEmpty()
.When(x => !string.IsNullOrEmpty(x.Email) || !string.IsNullOrEmpty(x.FirstName) || !string.IsNullOrEmpty(x.LastName))
.MinimumLength(3)
.MaximumLength(30)
.WithMessage("The Phone number should be present and within a range of 3 to 30 characters long.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public LocationBaseModelValidation()
.WithMessage("The description can be empty or within a range of 3 to 100 characters long.");

RuleFor(p => p.ContactPerson)
.Must(l => l != null)
.WithMessage("The Contact Person should be present")
.SetValidator(new ContactPersonModelValidation());

RuleFor(l=>l.ExternalId)
Expand Down
10 changes: 5 additions & 5 deletions src/MAVN.Service.PartnerManagement/Profiles/ServiceProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public ServiceProfile()

CreateMap<ContactPersonModel, ContactPerson>(MemberList.Destination);

CreateMap<PartnerContactResponse, ContactPerson>(MemberList.Destination)
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.PartnerContact.FirstName))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.PartnerContact.LastName))
.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.PartnerContact.Email))
.ForMember(dest => dest.PhoneNumber, opt => opt.MapFrom(src => src.PartnerContact.PhoneNumber));
CreateMap<PartnerContact, ContactPerson>(MemberList.Destination)
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName))
.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email))
.ForMember(dest => dest.PhoneNumber, opt => opt.MapFrom(src => src.PhoneNumber));

CreateMap<Partner, PartnerListDetailsModel>(MemberList.Destination);
CreateMap<Location, PartnerListLocationModel>(MemberList.Destination);
Expand Down
Loading

0 comments on commit 877b2ef

Please sign in to comment.