Skip to content

Commit

Permalink
Clean account accountgroup parent relation to bea ble to attach accou…
Browse files Browse the repository at this point in the history
…nts in different classifications
  • Loading branch information
fdonnet committed Oct 20, 2023
1 parent 29b7427 commit 4c88d70
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class AccountAccountGroupConfiguration : IEntityTypeConfiguration<Account
{
public void Configure(EntityTypeBuilder<AccountAccountGroup> builder)
{
builder.ToTable("AccountsAccountGroups");

builder.Property(a => a.AccountId)
.IsRequired();

Expand Down
64 changes: 64 additions & 0 deletions src/Ubik.Accounting.Api/Data/Init/AccountsAccountGroupsData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Ubik.Accounting.Api.Models;

namespace Ubik.Accounting.Api.Data.Init
{
internal class AccountsAccountGroupsData
{
internal static void Load(AccountingContext context)
{
if (!context.AccountsAccountGroups.Any())
{
var baseValuesGeneral = new BaseValuesGeneral();
var baseValuesForTenants = new BaseValuesForTenants();
var baseValuesForUsers = new BaseValuesForUsers();
var baseValuesForAccountGroups = new BaseValuesForAccountGroups();
var baseValuesForAccounts = new BaseValuesForAccounts();

var accountsAccountGroups = new AccountAccountGroup[]
{
new AccountAccountGroup
{
Id = Guid.NewGuid(),
AccountGroupId = baseValuesForAccountGroups.AccountGroupId1,
AccountId= baseValuesForAccounts.AccountId1,
CreatedBy = baseValuesForUsers.UserId1,
CreatedAt = baseValuesGeneral.GenerationTime,
ModifiedBy = baseValuesForUsers.UserId1,
ModifiedAt = baseValuesGeneral.GenerationTime,
Version = Guid.NewGuid(),
TenantId = baseValuesForTenants.TenantId
},
new AccountAccountGroup
{
Id = Guid.NewGuid(),
AccountGroupId = baseValuesForAccountGroups.AccountGroupId1,
AccountId= baseValuesForAccounts.AccountId2,
CreatedBy = baseValuesForUsers.UserId1,
CreatedAt = baseValuesGeneral.GenerationTime,
ModifiedBy = baseValuesForUsers.UserId1,
ModifiedAt = baseValuesGeneral.GenerationTime,
Version = Guid.NewGuid(),
TenantId = baseValuesForTenants.TenantId
},
new AccountAccountGroup
{
Id = Guid.NewGuid(),
AccountGroupId = baseValuesForAccountGroups.AccountGroupId2,
AccountId= baseValuesForAccounts.AccountId1,
CreatedBy = baseValuesForUsers.UserId1,
CreatedAt = baseValuesGeneral.GenerationTime,
ModifiedBy = baseValuesForUsers.UserId1,
ModifiedAt = baseValuesGeneral.GenerationTime,
Version = Guid.NewGuid(),
TenantId = baseValuesForTenants.TenantId
}
};
foreach (AccountAccountGroup aag in accountsAccountGroups)
{
context.AccountsAccountGroups.Add(aag);
}
context.SaveChanges();
}
}
}
}
1 change: 1 addition & 0 deletions src/Ubik.Accounting.Api/Data/Init/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public void Initialize(AccountingContext context)
AccountGroupClassificationsData.Load(context);
AccountGroupsData.Load(context);
AccountsData.Load(context);
AccountsAccountGroupsData.Load(context);
}
}
}
5 changes: 0 additions & 5 deletions src/Ubik.Accounting.Api/Features/Accounts/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ public async Task<bool> IfExistsWithDifferentIdAsync(string accountCode, Guid cu
return await _context.Accounts.AnyAsync(a => a.Code == accountCode && a.Id != currentId);
}

public async Task<bool> IfExistsAccountGroupAsync(Guid accountGroupId)
{
return await _context.AccountGroups.AnyAsync(g=> g.Id == accountGroupId);
}

