Skip to content

Commit

Permalink
Close account
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpaulovich committed Oct 9, 2020
1 parent 4afa75c commit 85bbeb7
Show file tree
Hide file tree
Showing 38 changed files with 817 additions and 126 deletions.
2 changes: 1 addition & 1 deletion accounts-api/src/Application/Services/ICurrencyExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
/// </summary>
public interface ICurrencyExchange
{
Task<PositiveMoney> Convert(PositiveMoney originalAmount, Currency destinationCurrency);
Task<Money> Convert(Money originalAmount, Currency destinationCurrency);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ public Task Execute(Guid accountId, decimal amount, string currency)
{
return this.Deposit(
new AccountId(accountId),
new PositiveMoney(amount, new Currency(currency)));
new Money(amount, new Currency(currency)));
}

private async Task Deposit(AccountId accountId, PositiveMoney amount)
private async Task Deposit(AccountId accountId, Money amount)
{
IAccount account = await this._accountRepository
.GetAccount(accountId)
.ConfigureAwait(false);

if (account is Account depositAccount)
{
PositiveMoney convertedAmount =
Money convertedAmount =
await this._currencyExchange
.Convert(amount, depositAccount.Currency)
.ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public OpenAccountUseCase(

/// <inheritdoc />
public Task Execute(decimal amount, string currency) =>
this.OpenAccount(new PositiveMoney(amount, new Currency(currency)));
this.OpenAccount(new Money(amount, new Currency(currency)));

private async Task OpenAccount(PositiveMoney amountToDeposit)
private async Task OpenAccount(Money amountToDeposit)
{
string externalUserId = this._userService
.GetCurrentUserId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public Task Execute(Guid originAccountId, Guid destinationAccountId, decimal amo
this.Transfer(
new AccountId(originAccountId),
new AccountId(destinationAccountId),
new PositiveMoney(amount, new Currency(currency)));
new Money(amount, new Currency(currency)));

private async Task Transfer(AccountId originAccountId, AccountId destinationAccountId,
PositiveMoney transferAmount)
Money transferAmount)
{
IAccount originAccount = await this._accountRepository
.GetAccount(originAccountId)
Expand All @@ -63,15 +63,15 @@ private async Task Transfer(AccountId originAccountId, AccountId destinationAcco

if (originAccount is Account withdrawAccount && destinationAccount is Account depositAccount)
{
PositiveMoney localCurrencyAmount =
Money localCurrencyAmount =
await this._currencyExchange
.Convert(transferAmount, withdrawAccount.Currency)
.ConfigureAwait(false);

Debit debit = this._accountFactory
.NewDebit(withdrawAccount, localCurrencyAmount, DateTime.Now);

if (withdrawAccount.GetCurrentBalance().Amount - debit.Amount.Amount < 0)
if (withdrawAccount.GetCurrentBalance().Subtract(debit.Amount).Amount < 0)
{
this._outputPort?.OutOfFunds();
return;
Expand All @@ -80,7 +80,7 @@ await this._currencyExchange
await this.Withdraw(withdrawAccount, debit)
.ConfigureAwait(false);

PositiveMoney destinationCurrencyAmount =
Money destinationCurrencyAmount =
await this._currencyExchange
.Convert(transferAmount, depositAccount.Currency)
.ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public WithdrawUseCase(
public Task Execute(Guid accountId, decimal amount, string currency) =>
this.Withdraw(
new AccountId(accountId),
new PositiveMoney(amount, new Currency(currency)));
new Money(amount, new Currency(currency)));

private async Task Withdraw(AccountId accountId, PositiveMoney withdrawAmount)
private async Task Withdraw(AccountId accountId, Money withdrawAmount)
{
string externalUserId = this._userService
.GetCurrentUserId();
Expand All @@ -63,15 +63,15 @@ private async Task Withdraw(AccountId accountId, PositiveMoney withdrawAmount)

if (account is Account withdrawAccount)
{
PositiveMoney localCurrencyAmount =
Money localCurrencyAmount =
await this._currencyExchange
.Convert(withdrawAmount, withdrawAccount.Currency)
.ConfigureAwait(false);

Debit debit = this._accountFactory
.NewDebit(withdrawAccount, localCurrencyAmount, DateTime.Now);

if (withdrawAccount.GetCurrentBalance().Amount - debit.Amount.Amount < 0)
if (withdrawAccount.GetCurrentBalance().Subtract(debit.Amount).Amount < 0)
{
this._outputPort?.OutOfFunds();
return;
Expand Down
4 changes: 2 additions & 2 deletions accounts-api/src/Domain/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public bool IsClosingAllowed() => this.GetCurrentBalance()
/// <inheritdoc />
public Money GetCurrentBalance()
{
PositiveMoney totalCredits = this.CreditsCollection
Money totalCredits = this.CreditsCollection
.GetTotal();

PositiveMoney totalDebits = this.DebitsCollection
Money totalDebits = this.DebitsCollection
.GetTotal();

Money totalAmount = totalCredits
Expand Down
4 changes: 2 additions & 2 deletions accounts-api/src/Domain/Credits/Credit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Credit(CreditId creditId, AccountId accountId, DateTime transactionDate,
this.CreditId = creditId;
this.AccountId = accountId;
this.TransactionDate = transactionDate;
this.Amount = new PositiveMoney(value, new Currency(currency));
this.Amount = new Money(value, new Currency(currency));
}

/// <summary>
Expand Down Expand Up @@ -54,7 +54,7 @@ public Credit(CreditId creditId, AccountId accountId, DateTime transactionDate,
/// <summary>
/// Gets or sets Amount.
/// </summary>
public PositiveMoney Amount { get; }
public Money Amount { get; }

/// <summary>
/// Calculate the sum of positive amounts.
Expand Down
2 changes: 1 addition & 1 deletion accounts-api/src/Domain/Credits/CreditNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public sealed class CreditNull : ICredit
{
public static CreditNull Instance { get; } = new CreditNull();
public CreditId CreditId { get; } = new CreditId(Guid.Empty);
public PositiveMoney Amount { get; } = new PositiveMoney(0, new Currency(string.Empty));
public Money Amount { get; } = new Money(0, new Currency(string.Empty));
}
}
8 changes: 4 additions & 4 deletions accounts-api/src/Domain/Credits/CreditsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public sealed class CreditsCollection : List<Credit>
/// Gets Total amount.
/// </summary>
/// <returns>Positive amount.</returns>
public PositiveMoney GetTotal()
public Money GetTotal()
{
if (this.Count == 0)
{
return new PositiveMoney(0, new Currency(string.Empty));
return new Money(0, new Currency(string.Empty));
}

PositiveMoney total = new PositiveMoney(0, this.First().Amount.Currency);
Money total = new Money(0, this.First().Amount.Currency);

return this.Aggregate(total, (current, credit) =>
new PositiveMoney(current.Amount + credit.Amount.Amount, current.Currency));
new Money(current.Amount + credit.Amount.Amount, current.Currency));
}
}
}
2 changes: 1 addition & 1 deletion accounts-api/src/Domain/Credits/ICredit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public interface ICredit
/// <summary>
/// Gets the Amount.
/// </summary>
PositiveMoney Amount { get; }
Money Amount { get; }
}
}
6 changes: 3 additions & 3 deletions accounts-api/src/Domain/Debits/Debit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Debit(DebitId DebitId, AccountId accountId, DateTime transactionDate, dec
this.DebitId = DebitId;
this.AccountId = accountId;
this.TransactionDate = transactionDate;
this.Amount = new PositiveMoney(value, new Currency(currency));
this.Amount = new Money(value, new Currency(currency));
}

/// <summary>
Expand All @@ -38,7 +38,7 @@ public Debit(DebitId DebitId, AccountId accountId, DateTime transactionDate, dec
/// <summary>
/// Gets the AccountId.
/// </summary>
public AccountId AccountId { get; }
public AccountId AccountId { get; }

public Account? Account { get; set; }

Expand All @@ -54,7 +54,7 @@ public Debit(DebitId DebitId, AccountId accountId, DateTime transactionDate, dec
/// <summary>
/// Gets or sets Amount.
/// </summary>
public PositiveMoney Amount { get; }
public Money Amount { get; }

/// <summary>
/// Calculate the sum of positive amounts.
Expand Down
2 changes: 1 addition & 1 deletion accounts-api/src/Domain/Debits/DebitNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public sealed class DebitNull : IDebit
{
public static DebitNull Instance { get; } = new DebitNull();
public DebitId DebitId { get; } = new DebitId(Guid.Empty);
public PositiveMoney Amount { get; } = new PositiveMoney(0, new Currency(string.Empty));
public Money Amount { get; } = new Money(0, new Currency(string.Empty));
}
}
8 changes: 4 additions & 4 deletions accounts-api/src/Domain/Debits/DebitsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public sealed class DebitsCollection : List<Debit>
/// Gets Total amount.
/// </summary>
/// <returns>Total.</returns>
public PositiveMoney GetTotal()
public Money GetTotal()
{
if (this.Count == 0)
{
return new PositiveMoney(0, new Currency(string.Empty));
return new Money(0, new Currency(string.Empty));
}

PositiveMoney total = new PositiveMoney(0, this.First().Amount.Currency);
Money total = new Money(0, this.First().Amount.Currency);

return this.Aggregate(total, (current, credit) =>
new PositiveMoney(current.Amount + credit.Amount.Amount, current.Currency));
new Money(current.Amount + credit.Amount.Amount, current.Currency));
}
}
}
2 changes: 1 addition & 1 deletion accounts-api/src/Domain/Debits/IDebit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public interface IDebit
/// <summary>
/// Gets the Amount.
/// </summary>
PositiveMoney Amount { get; }
Money Amount { get; }
}
}
4 changes: 2 additions & 2 deletions accounts-api/src/Domain/IAccountFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface IAccountFactory
/// <param name="amountToDeposit">Amount to Deposit.</param>
/// <param name="transactionDate">Transaction date.</param>
/// <returns>New Credit instance.</returns>
Credit NewCredit(Account account, PositiveMoney amountToDeposit, DateTime transactionDate);
Credit NewCredit(Account account, Money amountToDeposit, DateTime transactionDate);

/// <summary>
/// Creates a new Debit.
Expand All @@ -44,6 +44,6 @@ public interface IAccountFactory
/// <param name="amountToWithdraw">Amount to Withdraw.</param>
/// <param name="transactionDate">Transaction date.</param>
/// <returns>New Debit instance.</returns>
Debit NewDebit(Account account, PositiveMoney amountToWithdraw, DateTime transactionDate);
Debit NewDebit(Account account, Money amountToWithdraw, DateTime transactionDate);
}
}
5 changes: 5 additions & 0 deletions accounts-api/src/Domain/ValueObjects/Money.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public override int GetHashCode() =>

