diff --git a/EstateManagementUI.BlazorServer/Components/Pages/Contracts/Edit.razor.cs b/EstateManagementUI.BlazorServer/Components/Pages/Contracts/Edit.razor.cs index a6c9cc26..9b4b5d16 100644 --- a/EstateManagementUI.BlazorServer/Components/Pages/Contracts/Edit.razor.cs +++ b/EstateManagementUI.BlazorServer/Components/Pages/Contracts/Edit.razor.cs @@ -177,7 +177,7 @@ private async Task HandleAddFee() try { var estateId = await this.GetEstateId(); - var command = new ContractCommands.AddTransactionFeeForProductToContractCommand( + var command = new ContractCommands.AddTransactionFeeToProductCommand( CorrelationIdHelper.New(), estateId, ContractId, @@ -225,32 +225,45 @@ private void ClearMessages() private async Task RemoveProduct(Guid productId) { - if (contractModel == null || contractModel.Products == null) return; + //if (contractModel == null || contractModel.Products == null) return; - var product = contractModel.Products.FirstOrDefault(p => p.ContractProductId == productId); - if (product != null) - { + //var product = contractModel.Products.FirstOrDefault(p => p.ContractProductId == productId); + //if (product != null) + //{ // Note: Backend API for product removal not yet implemented // For now, show a message to the user errorMessage = "Product removal is not yet supported by the backend API. This feature will be available once the backend endpoint is implemented."; - } + //} } private async Task RemoveFee(Guid productId, Guid feeId) { - if (contractModel == null || contractModel.Products == null) return; + var estateId = await this.GetEstateId(); + + var command = new ContractCommands.RemoveTransactionFeeFromProductCommand( + CorrelationIdHelper.New(), + estateId, + ContractId, + productId, feeId + ); - var product = contractModel.Products.FirstOrDefault(p => p.ContractProductId == productId); - if (product?.TransactionFees != null) + var result = await Mediator.Send(command); + + if (result.IsSuccess) { - var fee = product.TransactionFees.FirstOrDefault(f => f.TransactionFeeId == feeId); - if (fee != null) - { - // Note: Backend API for fee removal not yet implemented - // For now, show a message to the user - errorMessage = "Transaction fee removal is not yet supported by the backend API. This feature will be available once the backend endpoint is implemented."; - } + successMessage = "Transaction fee removed successfully"; + + // Small delay so user sees confirmation (adjust duration as needed) + await Task.Delay(2500); + + await LoadContract(); } + else + { + feeErrorMessage = result.Message ?? "Failed to remove transaction fee"; + } + + StateHasChanged(); } private void BackToView() diff --git a/EstateManagmentUI.BusinessLogic/Client/ContractMethods.cs b/EstateManagmentUI.BusinessLogic/Client/ContractMethods.cs index bc4dedaa..132ea047 100644 --- a/EstateManagmentUI.BusinessLogic/Client/ContractMethods.cs +++ b/EstateManagmentUI.BusinessLogic/Client/ContractMethods.cs @@ -25,8 +25,11 @@ Task CreateContract(ContractCommands.CreateContractCommand request, CancellationToken cancellationToken); Task AddProductToContract(ContractCommands.AddProductToContractCommand request, CancellationToken cancellationToken); - Task AddTransactionFeeForProductToContract(ContractCommands.AddTransactionFeeForProductToContractCommand request, + Task AddTransactionFeeToProduct(ContractCommands.AddTransactionFeeToProductCommand request, CancellationToken cancellationToken); + Task RemoveTransactionFeeFromProduct(ContractCommands.RemoveTransactionFeeFromProductCommand request, + CancellationToken cancellationToken); + } public partial class ApiClient : IApiClient { @@ -127,8 +130,8 @@ public async Task AddProductToContract(ContractCommands.AddProductToCont return Result.Success(); } - public async Task AddTransactionFeeForProductToContract(ContractCommands.AddTransactionFeeForProductToContractCommand request, - CancellationToken cancellationToken) { + public async Task AddTransactionFeeToProduct(ContractCommands.AddTransactionFeeToProductCommand request, + CancellationToken cancellationToken) { var token = await this.GetToken(cancellationToken); if (token.IsFailed) return ResultHelpers.CreateFailure(token); @@ -143,5 +146,18 @@ public async Task AddTransactionFeeForProductToContract(ContractCommands return Result.Success(); } + public async Task RemoveTransactionFeeFromProduct(ContractCommands.RemoveTransactionFeeFromProductCommand request, + CancellationToken cancellationToken) + { + var token = await this.GetToken(cancellationToken); + if (token.IsFailed) + return ResultHelpers.CreateFailure(token); + + var apiResult = await this.TransactionProcessorClient.DisableTransactionFeeForProduct(token.Data, request.EstateId, request.ContractId, request.ProductId, request.FeeId, cancellationToken); + if (apiResult.IsFailed) + return ResultHelpers.CreateFailure(apiResult); + + return Result.Success(); + } } } diff --git a/EstateManagmentUI.BusinessLogic/RequestHandlers/DateRequestHandler.cs b/EstateManagmentUI.BusinessLogic/RequestHandlers/DateRequestHandler.cs index 68adb10f..481e6bbe 100644 --- a/EstateManagmentUI.BusinessLogic/RequestHandlers/DateRequestHandler.cs +++ b/EstateManagmentUI.BusinessLogic/RequestHandlers/DateRequestHandler.cs @@ -174,7 +174,8 @@ public class ContractRequestHandler : IRequestHandler>, IRequestHandler, IRequestHandler, - IRequestHandler, + IRequestHandler, + IRequestHandler, IRequestHandler>>, IRequestHandler>> { @@ -206,9 +207,15 @@ public async Task Handle(ContractCommands.AddProductToContractCommand re return await this.ApiClient.AddProductToContract(request, cancellationToken); } - public async Task Handle(ContractCommands.AddTransactionFeeForProductToContractCommand request, + public async Task Handle(ContractCommands.AddTransactionFeeToProductCommand request, CancellationToken cancellationToken) { - return await this.ApiClient.AddTransactionFeeForProductToContract(request, cancellationToken); + return await this.ApiClient.AddTransactionFeeToProduct(request, cancellationToken); + } + + public async Task Handle(ContractCommands.RemoveTransactionFeeFromProductCommand request, + CancellationToken cancellationToken) + { + return await this.ApiClient.RemoveTransactionFeeFromProduct(request, cancellationToken); } public async Task>> Handle(ContractQueries.GetRecentContractsQuery request, diff --git a/EstateManagmentUI.BusinessLogic/Requests/Requests.cs b/EstateManagmentUI.BusinessLogic/Requests/Requests.cs index cce41beb..b41adbe2 100644 --- a/EstateManagmentUI.BusinessLogic/Requests/Requests.cs +++ b/EstateManagmentUI.BusinessLogic/Requests/Requests.cs @@ -98,7 +98,8 @@ public record CreateOperatorCommand(CorrelationId CorrelationId, Guid EstateId, public static class ContractCommands { public record CreateContractCommand(CorrelationId CorrelationId, Guid EstateId, string Description, Guid OperatorId) : IRequest; public record AddProductToContractCommand(CorrelationId CorrelationId, Guid EstateId, Guid ContractId, string ProductName, string DisplayText, decimal? Value) : IRequest; - public record AddTransactionFeeForProductToContractCommand(CorrelationId CorrelationId, Guid EstateId, Guid ContractId, Guid ProductId, string Description, decimal Value, String CalculationType, String FeeType) : IRequest; + public record AddTransactionFeeToProductCommand(CorrelationId CorrelationId, Guid EstateId, Guid ContractId, Guid ProductId, string Description, decimal Value, String CalculationType, String FeeType) : IRequest; + public record RemoveTransactionFeeFromProductCommand(CorrelationId CorrelationId, Guid EstateId, Guid ContractId, Guid ProductId, Guid FeeId) : IRequest; } public static class Commands diff --git a/EstateManagmentUI.BusinessLogic/Services/TestMediatorService.cs b/EstateManagmentUI.BusinessLogic/Services/TestMediatorService.cs index d24fcfde..a59f5180 100644 --- a/EstateManagmentUI.BusinessLogic/Services/TestMediatorService.cs +++ b/EstateManagmentUI.BusinessLogic/Services/TestMediatorService.cs @@ -73,7 +73,7 @@ public Task Send(IRequest request, Cancellation OperatorCommands.UpdateOperatorCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteUpdateOperator(cmd)), ContractCommands.CreateContractCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteCreateContract(cmd)), ContractCommands.AddProductToContractCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteAddProductToContract(cmd)), - ContractCommands.AddTransactionFeeForProductToContractCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteAddTransactionFee(cmd)), + ContractCommands.AddTransactionFeeToProductCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteAddTransactionFee(cmd)), MerchantCommands.AssignContractToMerchantCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteAssignContractToMerchant(cmd)), MerchantCommands.RemoveContractFromMerchantCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteRemoveContractFromMerchant(cmd)), MerchantCommands.AddOperatorToMerchantCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteAddOperatorToMerchant(cmd)), @@ -237,7 +237,7 @@ private Result ExecuteAddProductToContract(ContractCommands.AddProductToContract return Result.Success(); } - private Result ExecuteAddTransactionFee(ContractCommands.AddTransactionFeeForProductToContractCommand cmd) + private Result ExecuteAddTransactionFee(ContractCommands.AddTransactionFeeToProductCommand cmd) { var contract = this._testDataStore.GetContract(cmd.EstateId, cmd.ContractId); if (contract == null) @@ -261,6 +261,26 @@ private Result ExecuteAddTransactionFee(ContractCommands.AddTransactionFeeForPro return Result.Success(); } + private Result ExecuteAddTransactionFee(ContractCommands.RemoveTransactionFeeFromProductCommand cmd) + { + var contract = this._testDataStore.GetContract(cmd.EstateId, cmd.ContractId); + if (contract == null) + return Result.Failure($"Contract {cmd.ContractId} not found"); + + var product = contract.Products?.FirstOrDefault(p => p.ContractProductId == cmd.ProductId); + if (product == null) + return Result.Failure($"Product {cmd.ProductId} not found in contract"); + + if (product.TransactionFees == null) + product.TransactionFees = new List(); + + var fee = product.TransactionFees.Single(f => f.TransactionFeeId == cmd.FeeId); + product.TransactionFees.Remove(fee); + + this._testDataStore.UpdateContract(cmd.EstateId, contract); + return Result.Success(); + } + private Result ExecuteAssignContractToMerchant(MerchantCommands.AssignContractToMerchantCommand cmd) { var merchant = this._testDataStore.GetMerchant(cmd.EstateId, cmd.MerchantId);