Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added existing email check and tests #74

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/core/src/Core.Infrastructure/Nexus/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public static class NexusErrorMessages
{
public const string CustomerNotFound = "There is no customer with this customer code";
public const string AccountNotFound = "There is no account for this customer";
public const string ExistingProperty = "A customer with this code already exists";
public const string ExistingCode = "A customer with this code already exists";
public const string ExistingEmail = "A customer with this email already exists";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ public NexusCustomerRepository(ITokenServer tokenServer, TokenOptions tokenSetti

public async Task CreateAsync(Customer customer, string? ip = null, CancellationToken cancellationToken = default)
{
var exists = await _tokenServer.Customers.Exists(customer.CustomerCode);
var customerCodeExists = await _tokenServer.Customers.Exists(customer.CustomerCode);

if (customerCodeExists)
{
throw new CustomErrorsException(NexusErrorCodes.ExistingProperty.ToString(), customer.CustomerCode, Constants.NexusErrorMessages.ExistingCode);
}

var query = new Dictionary<string, string>
{
{ "Email", customer.Email.TrimEnd() }
};

var existingCustomersWithEmail = await _tokenServer.Customers.Get(query);

if (exists)
if (existingCustomersWithEmail != null && existingCustomersWithEmail.Records.Any(existingCustomer => existingCustomer.Status != CustomerStatus.DELETED.ToString()))
{
throw new CustomErrorsException(NexusErrorCodes.ExistingProperty.ToString(), customer.CustomerCode, Constants.NexusErrorMessages.ExistingProperty);
throw new CustomErrorsException(NexusErrorCodes.ExistingProperty.ToString(), customer.Email, Constants.NexusErrorMessages.ExistingEmail);
}

var success = Enum.TryParse<CustomerStatus>(customer.Status.ToString(), out var status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public static CustomerResponse PrivateCustomer(string customerCode)
return new CustomerResponse(customerCode, "FirstName", "LastName", "2020-12-12", "123456", null, "PTrusted", "EUR", "NL", "[email protected]", "ACTIVE", "TestBankAccount", false, "Low", new Dictionary<string, string>());
}

public static CustomerResponse DeletedPrivateCustomer(string customerCode)
{
return new CustomerResponse(customerCode, "FirstName", "LastName", "2020-12-12", "123456", null, "PTrusted", "EUR", "NL", "[email protected]", "DELETED", "TestBankAccount", false, "Low", new Dictionary<string, string>());
}

public static IDictionary<string, string> AccountQuery(string customerCode)
{
return new Dictionary<string, string>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Nexus.Sdk.Shared.Requests;
using Nexus.Sdk.Shared.Responses;
using Nexus.Sdk.Token;

namespace Core.InfrastructureTests.Nexus.Repositories
Expand Down Expand Up @@ -144,6 +145,80 @@ public async Task CreateCustomer_Creating_Throws_ExistsPropertyError_TestAsync()
server.Verify(s => s.Customers.Create(It.IsAny<CreateCustomerRequest>(), It.IsAny<string>()), Times.Never);
}

[TestMethod()]
public async Task CreateCustomer_Creating_Throws_EmailExistsError_TestAsync()
{
var server = new Mock<ITokenServer>();
server.Setup(s => s.Customers.Get(It.IsAny<Dictionary<string, string>>()))
.Returns(Task.FromResult(new PagedResponse<CustomerResponse>(
page: 1,
total: 1,
totalPages: 1,
filteringParameters: new Dictionary<string, string>(),
records: [NexusSDKHelper.PrivateCustomer("TestCustomer123")])));

var repo = new NexusCustomerRepository(server.Object, DefaultOptions.TokenOptions);

var customer = new Customer()
{
CustomerCode = "TestCustomer",
CurrencyCode = string.Empty,
Email = "[email protected]",
IsMerchant = false,
Status = "ACTIVE",
TrustLevel = string.Empty,
BankAccount = string.Empty,
Data = new Dictionary<string, string>
{
{ "Key1", "Value1"},
{ "Key2", "Value2"}
}
};

var ex = await Assert.ThrowsExceptionAsync<CustomErrorsException>(async () => await repo.CreateAsync(customer));

Assert.AreEqual("ExistingProperty", ex.CustomErrors.Errors[0].Code);
Assert.AreEqual("A customer with this email already exists", ex.CustomErrors.Errors[0].Message);

server.Verify(s => s.Customers.Exists("TestCustomer"), Times.Once());
server.Verify(s => s.Customers.Create(It.IsAny<CreateCustomerRequest>(), It.IsAny<string>()), Times.Never);
}

[TestMethod()]
public async Task CreateCustomer_Creating_EmailExists_StatusDeleted_Success_TestAsync()
{
var server = new Mock<ITokenServer>();
server.Setup(s => s.Customers.Get(It.IsAny<Dictionary<string, string>>()))
.Returns(Task.FromResult(new PagedResponse<CustomerResponse>(
page: 1,
total: 1,
totalPages: 1,
filteringParameters: new Dictionary<string, string>(),
records: [NexusSDKHelper.DeletedPrivateCustomer("TestCustomer")])));

var repo = new NexusCustomerRepository(server.Object, DefaultOptions.TokenOptions);

var customer = new Customer()
{
CustomerCode = "TestCustomer123",
CurrencyCode = "EUR",
Email = "[email protected]",
IsMerchant = false,
Status = "ACTIVE",
TrustLevel = "PTrusted",
BankAccount = string.Empty,
Data = new Dictionary<string, string>
{
{ "FirstName", "Hans"},
{ "LastName", "Peter"}
}
};

await repo.CreateAsync(customer);

server.Verify(s => s.Customers.Get(It.IsAny<Dictionary<string, string>>()), Times.Once);
}

public async static Task Get_Returns_Valid_Customer_TestAsync()
{
var server = new Mock<ITokenServer>();
Expand Down
Loading