public async Task<Account> AddAsync(Account account)
{
await _context.Accounts.AddAsync(account);
Expand Down
10 changes: 0 additions & 10 deletions src/Ubik.Accounting.Api/Features/Accounts/Commands/AddAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ public async Task<AddAccountResult> Handle(AddAccountCommand request, Cancellati
if (accountExists)
throw new AccountAlreadyExistsException(request.Code);

//TODO Check if account group exists (if not null)
//if(request.AccountGroupId != null)
//{
// var accountGroupExists = await _serviceManager.AccountService.IfExistsAccountGroupAsync((Guid)request.AccountGroupId);
// if (!accountGroupExists)
// {
// throw new AccountGroupNotFoundExceptionForAccount((Guid)request.AccountGroupId);
// }
//}

await _serviceManager.AccountService.AddAsync(account);
await _serviceManager.SaveAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ public async Task<UpdateAccountResult> Handle(UpdateAccountCommand request, Canc
var account = await _serviceManager.AccountService.GetAsync(request.Id)
?? throw new AccountNotFoundException(request.Id);

////Check if account group exists
//if(request.AccountGroupId !=null)
//{
// var accountGroupExists = await _serviceManager.AccountService.IfExistsAccountGroupAsync((Guid)request.AccountGroupId);
// if (!accountGroupExists)
// {
// throw new AccountGroupNotFoundExceptionForAccount((Guid)request.AccountGroupId);
// }
//}

//Modify the found account
account = request.ToAccount(account);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public interface IAccountService
public Task<Account?> GetAsync(Guid id);
public Task<bool> IfExistsAsync(string accountCode);
public Task<bool> IfExistsWithDifferentIdAsync(string accountCode, Guid currentId);
public Task<bool> IfExistsAccountGroupAsync(Guid accountGroupId);
public Task<Account> AddAsync(Account account);
public Task<Account> UpdateAsync(Account account);
public Task<bool> DeleteAsync(Guid id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,6 @@ public async Task IfExist_TrueOrFalse_Ok(string accountCode, bool resultNeeded)
result.Should().Be(resultNeeded);
}

[Theory]
[InlineData("1524f11f-20dd-4888-88f8-428e59bbc22a", true)]
[InlineData("1524f11f-20dd-4888-88f8-428e59bbbbbb", false)]
public async Task IfExistAccountGroup_TrueOrFalse_Ok(string accountGroupId, bool resultNeeded)
{
//Arrange

//Act
var result = await _serviceManager.AccountService.IfExistsAccountGroupAsync(Guid.Parse(accountGroupId));

//Assert
result.Should().Be(resultNeeded);
}

[Theory]
[InlineData("1020", "7777f11f-20dd-4888-88f8-428e59bbc535", true)]
[InlineData("zzzz999", "7777f11f-20dd-4888-88f8-428e59bbc535", false)]
Expand Down Expand Up @@ -127,20 +113,6 @@ public async Task Add_AuditFieldsModified_Ok(Account account)
result.Should().Match<Account>(x => x.ModifiedBy != null && x.ModifiedAt != null);
}

[Theory]
[MemberData(nameof(GetAccounts), parameters: new object[] { 5, "1524f11f-20dd-4888-58f8-428e59bbc22b" })]
public async Task Add_Exception_AccountGroupIdNotExists(Account account)
{
//Arrange

//Act
Func<Task> act = async () => await _serviceManager.AccountService.AddAsync(account);

//Assert
await act.Should().ThrowAsync<Exception>();
}


[Fact]
public async Task Update_UpdatedAccount_Ok()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,34 +210,6 @@ public async Task Post_ProblemDetails_AccountCodeExist()
.And.Match<CustomProblemDetails>(x => x.Errors.First().Code == "ACCOUNT_ALREADY_EXISTS");
}

//[Fact]
//public async Task Post_ProblemDetails_AccountGroupNotFound()
//{
// //Arrange
// var httpClient = Factory.CreateDefaultClient();

// var accessToken = await AuthHelper.GetAccessTokenReadWrite();
// httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

// //Act
// var fakeAc = FakeGenerator.GenerateAccounts(1, _testValuesForAccountGroups.AccountGroupId1).First();
// var fake = fakeAc.ToAddAccountResult();
// fake.AccountGroupId = Guid.NewGuid();

// var postAccountJson = JsonSerializer.Serialize(fake);
// var content = new StringContent(postAccountJson.ToString(), Encoding.UTF8, "application/json");

// var response = await httpClient.PostAsync($"/Accounts", content);
// var result = await response.Content.ReadFromJsonAsync<CustomProblemDetails>();

// //Assert
// response.StatusCode.Should().Be(HttpStatusCode.NotFound);
// result.Should()
// .NotBeNull()
// .And.BeOfType<CustomProblemDetails>()
// .And.Match<CustomProblemDetails>(x => x.Errors.First().Code == "ACCOUNTGROUP_NOT_FOUND_FOR_ACCOUNT");
//}

[Fact]
public async Task Post_ProblemDetails_AccountEmptyFields()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,6 @@ await act.Should().ThrowAsync<AccountAlreadyExistsException>()
.Where(e => e.ErrorType == ServiceAndFeatureExceptionType.Conflict);
}

//TODO: see if we need to adpat
//[Fact]
//public async Task Add_AccountGroupNotFoundExceptionForAccount_AccountGroupIdNotExists()
//{
// //Arrange
// _serviceManager.AccountService.IfExistsAccountGroupAsync((Guid)_command.AccountGroupId!).Returns(false);

// //Act
// Func<Task> act = async () => await _handler.Handle(_command, CancellationToken.None);

// //Assert
// await act.Should().ThrowAsync<AccountGroupNotFoundExceptionForAccount>()
// .Where(e => e.ErrorType == ServiceAndFeatureExceptionType.NotFound);
//}

[Fact]
public async Task Add_CustomValidationException_EmptyValuesInFields()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,6 @@ await act.Should().ThrowAsync<AccountAlreadyExistsException>()
.Where(e => e.ErrorType == ServiceAndFeatureExceptionType.Conflict);
}

//TODO: see how to adapt
//[Fact]
//public async Task Upd_AccountGroupNotFoundExceptionForAccount_AccountGroupIdNotExistsId()
//{
// //Arrange
// _serviceManager.AccountService.IfExistsAccountGroupAsync((Guid)_command.AccountGroupId!).Returns(false);

// //Act
// Func<Task> act = async () => await _handler.Handle(_command, CancellationToken.None);

// //Assert
// await act.Should().ThrowAsync<AccountGroupNotFoundExceptionForAccount>()
// .Where(e => e.ErrorType == ServiceAndFeatureExceptionType.NotFound);
//}

[Fact]
public async Task Upd_AccountNotFoundException_AccountIdNotFound()
{
Expand Down

0 comments on commit 4c88d70

Please sign in to comment.