public bool IsZero() => this.Amount == 0;

public Money Subtract(Money debit) =>
new Money(Math.Round(this.Amount - debit.Amount, 2), this.Currency);

public Money Add(Money amount) => new Money(Math.Round(this.Amount + amount.Amount, 2), this.Currency);

public override string ToString() => string.Format($"{this.Amount} {this.Currency}");
}
}
47 changes: 0 additions & 47 deletions accounts-api/src/Domain/ValueObjects/PositiveMoney.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public sealed class CurrencyExchangeFake : ICurrencyExchange
{Currency.Real, 5.46346m}
};

public Task<PositiveMoney> Convert(PositiveMoney originalAmount, Currency destinationCurrency)
public Task<Money> Convert(Money originalAmount, Currency destinationCurrency)
{
// hardcoded rates from https://www.xe.com/currency/usd-us-dollar

decimal usdAmount = this._usdRates[originalAmount.Currency] / originalAmount.Amount;
decimal destinationAmount = this._usdRates[destinationCurrency] / usdAmount;

return Task.FromResult(
new PositiveMoney(
new Money(
destinationAmount,
destinationCurrency));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public CurrencyExchangeService(IHttpClientFactory httpClientFactory) =>
/// Converts allowed currencies into USD.
/// </summary>
/// <returns>Money.</returns>
public async Task<PositiveMoney> Convert(PositiveMoney originalAmount, Currency destinationCurrency)
public async Task<Money> Convert(Money originalAmount, Currency destinationCurrency)
{
HttpClient httpClient = this._httpClientFactory.CreateClient(HttpClientName);
Uri requestUri = new Uri(_exchangeUrl);
Expand All @@ -50,7 +50,7 @@ public async Task<PositiveMoney> Convert(PositiveMoney originalAmount, Currency
decimal usdAmount = this._usdRates[originalAmount.Currency] / originalAmount.Amount;
decimal destinationAmount = this._usdRates[destinationCurrency] / usdAmount;

return new PositiveMoney(
return new Money(
destinationAmount,
destinationCurrency);
}
Expand Down
4 changes: 2 additions & 2 deletions accounts-api/src/Infrastructure/DataAccess/EntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public Account NewAccount(string externalUserId, Currency currency)
/// <inheritdoc />
public Credit NewCredit(
Account account,
PositiveMoney amountToDeposit,
Money amountToDeposit,
DateTime transactionDate) =>
new Credit(new CreditId(Guid.NewGuid()), account.AccountId, transactionDate,
amountToDeposit.Amount, amountToDeposit.Currency.Code);

/// <inheritdoc />
public Debit NewDebit(
Account account,
PositiveMoney amountToWithdraw,
Money amountToWithdraw,
DateTime transactionDate) =>
new Debit(new DebitId(Guid.NewGuid()), account.AccountId, transactionDate, amountToWithdraw.Amount,
amountToWithdraw.Currency.Code);
Expand Down
Loading

0 comments on commit 85bbeb7

Please sign in to comment.