Skip to content

Commit

Permalink
Some more initial changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nagarwal4 committed Apr 16, 2024
1 parent 2d76370 commit 4289959
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,26 @@
// under the Apache License, Version 2.0. See the NOTICE file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

using Core.Domain;
using Core.Domain.Abstractions;
using Core.Domain.Entities.AccountAggregate;
using Core.Domain.Entities.CustomerAggregate;
using Core.Domain.Exceptions;
using Core.Domain.Primitives;
using Core.Domain.Repositories;
using MediatR;

namespace Core.Application.Commands.CustomerCommands
{
public class DeleteCustomerCommand : IRequest
{
public required string CustomerCode { get; set; }
public string CustomerCode { get; set; }

public DeleteCustomerCommand(string customerCode)
public string IP { get; set; }

public DeleteCustomerCommand(string customerCode, string iP)
{
CustomerCode = customerCode;
IP = iP;
}
}

public class DeleteCustomerCommandHandler : IRequestHandler<DeleteCustomerCommand>
{
//private readonly IAccountRepository _accountRepository;

//public CreateAccountCommandHandler(IAccountRepository accountRepository)
//{
// _accountRepository = accountRepository;
//}

//public async Task Handle(CreateAccountCommand request, CancellationToken cancellationToken)
//{
// var hasAccount = await _accountRepository.HasAccountAsync(request.CustomerCode, cancellationToken);

// if (hasAccount)
// {
// throw new CustomErrorsException(ApplicationErrorCode.ExistingPropertyError.ToString(), request.CustomerCode, "A customer can only have one account at the moment");
// }

// var account = Account.NewAccount(request.CustomerCode);

// await _accountRepository.CreateAsync(account, cancellationToken);
//}

private readonly ICustomerRepository _customerRepository;

public DeleteCustomerCommandHandler(
Expand All @@ -58,8 +34,8 @@ public async Task Handle(DeleteCustomerCommand request, CancellationToken cancel
{
// get the customer
var customer = await _customerRepository.GetAsync(request.CustomerCode, cancellationToken);

await _customerRepository.CreateAsync(customer, request.IP);
customer.CustomerCode = "5B2B5A2F-272A-4C79-9B4C-6BB51B5F25DF";
await _customerRepository.DeleteAsync(customer, request.IP);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public interface ICustomerRepository
public Task<IEnumerable<CustomerLimit>> GetLimitsAsync(string customerCode, CancellationToken cancellationToken = default);

public Task UpdateAsync(Customer customer, CancellationToken cancellationToken = default);

public Task DeleteAsync(Customer customer, string? ip = null, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// under the Apache License, Version 2.0. See the NOTICE file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

using Algorand.V2.Algod.Model;
using Core.Domain.Entities.CustomerAggregate;
using Core.Domain.Exceptions;
using Core.Domain.Repositories;
Expand All @@ -11,7 +10,7 @@
using Nexus.Sdk.Token;
using Nexus.Sdk.Token.Requests;
using Nexus.Sdk.Token.Responses;
using stellar_dotnet_sdk.responses;
using Account = Core.Domain.Entities.AccountAggregate.Account;

namespace Core.Infrastructure.Nexus.Repositories
{
Expand All @@ -22,7 +21,7 @@ public class NexusCustomerRepository : ICustomerRepository
private readonly ISigningService _signingService;

public NexusCustomerRepository(
ITokenServer tokenServer,
ITokenServer tokenServer,
TokenOptions tokenSettings,
ISigningService signingService)
{
Expand Down Expand Up @@ -151,6 +150,7 @@ public async Task<IEnumerable<CustomerLimit>> GetLimitsAsync(string customerCode

public async Task DeleteAsync(Customer customer, string? ip = null, CancellationToken cancellationToken = default)
{
customer.CustomerCode = "5B2B5A2F-272A-4C79-9B4C-6BB51B5F25DF";
var customerCodeExists = await _tokenServer.Customers.Exists(customer.CustomerCode);

if (!customerCodeExists)
Expand All @@ -171,45 +171,76 @@ public async Task DeleteAsync(Customer customer, string? ip = null, Cancellation
// Check if the customer has any accounts and balance
if (accounts.Records.Any())
{
foreach (var account in accounts.Records)
foreach (var acc in accounts.Records)
{
Account account = new()
{
AccountCode = acc.AccountCode,
CustomerCode = acc.CustomerCode,
PublicKey = acc.PublicKey
};

// Get the account balance
var response = await _tokenServer.Accounts.GetBalances(account.AccountCode);

var balances = response.Balances;
var tokensToBeRemoved = new List<string>();
var accountBalances = response.Balances;

foreach(var balance in balances)
if (response.Balances.Any(balance => balance.Amount > 0))
{
if (balance.Amount > 0)
{
throw new CustomErrorsException(NexusErrorCodes.NonZeroAccountBalance.ToString(), account.AccountCode, "Customer cannot be deleted due to non zero balance");
}

throw new CustomErrorsException(NexusErrorCodes.NonZeroAccountBalance.ToString(), account.AccountCode, "Customer cannot be deleted due to non-zero balance");
}

// First call update account to remove the trustlines
var updateAccount = new UpdateTokenAccountRequest
if (accountBalances.Any())
{
Settings = new UpdateTokenAccountSettings
{
AllowedTokens = new AllowedTokens
{
RemoveTokens = new string[] { }
}
}
};
var signableResponse = await _tokenServer.Accounts.Update(customer.CustomerCode, account.AccountCode, updateAccount);
// Get the token codes to be removed
var tokensToBeRemoved = accountBalances.Select(b => b.TokenCode).ToArray();

var submitRequest = await _signingService.SignStellarTransactionEnvelopeAsync(withdraw.PublicKey, signableResponse);
await _tokenServer.Submit.OnStellarAsync(submitRequest);
// First call update account to remove the trustlines
await RemoveTrustlines(customer, account, tokensToBeRemoved);
}
}
}

// then call delete customer
var deleteRequest = new DeleteCustomerRequest
{
CustomerCode = customer.CustomerCode
};

await _tokenServer.Customers.Delete(deleteRequest, ip);
}

private async Task RemoveTrustlines(Customer customer, Account account, string[]? tokensToBeRemoved = null)
{
var updateAccount = new UpdateTokenAccountRequest
{
Settings = new UpdateTokenAccountSettings
{
AllowedTokens = new AllowedTokens
{
DisableTokens = tokensToBeRemoved
//RemoveTokens = tokensToBeRemoved
}
}
};

var signableResponse = await _tokenServer.Accounts.Update(customer.CustomerCode, account.AccountCode, updateAccount);


switch (_tokenSettings.Blockchain)
{
case Blockchain.STELLAR:
{
var submitRequest = await _signingService.SignStellarTransactionEnvelopeAsync(account.PublicKey, signableResponse);
await _tokenServer.Submit.OnStellarAsync(submitRequest);
break;
}
case Blockchain.ALGORAND:
{
var submitRequest = await _signingService.SignAlgorandTransactionAsync(account.PublicKey, signableResponse);
await _tokenServer.Submit.OnAlgorandAsync(submitRequest);
break;
}
}
}

public Task SaveChangesAsync(CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public async Task<IActionResult> GetCustomerTokenLimitsAsync()
[RequiredScope("Customer.Create")]
public async Task<IActionResult> DeleteCustomerAsync()
{
var command = new CreateAccountCommand(GetUserId());
var command = new DeleteCustomerCommand(GetUserId(), GetIP());
await _sender.Send(command);
return CreatedAtRoute("GetAccount", null, new EmptyCustomResponse());
}
Expand Down

0 comments on commit 4289959

Please sign in to comment.