diff --git a/OpenSky.API/Controllers/FinancialController.cs b/OpenSky.API/Controllers/FinancialController.cs index c93f458..c78bfcb 100644 --- a/OpenSky.API/Controllers/FinancialController.cs +++ b/OpenSky.API/Controllers/FinancialController.cs @@ -97,7 +97,7 @@ public async Task>> BobsYourUncle() try { this.logger.LogInformation($"{this.User.Identity?.Name} | POST Financial/bobsYourUncle"); - + // ReSharper disable AssignNullToNotNullAttribute var user = await this.userManager.FindByNameAsync(this.User.Identity?.Name); if (user == null) @@ -133,6 +133,57 @@ public async Task>> BobsYourUncle() } } + /// ------------------------------------------------------------------------------------------------- + /// + /// Resets user balance + /// + /// + /// Damir, 29/11/2023 + /// + /// + /// An asynchronous result that yields the account balances. + /// + /// ------------------------------------------------------------------------------------------------- + [HttpPost("resetFunds", Name = "ResetFunds")] + public async Task>> ResetFunds() + { + try + { + this.logger.LogInformation($"{this.User.Identity?.Name} | POST Financial/resetFunds"); + + // ReSharper disable AssignNullToNotNullAttribute + var user = await this.userManager.FindByNameAsync(this.User.Identity?.Name); + if (user == null) + { + return new ApiResponse { Message = "Unable to find user record!", IsError = true }; + } + + var resetRecord = new FinancialRecord + { + ID = Guid.NewGuid(), + Timestamp = DateTime.UtcNow, + UserID = user.Id, + Category = FinancialCategory.None, + Expense = user.PersonalAccountBalance - user.PersonalAccountBalance, + Description = "Funds have been reset" + }; + await this.db.FinancialRecords.AddAsync(resetRecord); + + var saveEx = await this.db.SaveDatabaseChangesAsync(this.logger, "Failed to reset funds"); + if (saveEx != null) + { + throw saveEx; + } + + return new ApiResponse("Funds have been successfully reset!"); + } + catch (Exception ex) + { + this.logger.LogError(ex, $"{this.User.Identity?.Name} | POST Financial/resetFunds"); + return new ApiResponse(ex); + } + } + /// ------------------------------------------------------------------------------------------------- /// /// Get the current account balances.