Skip to content

Commit

Permalink
Merge pull request #232 from Brixel/fix-transactions-and-financial-years
Browse files Browse the repository at this point in the history
Fix transactions and financial years
  • Loading branch information
BerendWouters authored Oct 7, 2023
2 parents 796070e + e0bb5bf commit 171754f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

using Commands.Handlers.FinancialYear.AddFinancialYear;
using Commands.Handlers.Transaction.AddAttachments;
using Commands.Handlers.Transaction.AddAttachments;

using Domain;
using Domain.Interfaces;
Expand All @@ -17,12 +15,12 @@ public AddCreditTransactionHandler(IFinancialYearRepository financialYearReposit
_financialYearRepository = financialYearRepository;
_mediator = mediator;
}

public async Task<Guid> Handle(AddCreditTransactionCommand request, CancellationToken cancellationToken)
{
var financialYear =
await _financialYearRepository.GetFinancialYearByTransactionId(request.FinancialYearId, cancellationToken)
?? await _mediator.Send(new AddFinancialYearCommand(), cancellationToken);
await _financialYearRepository.GetByIdAsync(request.FinancialYearId, cancellationToken)
?? throw new Exception("Could not get financial year specified in AddTransaction command");

var totalAmount = request.TransactionTypeAmounts.Sum(x => x.Amount);
var transaction = new CreditTransaction(request.CounterPartyName, request.BankAccountId, totalAmount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Commands.Handlers.FinancialYear.AddFinancialYear;
using Commands.Handlers.Transaction.AddAttachments;
using Commands.Handlers.Transaction.AddAttachments;

using Domain;
using Domain.Interfaces;
Expand All @@ -19,8 +18,8 @@ public AddDebitTransactionHandler(IFinancialYearRepository financialYearReposito
public async Task<Guid> Handle(AddDebitTransactionCommand request, CancellationToken cancellationToken)
{
var financialYear =
await _financialYearRepository.GetFinancialYearByTransactionId(request.FinancialYearId, cancellationToken)
?? await _mediator.Send(new AddFinancialYearCommand(), cancellationToken);
await _financialYearRepository.GetByIdAsync(request.FinancialYearId, cancellationToken)
?? throw new Exception("Could not get financial year specified in AddTransaction command");

var totalAmount = request.TransactionTypeAmounts.Sum(x => x.Amount);
var transaction = new DebitTransaction(request.CounterPartyName, request.BankAccountId, totalAmount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Commands.Handlers.FinancialYear.AddFinancialYear;
using Commands.Handlers.Transaction.AddAttachments;
using Commands.Handlers.Transaction.AddAttachments;

using Domain.Interfaces;

Expand All @@ -18,9 +17,8 @@ public EditTransactionHandler(IFinancialYearRepository financialYearRepository,

public async Task Handle(EditTransactionCommand request, CancellationToken cancellationToken)
{
var financialYear =
await _financialYearRepository.GetFinancialYearByTransactionId(request.Id, cancellationToken)
?? throw new ArgumentException($"No transaction found for Id {request.Id}", nameof(request));
var financialYear = await _financialYearRepository.GetFinancialYearByTransactionId(request.Id, cancellationToken)
?? throw new ArgumentException($"No transaction found for Id {request.Id}", nameof(request));

if (financialYear.StartDate <= request.ReceivedDateTime &&
financialYear.EndDate >= request.ReceivedDateTime)
Expand All @@ -33,9 +31,8 @@ await _financialYearRepository.GetFinancialYearByTransactionId(request.Id, cance
var transaction = financialYear.Transactions.First(x => x.Id == request.Id);

var matchingFinancialYear =
await _financialYearRepository.GetFinancialYearByTransactionId(request.FinancialYearId,
cancellationToken)
?? await _mediator.Send(new AddFinancialYearCommand(), cancellationToken);
await _financialYearRepository.GetByIdAsync(request.FinancialYearId, cancellationToken)
?? throw new Exception("Could not get financial year specified in AddTransaction command");

matchingFinancialYear.AddTransaction(transaction);
matchingFinancialYear.ChangeTransaction(request.Id, request.CounterPartyName, request.MemberId, request.BankAccountId,
Expand Down
14 changes: 13 additions & 1 deletion Web/Pages/FinancialYears/FinancialYears.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@page "/financialyears"
@attribute [Authorize]
@using Commands.Handlers.FinancialYear.AddFinancialYear;
@using Queries.FinancialYears
@using Commands.Handlers.FinancialYear.CloseFinancialYear

Expand All @@ -14,6 +15,11 @@
ServerData="@(new Func<TableState, Task<TableData<FinancialYear>>>(ServerReload))">
<ToolBarContent>
<MudText Typo="Typo.h6">Financial years</MudText>
<MudSpacer />
<MudButton Class="ma-2" Color="Color.Primary" DisableElevation="true" OnClick="@AddFinancialYear" Size="Size.Small"
Variant="Variant.Filled">
Add Financial Year
</MudButton>
</ToolBarContent>
<HeaderContent>
<MudTh>
Expand Down Expand Up @@ -67,7 +73,7 @@
private async Task<TableData<FinancialYear>> ServerReload(TableState state)
{
StateHasChanged();

var query = new GetFinancialYearsQuery();
var data = await _mediator.Send(query);

Expand Down Expand Up @@ -99,4 +105,10 @@
table?.ReloadServerData();
}
}

private async Task AddFinancialYear()
{
await _mediator.Send(new AddFinancialYearCommand());
table?.ReloadServerData();
}
}

0 comments on commit 171754f

Please sign in to comment.